Skip to content

Commit 2d4b0ce

Browse files
committed
override add malloc_conceal api proposal
1 parent 5fd3288 commit 2d4b0ce

File tree

9 files changed

+111
-11
lines changed

9 files changed

+111
-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

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ 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*
64+
SNMALLOC_NAME_MANGLE(calloc_conceal)(size_t nmemb, size_t size)
65+
{
66+
bool overflow = false;
67+
size_t sz = bits::umul(size, nmemb, overflow);
68+
if (SNMALLOC_UNLIKELY(overflow))
69+
{
70+
return SNMALLOC_NAME_MANGLE(snmalloc_set_error)();
71+
}
72+
return ThreadAlloc::get().alloc<ZeroMem::YesZero, CoreDumpMem::NoDump>(sz);
73+
}
74+
#endif
75+
5776
SNMALLOC_EXPORT
5877
size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(
5978
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

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ 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+
madvise(p, size, MADV_NOCORE);
70+
}
71+
};
6672
};
6773
} // namespace snmalloc
6874
#endif

src/pal/pal_linux.h

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ namespace snmalloc
8585
madvise(p, size, MADV_FREE);
8686
}
8787
}
88+
89+
static void nodump(void* p, size_t size) noexcept
90+
{
91+
madvise(p, size, MADV_DONTDUMP);
92+
}
8893
};
8994
} // namespace snmalloc
9095
#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

+44-7
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,20 @@ 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(
91+
void *(*calloc_fn)(size_t, size_t),
92+
size_t nmemb,
93+
size_t size,
94+
int err,
95+
bool null)
9196
{
92-
printf("calloc(%zu, %zu) combined size %zu\n", nmemb, size, nmemb * size);
97+
printf("calloc");
98+
if (calloc_fn == our_calloc_conceal)
99+
printf("_conceal");
100+
101+
printf("(%zu, %zu) combined size %zu\n", nmemb, size, nmemb * size);
93102
errno = SUCCESS;
94-
void* p = our_calloc(nmemb, size);
103+
void* p = calloc_fn(nmemb, size);
95104

96105
if (p != nullptr)
97106
{
@@ -157,7 +166,7 @@ int main(int argc, char** argv)
157166
check_result(size + 1, 1, our_malloc(size + 1), SUCCESS, false);
158167
}
159168

160-
test_calloc(0, 0, SUCCESS, false);
169+
test_calloc(our_calloc, 0, 0, SUCCESS, false);
161170

162171
our_free(nullptr);
163172

@@ -173,10 +182,10 @@ int main(int argc, char** argv)
173182
if (overflow)
174183
break;
175184

176-
test_calloc(n, size, SUCCESS, false);
177-
test_calloc(n, 0, SUCCESS, false);
185+
test_calloc(our_calloc, n, size, SUCCESS, false);
186+
test_calloc(our_calloc, n, 0, SUCCESS, false);
178187
}
179-
test_calloc(0, size, SUCCESS, false);
188+
test_calloc(our_calloc, 0, size, SUCCESS, false);
180189
}
181190

182191
for (smallsizeclass_t sc = 0; sc < NUM_SMALL_SIZECLASSES; sc++)
@@ -210,6 +219,34 @@ int main(int argc, char** argv)
210219

211220
test_realloc(our_malloc(64), 4194304, SUCCESS, false);
212221

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

0 commit comments

Comments
 (0)