From e051db5a735911510972a6d902f38c8276708d9e Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Sat, 5 Sep 2020 01:09:10 +0300 Subject: [PATCH] Check for unsuccessful memory allocations --- Makefile | 60 ++++++++++++++++++++--------------------- blockstore_flush.cpp | 6 ++--- blockstore_impl.cpp | 2 +- blockstore_impl.h | 2 +- blockstore_init.cpp | 8 ++---- cluster_client.cpp | 2 +- malloc_or_die.h | 47 ++++++++++++++++++++++++++++++++ messenger.cpp | 4 +-- messenger.h | 22 ++++++++++++++- msgr_receive.cpp | 14 +++++----- osd_flush.cpp | 2 +- osd_peering_pg_test.cpp | 2 +- osd_primary.cpp | 13 +++------ osd_rmw.cpp | 9 ++----- osd_secondary.cpp | 2 +- 15 files changed, 124 insertions(+), 71 deletions(-) create mode 100644 malloc_or_die.h diff --git a/Makefile b/Makefile index 0fca512a..d71d05fb 100644 --- a/Makefile +++ b/Makefile @@ -66,37 +66,37 @@ allocator.o: allocator.cpp allocator.h g++ $(CXXFLAGS) -c -o $@ $< base64.o: base64.cpp base64.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore.o: blockstore.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore.o: blockstore.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_flush.o: blockstore_flush.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_flush.o: blockstore_flush.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_impl.o: blockstore_impl.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_impl.o: blockstore_impl.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_init.o: blockstore_init.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_init.o: blockstore_init.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_journal.o: blockstore_journal.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_journal.o: blockstore_journal.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_open.o: blockstore_open.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_open.o: blockstore_open.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_read.o: blockstore_read.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_read.o: blockstore_read.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_rollback.o: blockstore_rollback.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_rollback.o: blockstore_rollback.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_stable.o: blockstore_stable.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_stable.o: blockstore_stable.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_sync.o: blockstore_sync.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_sync.o: blockstore_sync.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -blockstore_write.o: blockstore_write.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +blockstore_write.o: blockstore_write.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< -cluster_client.o: cluster_client.cpp cluster_client.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +cluster_client.o: cluster_client.cpp cluster_client.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -dump_journal.o: dump_journal.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h ringloop.h +dump_journal.o: dump_journal.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< epoll_manager.o: epoll_manager.cpp epoll_manager.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< etcd_state_client.o: etcd_state_client.cpp base64.h etcd_state_client.h http_client.h json11/json11.hpp object_id.h osd_id.h osd_ops.h pg_states.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -fio_cluster.o: fio_cluster.cpp cluster_client.h epoll_manager.h etcd_state_client.h fio/fio.h fio/optgroup.h http_client.h json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +fio_cluster.o: fio_cluster.cpp cluster_client.h epoll_manager.h etcd_state_client.h fio/fio.h fio/optgroup.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< fio_engine.o: fio_engine.cpp blockstore.h fio/fio.h fio/optgroup.h json11/json11.hpp object_id.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< @@ -104,43 +104,43 @@ fio_sec_osd.o: fio_sec_osd.cpp fio/fio.h fio/optgroup.h object_id.h osd_id.h osd g++ $(CXXFLAGS) -c -o $@ $< http_client.o: http_client.cpp http_client.h json11/json11.hpp timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -messenger.o: messenger.cpp json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +messenger.o: messenger.cpp json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -msgr_receive.o: msgr_receive.cpp json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +msgr_receive.o: msgr_receive.cpp json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -msgr_send.o: msgr_send.cpp json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +msgr_send.o: msgr_send.cpp json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd.o: osd.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd.o: osd.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd_cluster.o: osd_cluster.cpp base64.h blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd_cluster.o: osd_cluster.cpp base64.h blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd_flush.o: osd_flush.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd_flush.o: osd_flush.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd_main.o: osd_main.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd_main.o: osd_main.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< osd_ops.o: osd_ops.cpp object_id.h osd_id.h osd_ops.h g++ $(CXXFLAGS) -c -o $@ $< -osd_peering.o: osd_peering.cpp base64.h blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd_peering.o: osd_peering.cpp base64.h blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< osd_peering_pg.o: osd_peering_pg.cpp cpp-btree/btree_map.h object_id.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h g++ $(CXXFLAGS) -c -o $@ $< osd_peering_pg_test.o: osd_peering_pg_test.cpp cpp-btree/btree_map.h object_id.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h g++ $(CXXFLAGS) -c -o $@ $< -osd_primary.o: osd_primary.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h osd_primary.h osd_rmw.h pg_states.h ringloop.h timerfd_manager.h +osd_primary.o: osd_primary.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h osd_primary.h osd_rmw.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd_primary_subops.o: osd_primary_subops.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h osd_primary.h osd_rmw.h pg_states.h ringloop.h timerfd_manager.h +osd_primary_subops.o: osd_primary_subops.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h osd_primary.h osd_rmw.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -osd_rmw.o: osd_rmw.cpp object_id.h osd_id.h osd_rmw.h xor.h +osd_rmw.o: osd_rmw.cpp malloc_or_die.h object_id.h osd_id.h osd_rmw.h xor.h g++ $(CXXFLAGS) -c -o $@ $< -osd_rmw_test.o: osd_rmw_test.cpp object_id.h osd_id.h osd_rmw.cpp osd_rmw.h test_pattern.h xor.h +osd_rmw_test.o: osd_rmw_test.cpp malloc_or_die.h object_id.h osd_id.h osd_rmw.cpp osd_rmw.h test_pattern.h xor.h g++ $(CXXFLAGS) -c -o $@ $< -osd_secondary.o: osd_secondary.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h +osd_secondary.o: osd_secondary.cpp blockstore.h cpp-btree/btree_map.h epoll_manager.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< osd_test.o: osd_test.cpp object_id.h osd_id.h osd_ops.h rw_blocking.h test_pattern.h g++ $(CXXFLAGS) -c -o $@ $< pg_states.o: pg_states.cpp pg_states.h g++ $(CXXFLAGS) -c -o $@ $< -qemu_proxy.o: qemu_proxy.cpp cluster_client.h etcd_state_client.h http_client.h json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h qemu_proxy.h ringloop.h timerfd_manager.h +qemu_proxy.o: qemu_proxy.cpp cluster_client.h etcd_state_client.h http_client.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h qemu_proxy.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< ringloop.o: ringloop.cpp ringloop.h g++ $(CXXFLAGS) -c -o $@ $< @@ -150,9 +150,9 @@ stub_bench.o: stub_bench.cpp object_id.h osd_id.h osd_ops.h rw_blocking.h g++ $(CXXFLAGS) -c -o $@ $< stub_osd.o: stub_osd.cpp object_id.h osd_id.h osd_ops.h rw_blocking.h g++ $(CXXFLAGS) -c -o $@ $< -stub_uring_osd.o: stub_uring_osd.cpp epoll_manager.h json11/json11.hpp messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h +stub_uring_osd.o: stub_uring_osd.cpp epoll_manager.h json11/json11.hpp malloc_or_die.h messenger.h object_id.h osd_id.h osd_ops.h ringloop.h timerfd_manager.h g++ $(CXXFLAGS) -c -o $@ $< -test.o: test.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h object_id.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h +test.o: test.cpp allocator.h blockstore.h blockstore_flush.h blockstore_impl.h blockstore_init.h blockstore_journal.h cpp-btree/btree_map.h crc32c.h malloc_or_die.h object_id.h osd_id.h osd_ops.h osd_peering_pg.h pg_states.h ringloop.h g++ $(CXXFLAGS) -c -o $@ $< test_allocator.o: test_allocator.cpp allocator.h g++ $(CXXFLAGS) -c -o $@ $< diff --git a/blockstore_flush.cpp b/blockstore_flush.cpp index ef11717b..74aea623 100644 --- a/blockstore_flush.cpp +++ b/blockstore_flush.cpp @@ -10,7 +10,7 @@ journal_flusher_t::journal_flusher_t(int flusher_count, blockstore_impl_t *bs) flusher_start_threshold = bs->journal_block_size / sizeof(journal_entry_stable); journal_trim_interval = flusher_start_threshold; journal_trim_counter = 0; - journal_superblock = bs->journal.inmemory ? bs->journal.buffer : memalign(MEM_ALIGNMENT, bs->journal_block_size); + journal_superblock = bs->journal.inmemory ? bs->journal.buffer : memalign_or_die(MEM_ALIGNMENT, bs->journal_block_size); co = new journal_flusher_co[flusher_count]; for (int i = 0; i < flusher_count; i++) { @@ -548,7 +548,7 @@ bool journal_flusher_co::scan_dirty(int wait_base) { submit_offset = dirty_it->second.location + offset - dirty_it->second.offset; submit_len = it == v.end() || it->offset >= end_offset ? end_offset-offset : it->offset-offset; - it = v.insert(it, (copy_buffer_t){ .offset = offset, .len = submit_len, .buf = memalign(MEM_ALIGNMENT, submit_len) }); + it = v.insert(it, (copy_buffer_t){ .offset = offset, .len = submit_len, .buf = memalign_or_die(MEM_ALIGNMENT, submit_len) }); copy_count++; if (bs->journal.inmemory) { @@ -633,7 +633,7 @@ bool journal_flusher_co::modify_meta_read(uint64_t meta_loc, flusher_meta_write_ if (wr.it == flusher->meta_sectors.end()) { // Not in memory yet, read it - wr.buf = memalign(MEM_ALIGNMENT, bs->meta_block_size); + wr.buf = memalign_or_die(MEM_ALIGNMENT, bs->meta_block_size); wr.it = flusher->meta_sectors.emplace(wr.sector, (meta_sector_t){ .offset = wr.sector, .len = bs->meta_block_size, diff --git a/blockstore_impl.cpp b/blockstore_impl.cpp index 16eaaaed..fb2b6b47 100644 --- a/blockstore_impl.cpp +++ b/blockstore_impl.cpp @@ -7,7 +7,7 @@ blockstore_impl_t::blockstore_impl_t(blockstore_config_t & config, ring_loop_t * ring_consumer.loop = [this]() { loop(); }; ringloop->register_consumer(&ring_consumer); initialized = 0; - zero_object = (uint8_t*)memalign(MEM_ALIGNMENT, block_size); + zero_object = (uint8_t*)memalign_or_die(MEM_ALIGNMENT, block_size); data_fd = meta_fd = journal.fd = -1; parse_config(config); try diff --git a/blockstore_impl.h b/blockstore_impl.h index 50216d57..fa252775 100644 --- a/blockstore_impl.h +++ b/blockstore_impl.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include @@ -17,6 +16,7 @@ #include "cpp-btree/btree_map.h" +#include "malloc_or_die.h" #include "allocator.h" //#define BLOCKSTORE_DEBUG diff --git a/blockstore_init.cpp b/blockstore_init.cpp index a177e493..22f62986 100644 --- a/blockstore_init.cpp +++ b/blockstore_init.cpp @@ -202,11 +202,7 @@ int blockstore_init_journal::loop() goto resume_7; printf("Reading blockstore journal\n"); if (!bs->journal.inmemory) - { - submitted_buf = memalign(MEM_ALIGNMENT, 2*bs->journal.block_size); - if (!submitted_buf) - throw std::bad_alloc(); - } + submitted_buf = memalign_or_die(MEM_ALIGNMENT, 2*bs->journal.block_size); else submitted_buf = bs->journal.buffer; // Read first block of the journal @@ -317,7 +313,7 @@ resume_1: if (journal_pos < bs->journal.used_start) end = bs->journal.used_start; if (!bs->journal.inmemory) - submitted_buf = memalign(MEM_ALIGNMENT, JOURNAL_BUFFER_SIZE); + submitted_buf = memalign_or_die(MEM_ALIGNMENT, JOURNAL_BUFFER_SIZE); else submitted_buf = bs->journal.buffer + journal_pos; data->iov = { diff --git a/cluster_client.cpp b/cluster_client.cpp index 7b12da6e..a4c03bac 100644 --- a/cluster_client.cpp +++ b/cluster_client.cpp @@ -337,7 +337,7 @@ void cluster_client_t::continue_rw(cluster_op_t *op) op_copy->inode = op->inode; op_copy->offset = op->offset; op_copy->len = op->len; - op_copy->buf = malloc(op->len); + op_copy->buf = malloc_or_die(op->len); op_copy->iov.push_back(op_copy->buf, op->len); op_copy->callback = [](cluster_op_t* op_copy) { diff --git a/malloc_or_die.h b/malloc_or_die.h new file mode 100644 index 00000000..cb4cb543 --- /dev/null +++ b/malloc_or_die.h @@ -0,0 +1,47 @@ +#pragma once + +#include + +inline void* memalign_or_die(size_t alignment, size_t size) +{ + void *buf = memalign(alignment, size); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", size); + exit(1); + } + return buf; +} + +inline void* malloc_or_die(size_t size) +{ + void *buf = malloc(size); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", size); + exit(1); + } + return buf; +} + +inline void* realloc_or_die(void *ptr, size_t size) +{ + void *buf = realloc(ptr, size); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", size); + exit(1); + } + return buf; +} + +inline void* calloc_or_die(size_t nmemb, size_t size) +{ + void *buf = calloc(nmemb, size); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", size * nmemb); + exit(1); + } + return buf; +} diff --git a/messenger.cpp b/messenger.cpp index 560d8489..1bb4fdd1 100644 --- a/messenger.cpp +++ b/messenger.cpp @@ -118,7 +118,7 @@ void osd_messenger_t::try_connect_peer_addr(osd_num_t peer_osd, const char *peer .peer_state = PEER_CONNECTING, .connect_timeout_id = timeout_id, .osd_num = peer_osd, - .in_buf = malloc(receive_buffer_size), + .in_buf = malloc_or_die(receive_buffer_size), }; tfd->set_fd_handler(peer_fd, true, [this](int peer_fd, int epoll_events) { @@ -390,7 +390,7 @@ void osd_messenger_t::accept_connections(int listen_fd) .peer_port = ntohs(addr.sin_port), .peer_fd = peer_fd, .peer_state = PEER_CONNECTED, - .in_buf = malloc(receive_buffer_size), + .in_buf = malloc_or_die(receive_buffer_size), }; // Add FD to epoll tfd->set_fd_handler(peer_fd, false, [this](int peer_fd, int epoll_events) diff --git a/messenger.h b/messenger.h index 2d60d138..673685ab 100644 --- a/messenger.h +++ b/messenger.h @@ -3,13 +3,13 @@ #include #include #include -#include #include #include #include #include +#include "malloc_or_die.h" #include "json11/json11.hpp" #include "osd_ops.h" #include "timerfd_manager.h" @@ -89,12 +89,22 @@ struct osd_op_buf_list_t int old = alloc; alloc = (((count+other.count+15)/16)*16); buf = (iovec*)malloc(sizeof(iovec) * alloc); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", sizeof(iovec) * alloc); + exit(1); + } memcpy(buf, inline_buf, sizeof(iovec) * old); } else { alloc = (((count+other.count+15)/16)*16); buf = (iovec*)realloc(buf, sizeof(iovec) * alloc); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", sizeof(iovec) * alloc); + exit(1); + } } } for (int i = 0; i < other.count; i++) @@ -112,12 +122,22 @@ struct osd_op_buf_list_t int old = alloc; alloc = ((alloc/16)*16 + 1); buf = (iovec*)malloc(sizeof(iovec) * alloc); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", sizeof(iovec) * alloc); + exit(1); + } memcpy(buf, inline_buf, sizeof(iovec)*old); } else { alloc = alloc < 16 ? 16 : (alloc+16); buf = (iovec*)realloc(buf, sizeof(iovec) * alloc); + if (!buf) + { + printf("Failed to allocate %lu bytes\n", sizeof(iovec) * alloc); + exit(1); + } } } buf[count++] = { .iov_base = nbuf, .iov_len = len }; diff --git a/msgr_receive.cpp b/msgr_receive.cpp index 96d2b801..3b548f3f 100644 --- a/msgr_receive.cpp +++ b/msgr_receive.cpp @@ -183,33 +183,33 @@ void osd_messenger_t::handle_op_hdr(osd_client_t *cl) if (cur_op->req.hdr.opcode == OSD_OP_SEC_READ) { if (cur_op->req.sec_rw.len > 0) - cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_rw.len); + cur_op->buf = memalign_or_die(MEM_ALIGNMENT, cur_op->req.sec_rw.len); cl->read_remaining = 0; } else if (cur_op->req.hdr.opcode == OSD_OP_SEC_WRITE || cur_op->req.hdr.opcode == OSD_OP_SEC_WRITE_STABLE) { if (cur_op->req.sec_rw.len > 0) - cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_rw.len); + cur_op->buf = memalign_or_die(MEM_ALIGNMENT, cur_op->req.sec_rw.len); cl->read_remaining = cur_op->req.sec_rw.len; } else if (cur_op->req.hdr.opcode == OSD_OP_SEC_STABILIZE || cur_op->req.hdr.opcode == OSD_OP_SEC_ROLLBACK) { if (cur_op->req.sec_stab.len > 0) - cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.sec_stab.len); + cur_op->buf = memalign_or_die(MEM_ALIGNMENT, cur_op->req.sec_stab.len); cl->read_remaining = cur_op->req.sec_stab.len; } else if (cur_op->req.hdr.opcode == OSD_OP_READ) { if (cur_op->req.rw.len > 0) - cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.rw.len); + cur_op->buf = memalign_or_die(MEM_ALIGNMENT, cur_op->req.rw.len); cl->read_remaining = 0; } else if (cur_op->req.hdr.opcode == OSD_OP_WRITE) { if (cur_op->req.rw.len > 0) - cur_op->buf = memalign(MEM_ALIGNMENT, cur_op->req.rw.len); + cur_op->buf = memalign_or_die(MEM_ALIGNMENT, cur_op->req.rw.len); cl->read_remaining = cur_op->req.rw.len; } if (cl->read_remaining > 0) @@ -259,7 +259,7 @@ bool osd_messenger_t::handle_reply_hdr(osd_client_t *cl) cl->read_op = op; cl->read_state = CL_READ_REPLY_DATA; cl->read_remaining = sizeof(obj_ver_id) * op->reply.hdr.retval; - op->buf = memalign(MEM_ALIGNMENT, cl->read_remaining); + op->buf = memalign_or_die(MEM_ALIGNMENT, cl->read_remaining); cl->recv_list.push_back(op->buf, cl->read_remaining); } else if (op->reply.hdr.opcode == OSD_OP_SHOW_CONFIG && op->reply.hdr.retval > 0) @@ -269,7 +269,7 @@ bool osd_messenger_t::handle_reply_hdr(osd_client_t *cl) cl->read_op = op; cl->read_state = CL_READ_REPLY_DATA; cl->read_remaining = op->reply.hdr.retval; - op->buf = malloc(op->reply.hdr.retval); + op->buf = malloc_or_die(op->reply.hdr.retval); cl->recv_list.push_back(op->buf, op->reply.hdr.retval); } else diff --git a/osd_flush.cpp b/osd_flush.cpp index eab1cb52..c12936cf 100644 --- a/osd_flush.cpp +++ b/osd_flush.cpp @@ -157,7 +157,7 @@ void osd_t::submit_flush_op(pool_id_t pool_id, pg_num_t pg_num, pg_flush_batch_t { osd_op_t *op = new osd_op_t(); // Copy buffer so it gets freed along with the operation - op->buf = malloc(sizeof(obj_ver_id) * count); + op->buf = malloc_or_die(sizeof(obj_ver_id) * count); memcpy(op->buf, data, sizeof(obj_ver_id) * count); if (peer_osd == this->osd_num) { diff --git a/osd_peering_pg_test.cpp b/osd_peering_pg_test.cpp index 2c1f4075..3c8f5461 100644 --- a/osd_peering_pg_test.cpp +++ b/osd_peering_pg_test.cpp @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) for (uint64_t osd_num = 1; osd_num <= 3; osd_num++) { pg_list_result_t r = { - .buf = (obj_ver_id*)malloc(sizeof(obj_ver_id) * 1024*1024*8), + .buf = (obj_ver_id*)malloc_or_die(sizeof(obj_ver_id) * 1024*1024*8), .total_count = 1024*1024*8, .stable_count = (uint64_t)(1024*1024*8 - (osd_num == 1 ? 10 : 0)), }; diff --git a/osd_primary.cpp b/osd_primary.cpp index c99fa1d2..ff27f09f 100644 --- a/osd_primary.cpp +++ b/osd_primary.cpp @@ -38,8 +38,8 @@ bool osd_t::prepare_primary_rw(osd_op_t *cur_op) finish_op(cur_op, -EINVAL); return false; } - osd_primary_op_data_t *op_data = (osd_primary_op_data_t*)calloc( - sizeof(osd_primary_op_data_t) + sizeof(osd_rmw_stripe_t) * (pool_cfg.scheme == POOL_SCHEME_REPLICATED ? 1 : pg_it->second.pg_size), 1 + osd_primary_op_data_t *op_data = (osd_primary_op_data_t*)calloc_or_die( + 1, sizeof(osd_primary_op_data_t) + sizeof(osd_rmw_stripe_t) * (pool_cfg.scheme == POOL_SCHEME_REPLICATED ? 1 : pg_it->second.pg_size) ); op_data->pg_num = pg_num; op_data->oid = oid; @@ -228,12 +228,7 @@ resume_1: // Object is degraded/misplaced and will be moved to op_data->stripes[0].read_start = 0; op_data->stripes[0].read_end = bs_block_size; - cur_op->rmw_buf = op_data->stripes[0].read_buf = memalign(MEM_ALIGNMENT, bs_block_size); - if (!cur_op->rmw_buf) - { - printf("Failed to allocate %u bytes\n", bs_block_size); - exit(1); - } + cur_op->rmw_buf = op_data->stripes[0].read_buf = memalign_or_die(MEM_ALIGNMENT, bs_block_size); } } else @@ -459,7 +454,7 @@ void osd_t::continue_primary_sync(osd_op_t *cur_op) { if (!cur_op->op_data) { - cur_op->op_data = (osd_primary_op_data_t*)calloc(sizeof(osd_primary_op_data_t), 1); + cur_op->op_data = (osd_primary_op_data_t*)calloc_or_die(1, sizeof(osd_primary_op_data_t)); } osd_primary_op_data_t *op_data = cur_op->op_data; if (op_data->st == 1) goto resume_1; diff --git a/osd_rmw.cpp b/osd_rmw.cpp index 318ced47..cf644985 100644 --- a/osd_rmw.cpp +++ b/osd_rmw.cpp @@ -1,8 +1,8 @@ -#include #include #include #include "xor.h" #include "osd_rmw.h" +#include "malloc_or_die.h" static inline void extend_read(uint32_t start, uint32_t end, osd_rmw_stripe_t & stripe) { @@ -152,12 +152,7 @@ void* alloc_read_buffer(osd_rmw_stripe_t *stripes, int read_pg_size, uint64_t ad } } // Allocate buffer - void *buf = memalign(MEM_ALIGNMENT, buf_size); - if (!buf) - { - printf("Failed to allocate %lu bytes\n", buf_size); - exit(1); - } + void *buf = memalign_or_die(MEM_ALIGNMENT, buf_size); uint64_t buf_pos = add_size; for (int role = 0; role < read_pg_size; role++) { diff --git a/osd_secondary.cpp b/osd_secondary.cpp index 3f4097c4..2736f3ed 100644 --- a/osd_secondary.cpp +++ b/osd_secondary.cpp @@ -108,7 +108,7 @@ void osd_t::exec_show_config(osd_op_t *cur_op) { // FIXME: Send the real config, not its source std::string cfg_str = json11::Json(config).dump(); - cur_op->buf = malloc(cfg_str.size()+1); + cur_op->buf = malloc_or_die(cfg_str.size()+1); memcpy(cur_op->buf, cfg_str.c_str(), cfg_str.size()+1); cur_op->iov.push_back(cur_op->buf, cfg_str.size()+1); finish_op(cur_op, cfg_str.size()+1);