xref: /freebsd/contrib/jemalloc/include/jemalloc/internal/safety_check.h (revision c43cad87172039ccf38172129c79755ea79e6102)
1 #ifndef JEMALLOC_INTERNAL_SAFETY_CHECK_H
2 #define JEMALLOC_INTERNAL_SAFETY_CHECK_H
3 
4 void safety_check_fail_sized_dealloc(bool current_dealloc, const void *ptr,
5     size_t true_size, size_t input_size);
6 void safety_check_fail(const char *format, ...);
7 
8 typedef void (*safety_check_abort_hook_t)(const char *message);
9 
10 /* Can set to NULL for a default. */
11 void safety_check_set_abort(safety_check_abort_hook_t abort_fn);
12 
13 JEMALLOC_ALWAYS_INLINE void
14 safety_check_set_redzone(void *ptr, size_t usize, size_t bumped_usize) {
15 	assert(usize < bumped_usize);
16 	for (size_t i = usize; i < bumped_usize && i < usize + 32; ++i) {
17 		*((unsigned char *)ptr + i) = 0xBC;
18 	}
19 }
20 
21 JEMALLOC_ALWAYS_INLINE void
22 safety_check_verify_redzone(const void *ptr, size_t usize, size_t bumped_usize)
23 {
24 	for (size_t i = usize; i < bumped_usize && i < usize + 32; ++i) {
25 		if (unlikely(*((unsigned char *)ptr + i) != 0xBC)) {
26 			safety_check_fail("Use after free error\n");
27 		}
28 	}
29 }
30 
31 #endif /*JEMALLOC_INTERNAL_SAFETY_CHECK_H */
32