xref: /freebsd/contrib/jemalloc/src/safety_check.c (revision c43cad87172039ccf38172129c79755ea79e6102)
1 #include "jemalloc/internal/jemalloc_preamble.h"
2 #include "jemalloc/internal/jemalloc_internal_includes.h"
3 
4 static safety_check_abort_hook_t safety_check_abort;
5 
6 void safety_check_fail_sized_dealloc(bool current_dealloc, const void *ptr,
7     size_t true_size, size_t input_size) {
8 	char *src = current_dealloc ? "the current pointer being freed" :
9 	    "in thread cache, possibly from previous deallocations";
10 
11 	safety_check_fail("<jemalloc>: size mismatch detected (true size %zu "
12 	    "vs input size %zu), likely caused by application sized "
13 	    "deallocation bugs (source address: %p, %s). Suggest building with "
14 	    "--enable-debug or address sanitizer for debugging. Abort.\n",
15 	    true_size, input_size, ptr, src);
16 }
17 
18 void safety_check_set_abort(safety_check_abort_hook_t abort_fn) {
19 	safety_check_abort = abort_fn;
20 }
21 
22 void safety_check_fail(const char *format, ...) {
23 	char buf[MALLOC_PRINTF_BUFSIZE];
24 
25 	va_list ap;
26 	va_start(ap, format);
27 	malloc_vsnprintf(buf, MALLOC_PRINTF_BUFSIZE, format, ap);
28 	va_end(ap);
29 
30 	if (safety_check_abort == NULL) {
31 		malloc_write(buf);
32 		abort();
33 	} else {
34 		safety_check_abort(buf);
35 	}
36 }
37