From: Julian Seward Date: Fri, 14 Jun 2002 10:17:05 +0000 (+0000) Subject: Patrick Ohly's --alignment= patch, to increase alignment of malloc'd X-Git-Tag: svn/VALGRIND_1_0_3~59 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f18008337fbf78f4c07212a2016da9243c42facb;p=thirdparty%2Fvalgrind.git Patrick Ohly's --alignment= patch, to increase alignment of malloc'd blocks if needed. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@422 --- diff --git a/cachegrind/docs/manual.html b/cachegrind/docs/manual.html index 8c61ce4956..873aa5c7a7 100644 --- a/cachegrind/docs/manual.html +++ b/cachegrind/docs/manual.html @@ -461,6 +461,17 @@ follows: maximum portability.

+

  • --alignment=<number> [default: 4]

    By + default valgrind's malloc, realloc, + etc, return 4-byte aligned addresses. These are suitable for + any accesses on x86 processors. + Some programs might however assume that malloc et + al return 8- or more aligned memory. + These programs are broken and should be fixed, but + if this is impossible for whatever reason the alignment can be + increased using this parameter. The supplied value must be + between 4 and 4096 inclusive, and must be a power of two.


  • +

  • --trace-children=no [the default]
    --trace-children=yes

    When enabled, Valgrind will trace into child processes. This diff --git a/coregrind/docs/manual.html b/coregrind/docs/manual.html index 8c61ce4956..873aa5c7a7 100644 --- a/coregrind/docs/manual.html +++ b/coregrind/docs/manual.html @@ -461,6 +461,17 @@ follows: maximum portability.


  • +

  • --alignment=<number> [default: 4]

    By + default valgrind's malloc, realloc, + etc, return 4-byte aligned addresses. These are suitable for + any accesses on x86 processors. + Some programs might however assume that malloc et + al return 8- or more aligned memory. + These programs are broken and should be fixed, but + if this is impossible for whatever reason the alignment can be + increased using this parameter. The supplied value must be + between 4 and 4096 inclusive, and must be a power of two.


  • +

  • --trace-children=no [the default]
    --trace-children=yes

    When enabled, Valgrind will trace into child processes. This diff --git a/coregrind/valgrind.in b/coregrind/valgrind.in index 23aa120304..fe31ce6bbc 100755 --- a/coregrind/valgrind.in +++ b/coregrind/valgrind.in @@ -61,6 +61,7 @@ do --leak-resolution=high) vgopts="$vgopts $arg"; shift;; --sloppy-malloc=no) vgopts="$vgopts $arg"; shift;; --sloppy-malloc=yes) vgopts="$vgopts $arg"; shift;; + --alignment=*) vgopts="$vgopts $arg"; shift;; --trace-children=no) vgopts="$vgopts $arg"; shift;; --trace-children=yes) vgopts="$vgopts $arg"; shift;; --workaround-gcc296-bugs=no) vgopts="$vgopts $arg"; shift;; @@ -130,6 +131,7 @@ if [ $# = 0 ] || [ z"$dousage" = z1 ]; then echo " amount of bt merging in leak check [low]" echo " --show-reachable=no|yes show reachable blocks in leak check? [no]" echo " --sloppy-malloc=no|yes round malloc sizes to next word? [no]" + echo " --alignment= set minimum alignment of allocations [4]" echo " --trace-children=no|yes Valgrind-ise child processes? [no]" echo " --logfile-fd= file descriptor for messages [2=stderr]" echo " --freelist-vol= volume of freed blocks queue [1000000]" diff --git a/coregrind/vg_clientmalloc.c b/coregrind/vg_clientmalloc.c index 607c633b98..0292aa404d 100644 --- a/coregrind/vg_clientmalloc.c +++ b/coregrind/vg_clientmalloc.c @@ -223,7 +223,8 @@ static ShadowChunk* client_malloc_shadow ( ThreadState* tst, align, size ); # endif - if (align == 0) + vg_assert(align >= 4); + if (align == 4) p = (Addr)VG_(malloc)(VG_AR_CLIENT, size); else p = (Addr)VG_(malloc_aligned)(VG_AR_CLIENT, align, size); @@ -271,7 +272,7 @@ void* VG_(client_malloc) ( ThreadState* tst, UInt size, VgAllocKind kind ) return VG_(malloc) ( VG_AR_CLIENT, size ); } - sc = client_malloc_shadow ( tst, 0, size, kind ); + sc = client_malloc_shadow ( tst, VG_(clo_alignment), size, kind ); VGP_POPCC; return (void*)(sc->data); } @@ -466,7 +467,8 @@ void* VG_(client_realloc) ( ThreadState* tst, void* ptrV, UInt size_new ) return ptrV; } else { /* new size is bigger */ - sc_new = client_malloc_shadow ( tst, 0, size_new, Vg_AllocMalloc ); + sc_new = client_malloc_shadow ( tst, VG_(clo_alignment), + size_new, Vg_AllocMalloc ); for (i = 0; i < sc->size; i++) ((UChar*)(sc_new->data))[i] = ((UChar*)(sc->data))[i]; VGM_(copy_address_range_perms) ( diff --git a/coregrind/vg_include.h b/coregrind/vg_include.h index fb675b949e..6c28cd4e0d 100644 --- a/coregrind/vg_include.h +++ b/coregrind/vg_include.h @@ -239,6 +239,9 @@ extern Int VG_(clo_leak_resolution); /* Round malloc sizes upwards to integral number of words? default: NO */ extern Bool VG_(clo_sloppy_malloc); +/* Minimum alignment in functions that don't specify alignment explicitly. + default: 0, i.e. use default of the machine (== 4) */ +extern Int VG_(clo_alignment); /* Allow loads from partially-valid addresses? default: YES */ extern Bool VG_(clo_partial_loads_ok); /* Simulate child processes? default: NO */ diff --git a/coregrind/vg_main.c b/coregrind/vg_main.c index afdcfe6850..3ed7ee0912 100644 --- a/coregrind/vg_main.c +++ b/coregrind/vg_main.c @@ -409,6 +409,7 @@ Bool VG_(clo_leak_check); Bool VG_(clo_show_reachable); Int VG_(clo_leak_resolution); Bool VG_(clo_sloppy_malloc); +Int VG_(clo_alignment); Bool VG_(clo_partial_loads_ok); Bool VG_(clo_trace_children); Int VG_(clo_logfile_fd); @@ -543,6 +544,7 @@ static void process_cmd_line_options ( void ) VG_(clo_show_reachable) = False; VG_(clo_leak_resolution) = 2; VG_(clo_sloppy_malloc) = False; + VG_(clo_alignment) = 4; VG_(clo_partial_loads_ok) = True; VG_(clo_trace_children) = False; VG_(clo_logfile_fd) = 2; /* stderr */ @@ -757,6 +759,9 @@ static void process_cmd_line_options ( void ) else if (STREQ(argv[i], "--sloppy-malloc=no")) VG_(clo_sloppy_malloc) = False; + else if (STREQN(12, argv[i], "--alignment=")) + VG_(clo_alignment) = (Int)VG_(atoll)(&argv[i][12]); + else if (STREQ(argv[i], "--trace-children=yes")) VG_(clo_trace_children) = True; else if (STREQ(argv[i], "--trace-children=no")) @@ -889,6 +894,16 @@ static void process_cmd_line_options ( void ) if (VG_(clo_verbosity < 0)) VG_(clo_verbosity) = 0; + if (VG_(clo_alignment) < 4 + || VG_(clo_alignment) > 4096 + || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { + VG_(message)(Vg_UserMsg, ""); + VG_(message)(Vg_UserMsg, + "Invalid --alignment= setting. " + "Should be a power of 2, >= 4, <= 4096."); + bad_option("--alignment"); + } + if (VG_(clo_GDB_attach) && VG_(clo_trace_children)) { VG_(message)(Vg_UserMsg, ""); VG_(message)(Vg_UserMsg, diff --git a/docs/manual.html b/docs/manual.html index 8c61ce4956..873aa5c7a7 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -461,6 +461,17 @@ follows: maximum portability.


  • +

  • --alignment=<number> [default: 4]

    By + default valgrind's malloc, realloc, + etc, return 4-byte aligned addresses. These are suitable for + any accesses on x86 processors. + Some programs might however assume that malloc et + al return 8- or more aligned memory. + These programs are broken and should be fixed, but + if this is impossible for whatever reason the alignment can be + increased using this parameter. The supplied value must be + between 4 and 4096 inclusive, and must be a power of two.


  • +

  • --trace-children=no [the default]
    --trace-children=yes

    When enabled, Valgrind will trace into child processes. This diff --git a/memcheck/docs/manual.html b/memcheck/docs/manual.html index 8c61ce4956..873aa5c7a7 100644 --- a/memcheck/docs/manual.html +++ b/memcheck/docs/manual.html @@ -461,6 +461,17 @@ follows: maximum portability.


  • +

  • --alignment=<number> [default: 4]

    By + default valgrind's malloc, realloc, + etc, return 4-byte aligned addresses. These are suitable for + any accesses on x86 processors. + Some programs might however assume that malloc et + al return 8- or more aligned memory. + These programs are broken and should be fixed, but + if this is impossible for whatever reason the alignment can be + increased using this parameter. The supplied value must be + between 4 and 4096 inclusive, and must be a power of two.


  • +

  • --trace-children=no [the default]
    --trace-children=yes

    When enabled, Valgrind will trace into child processes. This diff --git a/valgrind.in b/valgrind.in index 23aa120304..fe31ce6bbc 100755 --- a/valgrind.in +++ b/valgrind.in @@ -61,6 +61,7 @@ do --leak-resolution=high) vgopts="$vgopts $arg"; shift;; --sloppy-malloc=no) vgopts="$vgopts $arg"; shift;; --sloppy-malloc=yes) vgopts="$vgopts $arg"; shift;; + --alignment=*) vgopts="$vgopts $arg"; shift;; --trace-children=no) vgopts="$vgopts $arg"; shift;; --trace-children=yes) vgopts="$vgopts $arg"; shift;; --workaround-gcc296-bugs=no) vgopts="$vgopts $arg"; shift;; @@ -130,6 +131,7 @@ if [ $# = 0 ] || [ z"$dousage" = z1 ]; then echo " amount of bt merging in leak check [low]" echo " --show-reachable=no|yes show reachable blocks in leak check? [no]" echo " --sloppy-malloc=no|yes round malloc sizes to next word? [no]" + echo " --alignment= set minimum alignment of allocations [4]" echo " --trace-children=no|yes Valgrind-ise child processes? [no]" echo " --logfile-fd= file descriptor for messages [2=stderr]" echo " --freelist-vol= volume of freed blocks queue [1000000]" diff --git a/vg_clientmalloc.c b/vg_clientmalloc.c index 607c633b98..0292aa404d 100644 --- a/vg_clientmalloc.c +++ b/vg_clientmalloc.c @@ -223,7 +223,8 @@ static ShadowChunk* client_malloc_shadow ( ThreadState* tst, align, size ); # endif - if (align == 0) + vg_assert(align >= 4); + if (align == 4) p = (Addr)VG_(malloc)(VG_AR_CLIENT, size); else p = (Addr)VG_(malloc_aligned)(VG_AR_CLIENT, align, size); @@ -271,7 +272,7 @@ void* VG_(client_malloc) ( ThreadState* tst, UInt size, VgAllocKind kind ) return VG_(malloc) ( VG_AR_CLIENT, size ); } - sc = client_malloc_shadow ( tst, 0, size, kind ); + sc = client_malloc_shadow ( tst, VG_(clo_alignment), size, kind ); VGP_POPCC; return (void*)(sc->data); } @@ -466,7 +467,8 @@ void* VG_(client_realloc) ( ThreadState* tst, void* ptrV, UInt size_new ) return ptrV; } else { /* new size is bigger */ - sc_new = client_malloc_shadow ( tst, 0, size_new, Vg_AllocMalloc ); + sc_new = client_malloc_shadow ( tst, VG_(clo_alignment), + size_new, Vg_AllocMalloc ); for (i = 0; i < sc->size; i++) ((UChar*)(sc_new->data))[i] = ((UChar*)(sc->data))[i]; VGM_(copy_address_range_perms) ( diff --git a/vg_include.h b/vg_include.h index fb675b949e..6c28cd4e0d 100644 --- a/vg_include.h +++ b/vg_include.h @@ -239,6 +239,9 @@ extern Int VG_(clo_leak_resolution); /* Round malloc sizes upwards to integral number of words? default: NO */ extern Bool VG_(clo_sloppy_malloc); +/* Minimum alignment in functions that don't specify alignment explicitly. + default: 0, i.e. use default of the machine (== 4) */ +extern Int VG_(clo_alignment); /* Allow loads from partially-valid addresses? default: YES */ extern Bool VG_(clo_partial_loads_ok); /* Simulate child processes? default: NO */ diff --git a/vg_main.c b/vg_main.c index afdcfe6850..3ed7ee0912 100644 --- a/vg_main.c +++ b/vg_main.c @@ -409,6 +409,7 @@ Bool VG_(clo_leak_check); Bool VG_(clo_show_reachable); Int VG_(clo_leak_resolution); Bool VG_(clo_sloppy_malloc); +Int VG_(clo_alignment); Bool VG_(clo_partial_loads_ok); Bool VG_(clo_trace_children); Int VG_(clo_logfile_fd); @@ -543,6 +544,7 @@ static void process_cmd_line_options ( void ) VG_(clo_show_reachable) = False; VG_(clo_leak_resolution) = 2; VG_(clo_sloppy_malloc) = False; + VG_(clo_alignment) = 4; VG_(clo_partial_loads_ok) = True; VG_(clo_trace_children) = False; VG_(clo_logfile_fd) = 2; /* stderr */ @@ -757,6 +759,9 @@ static void process_cmd_line_options ( void ) else if (STREQ(argv[i], "--sloppy-malloc=no")) VG_(clo_sloppy_malloc) = False; + else if (STREQN(12, argv[i], "--alignment=")) + VG_(clo_alignment) = (Int)VG_(atoll)(&argv[i][12]); + else if (STREQ(argv[i], "--trace-children=yes")) VG_(clo_trace_children) = True; else if (STREQ(argv[i], "--trace-children=no")) @@ -889,6 +894,16 @@ static void process_cmd_line_options ( void ) if (VG_(clo_verbosity < 0)) VG_(clo_verbosity) = 0; + if (VG_(clo_alignment) < 4 + || VG_(clo_alignment) > 4096 + || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { + VG_(message)(Vg_UserMsg, ""); + VG_(message)(Vg_UserMsg, + "Invalid --alignment= setting. " + "Should be a power of 2, >= 4, <= 4096."); + bad_option("--alignment"); + } + if (VG_(clo_GDB_attach) && VG_(clo_trace_children)) { VG_(message)(Vg_UserMsg, ""); VG_(message)(Vg_UserMsg,