Using Valgrind to Detect Undefined Value Errors with Bit-Precision

Using Valgrind to Detect Undefined Value Errors with Bit-Precision

Using Valgrind to detect undefined value errors with bit-precision Julian Seward Nicholas Nethercote OpenWorks LLP Department of Computer Sciences Cambridge, UK University of Texas at Austin [email protected] [email protected] Abstract We present Memcheck, a tool that has been implemented with the dynamic binary instrumentation framework Val- grind. Memcheck detects a wide range of memory errors 1.1 Basic operation and features in programs as they run. This paper focuses on one kind Memcheck is a dynamic analysis tool, and so checks pro- of error that Memcheck detects: undefined value errors. grams for errors as they run. Memcheck performs four Such errors are common, and often cause bugs that are kinds of memory error checking. hard to find in programs written in languages such as C, First, it tracks the addressability of every byte of mem- C++ and Fortran. Memcheck's definedness checking im- ory, updating the information as memory is allocated and proves on that of previous tools by being accurate to the freed. With this information, it can detect all accesses to level of individual bits. This accuracy gives Memcheck unaddressable memory. a low false positive and false negative rate. Second, it tracks all heap blocks allocated with The definedness checking involves shadowing every malloc(), new and new[]. With this information it bit of data in registers and memory with a second bit can detect bad or repeated frees of heap blocks, and can that indicates if the bit has a defined value. Every value- detect memory leaks at program termination. creating operation is instrumented with a shadow oper- Third, it checks that memory blocks supplied as argu- ation that propagates shadow bits appropriately. Mem- ments to functions like strcpy() and memcpy() do check uses these shadow bits to detect uses of undefined not overlap. This does not require any additional state to values that could adversely affect a program's behaviour. be tracked. Under Memcheck, programs typically run 20–30 Fourth, it performs definedness checking: it tracks the times slower than normal. This is fast enough to use with definedness of every bit of data in registers and memory. large programs. Memcheck finds many errors in real pro- With this information it can detect undefined value errors grams, and has been used during the past two years by with bit-precision. thousands of programmers on a wide range of systems, All four kinds of checking are useful. However, of including OpenOffice, Mozilla, Opera, KDE, GNOME, the four, definedness checking is easily the most sophis- MySQL, Perl, Samba, The GIMP, and Unreal Tourna- ticated, and it is this checking that this paper focuses on. ment. Memcheck uses dynamic binary instrumentation to in- strument the program to be checked (the client) on-the- fly at run-time. Execution of the added instrumentation 1 Introduction code is interleaved with the program's normal execution, not disturbing normal program behaviour (other than The accidental use of undefined values is a notorious slowing it down), but doing extra work “on the side” to source of bugs in programs written in imperative lan- detect memory errors. Because it instruments and analy- guages such as C, C++ and Fortran. Such undefined ses executable machine code, rather than source code or value errors are easy to make, but can be extremely diffi- object code, it has the following nice properties. cult to track down manually, sometimes lurking unfound for years. • Wide applicability: it works with programs written In this paper we describe Memcheck, a practical tool in any language.1 which detects a wide range of memory errors, including undefined value errors. Memcheck is implemented using • Total coverage: all parts of the client are executed the dynamic binary instrumentation framework Valgrind under Memcheck's control, including dynamically [10, 9]. linked libraries and the dynamic linker, even if the int main(void) { void set_bit ( int* arr, int n ) { int x, y, z, *p; arr[n/32] |= (1 << (n%32)); char buf[10]; } write(1, buf, 1); // bug 1 int get_bit ( int* arr, int n ) { x = ( x == 0 ? y : z ); // bug 2 return 1 & (arr[n/32] >> (n%32)); return *p + x; // bug 3 } } int main ( void ) { int* arr = malloc(10 * sizeof(int)); Figure 1: Example program badprog.c set_bit(arr, 177); printf("%d\n", get_bit(arr, 178)); source code is not available on the system. In- return 0; } deed, it is only by doing this that Memcheck can be accurate—partial coverage leads either to lots of Figure 3: Unsafe use of a bit-array missed errors (false negatives) or lots of invalid er- rors (false positives). the stack of thread 1 (the main thread). The second in- • Ease of use: unlike many similar tools, it does not dicates that a conditional jump depends on an undefined require programs to be prepared (e.g. recompiled or value. The third indicates that an undefined value is used relinked) in any way. as a pointer. All three error messages include a stack trace that indicate precisely where the error is occurring. Memcheck is part of the Valgrind suite, which is free In order to get exact line numbers in the stack traces, the (GPL) software, and is available for download from the client must be compiled with debugging information. If Valgrind website [14]. It currently runs only on the this is missing, the code locations are less precise, as can x86/Linux platform, although work is currently under- be seen with the location within the write() function way to port it to other platforms such as AMD64/Linux, (not to be confused with the write() system call)— PowerPC/Linux, x86/FreeBSD, and PowerPC/MacOSX. GNU libc on this system was not compiled with debug- ging information. 1.2 Using Memcheck Attentive readers may note that the final line of Memcheck is easy to use. As an example, consider the badprog.c could cause a segmentation fault due to the (contrived) program badprog.c in Figure 1. It con- use of the uninitialised variable p as an address. On one tains three undefined value errors. To check the compiled system we tried this test on, exactly that happened, and so program badprog the user only has to type: Memcheck issued an additional “Invalid read of size 4” warning immediately before the memory access, thanks valgrind --tool=memcheck badprog to its addressability checking. For the run presented, the The --tool= option specifies which tool in the Val- memory access (un)luckily hit addressable memory, so grind suite is used. The program runs under Mem- no addressability error message was issued. check's control, typically 20–30 times slower than usual. This slow-down is partly due to Memcheck's definedness 1.3 Bit-level precision checking, partly due to its other checking, and partly due to Valgrind's inherent overhead. The program's output Memcheck is the first tool we are aware of that tracks is augmented by Memcheck's output, which goes by de- definedness down to the level of bits. Other tools track fault to standard error, although it can be redirected to a definedness at byte granularity (Purify) or word granu- file, file descriptor, or socket with a command line op- larity (Third Degree). tion. This means Memcheck correctly handles code which Figure 2 shows the resulting output. The first three deals with partially-defined bytes. In C and C++, two lines are printed by Memcheck on startup. The middle common idioms give rise to such bytes: use of bit arrays section shows three error messages issued by Memcheck. and use of bitfields in structures. Tools which track de- The final three lines are printed at termination, and sum- finedness at byte or word granularities necessarily give marise Memcheck's findings. Each line of Memcheck's inaccurate results in such situations – either they fail to output is prefixed with the client's process ID, 27607 in report genuine errors resulting from uses of uninitialised this case. bits, or they falsely flag errors resulting from correct uses The three error messages are quite precise. The first of partially defined bytes. indicates that the memory passed to the system call The program shown in Figure 3 uses an uninitialised write() via the buf argument contains undefined val- bit in a bit-array. Memcheck reports this, but Purify does ues; its last line indicates that the undefined value is on not2. Memcheck is known to have found previously- ==27607== Memcheck, a memory error detector for x86-linux. ==27607== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al. ==27607== For more details, rerun with: -v ==27607== ==27607== Syscall param write(buf) contains uninitialised or unaddressable byte(s) ==27607== at 0x420D2473: write (in /lib/tls/libc-2.3.2.so) ==27607== by 0x8048347: main (badprog.c:6) ==27607== Address 0x52BFE880 is on thread 1's stack <junk character printed by write()> ==27607== ==27607== Conditional jump or move depends on uninitialised value(s) ==27607== at 0x804834F: main (badprog.c:7) ==27607== ==27607== Use of uninitialised value of size 4 ==27607== at 0x804836B: main (badprog.c:8) ==27607== ==27607== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 11 from 1) ==27607== malloc/free: in use at exit: 0 bytes in 0 blocks. ==27607== malloc/free: 0 allocs, 0 frees, 0 bytes allocated. Figure 2: Example output for badprog.c undetected uses of single uninitialised bits in C++ struc- namic binary instrumentation; it does all the hard work of ture bitfields, in at least one large, widely used C++ code inserting instrumentation into machine code at run-time. base. Tools are created as plug-ins, written in C, to Valgrind's core.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us