Blame dwarfgen/fakemalloc.in

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