summaryrefslogtreecommitdiff
path: root/test/unit/double_free.c
blob: e73efe718b05d0e3bd7c902841fee4a1e32fe17e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "test/jemalloc_test.h"
#include "test/san.h"

#include "jemalloc/internal/safety_check.h"

bool fake_abort_called;
void fake_abort(const char *message) {
	(void)message;
	fake_abort_called = true;
}

void
test_double_free_pre(void) {
	safety_check_set_abort(&fake_abort);
	fake_abort_called = false;
}

void
test_double_free_post() {
	expect_b_eq(fake_abort_called, true, "Double-free check didn't fire.");
	safety_check_set_abort(NULL);
}

bool tcache_enabled() {
	bool enabled;
	size_t sz = sizeof(enabled);
	assert_d_eq(
	    mallctl("thread.tcache.enabled", &enabled, &sz, NULL, 0), 0,
	    "Unexpected mallctl failure");
	return enabled;
}

TEST_BEGIN(test_large_double_free_tcache) {
	test_skip_if(!config_opt_safety_checks);
	/*
	 * Skip debug builds, since too many assertions will be triggered with
	 * double-free before hitting the one we are interested in.
	 */
	test_skip_if(config_debug);

	test_double_free_pre();
	char *ptr = malloc(SC_LARGE_MINCLASS);
	bool guarded = extent_is_guarded(tsdn_fetch(), ptr);
	free(ptr);
	if (!guarded) {
		free(ptr);
	} else {
		/*
		 * Skip because guarded extents may unguard immediately on
		 * deallocation, in which case the second free will crash before
		 * reaching the intended safety check.
		 */
		fake_abort_called = true;
	}
	mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
	test_double_free_post();
}
TEST_END

TEST_BEGIN(test_large_double_free_no_tcache) {
	test_skip_if(!config_opt_safety_checks);
	test_skip_if(config_debug);

	test_double_free_pre();
	char *ptr = mallocx(SC_LARGE_MINCLASS, MALLOCX_TCACHE_NONE);
	bool guarded = extent_is_guarded(tsdn_fetch(), ptr);
	dallocx(ptr, MALLOCX_TCACHE_NONE);
	if (!guarded) {
		dallocx(ptr, MALLOCX_TCACHE_NONE);
	} else {
		/*
		 * Skip because guarded extents may unguard immediately on
		 * deallocation, in which case the second free will crash before
		 * reaching the intended safety check.
		 */
		fake_abort_called = true;
	}
	test_double_free_post();
}
TEST_END

TEST_BEGIN(test_small_double_free_tcache) {
	test_skip_if(!config_debug);
	test_skip_if(opt_debug_double_free_max_scan == 0);
	test_skip_if(!tcache_enabled());

	test_double_free_pre();
	char *ptr = malloc(1);
	bool guarded = extent_is_guarded(tsdn_fetch(), ptr);
	free(ptr);
	if (!guarded) {
		free(ptr);
	} else {
		/*
		 * Skip because guarded extents may unguard immediately on
		 * deallocation, in which case the second free will crash before
		 * reaching the intended safety check.
		 */
		fake_abort_called = true;
	}
	mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
	test_double_free_post();
}
TEST_END

TEST_BEGIN(test_small_double_free_arena) {
	test_skip_if(!config_debug);
	test_skip_if(!tcache_enabled());

	test_double_free_pre();
	/*
	 * Allocate one more pointer to keep the slab partially used after
	 * flushing the cache.
	 */
	char *ptr1 = malloc(1);
	char *ptr = malloc(1);
	bool guarded = extent_is_guarded(tsdn_fetch(), ptr);
	free(ptr);
	if (!guarded) {
		mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
		free(ptr);
	} else {
		/*
		 * Skip because guarded extents may unguard immediately on
		 * deallocation, in which case the second free will crash before
		 * reaching the intended safety check.
		 */
		fake_abort_called = true;
	}
	test_double_free_post();
	free(ptr1);
}
TEST_END

int
main(void) {
	return test(
	    test_large_double_free_no_tcache,
	    test_large_double_free_tcache,
	    test_small_double_free_tcache,
	    test_small_double_free_arena);
}