Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
2303: Add malloc_usable_size r=CodeMonkeyLeet a=thomasten



Co-authored-by: Thomas Tendyck <[email protected]>
  • Loading branch information
oeciteam and thomasten committed Jan 14, 2020
2 parents 347a1f2 + ed4bed3 commit 5ff92bd
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/LibcSupport.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inttypes.h | Partial | **Unsupported functions:** imaxabs(), imaxdiv() |
iso646.h | Yes | - |
limits.h | Yes | - |
locale.h | Partial | Only basic support for C/POSIX locale |
malloc.h | Partial | **Unsupported functions:** malloc_usable_size() |
malloc.h | Partial | - |
math.h | Partial | **Unsupported functions:** acosh(), asinh(), fmal(), lgamma(), lgammaf(), sinh(), sinhl(), tgamma() |
setjmp.h | Yes | - |
signal.h | No | - |
Expand Down
7 changes: 7 additions & 0 deletions enclave/core/debugmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ int oe_debug_posix_memalign(void** memptr, size_t alignment, size_t size)
return 0;
}

size_t oe_debug_malloc_usable_size(void* ptr)
{
if (!ptr)
return 0;
return _get_header(ptr)->size;
}

void oe_debug_malloc_dump(void)
{
_dump(true);
Expand Down
2 changes: 2 additions & 0 deletions enclave/core/debugmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ void* oe_debug_memalign(size_t alignment, size_t size);

int oe_debug_posix_memalign(void** memptr, size_t alignment, size_t size);

size_t oe_debug_malloc_usable_size(void* ptr);

#endif /* _OE_DEBUG_MALLOC_H */
7 changes: 7 additions & 0 deletions enclave/core/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ static int _dlmalloc_stats_fprintf(FILE* stream, const char* format, ...);
#define MEMALIGN oe_debug_memalign
#define POSIX_MEMALIGN oe_debug_posix_memalign
#define FREE oe_debug_free
#define MALLOC_USABLE_SIZE oe_debug_malloc_usable_size
#else
#define MALLOC dlmalloc
#define CALLOC dlcalloc
#define REALLOC dlrealloc
#define MEMALIGN dlmemalign
#define POSIX_MEMALIGN dlposix_memalign
#define FREE dlfree
#define MALLOC_USABLE_SIZE dlmalloc_usable_size
#endif

static oe_allocation_failure_callback_t _failure_callback;
Expand Down Expand Up @@ -153,6 +155,11 @@ void* oe_memalign(size_t alignment, size_t size)
return p;
}

size_t oe_malloc_usable_size(void* ptr)
{
return MALLOC_USABLE_SIZE(ptr);
}

/*
**==============================================================================
**
Expand Down
6 changes: 6 additions & 0 deletions include/openenclave/corelibc/bits/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ int posix_memalign(void** memptr, size_t alignment, size_t size)
return oe_posix_memalign(memptr, alignment, size);
}

OE_INLINE
size_t malloc_usable_size(void* ptr)
{
return oe_malloc_usable_size(ptr);
}

#endif /* _OE_BITS_MALLOC_H */
2 changes: 2 additions & 0 deletions include/openenclave/corelibc/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void* oe_memalign(size_t alignment, size_t size);

int oe_posix_memalign(void** memptr, size_t alignment, size_t size);

size_t oe_malloc_usable_size(void* ptr);

unsigned long int oe_strtoul(const char* nptr, char** endptr, int base);

long int oe_strtol(const char* nptr, char** endptr, int base);
Expand Down
29 changes: 29 additions & 0 deletions tests/memory/enc/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <openenclave/internal/globals.h>
#include <openenclave/internal/tests.h>

#include <malloc.h>
#include <stdint.h>
#include <stdlib.h>

Expand Down Expand Up @@ -168,3 +169,31 @@ void test_posix_memalign(void)
/* Should fail if alignment isn't possible. */
OE_TEST(posix_memalign(&ptr, max, 64) != 0);
}

void test_malloc_usable_size(void)
{
int* p1 = (int*)malloc(sizeof *p1);
OE_TEST(p1);
int* p2 = (int*)malloc(sizeof *p2);
OE_TEST(p2);

/* Ensure that p1 < p2 so that we can use p2 as upper bound. */
if (p1 > p2)
{
int* const tmp = p1;
p1 = p2;
p2 = tmp;
}
OE_TEST(p1 < p2);

const size_t s1 = malloc_usable_size(p1);
OE_TEST(sizeof *p1 <= s1 && s1 <= (size_t)(p2 - p1) * sizeof *p1);

OE_TEST(sizeof *p2 <= malloc_usable_size(p2));

const size_t end = s1 / sizeof *p1;
_set_buffer(p1, 0, end);
_check_buffer(p1, 0, end);
free(p1);
free(p2);
}
1 change: 1 addition & 0 deletions tests/memory/host/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static void _malloc_basic_test(oe_enclave_t* enclave)
OE_TEST(test_realloc(enclave) == OE_OK);
OE_TEST(test_memalign(enclave) == OE_OK);
OE_TEST(test_posix_memalign(enclave) == OE_OK);
OE_TEST(test_malloc_usable_size(enclave) == OE_OK);
}

static void _malloc_stress_test_single_thread(
Expand Down
1 change: 1 addition & 0 deletions tests/memory/memory.edl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enclave {
public void test_realloc();
public void test_memalign();
public void test_posix_memalign();
public void test_malloc_usable_size();

public void init_malloc_stress_test();
public void malloc_stress_test(int threads);
Expand Down

0 comments on commit 5ff92bd

Please sign in to comment.