Skip to content

Commit

Permalink
Specify ContinuousArenaMalloc realloc alignment.
Browse files Browse the repository at this point in the history
This is mostly for symmetry with alignment (malloc), making it easier to
verify that we're not breaking existing alignment constraints.
  • Loading branch information
jacobbramley committed Jan 12, 2024
1 parent cc12483 commit 21b3c79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 12 additions & 6 deletions Source/WTF/wtf/ContinuousArenaMalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ void ContinuousArenaMalloc::initializePerThread()
#endif
}

void *ContinuousArenaMalloc::internalAllocateAligned(size_t alignment,
size_t size)
void *ContinuousArenaMalloc::internalAllocateAligned(size_t alignment, size_t size)
{
ASSERT((alignment & (alignment - 1)) == 0);
ASSERT(s_Initialized);

void * result = mallocx(size, MALLOCX_ALIGN(alignment) | MALLOCX_TCACHE_NONE | MALLOCX_ARENA(s_arenaIndex));
void * result = mallocx(
size,
MALLOCX_ALIGN(alignment) | MALLOCX_TCACHE_NONE | MALLOCX_ARENA(s_arenaIndex)
);
#if __has_feature(capabilities)
// If either of these fail, try disabling capability revocation.
// See: https://github.com/CTSRD-CHERI/cheribsd/issues/1964
Expand All @@ -136,14 +138,18 @@ void *ContinuousArenaMalloc::internalAllocateAligned(size_t alignment,
return result;
}

void *ContinuousArenaMalloc::internalReallocate(void *ptr, size_t size)
void *ContinuousArenaMalloc::internalReallocateAligned(void *ptr, size_t alignment, size_t size)
{
ASSERT(s_Initialized);
void * result = rallocx(ptr, size, MALLOCX_TCACHE_NONE | MALLOCX_ARENA(s_arenaIndex));
void * result = rallocx(
ptr,
size,
MALLOCX_ALIGN(alignment) | MALLOCX_TCACHE_NONE | MALLOCX_ARENA(s_arenaIndex)
);
#if __has_feature(capabilities)
// If either of these fail, try disabling capability revocation.
// See: https://github.com/CTSRD-CHERI/cheribsd/issues/1964
ASSERT(cheri_is_aligned(result, size));
ASSERT(cheri_is_aligned(result, alignment));
#ifdef __CHERI_PURE_CAPABILITY__
ASSERT(cheri_is_subset(result, cheri_ddc_get()));
#elif !defined(ASSERT_DISABLED)
Expand Down
6 changes: 3 additions & 3 deletions Source/WTF/wtf/ContinuousArenaMalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ContinuousArenaMalloc {
}

static void* realloc(void* p, size_t size) {
void *ret = internalReallocate(p, size);
void *ret = tryRealloc(p, size);

if (!ret) {
CRASH();
Expand All @@ -72,7 +72,7 @@ class ContinuousArenaMalloc {
}

static void* tryRealloc(void* p, size_t size) {
return internalReallocate(p, size);
return internalReallocateAligned(p, sizeof(void *), size);
}

static bool isWithin(size_t non_cap_ptr) {
Expand Down Expand Up @@ -128,7 +128,7 @@ class ContinuousArenaMalloc {
#endif

static void* internalAllocateAligned(size_t alignment, size_t size);
static void* internalReallocate(void *p, size_t size);
static void* internalReallocateAligned(void *p, size_t alignment, size_t size);
static void internalFree(void* ptr);

// True iff [addr, addr+size) is a subset of or equal to [s_Start, s_End).
Expand Down

0 comments on commit 21b3c79

Please sign in to comment.