@ -1,4 +1,4 @@
/* $OpenBSD: malloc.c,v 1.157 2014/04/23 10:47:15 espie Exp $ */
/* $OpenBSD: malloc.c,v 1.158 2014/04/23 15:07:27 tedu Exp $ */
/*
/*
* Copyright ( c ) 2008 , 2010 , 2011 Otto Moerbeek < otto @ drijf . net >
* Copyright ( c ) 2008 , 2010 , 2011 Otto Moerbeek < otto @ drijf . net >
* Copyright ( c ) 2012 Matthew Dempsky < matthew @ openbsd . org >
* Copyright ( c ) 2012 Matthew Dempsky < matthew @ openbsd . org >
@ -167,7 +167,6 @@ struct malloc_readonly {
int malloc_move ; /* move allocations to end of page? */
int malloc_move ; /* move allocations to end of page? */
int malloc_realloc ; /* always realloc? */
int malloc_realloc ; /* always realloc? */
int malloc_xmalloc ; /* xmalloc behaviour? */
int malloc_xmalloc ; /* xmalloc behaviour? */
int malloc_zero ; /* zero fill? */
size_t malloc_guard ; /* use guard pages after allocations? */
size_t malloc_guard ; /* use guard pages after allocations? */
u_int malloc_cache ; /* free pages we cache */
u_int malloc_cache ; /* free pages we cache */
# ifdef MALLOC_STATS
# ifdef MALLOC_STATS
@ -414,7 +413,7 @@ map(struct dir_info *d, size_t sz, int zero_fill)
d - > free_regions_size - = psz ;
d - > free_regions_size - = psz ;
if ( zero_fill )
if ( zero_fill )
memset ( p , 0 , sz ) ;
memset ( p , 0 , sz ) ;
else if ( mopts . malloc_junk & &
else if ( mopts . malloc_junk = = 2 & &
mopts . malloc_freeunmap )
mopts . malloc_freeunmap )
memset ( p , SOME_FREEJUNK , sz ) ;
memset ( p , SOME_FREEJUNK , sz ) ;
return p ;
return p ;
@ -433,7 +432,7 @@ map(struct dir_info *d, size_t sz, int zero_fill)
d - > free_regions_size - = psz ;
d - > free_regions_size - = psz ;
if ( zero_fill )
if ( zero_fill )
memset ( p , 0 , sz ) ;
memset ( p , 0 , sz ) ;
else if ( mopts . malloc_junk & & mopts . malloc_freeunmap )
else if ( mopts . malloc_junk = = 2 & & mopts . malloc_freeunmap )
memset ( p , SOME_FREEJUNK , sz ) ;
memset ( p , SOME_FREEJUNK , sz ) ;
return p ;
return p ;
}
}
@ -463,6 +462,7 @@ omalloc_init(struct dir_info **dp)
* Default options
* Default options
*/
*/
mopts . malloc_abort = 1 ;
mopts . malloc_abort = 1 ;
mopts . malloc_junk = 1 ;
mopts . malloc_move = 1 ;
mopts . malloc_move = 1 ;
mopts . malloc_cache = MALLOC_DEFAULT_CACHE ;
mopts . malloc_cache = MALLOC_DEFAULT_CACHE ;
@ -536,7 +536,7 @@ omalloc_init(struct dir_info **dp)
mopts . malloc_junk = 0 ;
mopts . malloc_junk = 0 ;
break ;
break ;
case ' J ' :
case ' J ' :
mopts . malloc_junk = 1 ;
mopts . malloc_junk = 2 ;
break ;
break ;
case ' n ' :
case ' n ' :
case ' N ' :
case ' N ' :
@ -559,7 +559,8 @@ omalloc_init(struct dir_info **dp)
mopts . malloc_cache = MALLOC_DEFAULT_CACHE ;
mopts . malloc_cache = MALLOC_DEFAULT_CACHE ;
break ;
break ;
case ' S ' :
case ' S ' :
mopts . malloc_freeunmap = mopts . malloc_junk = 1 ;
mopts . malloc_freeunmap = 1 ;
mopts . malloc_junk = 2 ;
mopts . malloc_guard = MALLOC_PAGESIZE ;
mopts . malloc_guard = MALLOC_PAGESIZE ;
mopts . malloc_cache = 0 ;
mopts . malloc_cache = 0 ;
break ;
break ;
@ -575,12 +576,6 @@ omalloc_init(struct dir_info **dp)
case ' X ' :
case ' X ' :
mopts . malloc_xmalloc = 1 ;
mopts . malloc_xmalloc = 1 ;
break ;
break ;
case ' z ' :
mopts . malloc_zero = 0 ;
break ;
case ' Z ' :
mopts . malloc_zero = 1 ;
break ;
default : {
default : {
static const char q [ ] = " malloc() warning: "
static const char q [ ] = " malloc() warning: "
" unknown char in MALLOC_OPTIONS \n " ;
" unknown char in MALLOC_OPTIONS \n " ;
@ -591,13 +586,6 @@ omalloc_init(struct dir_info **dp)
}
}
}
}
/*
* We want junk in the entire allocation , and zero only in the part
* the user asked for .
*/
if ( mopts . malloc_zero )
mopts . malloc_junk = 1 ;
# ifdef MALLOC_STATS
# ifdef MALLOC_STATS
if ( mopts . malloc_stats & & ( atexit ( malloc_exit ) = = - 1 ) ) {
if ( mopts . malloc_stats & & ( atexit ( malloc_exit ) = = - 1 ) ) {
static const char q [ ] = " malloc() warning: atexit(2) failed. "
static const char q [ ] = " malloc() warning: atexit(2) failed. "
@ -971,7 +959,7 @@ malloc_bytes(struct dir_info *d, size_t size, void *f)
k + = ( lp - bp - > bits ) * MALLOC_BITS ;
k + = ( lp - bp - > bits ) * MALLOC_BITS ;
k < < = bp - > shift ;
k < < = bp - > shift ;
if ( mopts . malloc_junk & & bp - > size > 0 )
if ( mopts . malloc_junk = = 2 & & bp - > size > 0 )
memset ( ( char * ) bp - > page + k , SOME_JUNK , bp - > size ) ;
memset ( ( char * ) bp - > page + k , SOME_JUNK , bp - > size ) ;
return ( ( char * ) bp - > page + k ) ;
return ( ( char * ) bp - > page + k ) ;
}
}
@ -1069,16 +1057,16 @@ omalloc(size_t sz, int zero_fill, void *f)
sz - mopts . malloc_guard < MALLOC_PAGESIZE -
sz - mopts . malloc_guard < MALLOC_PAGESIZE -
MALLOC_LEEWAY ) {
MALLOC_LEEWAY ) {
/* fill whole allocation */
/* fill whole allocation */
if ( mopts . malloc_junk )
if ( mopts . malloc_junk = = 2 )
memset ( p , SOME_JUNK , psz - mopts . malloc_guard ) ;
memset ( p , SOME_JUNK , psz - mopts . malloc_guard ) ;
/* shift towards the end */
/* shift towards the end */
p = ( ( char * ) p ) + ( ( MALLOC_PAGESIZE - MALLOC_LEEWAY -
p = ( ( char * ) p ) + ( ( MALLOC_PAGESIZE - MALLOC_LEEWAY -
( sz - mopts . malloc_guard ) ) & ~ ( MALLOC_MINSIZE - 1 ) ) ;
( sz - mopts . malloc_guard ) ) & ~ ( MALLOC_MINSIZE - 1 ) ) ;
/* fill zeros if needed and overwritten above */
/* fill zeros if needed and overwritten above */
if ( zero_fill & & mopts . malloc_junk )
if ( zero_fill & & mopts . malloc_junk = = 2 )
memset ( p , 0 , sz - mopts . malloc_guard ) ;
memset ( p , 0 , sz - mopts . malloc_guard ) ;
} else {
} else {
if ( mopts . malloc_junk ) {
if ( mopts . malloc_junk = = 2 ) {
if ( zero_fill )
if ( zero_fill )
memset ( ( char * ) p + sz - mopts . malloc_guard ,
memset ( ( char * ) p + sz - mopts . malloc_guard ,
SOME_JUNK , psz - sz ) ;
SOME_JUNK , psz - sz ) ;
@ -1146,7 +1134,7 @@ malloc(size_t size)
malloc_recurse ( ) ;
malloc_recurse ( ) ;
return NULL ;
return NULL ;
}
}
r = omalloc ( size , mopts . malloc_zero , CALLER ) ;
r = omalloc ( size , 0 , CALLER ) ;
malloc_active - - ;
malloc_active - - ;
_MALLOC_UNLOCK ( ) ;
_MALLOC_UNLOCK ( ) ;
if ( r = = NULL & & mopts . malloc_xmalloc ) {
if ( r = = NULL & & mopts . malloc_xmalloc ) {
@ -1198,9 +1186,11 @@ ofree(void *p)
}
}
malloc_guarded - = mopts . malloc_guard ;
malloc_guarded - = mopts . malloc_guard ;
}
}
if ( mopts . malloc_junk & & ! mopts . malloc_freeunmap )
memset ( p , SOME_FREEJUNK ,
PAGEROUND ( sz ) - mopts . malloc_guard ) ;
if ( mopts . malloc_junk & & ! mopts . malloc_freeunmap ) {
size_t amt = mopts . malloc_junk = = 1 ? MALLOC_MAXCHUNK :
PAGEROUND ( sz ) - mopts . malloc_guard ;
memset ( p , SOME_FREEJUNK , amt ) ;
}
unmap ( g_pool , p , PAGEROUND ( sz ) ) ;
unmap ( g_pool , p , PAGEROUND ( sz ) ) ;
delete ( g_pool , r ) ;
delete ( g_pool , r ) ;
} else {
} else {
@ -1304,7 +1294,7 @@ orealloc(void *p, size_t newsz, void *f)
q = MAP_FAILED ;
q = MAP_FAILED ;
if ( q = = hint ) {
if ( q = = hint ) {
malloc_used + = needed ;
malloc_used + = needed ;
if ( mopts . malloc_junk )
if ( mopts . malloc_junk = = 2 )
memset ( q , SOME_JUNK , needed ) ;
memset ( q , SOME_JUNK , needed ) ;
r - > size = newsz ;
r - > size = newsz ;
STATS_SETF ( r , f ) ;
STATS_SETF ( r , f ) ;
@ -1331,7 +1321,7 @@ orealloc(void *p, size_t newsz, void *f)
STATS_SETF ( r , f ) ;
STATS_SETF ( r , f ) ;
return p ;
return p ;
} else {
} else {
if ( newsz > oldsz & & mopts . malloc_junk )
if ( newsz > oldsz & & mopts . malloc_junk = = 2 )
memset ( ( char * ) p + newsz , SOME_JUNK ,
memset ( ( char * ) p + newsz , SOME_JUNK ,
rnewsz - mopts . malloc_guard - newsz ) ;
rnewsz - mopts . malloc_guard - newsz ) ;
r - > size = gnewsz ;
r - > size = gnewsz ;
@ -1340,7 +1330,7 @@ orealloc(void *p, size_t newsz, void *f)
}
}
}
}
if ( newsz < = oldsz & & newsz > oldsz / 2 & & ! mopts . malloc_realloc ) {
if ( newsz < = oldsz & & newsz > oldsz / 2 & & ! mopts . malloc_realloc ) {
if ( mopts . malloc_junk & & newsz > 0 )
if ( mopts . malloc_junk = = 2 & & newsz > 0 )
memset ( ( char * ) p + newsz , SOME_JUNK , oldsz - newsz ) ;
memset ( ( char * ) p + newsz , SOME_JUNK , oldsz - newsz ) ;
STATS_SETF ( r , f ) ;
STATS_SETF ( r , f ) ;
return p ;
return p ;
@ -1525,7 +1515,7 @@ omemalign(size_t alignment, size_t sz, int zero_fill, void *f)
malloc_guarded + = mopts . malloc_guard ;
malloc_guarded + = mopts . malloc_guard ;
}
}
if ( mopts . malloc_junk ) {
if ( mopts . malloc_junk = = 2 ) {
if ( zero_fill )
if ( zero_fill )
memset ( ( char * ) p + sz - mopts . malloc_guard ,
memset ( ( char * ) p + sz - mopts . malloc_guard ,
SOME_JUNK , psz - sz ) ;
SOME_JUNK , psz - sz ) ;
@ -1556,7 +1546,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
malloc_recurse ( ) ;
malloc_recurse ( ) ;
goto err ;
goto err ;
}
}
r = omemalign ( alignment , size , mopts . malloc_zero , CALLER ) ;
r = omemalign ( alignment , size , 0 , CALLER ) ;
malloc_active - - ;
malloc_active - - ;
_MALLOC_UNLOCK ( ) ;
_MALLOC_UNLOCK ( ) ;
if ( r = = NULL ) {
if ( r = = NULL ) {