Debian Codegraph @

src/bin/dwarfgen/fakemalloc.in

raw
1 /*
2
3 This file is a simple way (on Linux) to do a certain kind
4 of test. "Code Testing Through Fault Injection" in ":login;"
5 magazine (December, 2014. Usenix.org) by Peter Gutmann offered
6 a simple example by an unnamed friend: instrument malloc() so
7 on call N it returns NULL. Try with N from 0 to some higher
8 number (I used 0 to 100). Run your chosen executable and
9 see how it fares. This test exposed a couple bugs in libdwarf.
10
11 Here are some of the example core dumps (running dwarfgen):
12 2000: Could not allocate Dwarf_Error structure, abort() in libdwarf.
13 1000: FAIL:bad alloc caughtstd::bad_alloc
14 500: FAIL:bad alloc caughtstd::bad_alloc
15 250: FAIL:bad alloc caughtstd::bad_alloc
16 125: Could not allocate Dwarf_Error structure, abort() in libdwarf.
17 Aborted (core dumped)
18 100: FAIL:bad alloc caughtstd::bad_alloc
19 46-60: FAIL:bad alloc caughtstd::bad_alloc
20 45- Could not allocate Dwarf_Error structure, abort() in libdwarf.
21 Aborted (core dumped)
22 42-44: FAIL:bad alloc caughtstd::bad_alloc
23 30- 41: Could not allocate Dwarf_Error structure, abort() in libdwarf.
24 Aborted (core dumped)
25 28-29 :FAIL:bad alloc caught std::bad_alloc
26 Aborted (core dumped)
27
28 8,9,10,11-27: Could not allocate Dwarf_Error structure, abort() in libdwarf.
29 Aborted (core dumped)
30 7: 6: FAIL:bad alloc caught std::bad_alloc
31
32 0,1,2,3,4,5: terminate called after throwing an instance of 'std::bad_alloc'
33 what(): std::bad_alloc
34 Aborted (core dumped)
35
36 -------HOW TO USE:
37 Configure generates the following for Makefile.
38 $(CXX) $(CXXFLAGS) -o $@ $(DGOBJECTS) $(LDFLAGS)
39
40 In the generated Makefile, replace the above line with
41 these two lines.
42 $(CC) $(CFLAGS) -c fakemalloc.o
43 $(CXX) $(CXXFLAGS) -o $@ $(DGOBJECTS) $(LDFLAGS) fakemalloc.o
44
45 Run tests using the script TESTmallocfail
46 -------
47
48 */
49
50 static unsigned count;
51 extern void * __libc_malloc();
52
53 void *malloc(unsigned len)
54 {
55 /* Perhaps the test should be count >= FAILCOUNT ??? */
56 if (count == FAILCOUNT) {
57 return 0;
58 }
59 count++;
60 return __libc_malloc(len);
61 }