Skip to content

Commit 368e510

Browse files
committed
override add malloc_conceal api proposal
1 parent 5fd3288 commit 368e510

File tree

9 files changed

+107
-11
lines changed

9 files changed

+107
-11
lines changed

src/mem/localalloc.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,18 @@ namespace snmalloc
418418
/**
419419
* Allocate memory of a dynamically known size.
420420
*/
421-
template<ZeroMem zero_mem = NoZero>
421+
template<ZeroMem zero_mem = NoZero, CoreDumpMem core_dump = YesDump>
422422
SNMALLOC_FAST_PATH ALLOCATOR void* alloc(size_t size)
423423
{
424+
void* result = nullptr;
424425
#ifdef SNMALLOC_PASS_THROUGH
425426
// snmalloc guarantees a lot of alignment, so we can depend on this
426427
// make pass through call aligned_alloc with the alignment snmalloc
427428
// would guarantee.
428-
void* result = external_alloc::aligned_alloc(
429+
result = external_alloc::aligned_alloc(
429430
natural_alignment(size), round_size(size));
431+
if constexpr (core_dump == NoDump)
432+
SharedStateHandle::Pal::nodump(result, size);
430433
if constexpr (zero_mem == YesZero)
431434
memset(result, 0, size);
432435
return result;
@@ -438,11 +441,17 @@ namespace snmalloc
438441
{
439442
// Small allocations are more likely. Improve
440443
// branch prediction by placing this case first.
441-
return capptr_reveal(small_alloc<zero_mem>(size));
444+
result = capptr_reveal(small_alloc<zero_mem>(size));
445+
if constexpr (core_dump == NoDump)
446+
SharedStateHandle::Pal::nodump(result, size);
447+
return result;
442448
}
443449

444-
return capptr_reveal(alloc_not_small<zero_mem>(size));
450+
result = capptr_reveal(alloc_not_small<zero_mem>(size));
451+
if constexpr (core_dump == NoDump)
452+
SharedStateHandle::Pal::nodump(result, size);
445453
#endif
454+
return result;
446455
}
447456

448457
/**

src/override/malloc.cc

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ extern "C"
5454
return ThreadAlloc::get().alloc<ZeroMem::YesZero>(sz);
5555
}
5656

57+
#if !defined(__OpenBSD__)
58+
SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(malloc_conceal)(size_t size)
59+
{
60+
return ThreadAlloc::get().alloc<NoZero, CoreDumpMem::NoDump>(size);
61+
}
62+
63+
SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(calloc_conceal)(size_t nmemb, size_t size)
64+
{
65+
bool overflow = false;
66+
size_t sz = bits::umul(size, nmemb, overflow);
67+
if (SNMALLOC_UNLIKELY(overflow))
68+
{
69+
return SNMALLOC_NAME_MANGLE(snmalloc_set_error)();
70+
}
71+
return ThreadAlloc::get().alloc<ZeroMem::YesZero, CoreDumpMem::NoDump>(sz);
72+
}
73+
#endif
74+
5775
SNMALLOC_EXPORT
5876
size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(
5977
MALLOC_USABLE_SIZE_QUALIFIER void* ptr)

src/pal/pal_consts.h

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ namespace snmalloc
9494
YesZero
9595
};
9696

97+
enum CoreDumpMem
98+
{
99+
NoDump,
100+
YesDump
101+
};
102+
97103
/**
98104
* Default Tag ID for the Apple class
99105
*/

src/pal/pal_freebsd.h

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ namespace snmalloc
6363
~static_cast<unsigned int>(CHERI_PERM_CHERIABI_VMMAP)));
6464
}
6565
# endif
66+
67+
static void nodump(void* p, size_t size) noexcept
68+
{
69+
SNMALLOC_ASSERT(is_aligned_block<page_size>(p, size));
70+
madvise(p, size, MADV_NOCORE)
71+
}
72+
};
6673
};
6774
} // namespace snmalloc
6875
#endif

src/pal/pal_linux.h

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ namespace snmalloc
8585
madvise(p, size, MADV_FREE);
8686
}
8787
}
88+
89+
static void nodump(void* p, size_t size) noexcept
90+
{
91+
SNMALLOC_ASSERT(is_aligned_block<page_size>(p, size));
92+
madvise(p, size, MADV_DONTDUMP);
93+
}
8894
};
8995
} // namespace snmalloc
9096
#endif

src/pal/pal_open_enclave.h

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ namespace snmalloc
4141
oe_memset_s(p, size, 0, size);
4242
}
4343

