summaryrefslogtreecommitdiffstats
path: root/deps/jemalloc/test/include/test/extent_hooks.h
blob: aad0a46c4ccaa27cb1542dfbb8de9c55edb0b786 (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
/*
 * Boilerplate code used for testing extent hooks via interception and
 * passthrough.
 */

static void	*extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr,
    size_t size, size_t alignment, bool *zero, bool *commit,
    unsigned arena_ind);
static bool	extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, bool committed, unsigned arena_ind);
static void	extent_destroy_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, bool committed, unsigned arena_ind);
static bool	extent_commit_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool	extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool	extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool	extent_purge_forced_hook(extent_hooks_t *extent_hooks,
    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);
static bool	extent_split_hook(extent_hooks_t *extent_hooks, void *addr,
    size_t size, size_t size_a, size_t size_b, bool committed,
    unsigned arena_ind);
static bool	extent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a,
    size_t size_a, void *addr_b, size_t size_b, bool committed,
    unsigned arena_ind);

static extent_hooks_t *default_hooks;
static extent_hooks_t hooks = {
	extent_alloc_hook,
	extent_dalloc_hook,
	extent_destroy_hook,
	extent_commit_hook,
	extent_decommit_hook,
	extent_purge_lazy_hook,
	extent_purge_forced_hook,
	extent_split_hook,
	extent_merge_hook
};

/* Control whether hook functions pass calls through to default hooks. */
static bool try_alloc = true;
static bool try_dalloc = true;
static bool try_destroy = true;
static bool try_commit = true;
static bool try_decommit = true;
static bool try_purge_lazy = true;
static bool try_purge_forced = true;
static bool try_split = true;
static bool try_merge = true;

/* Set to false prior to operations, then introspect after operations. */
static bool called_alloc;
static bool called_dalloc;
static bool called_destroy;
static bool called_commit;
static bool called_decommit;
static bool called_purge_lazy;
static bool called_purge_forced;
static bool called_split;
static bool called_merge;

/* Set to false prior to operations, then introspect after operations. */
static bool did_alloc;
static bool did_dalloc;
static bool did_destroy;
static bool did_commit;
static bool did_decommit;
static bool did_purge_lazy;
static bool did_purge_forced;
static bool did_split;
static bool did_merge;

#if 0
#  define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)
#else
#  define TRACE_HOOK(fmt, ...)
#endif

static void *
extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
    size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {
	void *ret;

	TRACE_HOOK("%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, "
	    "*zero=%s, *commit=%s, arena_ind=%u)\n", __func__, extent_hooks,
	    new_addr, size, alignment, *zero ?  "true" : "false", *commit ?
	    "true" : "false", arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->alloc, extent_alloc_hook,
	    "Wrong hook function");
	called_alloc = true;
	if (!try_alloc) {
		return NULL;
	}
	ret = default_hooks->alloc(default_hooks, new_addr, size, alignment,
	    zero, commit, 0);
	did_alloc = (ret != NULL);
	return ret;
}

static bool
extent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    bool committed, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
	    "arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
	    "true" : "false", arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->dalloc, extent_dalloc_hook,
	    "Wrong hook function");
	called_dalloc = true;
	if (!try_dalloc) {
		return true;
	}
	err = default_hooks->dalloc(default_hooks, addr, size, committed, 0);
	did_dalloc = !err;
	return err;
}

static void
extent_destroy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    bool committed, unsigned arena_ind) {
	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
	    "arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
	    "true" : "false", arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->destroy, extent_destroy_hook,
	    "Wrong hook function");
	called_destroy = true;
	if (!try_destroy) {
		return;
	}
	default_hooks->destroy(default_hooks, addr, size, committed, 0);
	did_destroy = true;
}

static bool
extent_commit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    size_t offset, size_t length, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
	    "length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
	    offset, length, arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->commit, extent_commit_hook,
	    "Wrong hook function");
	called_commit = true;
	if (!try_commit) {
		return true;
	}
	err = default_hooks->commit(default_hooks, addr, size, offset, length,
	    0);
	did_commit = !err;
	return err;
}

static bool
extent_decommit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    size_t offset, size_t length, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
	    "length=%zu, arena_ind=%u)\n", __func__, extent_hooks, addr, size,
	    offset, length, arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->decommit, extent_decommit_hook,
	    "Wrong hook function");
	called_decommit = true;
	if (!try_decommit) {
		return true;
	}
	err = default_hooks->decommit(default_hooks, addr, size, offset, length,
	    0);
	did_decommit = !err;
	return err;
}

static bool
extent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    size_t offset, size_t length, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
	    "length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
	    offset, length, arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->purge_lazy, extent_purge_lazy_hook,
	    "Wrong hook function");
	called_purge_lazy = true;
	if (!try_purge_lazy) {
		return true;
	}
	err = default_hooks->purge_lazy == NULL ||
	    default_hooks->purge_lazy(default_hooks, addr, size, offset, length,
	    0);
	did_purge_lazy = !err;
	return err;
}

static bool
extent_purge_forced_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    size_t offset, size_t length, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, "
	    "length=%zu arena_ind=%u)\n", __func__, extent_hooks, addr, size,
	    offset, length, arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->purge_forced, extent_purge_forced_hook,
	    "Wrong hook function");
	called_purge_forced = true;
	if (!try_purge_forced) {
		return true;
	}
	err = default_hooks->purge_forced == NULL ||
	    default_hooks->purge_forced(default_hooks, addr, size, offset,
	    length, 0);
	did_purge_forced = !err;
	return err;
}

static bool
extent_split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
    size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, size_a=%zu, "
	    "size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
	    addr, size, size_a, size_b, committed ? "true" : "false",
	    arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->split, extent_split_hook,
	    "Wrong hook function");
	called_split = true;
	if (!try_split) {
		return true;
	}
	err = (default_hooks->split == NULL ||
	    default_hooks->split(default_hooks, addr, size, size_a, size_b,
	    committed, 0));
	did_split = !err;
	return err;
}

static bool
extent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
    void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {
	bool err;

	TRACE_HOOK("%s(extent_hooks=%p, addr_a=%p, size_a=%zu, addr_b=%p "
	    "size_b=%zu, committed=%s, arena_ind=%u)\n", __func__, extent_hooks,
	    addr_a, size_a, addr_b, size_b, committed ? "true" : "false",
	    arena_ind);
	expect_ptr_eq(extent_hooks, &hooks,
	    "extent_hooks should be same as pointer used to set hooks");
	expect_ptr_eq(extent_hooks->merge, extent_merge_hook,
	    "Wrong hook function");
	expect_ptr_eq((void *)((uintptr_t)addr_a + size_a), addr_b,
	    "Extents not mergeable");
	called_merge = true;
	if (!try_merge) {
		return true;
	}
	err = (default_hooks->merge == NULL ||
	    default_hooks->merge(default_hooks, addr_a, size_a, addr_b, size_b,
	    committed, 0));
	did_merge = !err;
	return err;
}

static void
extent_hooks_prep(void) {
	size_t sz;

	sz = sizeof(default_hooks);
	expect_d_eq(mallctl("arena.0.extent_hooks", (void *)&default_hooks, &sz,
	    NULL, 0), 0, "Unexpected mallctl() error");
}