trinity: use Boehm-Demers-Weiser's garbage collecting memory allocator
Subsystems (2)
PATCH 1/1 trinity: use Boehm-Demers-Weiser's garbage collecting memory allocator
trinity currently quite often does not care about freeing memory on its own that was fetched through malloc(). Therefore it can happen if trinity runs for quite a long time, that oom-killer gets invoked. So for now use the Boehm-Demers-Weiser's garbage collecting memory allocator as a malloc replacement to avoid such situations.
In this patch, we wrap all BDW functions into bdw_* equivalents, so that we could later on still #ifdef something if we want to and replace all calls based on the build. Calling bdw_free() is still legimite, but the memory is reclaimed via bdw_gcollect(), which would solve trinity's memory management.
Dependency that we need when building (in case people don't want additional libs, follow-up patches could still #ifdef bdw_* wrappers to the glibc functions):
Fedora: yum install gc-devel
Debian: apt-get install libgc-dev
More information on Boehm-Demers-Weiser allocator can be found here:
http://www.hpl.hp.com/personal/Hans_Boehm/gc/
---
Based on top of ``[PATCH trinity] minor: move offsetof into trinity.h'', compile-tested.
Makefile | 4 +++- child.c | 3 +++ devices.c | 15 ++++++++------- files.c | 8 ++++---- include/trinity.h | 40 ++++++++++++++++++++++++++++++++++++++++ log.c | 6 ++++-- main.c | 2 ++ maps.c | 20 ++++++++++---------- net/alg.c | 4 +++- net/appletalk.c | 4 +++- net/atm.c | 6 ++++-- net/ax25.c | 4 +++- net/bpf.c | 12 ++++++------ net/caif.c | 4 +++- net/can.c | 5 ++++- net/decnet.c | 4 +++- net/econet.c | 4 +++- net/ipv4.c | 4 +++- net/ipv6.c | 4 +++- net/ipx.c | 4 +++- net/irda.c | 4 +++- net/llc.c | 4 +++- net/netlink.c | 4 +++- net/nfc.c | 4 +++- net/packet.c | 4 +++- net/phonet.c | 4 +++- net/pppox.c | 12 +++++++----- net/rose.c | 4 +++- net/tipc.c | 5 ++++- net/unix.c | 4 +++- net/x25.c | 4 +++- params.c | 2 +- random-address.c | 6 +++--- syscalls/execve.c | 5 +++-- syscalls/modify_ldt.c | 4 +++- syscalls/move_pages.c | 13 +++++++------ syscalls/sendmsg.c | 4 +++- syscalls/shmget.c | 8 ++++++-- syscalls/socketcall.c | 6 +++++- tables.c | 6 +++--- trinity.c | 2 ++ unicode.c | 2 +- 42 files changed, 190 insertions(+), 78 deletions(-)
Makefile +3 -1
@@ -21,6 +21,8 @@ CFLAGS += -Wwrite-strings # Only enabled during development. CFLAGS += -Werror +LDFLAGS = -lgc + V = @ Q = $(V:1=) QUIET_CC = $(Q:@=@echo ' CC '$@;)
@@ -57,7 +59,7 @@ DEPDIR= .deps -include $(SRCS:%.c=$(DEPDIR)/%.d) trinity: test $(OBJS) $(HEADERS) - $(QUIET_CC)$(CC) $(CFLAGS) -o trinity $(OBJS) + $(QUIET_CC)$(CC) $(CFLAGS) -o trinity $(OBJS) $(LDFLAGS) @mkdir -p tmp df = $(DEPDIR)/$(*F)
child.c +3 -0
@@ -13,6 +13,7 @@#include <sys/resource.h> #include <sys/prctl.h> +#include "trinity.h" #include "child.h" #include "syscall.h" #include "log.h"
@@ -153,5 +154,7 @@ int child_process(int childno)reenable_coredumps (); +bdw_gcollect (); +return ret; }
devices.c +8 -7
@@ -8,6 +8,7 @@#include <sys/types.h> #include <linux/kdev_t.h> +#include "trinity.h" #include "log.h" static struct {
@@ -37,9 +38,9 @@ static void parse_proc_devices(void) block =1 ; else if (sscanf(line, "%d %as", &major, &name) == 2) {if (block) { - new =realloc (block_devs, (bldevs+1 )*sizeof (*block_devs)); + new =bdw_realloc (block_devs, (bldevs+1 )*sizeof (*block_devs));if (!new) { -free (name); +bdw_free (name);continue ; } block_devs = new;
@@ -48,9 +49,9 @@ static void parse_proc_devices(void) block_devs[bldevs].name = name; bldevs++; }else { - new =realloc (char_devs, (chrdevs+1 )*sizeof (*char_devs)); + new =bdw_realloc (char_devs, (chrdevs+1 )*sizeof (*char_devs));if (!new) { -free (name); +bdw_free (name);continue ; } char_devs = new;
@@ -63,7 +64,7 @@ static void parse_proc_devices(void) }fclose (fp); -free (line); +bdw_free (line); }static void parse_proc_misc (void )
@@ -78,9 +79,9 @@ static void parse_proc_misc(void)return ;while (fscanf (fp,"%d %as" , &minor, &name) ==2 ) { - new =realloc (misc_devs, (miscdevs+1 )*sizeof (*misc_devs)); + new =bdw_realloc (misc_devs, (miscdevs+1 )*sizeof (*misc_devs));if (!new) { -free (name); +bdw_free (name);continue ; } misc_devs = new;
files.c +4 -4
@@ -100,7 +100,7 @@ static struct namelist *list_alloc(void) {struct namelist *node; - node =malloc (sizeof (struct namelist )); + node =bdw_malloc (sizeof (struct namelist ));if (node ==NULL )exit (EXIT_FAILURE );memset (node,0 ,sizeof (struct namelist ));
@@ -126,7 +126,7 @@ static void list_add(struct namelist *list, const char *name) list->next = list; list->prev = list; } - newnode->name =strdup (name); + newnode->name =bdw_strdup (name);__list_add (newnode, list, list->next); }
@@ -250,7 +250,7 @@ void generate_filelist(void)/* * Generate an index of pointers to the filenames */ - fileindex =malloc (sizeof (char *) * files_added); + fileindex =bdw_malloc (sizeof (char *) * files_added); node = names; do {
@@ -366,7 +366,7 @@ char * generate_pathname(void) case 91 ... 99:/* Create a bogus filename. */ - newpath =malloc (page_size);// FIXME: We leak this. + newpath =bdw_malloc (page_size);// FIXME: We leak this. if (newpath ==NULL )return get_filename ();// give up.
include/trinity.h +40 -0
@@ -1,6 +1,8 @@#ifndef _TRINITY_H#define _TRINITY_H 1 +#include <gc.h> +#include "types.h" #define UNLOCKED 0
@@ -25,6 +27,44 @@ void init_watchdog(void);extern unsigned int user_specified_children; +static inline void bdw_init (void ) +{ +GC_init (); + +GC_set_max_heap_size (100 *1024 *1024 ); +GC_expand_hp (1024 *1024 *5 ); +} + +static inline void *bdw_malloc (size_t size) +{ +return GC_malloc (size); +} + +static inline void *bdw_realloc (void *area,size_t size) +{ +return GC_realloc (area, size); +} + +static inline void *bdw_memalign (size_t boundary,size_t size) +{ +return GC_memalign (boundary, size); +} + +static inline char *bdw_strdup (const char *str) +{ +return GC_strdup (str); +} + +static inline void bdw_free (void *area) +{ +return GC_free (area); +} + +static inline void bdw_gcollect (void ) +{ +GC_gcollect (); +} +#define UNUSED (x) (void)(x)#define ARRAY_SIZE (arr) (sizeof(arr) / sizeof((arr)[0]))
log.c +4 -2
@@ -5,6 +5,8 @@#include <string.h> #include <unistd.h> #include <fcntl.h> + +#include "trinity.h" #include "params.h" // logging, monochrome, quiet_level #include "shm.h" #include "pids.h"
@@ -16,7 +18,7 @@ void open_logfiles(void)unsigned int i;char *logfilename; - logfilename =malloc (64 ); + logfilename =bdw_malloc (64 );sprintf (logfilename,"trinity.log" );unlink (logfilename); parentlogfile =fopen (logfilename,"a" );
@@ -34,7 +36,7 @@ void open_logfiles(void)exit (EXIT_FAILURE ); } } -free (logfilename); +bdw_free (logfilename); }void close_logfiles (void )
main.c +2 -0
@@ -309,6 +309,8 @@ static void handle_children(void) pid =waitpid (pid, &childstatus,WUNTRACED |WCONTINUED |WNOHANG );if (pid !=0 )handle_child (pid, childstatus); + +bdw_gcollect (); } }
maps.c +10 -10
@@ -7,6 +7,7 @@#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> +#include "trinity.h" // page_size #include "arch.h" #include "maps.h"
@@ -36,7 +37,7 @@ static struct map * alloc_map(void) {struct map *newmap; - newmap =malloc (sizeof (struct map )); + newmap =bdw_malloc (sizeof (struct map ));if (!newmap) {printf ("Couldn't allocate maps list!\n" );exit (EXIT_FAILURE );
@@ -94,7 +95,6 @@ static void * alloc_zero_map(struct map *map, int prot, const char *name) size *=2 ; tmpmap->ptr =mmap (NULL , size, prot,MAP_ANONYMOUS |MAP_SHARED ,-1 ,0 ); -if (tmpmap->ptr ==MAP_FAILED ) {printf ("mmap /dev/zero failure\n" );exit (EXIT_FAILURE );
@@ -102,7 +102,7 @@ static void * alloc_zero_map(struct map *map, int prot, const char *name) tmpmap->size = size; - tmpmap->name =malloc (80 ); + tmpmap->name =bdw_malloc (80 );if (!tmpmap->name) {fprintf (stderr,"malloc() failed in %s()." , __func__);exit (EXIT_FAILURE );
@@ -157,8 +157,8 @@ void destroy_maps(void)for (i =0 ; i < num_mappings; i++) { next = thismap->next;munmap (thismap->ptr, thismap->size); -free (thismap->name); -free (thismap); +bdw_free (thismap->name); +bdw_free (thismap); thismap = next; } num_mappings =0 ;
@@ -170,32 +170,32 @@ void init_buffers(void)output (2 ,"shm is at %p\n" , shm); - page_zeros =memalign (page_size, page_size *2 ); + page_zeros =bdw_memalign (page_size, page_size *2 );if (!page_zeros)exit (EXIT_FAILURE );memset (page_zeros,0 , page_size);output (2 ,"page_zeros @ %p\n" , page_zeros); - page_0xff =memalign (page_size, page_size *2 ); + page_0xff =bdw_memalign (page_size, page_size *2 );if (!page_0xff)exit (EXIT_FAILURE );memset (page_0xff,0xff , page_size);output (2 ,"page_0xff @ %p\n" , page_0xff); - page_rand =memalign (page_size, page_size *2 ); + page_rand =bdw_memalign (page_size, page_size *2 );if (!page_rand)exit (EXIT_FAILURE );memset (page_rand,0x55 , page_size);/* overwritten below */ output (2 ,"page_rand @ %p\n" , page_rand); - page_allocs =memalign (page_size, page_size *2 ); + page_allocs =bdw_memalign (page_size, page_size *2 );if (!page_allocs)exit (EXIT_FAILURE );memset (page_allocs,0xff , page_size);output (2 ,"page_allocs @ %p\n" , page_allocs);for (i =0 ; i < (page_size /sizeof (unsigned long *)); i++) - page_allocs[i] = (unsigned long )malloc (page_size); + page_allocs[i] = (unsigned long )bdw_malloc (page_size);setup_maps ();
net/alg.c +3 -1
@@ -3,6 +3,8 @@#include <sys/un.h> #include <netinet/in.h> #include <stdlib.h> + +#include "trinity.h" #include "config.h" #ifdef USE_IF_ALG
@@ -13,7 +15,7 @@ void gen_alg(unsigned long *addr, unsigned long *addrlen)struct sockaddr_alg *alg;unsigned int i; - alg =malloc (sizeof (struct sockaddr_alg )); + alg =bdw_malloc (sizeof (struct sockaddr_alg ));if (alg ==NULL )return ;
net/appletalk.c +3 -1
@@ -5,11 +5,13 @@#include <netinet/in.h> #include <linux/atalk.h> +#include "trinity.h" +void gen_appletalk (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_at *atalk; - atalk =malloc (sizeof (struct sockaddr_at )); + atalk =bdw_malloc (sizeof (struct sockaddr_at ));if (atalk ==NULL )return ;
net/atm.c +4 -2
@@ -5,11 +5,13 @@#include <linux/atm.h> #include <stdlib.h> +#include "trinity.h" +void gen_atmpvc (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_atmpvc *atmpvc; - atmpvc =malloc (sizeof (struct sockaddr_atmpvc )); + atmpvc =bdw_malloc (sizeof (struct sockaddr_atmpvc ));if (atmpvc ==NULL )return ;
@@ -26,7 +28,7 @@ void gen_atmsvc(unsigned long *addr, unsigned long *addrlen)struct sockaddr_atmsvc *atmsvc;unsigned int i; - atmsvc =malloc (sizeof (struct sockaddr_atmsvc )); + atmsvc =bdw_malloc (sizeof (struct sockaddr_atmsvc ));if (atmsvc ==NULL )return ;
net/ax25.c +3 -1
@@ -4,13 +4,15 @@#include <netinet/in.h> #include <linux/ax25.h> #include <stdlib.h> + +#include "trinity.h" #include "maps.h" // page_rand void gen_ax25 (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_ax25 *ax25; - ax25 =malloc (sizeof (struct sockaddr_ax25 )); + ax25 =bdw_malloc (sizeof (struct sockaddr_ax25 ));if (ax25 ==NULL )return ;
net/bpf.c +6 -6
@@ -320,17 +320,17 @@ void gen_seccomp_bpf(unsigned long *addr, unsigned long *addrlen)struct sock_fprog *bpf = (void *) addr;if (addrlen !=NULL ) { - bpf =malloc (sizeof (struct sock_fprog )); + bpf =bdw_malloc (sizeof (struct sock_fprog ));if (bpf ==NULL )return ; } bpf->len = avail =rand () %BPF_MAXINSNS ; - bpf->filter =malloc (bpf->len *sizeof (struct sock_filter )); + bpf->filter =bdw_malloc (bpf->len *sizeof (struct sock_filter ));if (bpf->filter ==NULL ) {if (addrlen !=NULL ) -free (bpf); +bdw_free (bpf);return ; }
@@ -358,17 +358,17 @@ void gen_bpf(unsigned long *addr, unsigned long *addrlen)struct sock_fprog *bpf = (void *) addr;if (addrlen !=NULL ) { - bpf =malloc (sizeof (struct sock_fprog )); + bpf =bdw_malloc (sizeof (struct sock_fprog ));if (bpf ==NULL )return ; } bpf->len =rand () %BPF_MAXINSNS ; - bpf->filter =malloc (bpf->len *sizeof (struct sock_filter )); + bpf->filter =bdw_malloc (bpf->len *sizeof (struct sock_filter ));if (bpf->filter ==NULL ) {if (addrlen !=NULL ) -free (bpf); +bdw_free (bpf);return ; }
net/caif.c +3 -1
@@ -3,6 +3,8 @@#include <sys/un.h> #include <netinet/in.h> #include <stdlib.h> + +#include "trinity.h" #include "config.h" #ifdef USE_CAIF
@@ -13,7 +15,7 @@ void gen_caif(unsigned long *addr, unsigned long *addrlen)struct sockaddr_caif *caif;unsigned int i; - caif =malloc (sizeof (struct sockaddr_caif )); + caif =bdw_malloc (sizeof (struct sockaddr_caif ));if (caif ==NULL )return ;
net/can.c +4 -1
@@ -5,13 +5,16 @@#include <linux/can.h> #include <stdlib.h> +#include "trinity.h" +void gen_can (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_can *can; - can =malloc (sizeof (struct sockaddr_can )); + can =bdw_malloc (sizeof (struct sockaddr_can ));if (can ==NULL )return ; + can->can_family =AF_CAN ; can->can_ifindex =rand (); can->can_addr.tp.rx_id =rand ();
net/decnet.c +3 -1
@@ -5,12 +5,14 @@#include <linux/dn.h> #include <stdlib.h> +#include "trinity.h" +void gen_decnet (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_dn *dn;unsigned int i; - dn =malloc (sizeof (struct sockaddr_dn )); + dn =bdw_malloc (sizeof (struct sockaddr_dn ));if (dn ==NULL )return ;
net/econet.c +3 -1
@@ -5,11 +5,13 @@#include <neteconet/ec.h> #include <stdlib.h> +#include "trinity.h" +void gen_econet (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_ec *ec; - ec =malloc (sizeof (struct sockaddr_ec )); + ec =bdw_malloc (sizeof (struct sockaddr_ec ));if (ec ==NULL )return ;
net/ipv4.c +3 -1
@@ -4,6 +4,8 @@#include <netinet/in.h> #include <stdlib.h> +#include "trinity.h" +in_addr_t random_ipv4_address (void ) {int addr =0 ;
@@ -61,7 +63,7 @@ void gen_ipv4(unsigned long *addr, unsigned long *addrlen) {struct sockaddr_in *ipv4; - ipv4 =malloc (sizeof (struct sockaddr_in )); + ipv4 =bdw_malloc (sizeof (struct sockaddr_in ));if (ipv4 ==NULL )return ;
net/ipv6.c +3 -1
@@ -7,11 +7,13 @@#include <linux/if_packet.h> #include <stdlib.h> +#include "trinity.h" +void gen_ipv6 (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_in6 *ipv6; - ipv6 =malloc (sizeof (struct sockaddr_in6 )); + ipv6 =bdw_malloc (sizeof (struct sockaddr_in6 ));if (ipv6 ==NULL )return ;
net/ipx.c +3 -1
@@ -5,12 +5,14 @@#include <linux/ipx.h> #include <stdlib.h> +#include "trinity.h" +void gen_ipx (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_ipx *ipx;unsigned int i; - ipx =malloc (sizeof (struct sockaddr_ipx )); + ipx =bdw_malloc (sizeof (struct sockaddr_ipx ));if (ipx ==NULL )return ;
net/irda.c +3 -1
@@ -5,12 +5,14 @@#include <linux/irda.h> #include <stdlib.h> +#include "trinity.h" +void gen_irda (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_irda *irda;unsigned int i; - irda =malloc (sizeof (struct sockaddr_irda )); + irda =bdw_malloc (sizeof (struct sockaddr_irda ));if (irda ==NULL )return ;
net/llc.c +3 -1
@@ -7,12 +7,14 @@#include <linux/llc.h> #include <stdlib.h> +#include "trinity.h" +void gen_llc (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_llc *llc;unsigned int i; - llc =malloc (sizeof (struct sockaddr_llc )); + llc =bdw_malloc (sizeof (struct sockaddr_llc ));if (llc ==NULL )return ; llc->sllc_family =AF_LLC ;
net/netlink.c +3 -1
@@ -5,11 +5,13 @@#include <linux/netlink.h> #include <stdlib.h> +#include "trinity.h" +void gen_netlink (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_nl *nl; - nl =malloc (sizeof (struct sockaddr_nl )); + nl =bdw_malloc (sizeof (struct sockaddr_nl ));if (nl ==NULL )return ;
net/nfc.c +3 -1
@@ -3,6 +3,8 @@#include <sys/un.h> #include <netinet/in.h> #include <stdlib.h> + +#include "trinity.h" #include "config.h" #include "compat.h"
@@ -11,7 +13,7 @@ void gen_nfc(unsigned long *addr, unsigned long *addrlen)struct sockaddr_nfc *nfc;// TODO: See also sockaddr_nfc_llcp - nfc =malloc (sizeof (struct sockaddr_nfc )); + nfc =bdw_malloc (sizeof (struct sockaddr_nfc ));if (nfc ==NULL )return ;
net/packet.c +3 -1
@@ -5,13 +5,15 @@#include <linux/if_packet.h> #include <stdlib.h> +#include "trinity.h" +void gen_packet (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_pkt *pkt;unsigned int i;//TODO: See also sockaddr_ll - pkt =malloc (sizeof (struct sockaddr_pkt )); + pkt =bdw_malloc (sizeof (struct sockaddr_pkt ));if (pkt ==NULL )return ;
net/phonet.c +3 -1
@@ -5,11 +5,13 @@#include <linux/phonet.h> #include <stdlib.h> +#include "trinity.h" +void gen_phonet (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_pn *pn; - pn =malloc (sizeof (struct sockaddr_pn )); + pn =bdw_malloc (sizeof (struct sockaddr_pn ));if (pn ==NULL )return ;
net/pppox.c +7 -5
@@ -6,6 +6,8 @@#include <linux/if_ether.h> /* for ETH_ALEN in if_pppox.h */ #include <linux/if_pppox.h> #include <stdlib.h> + +#include "trinity.h" #include "net.h" #include "sanitise.h"
@@ -23,7 +25,7 @@ void gen_pppox(unsigned long *addr, unsigned long *addrlen)switch (proto) {case PX_PROTO_OE : - pppox =malloc (sizeof (struct sockaddr_pppox )); + pppox =bdw_malloc (sizeof (struct sockaddr_pppox ));if (pppox ==NULL )return ;
@@ -49,7 +51,7 @@ void gen_pppox(unsigned long *addr, unsigned long *addrlen)switch (rand () %4 ) {case 0 :/* PPPoL2TP */ - pppol2tp =malloc (sizeof (struct sockaddr_pppol2tp )); + pppol2tp =bdw_malloc (sizeof (struct sockaddr_pppol2tp ));if (pppol2tp ==NULL )return ;
@@ -71,7 +73,7 @@ void gen_pppox(unsigned long *addr, unsigned long *addrlen) {struct sockaddr_pppol2tpin6 *pppol2tpin6; - pppol2tpin6 =malloc (sizeof (struct sockaddr_pppol2tpin6 )); + pppol2tpin6 =bdw_malloc (sizeof (struct sockaddr_pppol2tpin6 ));if (pppol2tpin6 ==NULL )return ;
@@ -102,7 +104,7 @@ void gen_pppox(unsigned long *addr, unsigned long *addrlen) {struct sockaddr_pppol2tpv3 *pppol2tpv3; - pppol2tpv3 =malloc (sizeof (struct sockaddr_pppol2tpv3 )); + pppol2tpv3 =bdw_malloc (sizeof (struct sockaddr_pppol2tpv3 ));if (pppol2tpv3 ==NULL )return ;
@@ -126,7 +128,7 @@ void gen_pppox(unsigned long *addr, unsigned long *addrlen) {struct sockaddr_pppol2tpv3in6 *pppol2tpv3in6; - pppol2tpv3in6 =malloc (sizeof (struct sockaddr_pppol2tpv3in6 )); + pppol2tpv3in6 =bdw_malloc (sizeof (struct sockaddr_pppol2tpv3in6 ));if (pppol2tpv3in6 ==NULL )return ;
net/rose.c +3 -1
@@ -5,13 +5,15 @@#include <linux/ax25.h> /* for ax25_address in rose.h */ #include <linux/rose.h> #include <stdlib.h> + +#include "trinity.h" #include "maps.h" // page_rand void gen_rose (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_rose *rose; - rose =malloc (sizeof (struct sockaddr_rose )); + rose =bdw_malloc (sizeof (struct sockaddr_rose ));if (rose ==NULL )return ;
net/tipc.c +4 -1
@@ -5,13 +5,16 @@#include <linux/tipc.h> #include <stdlib.h> +#include "trinity.h" +void gen_tipc (unsigned long *addr,unsigned long *addrlen) {struct sockaddr_tipc *tipc; - tipc =malloc (sizeof (struct sockaddr_tipc )); + tipc =bdw_malloc (sizeof (struct sockaddr_tipc ));if (tipc ==NULL )return ; + tipc->family =AF_TIPC ; tipc->addrtype =rand (); tipc->scope =rand ();
net/unix.c +3 -1
@@ -4,6 +4,8 @@#include <netinet/in.h> #include <linux/dn.h> #include <stdlib.h> + +#include "trinity.h" #include "maps.h" void gen_unixsock (unsigned long *addr,unsigned long *addrlen)
@@ -11,7 +13,7 @@ void gen_unixsock(unsigned long *addr, unsigned long *addrlen)struct sockaddr_un *unixsock;unsigned int len; - unixsock =malloc (sizeof (struct sockaddr_un )); + unixsock =bdw_malloc (sizeof (struct sockaddr_un ));if (unixsock ==NULL )return ;
net/x25.c +3 -1
@@ -4,6 +4,8 @@#include <netinet/in.h> #include <linux/x25.h> #include <stdlib.h> + +#include "trinity.h" #include "maps.h" // page_rand void gen_x25 (unsigned long *addr,unsigned long *addrlen)
@@ -11,7 +13,7 @@ void gen_x25(unsigned long *addr, unsigned long *addrlen)struct sockaddr_x25 *x25;unsigned int len; - x25 =malloc (sizeof (struct sockaddr_x25 )); + x25 =bdw_malloc (sizeof (struct sockaddr_x25 ));if (x25 ==NULL )return ;
params.c +1 -1
@@ -200,7 +200,7 @@ void parse_args(int argc, char *argv[])break ;case 'V' : - victim_path =strdup (optarg); + victim_path =bdw_strdup (optarg);//FIXME: Later, allow for multiple victim files break ;
random-address.c +3 -3
@@ -64,7 +64,7 @@ static void * _get_address(unsigned char null_allowed)break ;case 7 : addr =get_map ();break ; -case 8 : addr =malloc (page_size *2 ); +case 8 : addr =bdw_malloc (page_size *2 );break ;default :BUG ("unreachable!\n" );
@@ -145,10 +145,10 @@ struct iovec * alloc_iovec(unsigned int num)struct iovec *iov;unsigned int i; - iov =malloc (num *sizeof (struct iovec )); + iov =bdw_malloc (num *sizeof (struct iovec ));if (iov !=NULL ) {for (i =0 ; i < num; i++) { - iov[i].iov_base =malloc (page_size); + iov[i].iov_base =bdw_malloc (page_size); iov[i].iov_len = page_size; } }
syscalls/execve.c +3 -2
@@ -11,6 +11,7 @@*/ #include <stdio.h> #include <stdlib.h> +#include "trinity.h" // page_size #include "sanitise.h" #include "shm.h"
@@ -23,12 +24,12 @@ static unsigned long ** gen_ptrs_to_crap(void)unsigned int count =rand () %32 ;/* Fabricate argv */ - ptr =malloc (count *sizeof (void *));// FIXME: LEAK + ptr =bdw_malloc (count *sizeof (void *));if (ptr ==NULL )return NULL ;for (i =0 ; i < count; i++) { - ptr[i] =malloc (page_size);// FIXME: LEAK + ptr[i] =bdw_malloc (page_size);if (ptr[i] !=NULL )generate_random_page ((char *) ptr[i]); }
syscalls/modify_ldt.c +3 -1
@@ -8,6 +8,8 @@#include <sys/types.h> #define __ASSEMBLY__ 1#include <asm/ldt.h> + +#include "trinity.h" #include "sanitise.h" #include "shm.h"
@@ -22,7 +24,7 @@ static void sanitise_modify_ldt(int childno)case 0 :/* read the ldt into the memory pointed to by ptr. The number of bytes read is the smaller of bytecount and the actual size of the ldt. */ - ldt =malloc (ALLOCSIZE ); + ldt =bdw_malloc (ALLOCSIZE );if (ldt ==NULL )return ; shm->a3[childno] =ALLOCSIZE ;
syscalls/move_pages.c +7 -6
@@ -11,6 +11,7 @@#include <stdlib.h> #include <unistd.h> #include <sys/types.h> +#include "trinity.h" // page_size #include "sanitise.h" #include "arch.h"
@@ -32,7 +33,7 @@ static void sanitise_move_pages(int childno) shm->a6[childno] &= ~MPOL_MF_MOVE_ALL ; } - page_alloc = (unsigned long *)malloc (page_size); + page_alloc =bdw_malloc (page_size);if (page_alloc ==NULL )return ;
@@ -42,20 +43,20 @@ static void sanitise_move_pages(int childno) shm->a2[childno] = count;for (i =0 ; i < count; i++) { - page_alloc[i] = (unsigned long )malloc (page_size); + page_alloc[i] = (unsigned long )bdw_malloc (page_size);if (!page_alloc[i]) -return ;// FIXME: MEMORY LEAK +return ; page_alloc[i] &=PAGE_MASK ; } shm->a3[childno] = (unsigned long ) page_alloc; - nodes =malloc (count *sizeof (int )); + nodes =bdw_malloc (count *sizeof (int ));for (i =0 ; i < count; i++) nodes[i] = (int )rand () %2 ; - shm->a4[childno] = (unsigned long ) nodes; - shm->a5[childno] = (unsigned long )malloc (count *sizeof (int )); + shm->a4[childno] = (unsigned long ) nodes; + shm->a5[childno] = (unsigned long )bdw_malloc (count *sizeof (int )); }struct syscall syscall_move_pages = {
syscalls/sendmsg.c +3 -1
@@ -5,6 +5,8 @@#include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> + +#include "trinity.h" #include "compat.h" #include "random.h" #include "sanitise.h"
@@ -15,7 +17,7 @@ static void sanitise_sendmsg(int childno)struct msghdr *msg;// FIXME: Convert to use generic ARG_IOVEC - msg =malloc (sizeof (struct msghdr )); + msg =bdw_malloc (sizeof (struct msghdr ));if (msg ==NULL ) { shm->a2[childno] = (unsigned long )get_address ();return ;
syscalls/shmget.c +6 -2
@@ -5,6 +5,8 @@#include <stdlib.h> #include <sys/ipc.h> #include <sys/shm.h> + +#include "trinity.h" #include "sanitise.h" static void post_shmget (int syscallret)
@@ -14,11 +16,13 @@ static void post_shmget(int syscallret)if (syscallret ==-1 )return ; - shmid_ds =malloc (sizeof (struct shmid_ds )); + shmid_ds =bdw_malloc (sizeof (struct shmid_ds )); +if (!shmid_ds) +return ;shmctl (syscallret,IPC_RMID , shmid_ds); -free (shmid_ds); +bdw_free (shmid_ds); }struct syscall syscall_shmget = {
syscalls/socketcall.c +5 -1
@@ -3,6 +3,8 @@*/ #include <stdlib.h> #include <linux/net.h> + +#include "trinity.h" #include "compat.h" #include "sanitise.h" #include "shm.h"
@@ -11,7 +13,9 @@ static void sanitise_socketcall(int childno) {unsigned long *args; - args =malloc (6 *sizeof (unsigned long )); + args =bdw_malloc (6 *sizeof (unsigned long )); +if (!args) +return ; shm->a1[childno] =rand () %20 ;
tables.c +3 -3
@@ -505,7 +505,7 @@ int setup_syscall_group(unsigned int group) goto try_64bit; } - newsyscalls32 =malloc (count *sizeof (struct syscalltable )); + newsyscalls32 =bdw_malloc (count *sizeof (struct syscalltable ));if (newsyscalls32 ==NULL )return FALSE;
@@ -533,7 +533,7 @@ try_64bit:return FALSE; } - newsyscalls64 =malloc (count *sizeof (struct syscalltable )); + newsyscalls64 =bdw_malloc (count *sizeof (struct syscalltable ));if (newsyscalls64 ==NULL )return FALSE;
@@ -559,7 +559,7 @@ try_64bit:return FALSE; } - newsyscalls =malloc (count *sizeof (struct syscalltable )); + newsyscalls =bdw_malloc (count *sizeof (struct syscalltable ));if (newsyscalls ==NULL )exit (EXIT_FAILURE );
trinity.c +2 -0
@@ -154,6 +154,8 @@ int main(int argc, char* argv[])int childstatus;unsigned int i; +bdw_init (); + printf("Trinity v" __stringify(VERSION) " Dave Jones <[email protected]>\n"); progname = argv[0 ];
unicode.c +1 -1
@@ -126,7 +126,7 @@ void main(int argc, char* argv[])gettimeofday (&t,0 );srand ((t.tv_sec *getpid ()) ^ t.tv_usec); - page =malloc (4096 ); + page =bdw_malloc (4096 );memset (page,0 ,4096 );gen_unicode_page (page);
Signature · 1 line
1.7.11.7
Quoted 18 lines · Daniel Borkmann
trinity currently quite often does not care about freeing memory on its own that was fetched through
malloc(). Therefore it can happen if trinity runs for quite a long time, that oom-killer gets invoked. So for now use the Boehm-Demers-Weiser's garbage collecting memory allocator as a malloc replacement to avoid such situations.In this patch, we wrap all BDW functions into bdw_* equivalents, so that we could later on still #ifdef something if we want to and replace all calls based on the build. Calling
bdw_free()is still legimite, but the memory is reclaimed viabdw_gcollect(), which would solve trinity's memory management.Dependency that we need when building (in case people don't want additional libs, follow-up patches could still #ifdef bdw_* wrappers to the glibc functions):
Fedora: yum install gc-devel
Quoted 7 lines
Debian: apt-get install libgc-dev
More information on Boehm-Demers-Weiser allocator can be found here:
http://www.hpl.hp.com/personal/Hans_Boehm/gc/
Signed-off-by Daniel Borkmann <[email protected]>
Please ignore this for now, will send a further improved v2 soon.