summaryrefslogtreecommitdiff
path: root/test/unit/prof_hook.c
blob: a48b237b208f6f6eaa9696c8346a0ad7a4bc26ec (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "test/jemalloc_test.h"

/*
 * The MALLOC_CONF of this test has lg_prof_sample:0, meaning that every single
 * allocation will be sampled (and trigger relevant hooks).
 */

const char *dump_filename = "/dev/null";

prof_backtrace_hook_t default_bt_hook;

bool mock_bt_hook_called = false;
bool mock_dump_hook_called = false;
bool mock_prof_sample_hook_called = false;
bool mock_prof_sample_free_hook_called = false;

void *sampled_ptr = NULL;
size_t sampled_ptr_sz = 0;
void *free_sampled_ptr = NULL;
size_t free_sampled_ptr_sz = 0;

void
mock_bt_hook(void **vec, unsigned *len, unsigned max_len) {
	*len = max_len;
	for (unsigned i = 0; i < max_len; ++i) {
		vec[i] = (void *)((uintptr_t)i);
	}
	mock_bt_hook_called = true;
}

void
mock_bt_augmenting_hook(void **vec, unsigned *len, unsigned max_len) {
	default_bt_hook(vec, len, max_len);
	expect_u_gt(*len, 0, "Default backtrace hook returned empty backtrace");
	expect_u_lt(*len, max_len,
	    "Default backtrace hook returned too large backtrace");

	/* Add a separator between default frames and augmented */
	vec[*len] = (void *)0x030303030;
	(*len)++;

	/* Add more stack frames */
	for (unsigned i = 0; i < 3; ++i) {
		if (*len == max_len) {
			break;
		}
		vec[*len] = (void *)((uintptr_t)i);
		(*len)++;
	}


	mock_bt_hook_called = true;
}

void
mock_dump_hook(const char *filename) {
	mock_dump_hook_called = true;
	expect_str_eq(filename, dump_filename,
	    "Incorrect file name passed to the dump hook");
}

void
mock_prof_sample_hook(const void *ptr, size_t sz, void **vec, unsigned len) {
	mock_prof_sample_hook_called = true;
	sampled_ptr = (void *)ptr;
	sampled_ptr_sz = sz;
	for (unsigned i = 0; i < len; i++) {
		expect_ptr_not_null((void **)vec[i],
		    "Backtrace should not contain NULL");
	}
}

void
mock_prof_sample_free_hook(const void *ptr, size_t sz) {
	mock_prof_sample_free_hook_called = true;
	free_sampled_ptr = (void *)ptr;
	free_sampled_ptr_sz = sz;
}

TEST_BEGIN(test_prof_backtrace_hook_replace) {

	test_skip_if(!config_prof);

	mock_bt_hook_called = false;

	void *p0 = mallocx(1, 0);
	assert_ptr_not_null(p0, "Failed to allocate");

	expect_false(mock_bt_hook_called, "Called mock hook before it's set");

	prof_backtrace_hook_t null_hook = NULL;
	expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
	    NULL, 0, (void *)&null_hook,  sizeof(null_hook)),
		EINVAL, "Incorrectly allowed NULL backtrace hook");

	size_t default_bt_hook_sz = sizeof(prof_backtrace_hook_t);
	prof_backtrace_hook_t hook = &mock_bt_hook;
	expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
	    (void *)&default_bt_hook, &default_bt_hook_sz, (void *)&hook,
	    sizeof(hook)), 0, "Unexpected mallctl failure setting hook");

	void *p1 = mallocx(1, 0);
	assert_ptr_not_null(p1, "Failed to allocate");

	expect_true(mock_bt_hook_called, "Didn't call mock hook");

	prof_backtrace_hook_t current_hook;
	size_t current_hook_sz = sizeof(prof_backtrace_hook_t);
	expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
	    (void *)&current_hook, &current_hook_sz, (void *)&default_bt_hook,
	    sizeof(default_bt_hook)), 0,
	    "Unexpected mallctl failure resetting hook to default");

	expect_ptr_eq(current_hook, hook,
	    "Hook returned by mallctl is not equal to mock hook");

	dallocx(p1, 0);
	dallocx(p0, 0);
}
TEST_END

