Home | History | Annotate | Line # | Download | only in cpp
      1  1.1  christos #include <stdio.h>
      2  1.1  christos 
      3  1.1  christos #include "test/jemalloc_test.h"
      4  1.1  christos 
      5  1.1  christos static_assert(JEMALLOC_INFALLIBLE_NEW == 1);
      6  1.1  christos 
      7  1.1  christos /*
      8  1.1  christos  * Verifies that, when jemalloc is built with --enable-cxx-infallible-new,
      9  1.1  christos  * throwing operator new on OOM aborts via safety_check_fail. The test hook
     10  1.1  christos  * intercepts the abort and asserts the size-bearing message prefix.
     11  1.1  christos  */
     12  1.1  christos bool fake_abort_called;
     13  1.1  christos void
     14  1.1  christos fake_abort(const char *message) {
     15  1.1  christos 	const char *expected_start = "<jemalloc>: Allocation of size";
     16  1.1  christos 	if (strncmp(message, expected_start, strlen(expected_start)) != 0) {
     17  1.1  christos 		abort();
     18  1.1  christos 	}
     19  1.1  christos 	fake_abort_called = true;
     20  1.1  christos }
     21  1.1  christos 
     22  1.1  christos static bool
     23  1.1  christos own_operator_new(void) {
     24  1.1  christos 	uint64_t before, after;
     25  1.1  christos 	size_t   sz = sizeof(before);
     26  1.1  christos 
     27  1.1  christos 	/* thread.allocated is always available, even w/o config_stats. */
     28  1.1  christos 	expect_d_eq(mallctl("thread.allocated", (void *)&before, &sz, NULL, 0),
     29  1.1  christos 	    0, "Unexpected mallctl failure reading stats");
     30  1.1  christos 	void *volatile ptr = ::operator new((size_t)8);
     31  1.1  christos 	expect_ptr_not_null(ptr, "Unexpected allocation failure");
     32  1.1  christos 	expect_d_eq(mallctl("thread.allocated", (void *)&after, &sz, NULL, 0),
     33  1.1  christos 	    0, "Unexpected mallctl failure reading stats");
     34  1.1  christos 
     35  1.1  christos 	return (after != before);
     36  1.1  christos }
     37  1.1  christos 
     38  1.1  christos TEST_BEGIN(test_failing_alloc) {
     39  1.1  christos 	test_hooks_safety_check_abort = &fake_abort;
     40  1.1  christos 
     41  1.1  christos 	/*
     42  1.1  christos 	 * Not owning operator new is only expected to happen on MinGW which
     43  1.1  christos 	 * does not support operator new / delete replacement.
     44  1.1  christos 	 */
     45  1.1  christos #ifdef _WIN32
     46  1.1  christos 	test_skip_if(!own_operator_new());
     47  1.1  christos #else
     48  1.1  christos 	expect_true(own_operator_new(), "No operator new overload");
     49  1.1  christos #endif
     50  1.1  christos 	void *volatile ptr = (void *)1;
     51  1.1  christos 	try {
     52  1.1  christos 		/* Too big of an allocation to succeed. */
     53  1.1  christos 		ptr = ::operator new((size_t)-1);
     54  1.1  christos 	} catch (...) {
     55  1.1  christos 		abort();
     56  1.1  christos 	}
     57  1.1  christos 	expect_ptr_null(ptr, "Allocation should have failed");
     58  1.1  christos 	expect_b_eq(fake_abort_called, true, "Abort hook not invoked");
     59  1.1  christos 	test_hooks_safety_check_abort = NULL;
     60  1.1  christos }
     61  1.1  christos TEST_END
     62  1.1  christos 
     63  1.1  christos int
     64  1.1  christos main(void) {
     65  1.1  christos 	return test(test_failing_alloc);
     66  1.1  christos }
     67