@@ -41,11 +41,22 @@ static void raw_free(void* p) { ALLOW_C_FUNCTION(::free, ::fre
41
41
static const size_t malloc_alignment = 2 * sizeof (void *); // could we use max_align_t?
42
42
STATIC_ASSERT (is_aligned(sizeof (NMTPreInitAllocation), malloc_alignment));
43
43
44
+ // To keep matters simple we just raise a fatal error on OOM. Since preinit allocation
45
+ // is just used for pre-VM-initialization mallocs, none of which are optional, we don't
46
+ // need a finer grained error handling.
47
+ static void fail_oom (size_t size) {
48
+ vm_exit_out_of_memory (size, OOM_MALLOC_ERROR, " VM early initialization phase" );
49
+ }
50
+
44
51
// --------- NMTPreInitAllocation --------------
45
52
46
53
NMTPreInitAllocation* NMTPreInitAllocation::do_alloc (size_t payload_size) {
47
54
const size_t outer_size = sizeof (NMTPreInitAllocation) + payload_size;
55
+ guarantee (outer_size > payload_size, " Overflow" );
48
56
void * p = raw_malloc (outer_size);
57
+ if (p == nullptr ) {
58
+ fail_oom (outer_size);
59
+ }
49
60
NMTPreInitAllocation* a = new (p) NMTPreInitAllocation (payload_size);
50
61
return a;
51
62
}
@@ -54,7 +65,11 @@ NMTPreInitAllocation* NMTPreInitAllocation::do_reallocate(NMTPreInitAllocation*
54
65
assert (old->next == NULL , " unhang from map first" );
55
66
// We just reallocate the old block, header and all.
56
67
const size_t new_outer_size = sizeof (NMTPreInitAllocation) + new_payload_size;
68
+ guarantee (new_outer_size > new_payload_size, " Overflow" );
57
69
void * p = raw_realloc (old, new_outer_size);
70
+ if (p == nullptr ) {
71
+ fail_oom (new_outer_size);
72
+ }
58
73
// re-stamp header with new size
59
74
NMTPreInitAllocation* a = new (p) NMTPreInitAllocation (new_payload_size);
60
75
return a;
0 commit comments