Skip to content

Commit a82070e

Browse files
cpetersoJason Evans
authored andcommitted
Add JEMALLOC_ALLOC_JUNK and JEMALLOC_FREE_JUNK macros
Replace hardcoded 0xa5 and 0x5a junk values with JEMALLOC_ALLOC_JUNK and JEMALLOC_FREE_JUNK macros, respectively.
1 parent f86bc08 commit a82070e

File tree

7 files changed

+40
-32
lines changed

7 files changed

+40
-32
lines changed

include/jemalloc/internal/tcache.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,10 @@ tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
381381
}
382382
if (likely(!zero)) {
383383
if (slow_path && config_fill) {
384-
if (unlikely(opt_junk_alloc))
385-
memset(ret, 0xa5, usize);
386-
else if (unlikely(opt_zero))
384+
if (unlikely(opt_junk_alloc)) {
385+
memset(ret, JEMALLOC_ALLOC_JUNK,
386+
usize);
387+
} else if (unlikely(opt_zero))
387388
memset(ret, 0, usize);
388389
}
389390
} else

include/jemalloc/internal/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
*/
4141
#define MALLOC_PRINTF_BUFSIZE 4096
4242

43+
/* Junk fill patterns. */
44+
#define JEMALLOC_ALLOC_JUNK 0xa5
45+
#define JEMALLOC_FREE_JUNK 0x5a
46+
4347
/*
4448
* Wrap a cpp argument that contains commas such that it isn't broken up into
4549
* multiple arguments.

src/arena.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,15 +2249,16 @@ void
22492249
arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, bool zero)
22502250
{
22512251

2252+
size_t redzone_size = bin_info->redzone_size;
2253+
22522254
if (zero) {
2253-
size_t redzone_size = bin_info->redzone_size;
2254-
memset((void *)((uintptr_t)ptr - redzone_size), 0xa5,
2255-
redzone_size);
2256-
memset((void *)((uintptr_t)ptr + bin_info->reg_size), 0xa5,
2257-
redzone_size);
2255+
memset((void *)((uintptr_t)ptr - redzone_size),
2256+
JEMALLOC_ALLOC_JUNK, redzone_size);
2257+
memset((void *)((uintptr_t)ptr + bin_info->reg_size),
2258+
JEMALLOC_ALLOC_JUNK, redzone_size);
22582259
} else {
2259-
memset((void *)((uintptr_t)ptr - bin_info->redzone_size), 0xa5,
2260-
bin_info->reg_interval);
2260+
memset((void *)((uintptr_t)ptr - redzone_size),
2261+
JEMALLOC_ALLOC_JUNK, bin_info->reg_interval);
22612262
}
22622263
}
22632264

@@ -2293,22 +2294,22 @@ arena_redzones_validate(void *ptr, arena_bin_info_t *bin_info, bool reset)
22932294

22942295
for (i = 1; i <= redzone_size; i++) {
22952296
uint8_t *byte = (uint8_t *)((uintptr_t)ptr - i);
2296-
if (*byte != 0xa5) {
2297+
if (*byte != JEMALLOC_ALLOC_JUNK) {
22972298
error = true;
22982299
arena_redzone_corruption(ptr, size, false, i,
22992300
*byte);
23002301
if (reset)
2301-
*byte = 0xa5;
2302+
*byte = JEMALLOC_ALLOC_JUNK;
23022303
}
23032304
}
23042305
for (i = 0; i < redzone_size; i++) {
23052306
uint8_t *byte = (uint8_t *)((uintptr_t)ptr + size + i);
2306-
if (*byte != 0xa5) {
2307+
if (*byte != JEMALLOC_ALLOC_JUNK) {
23072308
error = true;
23082309
arena_redzone_corruption(ptr, size, true, i,
23092310
*byte);
23102311
if (reset)
2311-
*byte = 0xa5;
2312+
*byte = JEMALLOC_ALLOC_JUNK;
23122313
}
23132314
}
23142315
}
@@ -2327,7 +2328,7 @@ arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info)
23272328
size_t redzone_size = bin_info->redzone_size;
23282329

23292330
arena_redzones_validate(ptr, bin_info, false);
2330-
memset((void *)((uintptr_t)ptr - redzone_size), 0x5a,
2331+
memset((void *)((uintptr_t)ptr - redzone_size), JEMALLOC_FREE_JUNK,
23312332
bin_info->reg_interval);
23322333
}
23332334
#ifdef JEMALLOC_JET
@@ -2458,7 +2459,7 @@ arena_malloc_large(tsd_t *tsd, arena_t *arena, szind_t binind, bool zero)
24582459
if (!zero) {
24592460
if (config_fill) {
24602461
if (unlikely(opt_junk_alloc))
2461-
memset(ret, 0xa5, usize);
2462+
memset(ret, JEMALLOC_ALLOC_JUNK, usize);
24622463
else if (unlikely(opt_zero))
24632464
memset(ret, 0, usize);
24642465
}
@@ -2563,7 +2564,7 @@ arena_palloc_large(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment,
25632564

25642565
if (config_fill && !zero) {
25652566
if (unlikely(opt_junk_alloc))
2566-
memset(ret, 0xa5, usize);
2567+
memset(ret, JEMALLOC_ALLOC_JUNK, usize);
25672568
else if (unlikely(opt_zero))
25682569
memset(ret, 0, usize);
25692570
}
@@ -2776,7 +2777,7 @@ arena_dalloc_junk_large(void *ptr, size_t usize)
27762777
{
27772778

27782779
if (config_fill && unlikely(opt_junk_free))
2779-
memset(ptr, 0x5a, usize);
2780+
memset(ptr, JEMALLOC_FREE_JUNK, usize);
27802781
}
27812782
#ifdef JEMALLOC_JET
27822783
#undef arena_dalloc_junk_large
@@ -2977,7 +2978,7 @@ arena_ralloc_junk_large(void *ptr, size_t old_usize, size_t usize)
29772978
{
29782979

29792980
if (config_fill && unlikely(opt_junk_free)) {
2980-
memset((void *)((uintptr_t)ptr + usize), 0x5a,
2981+
memset((void *)((uintptr_t)ptr + usize), JEMALLOC_FREE_JUNK,
29812982
old_usize - usize);
29822983
}
29832984
}
@@ -3012,7 +3013,8 @@ arena_ralloc_large(void *ptr, size_t oldsize, size_t usize_min,
30123013
usize_min, usize_max, zero);
30133014
if (config_fill && !ret && !zero) {
30143015
if (unlikely(opt_junk_alloc)) {
3015-
memset((void *)((uintptr_t)ptr + oldsize), 0xa5,
3016+
memset((void *)((uintptr_t)ptr + oldsize),
3017+
JEMALLOC_ALLOC_JUNK,
30163018
isalloc(ptr, config_prof) - oldsize);
30173019
} else if (unlikely(opt_zero)) {
30183020
memset((void *)((uintptr_t)ptr + oldsize), 0,

src/ckh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ ckh_delete(tsd_t *tsd, ckh_t *ckh)
423423

424424
idalloctm(tsd, ckh->tab, tcache_get(tsd, false), true, true);
425425
if (config_debug)
426-
memset(ckh, 0x5a, sizeof(ckh_t));
426+
memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
427427
}
428428

429429
size_t

src/huge.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ huge_palloc(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment,
9292
if (!is_zeroed)
9393
memset(ret, 0, usize);
9494
} else if (config_fill && unlikely(opt_junk_alloc))
95-
memset(ret, 0xa5, usize);
95+
memset(ret, JEMALLOC_ALLOC_JUNK, usize);
9696

9797
arena_decay_tick(tsd, arena);
9898
return (ret);
@@ -112,7 +112,7 @@ huge_dalloc_junk(void *ptr, size_t usize)
112112
* unmapped.
113113
*/
114114
if (!config_munmap || (have_dss && chunk_in_dss(ptr)))
115-
memset(ptr, 0x5a, usize);
115+
memset(ptr, JEMALLOC_FREE_JUNK, usize);
116116
}
117117
}
118118
#ifdef JEMALLOC_JET
@@ -147,7 +147,8 @@ huge_ralloc_no_move_similar(void *ptr, size_t oldsize, size_t usize_min,
147147
if (oldsize > usize) {
148148
size_t sdiff = oldsize - usize;
149149
if (config_fill && unlikely(opt_junk_free)) {
150-
memset((void *)((uintptr_t)ptr + usize), 0x5a, sdiff);
150+
memset((void *)((uintptr_t)ptr + usize),
151+
JEMALLOC_FREE_JUNK, sdiff);
151152
post_zeroed = false;
152153
} else {
153154
post_zeroed = !chunk_purge_wrapper(arena, &chunk_hooks,
@@ -174,8 +175,8 @@ huge_ralloc_no_move_similar(void *ptr, size_t oldsize, size_t usize_min,
174175
usize - oldsize);
175176
}
176177
} else if (config_fill && unlikely(opt_junk_alloc)) {
177-
memset((void *)((uintptr_t)ptr + oldsize), 0xa5, usize -
178-
oldsize);
178+
memset((void *)((uintptr_t)ptr + oldsize),
179+
JEMALLOC_ALLOC_JUNK, usize - oldsize);
179180
}
180181
}
181182
}
@@ -268,8 +269,8 @@ huge_ralloc_no_move_expand(void *ptr, size_t oldsize, size_t usize, bool zero) {
268269
CHUNK_CEILING(oldsize));
269270
}
270271
} else if (config_fill && unlikely(opt_junk_alloc)) {
271-
memset((void *)((uintptr_t)ptr + oldsize), 0xa5, usize -
272-
oldsize);
272+
memset((void *)((uintptr_t)ptr + oldsize), JEMALLOC_ALLOC_JUNK,
273+
usize - oldsize);
273274
}
274275

