Add opt.experimental_infallible_new.

This allows a guarantee that operator new never throws.

Fix the .gitignore rules to include test/integration/cpp while we're here.
This commit is contained in:
David Goldblatt
2021-06-21 13:40:30 -07:00
committed by David Goldblatt
parent 0689448b1e
commit 4452a4812f
14 changed files with 134 additions and 3 deletions

View File

@@ -1,4 +1,3 @@
#include <memory>
#include "test/jemalloc_test.h"
TEST_BEGIN(test_basic) {

View File

@@ -0,0 +1,23 @@
#include <memory>
#include "test/jemalloc_test.h"
TEST_BEGIN(test_failing_alloc) {
bool saw_exception = false;
try {
/* Too big of an allocation to succeed. */
void *volatile ptr = ::operator new((size_t)-1);
(void)ptr;
} catch (...) {
saw_exception = true;
}
expect_true(saw_exception, "Didn't get a failure");
}
TEST_END
int
main(void) {
return test(
test_failing_alloc);
}

View File

@@ -0,0 +1,8 @@
#!/bin/sh
XMALLOC_STR=""
if [ "x${enable_xmalloc}" = "x1" ] ; then
XMALLOC_STR="xmalloc:false,"
fi
export MALLOC_CONF="${XMALLOC_STR}experimental_infallible_new:false"

View File

@@ -0,0 +1,61 @@
#include <stdio.h>
/*
* We can't test C++ in unit tests, and we can't change the safety check failure
* hook in integration tests. So we check that we *actually* abort on failure,
* by forking and checking the child process exit code.
*/
/* It's a unix system? */
#ifdef __unix__
/* I know this! */
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
static const bool can_fork = true;
#else
static const bool can_fork = false;
#endif
#include "test/jemalloc_test.h"
TEST_BEGIN(test_failing_alloc) {
test_skip_if(!can_fork);
#ifdef __unix__
pid_t pid = fork();
expect_d_ne(pid, -1, "Unexpected fork failure");
if (pid == 0) {
/*
* In the child, we'll print an error message to stderr before
* exiting. Close stderr to avoid spamming output for this
* expected failure.
*/
fclose(stderr);
try {
/* Too big of an allocation to succeed. */
void *volatile ptr = ::operator new((size_t)-1);
(void)ptr;
} catch (...) {
/*
* Swallow the exception; remember, we expect this to
* fail via an abort within new, not because an
* exception didn't get caught.
*/
}
} else {
int status;
pid_t err = waitpid(pid, &status, 0);
expect_d_ne(-1, err, "waitpid failure");
expect_false(WIFEXITED(status),
"Should have seen an abnormal failure");
}
#endif
}
TEST_END
int
main(void) {
return test(
test_failing_alloc);
}

View File

@@ -0,0 +1,8 @@
#!/bin/sh
XMALLOC_STR=""
if [ "x${enable_xmalloc}" = "x1" ] ; then
XMALLOC_STR="xmalloc:false,"
fi
export MALLOC_CONF="${XMALLOC_STR}experimental_infallible_new:true"