Check malloc returning null

This commit is contained in:
2024-01-22 15:43:03 -08:00
parent e1a12488dc
commit 623b56db9d

View File

@@ -13,6 +13,13 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
__attribute__((always_inline)) void *safe_malloc(size_t s) {
if (void *p = malloc(s)) {
return p;
}
abort();
}
// ==================== BEGIN ARENA IMPL ==================== // ==================== BEGIN ARENA IMPL ====================
/// Group allocations with similar lifetimes to amortize the cost of malloc/free /// Group allocations with similar lifetimes to amortize the cost of malloc/free
@@ -72,7 +79,6 @@ constexpr inline uint32_t nextPowerOfTwo(uint32_t x) {
return x <= 1 ? 1 : 1 << (32 - __builtin_clz(x - 1)); return x <= 1 ? 1 : 1 << (32 - __builtin_clz(x - 1));
} }
/// \private
struct Arena::ArenaImpl { struct Arena::ArenaImpl {
Arena::ArenaImpl *prev; Arena::ArenaImpl *prev;
int capacity; int capacity;
@@ -86,7 +92,7 @@ static_assert(alignof(Arena::ArenaImpl) == 8);
Arena::Arena(int initialSize) : impl(nullptr) { Arena::Arena(int initialSize) : impl(nullptr) {
if (initialSize > 0) { if (initialSize > 0) {
auto allocationSize = align_up(initialSize + sizeof(ArenaImpl), 16); auto allocationSize = align_up(initialSize + sizeof(ArenaImpl), 16);
impl = (Arena::ArenaImpl *)malloc(allocationSize); impl = (Arena::ArenaImpl *)safe_malloc(allocationSize);
impl->prev = nullptr; impl->prev = nullptr;
impl->capacity = allocationSize - sizeof(ArenaImpl); impl->capacity = allocationSize - sizeof(ArenaImpl);
impl->used = 0; impl->used = 0;
@@ -122,7 +128,7 @@ void *operator new(size_t size, std::align_val_t align, Arena &arena) {
arena.impl->capacity * 2) arena.impl->capacity * 2)
: 0)), : 0)),
16); 16);
auto *impl = (Arena::ArenaImpl *)malloc(allocationSize); auto *impl = (Arena::ArenaImpl *)safe_malloc(allocationSize);
impl->prev = arena.impl; impl->prev = arena.impl;
impl->capacity = allocationSize - sizeof(Arena::ArenaImpl); impl->capacity = allocationSize - sizeof(Arena::ArenaImpl);
impl->used = 0; impl->used = 0;
@@ -158,8 +164,6 @@ template <class T> struct ArenaAlloc {
} }
void deallocate(T *, size_t) noexcept {} void deallocate(T *, size_t) noexcept {}
private:
}; };
template <class T> using Vector = std::vector<T, ArenaAlloc<T>>; template <class T> using Vector = std::vector<T, ArenaAlloc<T>>;
@@ -393,9 +397,7 @@ private:
std::span<const uint8_t> bytecode; std::span<const uint8_t> bytecode;
}; };
inline Arbitrary gArbitrary; Arbitrary gArbitrary;
void initFuzz(const uint8_t *data, size_t size);
uint32_t Arbitrary::bounded(uint32_t s) { uint32_t Arbitrary::bounded(uint32_t s) {
if (s == 1) { if (s == 1) {
@@ -621,7 +623,7 @@ struct Node {
Node *createNode(const Key &key, Node *parent, int64_t pointVersion, Node *createNode(const Key &key, Node *parent, int64_t pointVersion,
Random &rand) { Random &rand) {
assert(key.len <= std::numeric_limits<int>::max()); assert(key.len <= std::numeric_limits<int>::max());
Node *result = (Node *)malloc(sizeof(Node) + key.len); Node *result = (Node *)safe_malloc(sizeof(Node) + key.len);
result->maxVersion = pointVersion; result->maxVersion = pointVersion;
result->pointVersion = pointVersion; result->pointVersion = pointVersion;
result->child[0] = nullptr; result->child[0] = nullptr;
@@ -1155,7 +1157,7 @@ void ConflictSet::setOldestVersion(int64_t oldestVersion) {
} }
ConflictSet::ConflictSet(int64_t oldestVersion, uint64_t seed) ConflictSet::ConflictSet(int64_t oldestVersion, uint64_t seed)
: impl(new(malloc(sizeof(Impl))) Impl{oldestVersion, seed}) {} : impl(new(safe_malloc(sizeof(Impl))) Impl{oldestVersion, seed}) {}
ConflictSet::~ConflictSet() { ConflictSet::~ConflictSet() {
if (impl) { if (impl) {
@@ -1194,7 +1196,7 @@ ConflictSet_setOldestVersion(void *cs, int64_t oldestVersion) {
} }
__attribute__((__visibility__("default"))) void * __attribute__((__visibility__("default"))) void *
ConflictSet_create(int64_t oldestVersion, uint64_t seed) { ConflictSet_create(int64_t oldestVersion, uint64_t seed) {
return new (malloc(sizeof(ConflictSet::Impl))) return new (safe_malloc(sizeof(ConflictSet::Impl)))
ConflictSet::Impl{oldestVersion, seed}; ConflictSet::Impl{oldestVersion, seed};
} }
__attribute__((__visibility__("default"))) void ConflictSet_destroy(void *cs) { __attribute__((__visibility__("default"))) void ConflictSet_destroy(void *cs) {