TEST_BEGIN(test_prof_backtrace_hook_augment) {

	test_skip_if(!config_prof);

	mock_bt_hook_called = false;

	void *p0 = mallocx(1, 0);
	assert_ptr_not_null(p0, "Failed to allocate");

	expect_false(mock_bt_hook_called, "Called mock hook before it's set");

	size_t default_bt_hook_sz = sizeof(prof_backtrace_hook_t);
	prof_backtrace_hook_t hook = &mock_bt_augmenting_hook;
	expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
	    (void *)&default_bt_hook, &default_bt_hook_sz, (void *)&hook,
	    sizeof(hook)), 0, "Unexpected mallctl failure setting hook");

	void *p1 = mallocx(1, 0);
	assert_ptr_not_null(p1, "Failed to allocate");

	expect_true(mock_bt_hook_called, "Didn't call mock hook");

	prof_backtrace_hook_t current_hook;
	size_t current_hook_sz = sizeof(prof_backtrace_hook_t);
	expect_d_eq(mallctl("experimental.hooks.prof_backtrace",
	    (void *)&current_hook, &current_hook_sz, (void *)&default_bt_hook,
	    sizeof(default_bt_hook)), 0,
	    "Unexpected mallctl failure resetting hook to default");

	expect_ptr_eq(current_hook, hook,
	    "Hook returned by mallctl is not equal to mock hook");

	dallocx(p1, 0);
	dallocx(p0, 0);
}
TEST_END

TEST_BEGIN(test_prof_dump_hook) {

	test_skip_if(!config_prof);
	expect_u_eq(opt_prof_bt_max, 200, "Unexpected backtrace stack depth");

	mock_dump_hook_called = false;

	expect_d_eq(mallctl("prof.dump", NULL, NULL, (void *)&dump_filename,
	    sizeof(dump_filename)), 0, "Failed to dump heap profile");

	expect_false(mock_dump_hook_called, "Called dump hook before it's set");

	size_t default_bt_hook_sz = sizeof(prof_dump_hook_t);
	prof_dump_hook_t hook = &mock_dump_hook;
	expect_d_eq(mallctl("experimental.hooks.prof_dump",
	    (void *)&default_bt_hook, &default_bt_hook_sz, (void *)&hook,
	    sizeof(hook)), 0, "Unexpected mallctl failure setting hook");

	expect_d_eq(mallctl("prof.dump", NULL, NULL, (void *)&dump_filename,
	    sizeof(dump_filename)), 0, "Failed to dump heap profile");

	expect_true(mock_dump_hook_called, "Didn't call mock hook");

	prof_dump_hook_t current_hook;
	size_t current_hook_sz = sizeof(prof_dump_hook_t);
	expect_d_eq(mallctl("experimental.hooks.prof_dump",
	    (void *)&current_hook, &current_hook_sz, (void *)&default_bt_hook,
	    sizeof(default_bt_hook)), 0,
	    "Unexpected mallctl failure resetting hook to default");

	expect_ptr_eq(current_hook, hook,
	    "Hook returned by mallctl is not equal to mock hook");
}
TEST_END

/* Need the do_write flag because NULL is a valid to_write value. */
static void
read_write_prof_sample_hook(prof_sample_hook_t *to_read, bool do_write,
    prof_sample_hook_t to_write) {
	size_t hook_sz = sizeof(prof_sample_hook_t);
	expect_d_eq(mallctl("experimental.hooks.prof_sample",
	    (void *)to_read, &hook_sz, do_write ? &to_write : NULL, hook_sz), 0,
	    "Unexpected prof_sample_hook mallctl failure");
}

static void
write_prof_sample_hook(prof_sample_hook_t new_hook) {
	read_write_prof_sample_hook(NULL, true, new_hook);
}

static prof_sample_hook_t
read_prof_sample_hook(void) {
	prof_sample_hook_t curr_hook;
	read_write_prof_sample_hook(&curr_hook, false, NULL);

	return curr_hook;
}

static void
read_write_prof_sample_free_hook(prof_sample_free_hook_t *to_read,
    bool do_write, prof_sample_free_hook_t to_write) {
	size_t hook_sz = sizeof(prof_sample_free_hook_t);
	expect_d_eq(mallctl("experimental.hooks.prof_sample_free",
	    (void *)to_read, &hook_sz, do_write ? &to_write : NULL, hook_sz), 0,
	    "Unexpected prof_sample_free_hook mallctl failure");
}

static void
write_prof_sample_free_hook(prof_sample_free_hook_t new_hook) {
	read_write_prof_sample_free_hook(NULL, true, new_hook);
}

