diff --git a/test/core/test_emmalloc_memory_statistics.c b/test/core/test_emmalloc_memory_statistics.c new file mode 100644 index 0000000000000..4ff4d7011d5bd --- /dev/null +++ b/test/core/test_emmalloc_memory_statistics.c @@ -0,0 +1,22 @@ +#include +#include + +size_t round_to_4k(size_t val) { + return (val + 4095) & ~4095; +} + +int main() { + void *ptr = malloc(32*1024*1024); + void *ptr2 = malloc(4*1024*1024); + void *ptr3 = malloc(64*1024*1024); + void *ptr4 = malloc(16*1024); + void *ptr5 = malloc(2*1024*1024); + printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5)); + free(ptr2); + free(ptr4); + printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions()); + printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size()); + printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory()); + emmalloc_dump_free_dynamic_memory_fragmentation_map(); + printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory())); +} diff --git a/test/core/test_emmalloc_memory_statistics.cpp b/test/core/test_emmalloc_memory_statistics.cpp deleted file mode 100644 index 90faa422ca375..0000000000000 --- a/test/core/test_emmalloc_memory_statistics.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -template -T round_to_4k(T val){ - return (T)(((size_t)val + 4095) & ~4095); -} - -int main() -{ - void *ptr = malloc(32*1024*1024); - void *ptr2 = malloc(4*1024*1024); - void *ptr3 = malloc(64*1024*1024); - void *ptr4 = malloc(16*1024); - void *ptr5 = malloc(2*1024*1024); - printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5)); - free(ptr2); - free(ptr4); - printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions()); - printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size()); - printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory()); - emmalloc_dump_free_dynamic_memory_fragmentation_map(); - printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory())); -} diff --git a/test/core/test_mallinfo.c b/test/core/test_mallinfo.c new file mode 100644 index 0000000000000..7182bda2cde4a --- /dev/null +++ b/test/core/test_mallinfo.c @@ -0,0 +1,55 @@ +// Copyright 2016 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +// Tests that we can use the dlmalloc mallinfo() function to obtain information +// about malloc()ed blocks and compute how much memory is used/freed. + +#include +#include +#include +#include +#include +#include + +size_t getTotalMemory() { + return (size_t)EM_ASM_PTR(return HEAP8.length); +} + +size_t getFreeMemory() { + struct mallinfo i = mallinfo(); + uintptr_t totalMemory = getTotalMemory(); + uintptr_t dynamicTop = (uintptr_t)sbrk(0); + return totalMemory - dynamicTop + i.fordblks; +} + +int main() { + size_t total_mem = getTotalMemory(); + size_t free_mem = getFreeMemory(); + + printf("Before allocation:\n"); + printf("Total memory: %zu bytes\n", getTotalMemory()); + printf("Free memory: %zu bytes\n", getFreeMemory()); + printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); + assert(getTotalMemory() == total_mem); + assert(getFreeMemory() == free_mem); + + void *ptr = malloc(1024*1024); + printf("\nAfter 1MB allocation: %p\n", ptr); + printf("Total memory: %zu bytes\n", getTotalMemory()); + printf("Free memory: %zu bytes\n", getFreeMemory()); + printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); + assert(getTotalMemory() == total_mem); + assert(getFreeMemory() < free_mem); + + free(ptr); + printf("\nAfter freeing:\n"); + printf("Total memory: %zu bytes\n", getTotalMemory()); + printf("Free memory: %zu bytes\n", getFreeMemory()); + printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); + assert(getTotalMemory() == total_mem); + assert(getFreeMemory() == free_mem); + + printf("OK.\n"); +} diff --git a/test/core/test_mallinfo.out b/test/core/test_mallinfo.out new file mode 100644 index 0000000000000..d5c32f4ac0c74 --- /dev/null +++ b/test/core/test_mallinfo.out @@ -0,0 +1 @@ +OK. diff --git a/test/core/test_memorygrowth.c b/test/core/test_memorygrowth.c index 4e941c7341112..80a7f12254610 100644 --- a/test/core/test_memorygrowth.c +++ b/test/core/test_memorygrowth.c @@ -13,24 +13,25 @@ int main(int argc, char **argv) { - char *buf1 = (char*)malloc(100); - char *data1 = (char*)"hello"; + char *buf1 = malloc(100); + char *data1 = "hello"; memcpy(buf1, data1, strlen(data1)+1); - float *buf2 = (float*)malloc(100); + float *buf2 = malloc(100); float pie = 4.955; memcpy(buf2, &pie, sizeof(float)); printf("*pre: %s,%.3f*\n", buf1, buf2[0]); - int totalMemory = EM_ASM_INT({ return HEAP8.length }); - char *buf3 = (char*)malloc(totalMemory+1); - buf3[argc] = (long)buf2; - if (argc % 7 == 6) printf("%ld\n", (long)memcpy(buf3, buf1, argc)); - char *buf4 = (char*)malloc(100); - float *buf5 = (float*)malloc(100); + size_t totalMemory = (size_t)EM_ASM_PTR({ return HEAP8.length }); + //printf("totalMemory: %zu, argc: %d\n", totalMemory, argc); + char *buf3 = malloc(totalMemory+1); + assert(buf3 && "fail to perform large allocation"); + if (argc % 7 == 6) printf("%p\n", memcpy(buf3, buf1, argc)); + char *buf4 = malloc(100); + float *buf5 = malloc(100); //printf("totalMemory: %d bufs: %d,%d,%d,%d,%d\n", totalMemory, buf1, buf2, buf3, buf4, buf5); - assert((long)buf4 > (long)totalMemory && (long)buf5 > (long)totalMemory); + assert((intptr_t)buf4 > (intptr_t)totalMemory && (intptr_t)buf5 > (intptr_t)totalMemory); printf("*%s,%.3f*\n", buf1, buf2[0]); // the old heap data should still be there diff --git a/test/embind/test_i64_binding.cpp b/test/embind/test_i64_binding.cpp index cb9758b1feb8b..cf8cb54b98ef6 100644 --- a/test/embind/test_i64_binding.cpp +++ b/test/embind/test_i64_binding.cpp @@ -13,28 +13,13 @@ using namespace emscripten; using namespace std; -void fail() -{ - cout << "fail\n"; -} - -void pass() -{ - cout << "pass\n"; -} +#define assert_js(X) assert(run_js(X)) void test(string message) { cout << "test:\n" << message << "\n"; } -void ensure(bool value) -{ - if (value) - pass(); - else - fail(); -} void execute_js(string js_code) { @@ -46,14 +31,14 @@ void execute_js(string js_code) }, js_code_pointer); } -void ensure_js(string js_code) +int run_js(string js_code) { js_code.append(";"); const char* js_code_pointer = js_code.c_str(); - ensure(EM_ASM_INT({ + return EM_ASM_INT({ var js_code = UTF8ToString($0); return eval(js_code); - }, js_code_pointer)); + }, js_code_pointer); } void ensure_js_throws(string js_code, string error_type) @@ -61,7 +46,7 @@ void ensure_js_throws(string js_code, string error_type) js_code.append(";"); const char* js_code_pointer = js_code.c_str(); const char* error_type_pointer = error_type.c_str(); - ensure(EM_ASM_INT({ + assert(EM_ASM_INT({ var js_code = UTF8ToString($0); var error_type = UTF8ToString($1); try { @@ -91,14 +76,14 @@ int main() test("vector"); val myval(std::vector{1, 2, 3, -4}); val::global().set("v64", myval); - ensure_js("v64.get(0) === 1n"); - ensure_js("v64.get(1) === 2n"); - ensure_js("v64.get(2) === 3n"); - ensure_js("v64.get(3) === -4n"); + assert_js("v64.get(0) === 1n"); + assert_js("v64.get(1) === 2n"); + assert_js("v64.get(2) === 3n"); + assert_js("v64.get(3) === -4n"); execute_js("v64.push_back(1234n)"); - ensure_js("v64.size() === 5"); - ensure_js("v64.get(4) === 1234n"); + assert_js("v64.size() === 5"); + assert_js("v64.get(4) === 1234n"); test("vector Cannot convert number to int64_t"); ensure_js_throws("v64.push_back(1234)", "TypeError"); @@ -109,14 +94,14 @@ int main() test("vector"); val myval2(vector{1, 2, 3, 4}); val::global().set("vU64", myval2); - ensure_js("vU64.get(0) === 1n"); - ensure_js("vU64.get(1) === 2n"); - ensure_js("vU64.get(2) === 3n"); - ensure_js("vU64.get(3) === 4n"); + assert_js("vU64.get(0) === 1n"); + assert_js("vU64.get(1) === 2n"); + assert_js("vU64.get(2) === 3n"); + assert_js("vU64.get(3) === 4n"); execute_js("vU64.push_back(1234n)"); - ensure_js("vU64.size() === 5"); - ensure_js("vU64.get(4) === 1234n"); + assert_js("vU64.size() === 5"); + assert_js("vU64.get(4) === 1234n"); test("vector Cannot convert number to uint64_t"); ensure_js_throws("vU64.push_back(1234)", "TypeError"); diff --git a/test/embind/test_i64_binding.out b/test/embind/test_i64_binding.out index d9ebc4ecf2b6a..b695f8cb703b5 100644 --- a/test/embind/test_i64_binding.out +++ b/test/embind/test_i64_binding.out @@ -1,33 +1,16 @@ start test: vector -pass -pass -pass -pass -pass -pass test: vector Cannot convert number to int64_t -pass test: vector Cannot convert bigint that is too big -pass test: vector -pass -pass -pass -pass -pass -pass test: vector Cannot convert number to uint64_t -pass test: vector Cannot convert bigint that is too big -pass test: vector Cannot convert bigint that is negative -pass end diff --git a/test/mallinfo.cpp b/test/mallinfo.cpp deleted file mode 100644 index c6d81e5f3cf01..0000000000000 --- a/test/mallinfo.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2016 The Emscripten Authors. All rights reserved. -// Emscripten is available under two separate licenses, the MIT license and the -// University of Illinois/NCSA Open Source License. Both these licenses can be -// found in the LICENSE file. - -// Tests that we can use the dlmalloc mallinfo() function to obtain information about malloc()ed blocks and compute how much memory is used/freed. - -#include -#include -#include -#include -#include - -struct s_mallinfo { - int arena; /* non-mmapped space allocated from system */ - int ordblks; /* number of free chunks */ - int smblks; /* always 0 */ - int hblks; /* always 0 */ - int hblkhd; /* space in mmapped regions */ - int usmblks; /* maximum total allocated space */ - int fsmblks; /* always 0 */ - int uordblks; /* total allocated space */ - int fordblks; /* total free space */ - int keepcost; /* releasable (via malloc_trim) space */ -}; - -extern "C" { - extern s_mallinfo mallinfo(); -} - -unsigned int getTotalMemory() -{ - return EM_ASM_INT(return HEAP8.length); -} - -unsigned int getFreeMemory() -{ - s_mallinfo i = mallinfo(); - uintptr_t totalMemory = getTotalMemory(); - uintptr_t dynamicTop = (uintptr_t)sbrk(0); - return totalMemory - dynamicTop + i.fordblks; -} - -int main() -{ - printf("Before allocation:\n"); - printf("Total memory: %u bytes\n", getTotalMemory()); - printf("Free memory: %u bytes\n", getFreeMemory()); - printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); - assert(getTotalMemory() == 16777216); - assert(getFreeMemory() >= 10000000); // 11529552 in test - - void *ptr = malloc(1024*1024); - printf("\nAfter 1MB allocation:\n"); - printf("Total memory: %u bytes\n", getTotalMemory()); - printf("Free memory: %u bytes\n", getFreeMemory()); - printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); - assert(getTotalMemory() == 16777216); - assert(getFreeMemory() >= 9000000); // 10480968 in test - - free(ptr); - printf("\nAfter freeing:\n"); - printf("Total memory: %u bytes\n", getTotalMemory()); - printf("Free memory: %u bytes\n", getFreeMemory()); - printf("Used: %u bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory()); - assert(getTotalMemory() == 16777216); - assert(getFreeMemory() >= 10000000); // 11529552 in test - - printf("OK.\n"); -} diff --git a/test/test_core.py b/test/test_core.py index fc801f362d567..d2fde2ab58ffd 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -978,16 +978,16 @@ def test_emmalloc_usable_size(self, *args): @no_asan('ASan does not support custom memory allocators') @no_lsan('LSan does not support custom memory allocators') @no_ubsan('UBSan changes memory consumption') - def test_emmalloc_memory_statistics(self, *args): + def test_emmalloc_memory_statistics(self): if self.is_wasm64(): out_suffix = '64' else: out_suffix = '' + self.set_setting('INITIAL_MEMORY', '128mb') self.set_setting('MALLOC', 'emmalloc') - self.emcc_args += ['-sINITIAL_MEMORY=128MB', '-g'] + list(args) - - self.do_core_test('test_emmalloc_memory_statistics.cpp', out_suffix=out_suffix) + self.emcc_args += ['-g'] + self.do_core_test('test_emmalloc_memory_statistics.c', out_suffix=out_suffix) @no_optimize('output is sensitive to optimization flags, so only test unoptimized builds') @no_wasm64('output is sensitive to absolute data size') @@ -7504,10 +7504,12 @@ def test2(): do_test(test2, level=2, prefix='hello_libcxx') - def test_embind(self): - # Verify that both the old `--bind` arg and the new `-lembind` arg work - for args in [['-lembind'], ['--bind']]: - create_file('test_embind.cpp', r''' + @parameterized({ + '': (['-lembind', '-sDYNAMIC_EXECUTION=0'],), + 'flag': (['--bind'],), + }) + def test_embind(self, args): + create_file('test_embind.cpp', r''' #include #include @@ -7522,8 +7524,8 @@ def test_embind(self): return 0; } - ''') - self.do_runf('test_embind.cpp', 'abs(-10): 10\nabs(-11): 11', emcc_args=args) + ''') + self.do_runf('test_embind.cpp', 'abs(-10): 10\nabs(-11): 11', emcc_args=args) @parameterized({ '': ([],), @@ -8691,7 +8693,7 @@ def test_brk(self): @no_asan('mallinfo is not part of ASan malloc') @no_lsan('mallinfo is not part of LSan malloc') def test_mallinfo(self): - self.do_runf(test_file('mallinfo.cpp'), 'OK.') + self.do_core_test('test_mallinfo.c') @no_asan('cannot replace malloc/free with ASan') @no_lsan('cannot replace malloc/free with LSan')