Skip to content

Commit fed1f9f

Browse files
committed
Fix intermittent xallocx() test failures.
Modify xallocx() tests that expect to expand in place to use a separate arena. This avoids the potential for interposed internal allocations from e.g. heap profile sampling to disrupt the tests. This resolves jemalloc#286.
1 parent a784e41 commit fed1f9f

File tree

1 file changed

+65
-43
lines changed

1 file changed

+65
-43
lines changed

test/integration/xallocx.c

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
#include "test/jemalloc_test.h"
22

3+
/*
4+
* Use a separate arena for xallocx() extension/contraction tests so that
5+
* internal allocation e.g. by heap profiling can't interpose allocations where
6+
* xallocx() would ordinarily be able to extend.
7+
*/
8+
static unsigned
9+
arena_ind(void)
10+
{
11+
static unsigned ind = 0;
12+
13+
if (ind == 0) {
14+
size_t sz = sizeof(ind);
15+
assert_d_eq(mallctl("arenas.extend", &ind, &sz, NULL, 0), 0,
16+
"Unexpected mallctl failure creating arena");
17+
}
18+
19+
return (ind);
20+
}
21+
322
TEST_BEGIN(test_same_size)
423
{
524
void *p;
@@ -218,6 +237,7 @@ TEST_END
218237

219238
TEST_BEGIN(test_extra_large)
220239
{
240+
int flags = MALLOCX_ARENA(arena_ind());
221241
size_t smallmax, large0, large1, large2, huge0, hugemax;
222242
void *p;
223243

@@ -229,61 +249,62 @@ TEST_BEGIN(test_extra_large)
229249
huge0 = get_huge_size(0);
230250
hugemax = get_huge_size(get_nhuge()-1);
231251

232-
p = mallocx(large2, 0);
252+
p = mallocx(large2, flags);
233253
assert_ptr_not_null(p, "Unexpected mallocx() error");
234254

235-
assert_zu_eq(xallocx(p, large2, 0, 0), large2,
255+
assert_zu_eq(xallocx(p, large2, 0, flags), large2,
236256
"Unexpected xallocx() behavior");
237257
/* Test size decrease with zero extra. */
238-
assert_zu_eq(xallocx(p, large0, 0, 0), large0,
258+
assert_zu_eq(xallocx(p, large0, 0, flags), large0,
239259
"Unexpected xallocx() behavior");
240-
assert_zu_eq(xallocx(p, smallmax, 0, 0), large0,
260+
assert_zu_eq(xallocx(p, smallmax, 0, flags), large0,
241261
"Unexpected xallocx() behavior");
242262

243-
assert_zu_eq(xallocx(p, large2, 0, 0), large2,
263+
assert_zu_eq(xallocx(p, large2, 0, flags), large2,
244264
"Unexpected xallocx() behavior");
245265
/* Test size decrease with non-zero extra. */
246-
assert_zu_eq(xallocx(p, large0, large2 - large0, 0), large2,
266+
assert_zu_eq(xallocx(p, large0, large2 - large0, flags), large2,
247267
"Unexpected xallocx() behavior");
248-
assert_zu_eq(xallocx(p, large1, large2 - large1, 0), large2,
268+
assert_zu_eq(xallocx(p, large1, large2 - large1, flags), large2,
249269
"Unexpected xallocx() behavior");
250-
assert_zu_eq(xallocx(p, large0, large1 - large0, 0), large1,
270+
assert_zu_eq(xallocx(p, large0, large1 - large0, flags), large1,
251271
"Unexpected xallocx() behavior");
252-
assert_zu_eq(xallocx(p, smallmax, large0 - smallmax, 0), large0,
272+
assert_zu_eq(xallocx(p, smallmax, large0 - smallmax, flags), large0,
253273
"Unexpected xallocx() behavior");
254274

255-
assert_zu_eq(xallocx(p, large0, 0, 0), large0,
275+
assert_zu_eq(xallocx(p, large0, 0, flags), large0,
256276
"Unexpected xallocx() behavior");
257277
/* Test size increase with zero extra. */
258-
assert_zu_eq(xallocx(p, large2, 0, 0), large2,
278+
assert_zu_eq(xallocx(p, large2, 0, flags), large2,
259279
"Unexpected xallocx() behavior");
260-
assert_zu_eq(xallocx(p, huge0, 0, 0), large2,
280+
assert_zu_eq(xallocx(p, huge0, 0, flags), large2,
261281
"Unexpected xallocx() behavior");
262282

263-
assert_zu_eq(xallocx(p, large0, 0, 0), large0,
283+
assert_zu_eq(xallocx(p, large0, 0, flags), large0,
264284
"Unexpected xallocx() behavior");
265285
/* Test size increase with non-zero extra. */
266-
assert_zu_lt(xallocx(p, large0, huge0 - large0, 0), huge0,
286+
assert_zu_lt(xallocx(p, large0, huge0 - large0, flags), huge0,
267287
"Unexpected xallocx() behavior");
268288

269-
assert_zu_eq(xallocx(p, large0, 0, 0), large0,
289+
assert_zu_eq(xallocx(p, large0, 0, flags), large0,
270290
"Unexpected xallocx() behavior");
271291
/* Test size increase with non-zero extra. */
272-
assert_zu_eq(xallocx(p, large0, large2 - large0, 0), large2,
292+
assert_zu_eq(xallocx(p, large0, large2 - large0, flags), large2,
273293
"Unexpected xallocx() behavior");
274294

275-
assert_zu_eq(xallocx(p, large2, 0, 0), large2,
295+
assert_zu_eq(xallocx(p, large2, 0, flags), large2,
276296
"Unexpected xallocx() behavior");
277297
/* Test size+extra overflow. */
278-
assert_zu_lt(xallocx(p, large2, hugemax - large2 + 1, 0), huge0,
298+
assert_zu_lt(xallocx(p, large2, hugemax - large2 + 1, flags), huge0,
279299
"Unexpected xallocx() behavior");
280300

281-
dallocx(p, 0);
301+
dallocx(p, flags);
282302
}
283303
TEST_END
284304

285305
TEST_BEGIN(test_extra_huge)
286306
{
307+
int flags = MALLOCX_ARENA(arena_ind());
287308
size_t largemax, huge0, huge1, huge2, hugemax;
288309
void *p;
289310

@@ -294,56 +315,56 @@ TEST_BEGIN(test_extra_huge)
294315
huge2 = get_huge_size(2);
295316
hugemax = get_huge_size(get_nhuge()-1);
296317

297-
p = mallocx(huge2, 0);
318+
p = mallocx(huge2, flags);
298319
assert_ptr_not_null(p, "Unexpected mallocx() error");
299320

300-
assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
321+
assert_zu_eq(xallocx(p, huge2, 0, flags), huge2,
301322
"Unexpected xallocx() behavior");
302323
/* Test size decrease with zero extra. */
303-
assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
324+
assert_zu_ge(xallocx(p, huge0, 0, flags), huge0,
304325
"Unexpected xallocx() behavior");
305-
assert_zu_ge(xallocx(p, largemax, 0, 0), huge0,
326+
assert_zu_ge(xallocx(p, largemax, 0, flags), huge0,
306327
"Unexpected xallocx() behavior");
307328

308-
assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
329+
assert_zu_eq(xallocx(p, huge2, 0, flags), huge2,
309330
"Unexpected xallocx() behavior");
310331
/* Test size decrease with non-zero extra. */
311-
assert_zu_eq(xallocx(p, huge0, huge2 - huge0, 0), huge2,
332+
assert_zu_eq(xallocx(p, huge0, huge2 - huge0, flags), huge2,
312333
"Unexpected xallocx() behavior");
313-
assert_zu_eq(xallocx(p, huge1, huge2 - huge1, 0), huge2,
334+
assert_zu_eq(xallocx(p, huge1, huge2 - huge1, flags), huge2,
314335
"Unexpected xallocx() behavior");
315-
assert_zu_eq(xallocx(p, huge0, huge1 - huge0, 0), huge1,
336+
assert_zu_eq(xallocx(p, huge0, huge1 - huge0, flags), huge1,
316337
"Unexpected xallocx() behavior");
317-
assert_zu_ge(xallocx(p, largemax, huge0 - largemax, 0), huge0,
338+
assert_zu_ge(xallocx(p, largemax, huge0 - largemax, flags), huge0,
318339
"Unexpected xallocx() behavior");
319340

320-
assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
341+
assert_zu_ge(xallocx(p, huge0, 0, flags), huge0,
321342
"Unexpected xallocx() behavior");
322343
/* Test size increase with zero extra. */
323-
assert_zu_le(xallocx(p, huge2, 0, 0), huge2,
344+
assert_zu_le(xallocx(p, huge2, 0, flags), huge2,
324345
"Unexpected xallocx() behavior");
325-
assert_zu_le(xallocx(p, hugemax+1, 0, 0), huge2,
346+
assert_zu_le(xallocx(p, hugemax+1, 0, flags), huge2,
326347
"Unexpected xallocx() behavior");
327348

328-
assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
349+
assert_zu_ge(xallocx(p, huge0, 0, flags), huge0,
329350
"Unexpected xallocx() behavior");
330351
/* Test size increase with non-zero extra. */
331-
assert_zu_le(xallocx(p, huge0, SIZE_T_MAX - huge0, 0), hugemax,
352+
assert_zu_le(xallocx(p, huge0, SIZE_T_MAX - huge0, flags), hugemax,
332353
"Unexpected xallocx() behavior");
333354

334-
assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
355+
assert_zu_ge(xallocx(p, huge0, 0, flags), huge0,
335356
"Unexpected xallocx() behavior");
336357
/* Test size increase with non-zero extra. */
337-
assert_zu_le(xallocx(p, huge0, huge2 - huge0, 0), huge2,
358+
assert_zu_le(xallocx(p, huge0, huge2 - huge0, flags), huge2,
338359
"Unexpected xallocx() behavior");
339360

340-
assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
361+
assert_zu_eq(xallocx(p, huge2, 0, flags), huge2,
341362
"Unexpected xallocx() behavior");
342363
/* Test size+extra overflow. */
343-
assert_zu_le(xallocx(p, huge2, hugemax - huge2 + 1, 0), hugemax,
364+
assert_zu_le(xallocx(p, huge2, hugemax - huge2 + 1, flags), hugemax,
344365
"Unexpected xallocx() behavior");
345366

346-
dallocx(p, 0);
367+
dallocx(p, flags);
347368
}
348369
TEST_END
349370

@@ -388,12 +409,13 @@ validate_fill(const void *p, uint8_t c, size_t offset, size_t len)
388409
static void
389410
test_zero(size_t szmin, size_t szmax)
390411
{
412+
int flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;
391413
size_t sz, nsz;
392414
void *p;
393415
#define FILL_BYTE 0x7aU
394416

395417
sz = szmax;
396-
p = mallocx(sz, MALLOCX_ZERO);
418+
p = mallocx(sz, flags);
397419
assert_ptr_not_null(p, "Unexpected mallocx() error");
398420
assert_false(validate_fill(p, 0x00, 0, sz), "Memory not filled: sz=%zu",
399421
sz);
@@ -408,14 +430,14 @@ test_zero(size_t szmin, size_t szmax)
408430

409431
/* Shrink in place so that we can expect growing in place to succeed. */
410432
sz = szmin;
411-
assert_zu_eq(xallocx(p, sz, 0, MALLOCX_ZERO), sz,
433+
assert_zu_eq(xallocx(p, sz, 0, flags), sz,
412434
"Unexpected xallocx() error");
413435
assert_false(validate_fill(p, FILL_BYTE, 0, sz),
414436
"Memory not filled: sz=%zu", sz);
415437

416438
for (sz = szmin; sz < szmax; sz = nsz) {
417-
nsz = nallocx(sz+1, MALLOCX_ZERO);
418-
assert_zu_eq(xallocx(p, sz+1, 0, MALLOCX_ZERO), nsz,
439+
nsz = nallocx(sz+1, flags);
440+
assert_zu_eq(xallocx(p, sz+1, 0, flags), nsz,
419441
"Unexpected xallocx() failure");
420442
assert_false(validate_fill(p, FILL_BYTE, 0, sz),
421443
"Memory not filled: sz=%zu", sz);
@@ -426,7 +448,7 @@ test_zero(size_t szmin, size_t szmax)
426448
"Memory not filled: nsz=%zu", nsz);
427449
}
428450

429-
dallocx(p, 0);
451+
dallocx(p, flags);
430452
}
431453

432454
TEST_BEGIN(test_zero_large)

0 commit comments

Comments
 (0)