static prof_sample_free_hook_t
read_prof_sample_free_hook(void) {
	prof_sample_free_hook_t curr_hook;
	read_write_prof_sample_free_hook(&curr_hook, false, NULL);

	return curr_hook;
}

static void
check_prof_sample_hooks(bool sample_hook_set, bool sample_free_hook_set) {
	expect_false(mock_prof_sample_hook_called,
	    "Should not have called prof_sample hook");
	expect_false(mock_prof_sample_free_hook_called,
	    "Should not have called prof_sample_free hook");
	expect_ptr_null(sampled_ptr, "Unexpected sampled ptr");
	expect_zu_eq(sampled_ptr_sz, 0, "Unexpected sampled ptr size");
	expect_ptr_null(free_sampled_ptr, "Unexpected free sampled ptr");
	expect_zu_eq(free_sampled_ptr_sz, 0,
	    "Unexpected free sampled ptr size");

	prof_sample_hook_t curr_hook = read_prof_sample_hook();
	expect_ptr_eq(curr_hook, sample_hook_set ? mock_prof_sample_hook : NULL,
	    "Unexpected non NULL default hook");

	prof_sample_free_hook_t curr_free_hook = read_prof_sample_free_hook();
	expect_ptr_eq(curr_free_hook, sample_free_hook_set ?
	    mock_prof_sample_free_hook : NULL,
	    "Unexpected non NULL default hook");

	size_t alloc_sz = 10;
	void *p = mallocx(alloc_sz, 0);
	expect_ptr_not_null(p, "Failed to allocate");
	expect_true(mock_prof_sample_hook_called == sample_hook_set,
	   "Incorrect prof_sample hook usage");
	if (sample_hook_set) {
		expect_ptr_eq(p, sampled_ptr, "Unexpected sampled ptr");
		expect_zu_eq(alloc_sz, sampled_ptr_sz,
		    "Unexpected sampled usize");
	}

	dallocx(p, 0);
	expect_true(mock_prof_sample_free_hook_called == sample_free_hook_set,
	   "Incorrect prof_sample_free hook usage");
	if (sample_free_hook_set) {
		size_t usz = sz_s2u(alloc_sz);
		expect_ptr_eq(p, free_sampled_ptr, "Unexpected sampled ptr");
		expect_zu_eq(usz, free_sampled_ptr_sz, "Unexpected sampled usize");
	}

	sampled_ptr = free_sampled_ptr = NULL;
	sampled_ptr_sz = free_sampled_ptr_sz = 0;
	mock_prof_sample_hook_called = false;
	mock_prof_sample_free_hook_called = false;
}

TEST_BEGIN(test_prof_sample_hooks) {
	test_skip_if(!config_prof);

	check_prof_sample_hooks(false, false);

	write_prof_sample_hook(mock_prof_sample_hook);
	check_prof_sample_hooks(true, false);

	write_prof_sample_free_hook(mock_prof_sample_free_hook);
	check_prof_sample_hooks(true, true);

	write_prof_sample_hook(NULL);
	check_prof_sample_hooks(false, true);

	write_prof_sample_free_hook(NULL);
	check_prof_sample_hooks(false, false);

	/* Test read+write together. */
	prof_sample_hook_t sample_hook;
	read_write_prof_sample_hook(&sample_hook, true, mock_prof_sample_hook);
	expect_ptr_null(sample_hook, "Unexpected non NULL default hook");
	check_prof_sample_hooks(true, false);

	prof_sample_free_hook_t sample_free_hook;
	read_write_prof_sample_free_hook(&sample_free_hook, true,
	    mock_prof_sample_free_hook);
	expect_ptr_null(sample_free_hook, "Unexpected non NULL default hook");
	check_prof_sample_hooks(true, true);

	read_write_prof_sample_hook(&sample_hook, true, NULL);
	expect_ptr_eq(sample_hook, mock_prof_sample_hook,
	    "Unexpected prof_sample hook");
	check_prof_sample_hooks(false, true);

	read_write_prof_sample_free_hook(&sample_free_hook, true, NULL);
	expect_ptr_eq(sample_free_hook, mock_prof_sample_free_hook,
	    "Unexpected prof_sample_free hook");
	check_prof_sample_hooks(false, false);
}
TEST_END

int
main(void) {
	return test(
	    test_prof_backtrace_hook_replace,
	    test_prof_backtrace_hook_augment,
	    test_prof_dump_hook,
	    test_prof_sample_hooks);
}