Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 486d249

Browse files
committed
Merge branch 'dev'
2 parents 9898051 + b8e966f commit 486d249

File tree

7 files changed

+114
-54
lines changed

7 files changed

+114
-54
lines changed

ChangeLog

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ brevity. Much more detail can be found in the git revision history:
44

55
https://github.com/jemalloc/jemalloc
66

7+
* 4.0.2 (September 21, 2015)
8+
9+
This bugfix release addresses a few bugs specific to heap profiling.
10+
11+
Bug fixes:
12+
- Fix ixallocx_prof_sample() to never modify nor create sampled small
13+
allocations. xallocx() is in general incapable of moving small allocations,
14+
so this fix removes buggy code without loss of generality.
15+
- Fix irallocx_prof_sample() to always allocate large regions, even when
16+
alignment is non-zero.
17+
- Fix prof_alloc_rollback() to read tdata from thread-specific data rather
18+
than dereferencing a potentially invalid tctx.
19+
720
* 4.0.1 (September 15, 2015)
821

922
This is a bugfix release that is somewhat high risk due to the amount of

Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ check_unit: tests_unit check_unit_dir
353353
check_integration_prof: tests_integration check_integration_dir
354354
ifeq ($(enable_prof), 1)
355355
$(MALLOC_CONF)="prof:true" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%)
356+
$(MALLOC_CONF)="prof:true,prof_active:false" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%)
356357
endif
357358
check_integration: tests_integration check_integration_dir
358359
$(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%)