275276
return (false);

src/quarantine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ quarantine(tsd_t *tsd, void *ptr)
160160
&& usize <= SMALL_MAXCLASS)
161161
arena_quarantine_junk_small(ptr, usize);
162162
else
163-
memset(ptr, 0x5a, usize);
163+
memset(ptr, JEMALLOC_FREE_JUNK, usize);
164164
}
165165
} else {
166166
assert(quarantine->curbytes == 0);

test/unit/junk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ arena_dalloc_junk_small_intercept(void *ptr, arena_bin_info_t *bin_info)
2929

3030
arena_dalloc_junk_small_orig(ptr, bin_info);
3131
for (i = 0; i < bin_info->reg_size; i++) {
32-
assert_c_eq(((char *)ptr)[i], 0x5a,
32+
assert_c_eq(((char *)ptr)[i], JEMALLOC_FREE_JUNK,
3333
"Missing junk fill for byte %zu/%zu of deallocated region",
3434
i, bin_info->reg_size);
3535
}
@@ -44,7 +44,7 @@ arena_dalloc_junk_large_intercept(void *ptr, size_t usize)
4444

4545
arena_dalloc_junk_large_orig(ptr, usize);
4646
for (i = 0; i < usize; i++) {
47-
assert_c_eq(((char *)ptr)[i], 0x5a,
47+
assert_c_eq(((char *)ptr)[i], JEMALLOC_FREE_JUNK,
4848
"Missing junk fill for byte %zu/%zu of deallocated region",
4949
i, usize);
5050
}
@@ -98,7 +98,7 @@ test_junk(size_t sz_min, size_t sz_max)
9898

9999
for (i = sz_prev; i < sz; i++) {
100100
if (opt_junk_alloc) {
101-
assert_c_eq(s[i], 0xa5,
101+
assert_c_eq(s[i], JEMALLOC_ALLOC_JUNK,
102102
"Newly allocated byte %zu/%zu isn't "
103103
"junk-filled", i, sz);
104104
}

0 commit comments

Comments
 (0)