Skip to content

abort() on malloc() failure in new with exceptions disabled #11079

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 10 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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/libcxx/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
else
#ifndef _LIBCPP_NO_EXCEPTIONS
throw std::bad_alloc();
#else
#ifdef __EMSCRIPTEN__
abort();
#else
break;
#endif
#endif
}
return p;
Expand Down
4 changes: 4 additions & 0 deletions system/lib/libcxxabi/src/stdlib_new_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ operator new(std::size_t size) _THROW_BAD_ALLOC
else
#ifndef _LIBCXXABI_NO_EXCEPTIONS
throw std::bad_alloc();
#else
#ifdef __EMSCRIPTEN__
abort();
#else
break;
#endif
#endif
}
return p;
Expand Down
23 changes: 23 additions & 0 deletions tests/core/test_memorygrowth_new_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <emscripten.h>
#include <stdio.h>
#include <vector>

EMSCRIPTEN_KEEPALIVE extern "C" void allocate_too_much() {
std::vector<int> x;
puts("allocating more than TOTAL_MEMORY; this will fail.");
x.resize(20 * 1024 * 1024);
puts("oh no, it didn't fail!");
}

int main() {
EM_ASM({
// Catch the failure here so we can report it.
try {
_allocate_too_much();
out("no error happened");
} catch (e) {
assert(("" + e).indexOf("abort") >= 0, "expect an abort");
out("an expected error occurred");
}
});
}
1 change: 1 addition & 0 deletions tests/core/test_memorygrowth_new_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
an expected error occurred
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,11 @@ def test_memorygrowth_3_force_fail_reallocBuffer(self):
self.emcc_args += ['-Wno-almost-asm', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'TEST_MEMORY_GROWTH_FAILS=1']
self.do_run_in_out_file_test('tests', 'core', 'test_memorygrowth_3')

def test_memorygrowth_new_error(self):
# test that C++ new properly errors if we fail to malloc
self.emcc_args += ['-Wno-almost-asm', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'MAXIMUM_MEMORY=18MB']
self.do_run_in_out_file_test('tests', 'core', 'test_memorygrowth_new_error')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does doesn't seem related to memory growth. Why not just not set any settings and leave the default 16Mb memory. The result should be the same.

Then we can just call this test something like: test_aborting_new? (The name "new error" makes it sounds to me like new is an adjective).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a comment - this is actually related to memory growth. When growth is disabled, we abort() in another code path (the code to grow memory looks very different in the two cases).


@no_asmjs()
@no_wasm2js('no WebAssembly.Memory()')
@no_asan('ASan alters the memory size')
Expand Down