Skip to content

Fix emmalloc tests to allow a bit of slop #10183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions system/lib/emmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ static int acquire_and_attempt_region_resize(Region *region, size_t size)

void *emmalloc_aligned_realloc(void *ptr, size_t alignment, size_t size)
{
#ifdef EMMALLOC_DEBUG_LOG
MAIN_THREAD_ASYNC_EM_ASM(console.log('aligned_realloc(ptr=' + $0.toString(16) + ', alignment=' + $1 + ', size=' + $2), ptr, alignment, size);
#endif

if (!ptr)
return emmalloc_memalign(alignment, size);

Expand Down
105 changes: 14 additions & 91 deletions tests/core/test_emmalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,12 @@

extern "C" void emmalloc_blank_slate_from_orbit();

// Test emmalloc internals, but through the external interface. We expect
// very specific outputs here based on the internals, this test would not
// pass in another malloc.

void* check_where_we_would_malloc(size_t size) {
void* temp = malloc(size);
free(temp);
return temp;
}

void check_where_we_would_malloc(size_t size, void* expected) {
void* temp = malloc(size);
assert(temp == expected);
free(temp);
}

void stage(const char* name) {
EM_ASM({
out('\n>> ' + UTF8ToString($0) + '\n');
}, name);
}

const size_t ALLOCATION_UNIT = 8;

void basics() {
stage("basics");
stage("allocate 0");
Expand All @@ -55,83 +37,30 @@ void basics() {
stage("allocate 10");
assert(second == first);
void* third = malloc(10);
assert(size_t(third) == size_t(first) + ((100 + ALLOCATION_UNIT - 1)&(-ALLOCATION_UNIT)) + ALLOCATION_UNIT); // allocation units are multiples of ALLOCATION_UNIT
assert(!emmalloc_validate_memory_regions());
stage("allocate 10 more");
void* four = malloc(10);
assert(size_t(four) == size_t(third) + (2*ALLOCATION_UNIT) + ALLOCATION_UNIT); // payload (10 = 2 allocation units) and metadata
assert(!emmalloc_validate_memory_regions());
stage("free the first");
free(second);
stage("several temp alloc/frees");
// we reuse the first area, despite stuff later.
for (int i = 0; i < 4; i++) {
check_where_we_would_malloc(100, first);
}
stage("free all");
free(third);
free(four);
stage("allocate various sizes to see they all start at the start");
for (int i = 1; i < 1500; i++) {
check_where_we_would_malloc(i, first);
}
}

void blank_slate() {
stage("blank_slate");
emmalloc_blank_slate_from_orbit();
void* ptr = malloc(0);
free(ptr);
for (int i = 0; i < 3; i++) {
void* two = malloc(0);
assert(two == ptr);
free(two);
}
for (int i = 0; i < 3; i++) {
emmalloc_blank_slate_from_orbit();
void* two = malloc(0);
// emmalloc_blank_slate_from_orbit clears out the free list without freeing the blocks on it
// Effectively, memory is leaked and we do not expect the pointers to be the same
assert(two != ptr);
free(two);
}
assert(!emmalloc_validate_memory_regions());
}

void previous_sbrk() {
stage("previous_sbrk");
emmalloc_blank_slate_from_orbit();
void* old = sbrk(0);
assert((size_t)old % ALLOCATION_UNIT == 0);
assert((size_t)old % 4 == 0);
sbrk(3); // unalign things
void* other = malloc(10);
free(other);
assert(other != old);
}

void min_alloc() {
stage("min_alloc");
emmalloc_blank_slate_from_orbit();
void* start = check_where_we_would_malloc(1);
for (int i = 1; i < 100; i++) {
void* temp = malloc(i);
void* expected = (char*)start + ALLOCATION_UNIT + ALLOCATION_UNIT * ((i + ALLOCATION_UNIT - 1) / ALLOCATION_UNIT);
check_where_we_would_malloc(1, expected);
free(temp);
}
}

void space_at_end() {
stage("space_at_end");
emmalloc_blank_slate_from_orbit();
void* start = check_where_we_would_malloc(1);
for (int i = 1; i < 50; i++) {
for (int j = 1; j < 50; j++) {
void* temp = malloc(i);
free(temp);
check_where_we_would_malloc(j, start);
}
}
}

void calloc() {
void test_calloc() {
stage("calloc");
emmalloc_blank_slate_from_orbit();
char* ptr = (char*)malloc(10);
Expand All @@ -142,7 +71,7 @@ void calloc() {
assert(ptr[0] == 0);
}

void realloc() {
void test_realloc() {
stage("realloc0");
emmalloc_blank_slate_from_orbit();
for (int i = 0; i < 2; i++) {
Expand All @@ -168,18 +97,16 @@ void realloc() {
}
stage("realloc1");
emmalloc_blank_slate_from_orbit();
{
// realloc of NULL is like malloc
void* ptr = check_where_we_would_malloc(10);
assert(realloc(NULL, 10) == ptr);
}

// realloc of NULL is like malloc
assert(realloc(NULL, 10) != 0);

stage("realloc2");
emmalloc_blank_slate_from_orbit();
{
// realloc to 0 is like free
void* ptr = malloc(10);
assert(realloc(ptr, 0) == NULL);
assert(check_where_we_would_malloc(10) == ptr);
}
stage("realloc3");
emmalloc_blank_slate_from_orbit();
Expand Down Expand Up @@ -230,7 +157,7 @@ void aligned() {
void randoms() {
stage("randoms");
emmalloc_blank_slate_from_orbit();
void* start = check_where_we_would_malloc(10);
void* start = malloc(10);
const int N = 1000;
const int BINS = 128;
void* bins[BINS];
Expand Down Expand Up @@ -285,20 +212,16 @@ void randoms() {
for (int i = 0; i < BINS; i++) {
if (bins[i]) free(bins[i]);
}
// it's all freed, should be a blank slate
assert(check_where_we_would_malloc(10) == start);
assert(!emmalloc_validate_memory_regions());
}

int main() {
stage("beginning");

basics();
blank_slate();
previous_sbrk();
min_alloc();
space_at_end();
calloc();
realloc();
test_calloc();
test_realloc();
aligned();
randoms();

Expand Down
7 changes: 6 additions & 1 deletion tests/core/test_emmalloc_memory_statistics.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <stdio.h>
#include <emscripten/emmalloc.h>

template<typename T>
T round_to_4k(T val){
return (T)(((size_t)val + 4095) & ~4095);
}

int main()
{
void *ptr = malloc(32*1024*1024);
Expand All @@ -21,5 +26,5 @@ int main()
for(int i = 0; i < 32; ++i)
printf("%zu ", freeMemorySizeMap[i]);
printf("\n");
printf("%zu\n", emmalloc_unclaimed_heap_memory());
printf("%zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
}
2 changes: 1 addition & 1 deletion tests/core/test_emmalloc_memory_statistics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
4210892
3
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
21997632
21999616
57 changes: 33 additions & 24 deletions tests/core/test_emmalloc_trim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,60 @@
#include <emscripten/emmalloc.h>
#include <emscripten/heap.h>

template<typename T>
T round_to_4k(T val){
return (T)(((size_t)val + 4095) & ~4095);
}

int main()
{
printf("heap size: %zu\n", emscripten_get_heap_size());
printf("dynamic heap 0: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 0: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 0: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 0: %p\n", round_to_4k(sbrk(0)));

void *ptr = malloc(32*1024*1024);
void *ptr2 = malloc(4*1024*1024);
printf("%d\n", (int)(ptr && ptr2));
printf("dynamic heap 1: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 1: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 1: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 1: %p\n", sbrk(0));
printf("dynamic heap 1: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 1: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 1: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 1: %p\n", round_to_4k(sbrk(0)));

int success = emmalloc_trim(0);
printf("1st trim: %d\n", success);
printf("dynamic heap 1: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 1: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 1: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 1: %p\n", sbrk(0));
printf("dynamic heap 1: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 1: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 1: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 1: %p\n", round_to_4k(sbrk(0)));

success = emmalloc_trim(0);
printf("2nd trim: %d\n", success);
printf("dynamic heap 2: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 2: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 2: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 2: %p\n", sbrk(0));
printf("dynamic heap 2: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 2: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 2: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 2: %p\n", round_to_4k(sbrk(0)));
free(ptr2);

success = emmalloc_trim(100000);
printf("3rd trim: %d\n", success);
printf("dynamic heap 3: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 3: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 3: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 3: %p\n", sbrk(0));
printf("dynamic heap 3: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 3: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 3: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 3: %p\n", round_to_4k(sbrk(0)));

success = emmalloc_trim(100000);
printf("4th trim: %d\n", success);
printf("dynamic heap 4: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 4: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 4: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 4: %p\n", sbrk(0));
printf("dynamic heap 4: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 4: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 4: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 4: %p\n", round_to_4k(sbrk(0)));

success = emmalloc_trim(0);
printf("5th trim: %d\n", success);
printf("dynamic heap 5: %zu\n", emmalloc_dynamic_heap_size());
printf("free dynamic memory 5: %zu\n", emmalloc_free_dynamic_memory());
printf("unclaimed heap memory 5: %zu\n", emmalloc_unclaimed_heap_memory());
printf("sbrk 5: %p\n", sbrk(0));
printf("dynamic heap 5: %zu\n", round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory 5: %zu\n", round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory 5: %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk 5: %p\n", round_to_4k(sbrk(0)));
}
46 changes: 25 additions & 21 deletions tests/core/test_emmalloc_trim.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
heap size: 134217728
dynamic heap 0: 4096
free dynamic memory 0: 4096
unclaimed heap memory 0: 2142171136
sbrk 0: 0x502000
1
dynamic heap 1: 37748880
free dynamic memory 1: 84
unclaimed heap memory 1: 2104421504
sbrk 1: 0x2901380
dynamic heap 1: 37752832
free dynamic memory 1: 4096
unclaimed heap memory 1: 2104422400
sbrk 1: 0x2902000
1st trim: 1
dynamic heap 1: 37748788
dynamic heap 1: 37752832
free dynamic memory 1: 0
unclaimed heap memory 1: 2104421596
sbrk 1: 0x2901324
unclaimed heap memory 1: 2104422400
sbrk 1: 0x2902000
2nd trim: 0
dynamic heap 2: 37748788
dynamic heap 2: 37752832
free dynamic memory 2: 0
unclaimed heap memory 2: 2104421596
sbrk 2: 0x2901324
unclaimed heap memory 2: 2104422400
sbrk 2: 0x2902000
3rd trim: 1
dynamic heap 3: 33654492
free dynamic memory 3: 100008
unclaimed heap memory 3: 2108515892
sbrk 3: 0x25199cc
dynamic heap 3: 33656832
free dynamic memory 3: 102400
unclaimed heap memory 3: 2108518400
sbrk 3: 0x251a000
4th trim: 0
dynamic heap 4: 33654492
free dynamic memory 4: 100008
unclaimed heap memory 4: 2108515892
sbrk 4: 0x25199cc
dynamic heap 4: 33656832
free dynamic memory 4: 102400
unclaimed heap memory 4: 2108518400
sbrk 4: 0x251a000
5th trim: 1
dynamic heap 5: 33554476
dynamic heap 5: 33558528
free dynamic memory 5: 0
unclaimed heap memory 5: 2108615908
sbrk 5: 0x250131c
unclaimed heap memory 5: 2108616704
sbrk 5: 0x2502000
Loading