summaryrefslogtreecommitdiffstats
path: root/include/jemalloc
diff options
context:
space:
mode:
authorJason Evans <je@fb.com>2016-02-25 23:29:49 (GMT)
committerJason Evans <je@fb.com>2016-02-25 23:29:49 (GMT)
commit0c516a00c4cb28cff55ce0995f756b5aae074c9e (patch)
tree9752d36c7303bae8567cc01ec0347d658c6d7207 /include/jemalloc
parent767d85061a6fb88ec977bbcd9b429a43aff391e6 (diff)
downloadjemalloc-0c516a00c4cb28cff55ce0995f756b5aae074c9e.zip
jemalloc-0c516a00c4cb28cff55ce0995f756b5aae074c9e.tar.gz
jemalloc-0c516a00c4cb28cff55ce0995f756b5aae074c9e.tar.bz2
Make *allocx() size class overflow behavior defined.
Limit supported size and alignment to HUGE_MAXCLASS, which in turn is now limited to be less than PTRDIFF_MAX. This resolves #278 and #295.
Diffstat (limited to 'include/jemalloc')
-rw-r--r--include/jemalloc/internal/arena.h3
-rw-r--r--include/jemalloc/internal/huge.h4
-rw-r--r--include/jemalloc/internal/jemalloc_internal.h.in15
-rwxr-xr-xinclude/jemalloc/internal/size_classes.sh4
-rw-r--r--include/jemalloc/internal/tcache.h10
-rw-r--r--include/jemalloc/jemalloc_macros.h.in3
6 files changed, 19 insertions, 20 deletions
diff --git a/include/jemalloc/internal/arena.h b/include/jemalloc/internal/arena.h
index 470eee6..891b9d7 100644
--- a/include/jemalloc/internal/arena.h
+++ b/include/jemalloc/internal/arena.h
@@ -536,8 +536,7 @@ extern arena_dalloc_junk_small_t *arena_dalloc_junk_small;
void arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info);
#endif
void arena_quarantine_junk_small(void *ptr, size_t usize);
-void *arena_malloc_large(tsd_t *tsd, arena_t *arena, size_t size,
- szind_t ind, bool zero);
+void *arena_malloc_large(tsd_t *tsd, arena_t *arena, szind_t ind, bool zero);
void *arena_malloc_hard(tsd_t *tsd, arena_t *arena, size_t size, szind_t ind,
bool zero, tcache_t *tcache);
void *arena_palloc(tsd_t *tsd, arena_t *arena, size_t usize,
diff --git a/include/jemalloc/internal/huge.h b/include/jemalloc/internal/huge.h
index 68d3789..cb6f69e 100644
--- a/include/jemalloc/internal/huge.h
+++ b/include/jemalloc/internal/huge.h
@@ -9,9 +9,9 @@
/******************************************************************************/
#ifdef JEMALLOC_H_EXTERNS
-void *huge_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero,
+void *huge_malloc(tsd_t *tsd, arena_t *arena, size_t usize, bool zero,
tcache_t *tcache);
-void *huge_palloc(tsd_t *tsd, arena_t *arena, size_t size, size_t alignment,
+void *huge_palloc(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment,
bool zero, tcache_t *tcache);
bool huge_ralloc_no_move(tsd_t *tsd, void *ptr, size_t oldsize,
size_t usize_min, size_t usize_max, bool zero);
diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in
index 611ed36..3f54391 100644
--- a/include/jemalloc/internal/jemalloc_internal.h.in
+++ b/include/jemalloc/internal/jemalloc_internal.h.in
@@ -642,7 +642,7 @@ JEMALLOC_ALWAYS_INLINE size_t
index2size(szind_t index)
{
- assert(index <= NSIZES);
+ assert(index < NSIZES);
return (index2size_lookup(index));
}
@@ -745,17 +745,16 @@ sa2u(size_t size, size_t alignment)
return (usize);
}
- /* Huge size class. Beware of size_t overflow. */
+ /* Huge size class. Beware of overflow. */
+
+ if (unlikely(alignment > HUGE_MAXCLASS))
+ return (0);
/*
* We can't achieve subchunk alignment, so round up alignment to the
* minimum that can actually be supported.
*/
alignment = CHUNK_CEILING(alignment);
- if (alignment == 0) {
- /* size_t overflow. */
- return (0);
- }
/* Make sure result is a huge size class. */
if (size <= chunksize)
@@ -1106,7 +1105,7 @@ iralloct_realign(tsd_t *tsd, void *ptr, size_t oldsize, size_t size,
size_t usize, copysize;
usize = sa2u(size + extra, alignment);
- if (usize == 0)
+ if (unlikely(usize == 0 || usize > HUGE_MAXCLASS))
return (NULL);
p = ipalloct(tsd, usize, alignment, zero, tcache, arena);
if (p == NULL) {
@@ -1114,7 +1113,7 @@ iralloct_realign(tsd_t *tsd, void *ptr, size_t oldsize, size_t size,
return (NULL);
/* Try again, without extra this time. */
usize = sa2u(size, alignment);
- if (usize == 0)
+ if (unlikely(usize == 0 || usize > HUGE_MAXCLASS))
return (NULL);
p = ipalloct(tsd, usize, alignment, zero, tcache, arena);
if (p == NULL)
diff --git a/include/jemalloc/internal/size_classes.sh b/include/jemalloc/internal/size_classes.sh
index fc82036..2b0ca29 100755
--- a/include/jemalloc/internal/size_classes.sh
+++ b/include/jemalloc/internal/size_classes.sh
@@ -142,10 +142,10 @@ size_classes() {
# All remaining groups.
lg_grp=$((${lg_grp} + ${lg_g}))
- while [ ${lg_grp} -lt ${ptr_bits} ] ; do
+ while [ ${lg_grp} -lt $((${ptr_bits} - 1)) ] ; do
sep_line
ndelta=1
- if [ ${lg_grp} -eq $((${ptr_bits} - 1)) ] ; then
+ if [ ${lg_grp} -eq $((${ptr_bits} - 2)) ] ; then
ndelta_limit=$((${g} - 1))
else
ndelta_limit=${g}
diff --git a/include/jemalloc/internal/tcache.h b/include/jemalloc/internal/tcache.h
index 25eaf14..8357820 100644
--- a/include/jemalloc/internal/tcache.h
+++ b/include/jemalloc/internal/tcache.h
@@ -344,7 +344,6 @@ tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
void *ret;
tcache_bin_t *tbin;
bool tcache_success;
- size_t usize JEMALLOC_CC_SILENCE_INIT(0);
assert(binind < nhbins);
tbin = &tcache->tbins[binind];
@@ -359,14 +358,15 @@ tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
if (unlikely(arena == NULL))
return (NULL);
- usize = index2size(binind);
- assert(usize <= tcache_maxclass);
- ret = arena_malloc_large(tsd, arena, usize, binind, zero);
+ ret = arena_malloc_large(tsd, arena, binind, zero);
if (ret == NULL)
return (NULL);
} else {
+ size_t usize JEMALLOC_CC_SILENCE_INIT(0);
+
/* Only compute usize on demand */
- if (config_prof || (slow_path && config_fill) || unlikely(zero)) {
+ if (config_prof || (slow_path && config_fill) ||
+ unlikely(zero)) {
usize = index2size(binind);
assert(usize <= tcache_maxclass);
}
diff --git a/include/jemalloc/jemalloc_macros.h.in b/include/jemalloc/jemalloc_macros.h.in
index d164eda..9f356f9 100644
--- a/include/jemalloc/jemalloc_macros.h.in
+++ b/include/jemalloc/jemalloc_macros.h.in
@@ -16,7 +16,8 @@
# define MALLOCX_ALIGN(a) ((int)(ffs(a)-1))
# else
# define MALLOCX_ALIGN(a) \
- ((int)((a < (size_t)INT_MAX) ? ffs((int)a)-1 : ffs((int)(a>>32))+31))
+ ((int)(((a) < (size_t)INT_MAX) ? ffs((int)(a))-1 : \
+ ffs((int)((a)>>32))+31))
# endif
# define MALLOCX_ZERO ((int)0x40)
/*