44+
static void dump(void* p, size_t size) noexcept
45+
{
46+
UNUSED(p);
47+
UNUSED(size);
48+
}
49+
4450
/**
4551
* Source of Entropy
4652
*/

src/pal/pal_posix.h

+6
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ namespace snmalloc
318318
return nullptr;
319319
}
320320

321+
static void nodump(void* p, size_t size) noexcept
322+
{
323+
UNUSED(p);
324+
UNUSED(size);
325+
}
326+
321327
/**
322328
* Source of Entropy
323329
*

src/pal/pal_windows.h

+6
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ namespace snmalloc
195195
return VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
196196
}
197197

198+
static void nodump(void* p, size_t size) noexcept
199+
{
200+
UNUSED(p);
201+
UNUSED(size);
202+
}
203+
198204
/**
199205
* Source of Entropy
200206
*/

src/test/func/malloc/malloc.cc

+39-7
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
8787
our_free(p);
8888
}
8989

90-
void test_calloc(size_t nmemb, size_t size, int err, bool null)
90+
void test_calloc(void *(*calloc_fn)(size_t, size_t), size_t nmemb, size_t size, int err, bool null)
9191
{
92-
printf("calloc(%zu, %zu) combined size %zu\n", nmemb, size, nmemb * size);
92+
printf("calloc");
93+
if (calloc_fn == our_calloc_conceal)
94+
printf("_conceal");
95+
96+
printf("(%zu, %zu) combined size %zu\n", nmemb, size, nmemb * size);
9397
errno = SUCCESS;
94-
void* p = our_calloc(nmemb, size);
98+
void* p = calloc_fn(nmemb, size);
9599

96100
if (p != nullptr)
97101
{
@@ -157,7 +161,7 @@ int main(int argc, char** argv)
157161
check_result(size + 1, 1, our_malloc(size + 1), SUCCESS, false);
158162
}
159163

160-
test_calloc(0, 0, SUCCESS, false);
164+
test_calloc(our_calloc, 0, 0, SUCCESS, false);
161165

162166
our_free(nullptr);
163167

@@ -173,10 +177,10 @@ int main(int argc, char** argv)
173177
if (overflow)
174178
break;
175179

176-
test_calloc(n, size, SUCCESS, false);
177-
test_calloc(n, 0, SUCCESS, false);
180+
test_calloc(our_calloc, n, size, SUCCESS, false);
181+
test_calloc(our_calloc, n, 0, SUCCESS, false);
178182
}
179-
test_calloc(0, size, SUCCESS, false);
183+
test_calloc(our_calloc, 0, size, SUCCESS, false);
180184
}
181185

182186
for (smallsizeclass_t sc = 0; sc < NUM_SMALL_SIZECLASSES; sc++)
@@ -210,6 +214,34 @@ int main(int argc, char** argv)
210214

211215
test_realloc(our_malloc(64), 4194304, SUCCESS, false);
212216

217+
for (smallsizeclass_t sc = 0; sc < (MAX_SMALL_SIZECLASS_BITS + 4); sc++)
218+
{
219+
const size_t size = bits::one_at_bit(sc);
220+
printf("malloc_conceal: %zu\n", size);
221+
errno = SUCCESS;
222+
check_result(size, 1, our_malloc_conceal(size), SUCCESS, false);
223+
errno = SUCCESS;
224+
check_result(size + 1, 1, our_malloc_conceal(size + 1), SUCCESS, false);
225+
}
226+
227+
for (smallsizeclass_t sc = 0; sc < NUM_SMALL_SIZECLASSES; sc++)
228+
{
229+
const size_t size = sizeclass_to_size(sc);
230+
231+
bool overflow = false;
232+
for (size_t n = 1;
233+
bits::umul(size, n, overflow) <= MAX_SMALL_SIZECLASS_SIZE;
234+
n *= 5)
235+
{
236+
if (overflow)
237+
break;
238+
239+
test_calloc(our_calloc_conceal, n, size, SUCCESS, false);
240+
test_calloc(our_calloc_conceal, n, 0, SUCCESS, false);
241+
}
242+
test_calloc(our_calloc_conceal, 0, size, SUCCESS, false);
243+
}
244+
213245
test_posix_memalign(0, 0, EINVAL, true);
214246
test_posix_memalign(((size_t)-1) / 2, 0, EINVAL, true);
215247
test_posix_memalign(OS_PAGE_SIZE, sizeof(uintptr_t) / 2, EINVAL, true);

0 commit comments

Comments
 (0)