include/jemalloc/internal/tsd.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ a_name##tsd_boot0(void) \
190190
return (false); \
191191
} \
192192
a_attr void \
193-
a_name##tsd_boot1() \
193+
a_name##tsd_boot1(void) \
194194
{ \
195195
\
196196
/* Do nothing. */ \
@@ -235,7 +235,7 @@ a_name##tsd_boot0(void) \
235235
return (false); \
236236
} \
237237
a_attr void \
238-
a_name##tsd_boot1() \
238+
a_name##tsd_boot1(void) \
239239
{ \
240240
\
241241
/* Do nothing. */ \
@@ -345,7 +345,7 @@ a_name##tsd_boot0(void) \
345345
return (false); \
346346
} \
347347
a_attr void \
348-
a_name##tsd_boot1() \
348+
a_name##tsd_boot1(void) \
349349
{ \
350350
a_name##tsd_wrapper_t *wrapper; \
351351
wrapper = (a_name##tsd_wrapper_t *) \
@@ -467,7 +467,7 @@ a_name##tsd_boot0(void) \
467467
return (false); \
468468
} \
469469
a_attr void \
470-
a_name##tsd_boot1() \
470+
a_name##tsd_boot1(void) \
471471
{ \
472472
a_name##tsd_wrapper_t *wrapper; \
473473
wrapper = (a_name##tsd_wrapper_t *) \

src/arena.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,7 @@ arena_dalloc_junk_large_t *arena_dalloc_junk_large =
25602560
JEMALLOC_N(arena_dalloc_junk_large_impl);
25612561
#endif
25622562

2563-
void
2563+
static void
25642564
arena_dalloc_large_locked_impl(arena_t *arena, arena_chunk_t *chunk,
25652565
void *ptr, bool junked)
25662566
{

src/jemalloc.c

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,7 @@ imallocx_flags_decode_hard(tsd_t *tsd, size_t size, int flags, size_t *usize,
19231923
*alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);
19241924
*usize = sa2u(size, *alignment);
19251925
}
1926+
assert(*usize != 0);
19261927
*zero = MALLOCX_ZERO_GET(flags);
19271928
if ((flags & MALLOCX_TCACHE_MASK) != 0) {
19281929
if ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE)
@@ -1965,41 +1966,29 @@ imallocx_flags(tsd_t *tsd, size_t usize, size_t alignment, bool zero,
19651966
tcache_t *tcache, arena_t *arena)
19661967
{
19671968

1968-
if (alignment != 0)
1969+
if (unlikely(alignment != 0))
19691970
return (ipalloct(tsd, usize, alignment, zero, tcache, arena));
1970-
if (zero)
1971+
if (unlikely(zero))
19711972
return (icalloct(tsd, usize, tcache, arena));
19721973
return (imalloct(tsd, usize, tcache, arena));
19731974
}
19741975

1975-
JEMALLOC_ALWAYS_INLINE_C void *
1976-
imallocx_maybe_flags(tsd_t *tsd, size_t size, int flags, size_t usize,
1977-
size_t alignment, bool zero, tcache_t *tcache, arena_t *arena)
1978-
{
1979-
1980-
if (likely(flags == 0))
1981-
return (imalloc(tsd, size));
1982-
return (imallocx_flags(tsd, usize, alignment, zero, tcache, arena));
1983-
}
1984-
19851976
static void *
1986-
imallocx_prof_sample(tsd_t *tsd, size_t size, int flags, size_t usize,
1987-
size_t alignment, bool zero, tcache_t *tcache, arena_t *arena)
1977+
imallocx_prof_sample(tsd_t *tsd, size_t usize, size_t alignment, bool zero,
1978+
tcache_t *tcache, arena_t *arena)
19881979
{
19891980
void *p;
19901981

19911982
if (usize <= SMALL_MAXCLASS) {
19921983
assert(((alignment == 0) ? s2u(LARGE_MINCLASS) :
19931984
sa2u(LARGE_MINCLASS, alignment)) == LARGE_MINCLASS);
1994-
p = imallocx_maybe_flags(tsd, LARGE_MINCLASS, flags,
1995-
LARGE_MINCLASS, alignment, zero, tcache, arena);
1985+
p = imallocx_flags(tsd, LARGE_MINCLASS, alignment, zero, tcache,
1986+
arena);
19961987
if (p == NULL)
19971988
return (NULL);
19981989
arena_prof_promoted(p, usize);
1999-
} else {
2000-
p = imallocx_maybe_flags(tsd, size, flags, usize, alignment,
2001-
zero, tcache, arena);
2002-
}
1990+
} else
1991+
p = imallocx_flags(tsd, usize, alignment, zero, tcache, arena);
20031992

20041993
return (p);
20051994
}
@@ -2018,12 +2007,11 @@ imallocx_prof(tsd_t *tsd, size_t size, int flags, size_t *usize)
20182007
&zero, &tcache, &arena)))
20192008
return (NULL);
20202009
tctx = prof_alloc_prep(tsd, *usize, prof_active_get_unlocked(), true);
2021-
if (likely((uintptr_t)tctx == (uintptr_t)1U)) {
2022-
p = imallocx_maybe_flags(tsd, size, flags, *usize, alignment,
2023-
zero, tcache, arena);
2024-
} else if ((uintptr_t)tctx > (uintptr_t)1U) {
2025-
p = imallocx_prof_sample(tsd, size, flags, *usize, alignment,
2026-
zero, tcache, arena);
2010+
if (likely((uintptr_t)tctx == (uintptr_t)1U))
2011+
p = imallocx_flags(tsd, *usize, alignment, zero, tcache, arena);
2012+
else if ((uintptr_t)tctx > (uintptr_t)1U) {
2013+
p = imallocx_prof_sample(tsd, *usize, alignment, zero, tcache,
2014+
arena);
20272015
} else
20282016
p = NULL;
20292017
if (unlikely(p == NULL)) {
@@ -2098,8 +2086,8 @@ je_mallocx(size_t size, int flags)
20982086
}
20992087

21002088
static void *
2101-
irallocx_prof_sample(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,
2102-
size_t alignment, size_t usize, bool zero, tcache_t *tcache, arena_t *arena,
2089+
irallocx_prof_sample(tsd_t *tsd, void *old_ptr, size_t old_usize,
2090+
size_t usize, size_t alignment, bool zero, tcache_t *tcache, arena_t *arena,
21032091
prof_tctx_t *tctx)
21042092
{
21052093
void *p;
@@ -2113,7 +2101,7 @@ irallocx_prof_sample(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,
21132101
return (NULL);
21142102
arena_prof_promoted(p, usize);
21152103
} else {
2116-
p = iralloct(tsd, old_ptr, old_usize, size, alignment, zero,
2104+
p = iralloct(tsd, old_ptr, old_usize, usize, alignment, zero,
21172105
tcache, arena);
21182106
}
21192107

@@ -2133,8 +2121,8 @@ irallocx_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,
21332121
old_tctx = prof_tctx_get(old_ptr);
21342122
tctx = prof_alloc_prep(tsd, *usize, prof_active, true);
21352123
if (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {
2136-
p = irallocx_prof_sample(tsd, old_ptr, old_usize, size,
2137-
alignment, *usize, zero, tcache, arena, tctx);
2124+
p = irallocx_prof_sample(tsd, old_ptr, old_usize, *usize,
2125+
alignment, zero, tcache, arena, tctx);
21382126
} else {
21392127
p = iralloct(tsd, old_ptr, old_usize, size, alignment, zero,
21402128
tcache, arena);
@@ -2251,26 +2239,13 @@ ixallocx_helper(void *ptr, size_t old_usize, size_t size, size_t extra,
22512239

22522240
static size_t
22532241
ixallocx_prof_sample(void *ptr, size_t old_usize, size_t size, size_t extra,
2254-
size_t alignment, size_t usize_max, bool zero, prof_tctx_t *tctx)
2242+
size_t alignment, bool zero, prof_tctx_t *tctx)
22552243
{
22562244
size_t usize;
22572245

22582246
if (tctx == NULL)
22592247
return (old_usize);
2260-
/* Use minimum usize to determine whether promotion may happen. */
2261-
if (((alignment == 0) ? s2u(size) : sa2u(size, alignment)) <=
2262-
SMALL_MAXCLASS) {
2263-
if (ixalloc(ptr, old_usize, SMALL_MAXCLASS+1,
2264-
(SMALL_MAXCLASS+1 >= size+extra) ? 0 : size+extra -
2265-
(SMALL_MAXCLASS+1), alignment, zero))
2266-
return (old_usize);
2267-
usize = isalloc(ptr, config_prof);
2268-
if (usize_max < LARGE_MINCLASS)
2269-
arena_prof_promoted(ptr, usize);
2270-
} else {
2271-
usize = ixallocx_helper(ptr, old_usize, size, extra, alignment,
2272-
zero);
2273-
}
2248+
usize = ixallocx_helper(ptr, old_usize, size, extra, alignment, zero);
22742249

22752250
return (usize);
22762251
}
@@ -2293,15 +2268,16 @@ ixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,
22932268
*/
22942269
usize_max = (alignment == 0) ? s2u(size+extra) : sa2u(size+extra,
22952270
alignment);
2271+
assert(usize_max != 0);
22962272
tctx = prof_alloc_prep(tsd, usize_max, prof_active, false);
22972273
if (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {
22982274
usize = ixallocx_prof_sample(ptr, old_usize, size, extra,
2299-
alignment, usize_max, zero, tctx);
2275+
alignment, zero, tctx);
23002276
} else {
23012277
usize = ixallocx_helper(ptr, old_usize, size, extra, alignment,
23022278
zero);
23032279
}
2304-
if (unlikely(usize == old_usize)) {
2280+
if (usize == old_usize) {
23052281
prof_alloc_rollback(tsd, tctx, false);
23062282
return (usize);
23072283
}

src/prof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated)
209209
*/
210210
tdata = prof_tdata_get(tsd, true);
211211
if (tdata != NULL)
212-
prof_sample_threshold_update(tctx->tdata);
212+
prof_sample_threshold_update(tdata);
213213
}
214214

215215
if ((uintptr_t)tctx > (uintptr_t)1U) {

test/integration/mallocx.c

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

3+
static unsigned
4+
get_nsizes_impl(const char *cmd)
5+
{
6+
unsigned ret;
7+
size_t z;
8+
9+
z = sizeof(unsigned);
10+
assert_d_eq(mallctl(cmd, &ret, &z, NULL, 0), 0,
11+
"Unexpected mallctl(\"%s\", ...) failure", cmd);
12+
13+
return (ret);
14+
}
15+
16+
static unsigned
17+
get_nhuge(void)
18+
{
19+
20+
return (get_nsizes_impl("arenas.nhchunks"));
21+
}
22+
23+
static size_t
24+
get_size_impl(const char *cmd, size_t ind)
25+
{
26+
size_t ret;
27+
size_t z;
28+
size_t mib[4];
29+
size_t miblen = 4;
30+
31+
z = sizeof(size_t);
32+
assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
33+
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
34+
mib[2] = ind;
35+
z = sizeof(size_t);
36+
assert_d_eq(mallctlbymib(mib, miblen, &ret, &z, NULL, 0),
37+
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
38+
39+
return (ret);
40+
}
41+
42+
static size_t
43+
get_huge_size(size_t ind)
44+
{
45+
46+
return (get_size_impl("arenas.hchunk.0.size", ind));
47+
}
48+
49+
TEST_BEGIN(test_oom)
50+
{
51+
size_t hugemax, size, alignment;
52+
53+
hugemax = get_huge_size(get_nhuge()-1);
54+
55+
/* In practice hugemax is too large to be allocated. */
56+
assert_ptr_null(mallocx(hugemax, 0),
57+
"Expected OOM for mallocx(size=%#zx, 0)", hugemax);
58+
59+
#if LG_SIZEOF_PTR == 3
60+
size = ZU(0x8000000000000000);
61+
alignment = ZU(0x8000000000000000);
62+
#else
63+
size = ZU(0x80000000);
64+
alignment = ZU(0x80000000);
65+
#endif
66+
assert_ptr_null(mallocx(size, MALLOCX_ALIGN(alignment)),
67+
"Expected OOM for mallocx(size=%#zx, MALLOCX_ALIGN(%#zx)", size,
68+
alignment);
69+
}
70+
TEST_END
71+
372
TEST_BEGIN(test_basic)
473
{
574
#define MAXSZ (((size_t)1) << 26)
@@ -96,6 +165,7 @@ main(void)
96165
{
97166

98167
return (test(
168+
test_oom,
99169
test_basic,
100170
test_alignment_and_size));
101171
}

0 commit comments

Comments
 (0)