8 #ifndef ATHENAAUDITORS_MEMORYHOOKSSTDCMALLOC_H
9 #define ATHENAAUDITORS_MEMORYHOOKSSTDCMALLOC_H
35 static std::string
name;
41 static void printAllocation(
void*
ptr,
size_t n );
45 # pragma GCC diagnostic push
46 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
168 static void my_init_hook (
void);
169 #if HAVE_MALLOC_HOOKS
170 static void *my_malloc_hook (
size_t,
const void *);
171 static void *my_realloc_hook (
void *,
size_t,
const void *);
172 static void *my_memalign_hook (
size_t,
size_t,
const void *);
173 static void my_free_hook (
void*,
const void *);
180 #if HAVE_MALLOC_HOOKS
181 static void *(*old_malloc_hook)(size_t,
const void *);
182 static void *(*old_realloc_hook)(
void *, size_t,
const void *);
183 static void *(*old_memalign_hook)(size_t, size_t,
const void *);
184 static void (*old_free_hook)(
void*,
const void *);
188 static long long counter_m(0);
189 static long long counter_f(0);
190 static long long counter_fna(0);
191 static long long counter_r(0);
192 static long long counter_rs(0);
193 static long long counter_rnr(0);
194 static long long counter_rg(0);
195 static long long counter_rm(0);
196 static long long counter_rf(0);
197 static long long counter_ma(0);
203 #if HAVE_MALLOC_HOOKS
204 static void *my_malloc_hook (
size_t size,
const void* )
208 __malloc_hook = old_malloc_hook;
209 __realloc_hook = old_realloc_hook;
210 __memalign_hook = old_memalign_hook;
211 __free_hook = old_free_hook;
231 if(collectStacktraces)
232 unw_backtrace (
b->allocatedFrom.data(),
b->allocatedFrom.capacity());
234 if (
i == allocset_tc.end() )
236 allocset_tc.insert( *
b );
240 std::cerr <<
"ALREADY EXISTRING " << std::hex <<
result << std::dec <<
"\n";
247 old_malloc_hook = __malloc_hook;
248 old_realloc_hook = __realloc_hook;
249 old_memalign_hook = __memalign_hook;
250 old_free_hook = __free_hook;
253 __malloc_hook = my_malloc_hook;
254 __realloc_hook = my_realloc_hook;
255 __memalign_hook = my_memalign_hook;
256 __free_hook = my_free_hook;
261 my_free_hook (
void *
ptr,
const void* )
264 __malloc_hook = old_malloc_hook;
265 __realloc_hook = old_realloc_hook;
266 __memalign_hook = old_memalign_hook;
267 __free_hook = old_free_hook;
272 uintptr_t
p=(uintptr_t)
ptr;
277 if (
i != allocset_tc.end() )
286 std::cerr <<
"free d: " <<
p <<
" " <<
n <<
" " <<
n->getDeltaPayload() <<
" - " <<
deltaLow <<
"\n";
289 p-=
n->getDeltaPayload();
291 node* nn=
n->getNext();
313 old_malloc_hook = __malloc_hook;
314 old_realloc_hook = __realloc_hook;
315 old_memalign_hook = __memalign_hook;
316 old_free_hook = __free_hook;
319 __malloc_hook = my_malloc_hook;
320 __realloc_hook = my_realloc_hook;
321 __memalign_hook = my_memalign_hook;
322 __free_hook = my_free_hook;
327 my_realloc_hook(
void *
ptr,
size_t size,
const void * )
331 __malloc_hook = old_malloc_hook;
332 __realloc_hook = old_realloc_hook;
333 __memalign_hook = old_memalign_hook;
334 __free_hook = old_free_hook;
338 size_t sizeNoRedZones(
size);
342 bool we_allocated(
false);
345 uintptr_t
p=(uintptr_t)
ptr;
349 if (
i != allocset_tc.end() )
354 n=
new ((
void*)
p)
node(
false);
355 oldsize=
n->getSize();
368 if (
size == 0 && we_allocated )
386 if(
size>0 && ! finished )
389 if ( ! we_allocated )
410 n->setSize(sizeNoRedZones);
414 if ( oldsize < sizeNoRedZones )
417 memset((
void*)(
result+
deltaLow+oldsize),0xaa,sizeNoRedZones-oldsize);
436 if ( oldsize < sizeNoRedZones )
438 memset((
void*)(
result+
deltaLow+oldsize),0xaa,sizeNoRedZones-oldsize);
470 if(collectStacktraces)
471 unw_backtrace (
b->allocatedFrom.data(),
b->allocatedFrom.capacity());
472 allocset_tc.insert( *
b );
475 n->setSize(sizeNoRedZones);
486 if(
size > 0 && finished && we_allocated )
505 old_malloc_hook = __malloc_hook;
506 old_realloc_hook = __realloc_hook;
507 old_memalign_hook = __memalign_hook;
508 old_free_hook = __free_hook;
511 __malloc_hook = my_malloc_hook;
512 __realloc_hook = my_realloc_hook;
513 __memalign_hook = my_memalign_hook;
514 __free_hook = my_free_hook;
684 #if HAVE_MALLOC_HOOKS
686 my_memalign_hook (
size_t alignment,
size_t size,
const void * )
690 __malloc_hook = old_malloc_hook;
691 __realloc_hook = old_realloc_hook;
692 __memalign_hook = old_memalign_hook;
693 __free_hook = old_free_hook;
716 if(collectStacktraces)
717 unw_backtrace (
b->allocatedFrom.data(),
b->allocatedFrom.capacity());
719 if (
i == allocset_tc.end() )
721 allocset_tc.insert( *
b );
724 #if HAVE_MALLOC_HOOKS
726 old_malloc_hook = __malloc_hook;
727 old_realloc_hook = __realloc_hook;
728 old_memalign_hook = __memalign_hook;
729 old_free_hook = __free_hook;
732 __malloc_hook = my_malloc_hook;
733 __realloc_hook = my_realloc_hook;
734 __memalign_hook = my_memalign_hook;
735 __free_hook = my_free_hook;
751 std::cerr <<
"STOP " << s_level <<
"\n";
753 __malloc_hook = old_malloc_hook;
754 __realloc_hook = old_realloc_hook;
755 __memalign_hook = old_memalign_hook;
756 __free_hook = old_free_hook;
758 old_malloc_hook = __malloc_hook;
759 old_realloc_hook = __realloc_hook;
760 old_memalign_hook = __memalign_hook;
761 old_free_hook = __free_hook;
769 __malloc_hook = my_malloc_hook;
770 __realloc_hook = my_realloc_hook;
771 __memalign_hook = my_memalign_hook;
772 __free_hook = my_free_hook;
775 std::cerr <<
"START " << s_level <<
"\n";
779 void printAllocation(
void*
ptr,
size_t n )
781 bool hasRedzone=
false;
782 unsigned char *cptr=(
unsigned char*)
ptr;
784 unsigned char buf[16];
786 std::cerr <<
"Printing information about allocation at: " <<
ptr <<
" of size " <<
n <<
"\n";
788 std::cerr <<
"Payload:\n";
789 std::cerr << std::hex << std::setfill(
'0');
790 for(
unsigned int l(0);
l<
n;
l+=16 )
792 for( nread = 0; nread < 16; nread++ )
793 buf[nread]= *(cptr+
l+nread);
796 for(
int i = 0;
i < 16;
i++ )
798 if(
i % 8 == 0 ) std::cerr <<
' ';
799 if(
i < nread &&
l+
i<
n )
800 std::cerr <<
' ' << std::setw(2) << (
unsigned)buf[
i];
807 for(
int i = 0;
i < nread;
i++)
809 if( buf[
i] < 32 || buf[
i] > 128 ) std::cerr <<
'.';
810 else std::cerr <<
buf[
i];
818 std::cerr <<
"Redzone after" <<
"\n";
819 std::cerr << std::hex << std::setfill(
'0');
820 for(
int l(0);
l<32;
l+=16 )
822 for( nread = 0; nread < 16; nread++ )
823 buf[nread]= *(cptr+
n+
l+nread);
826 for(
int i = 0;
i < 16;
i++ )
828 if(
i % 8 == 0 ) std::cerr <<
' ';
830 std::cerr <<
' ' << std::setw(2) << (
unsigned int)buf[
i];
837 for(
int i = 0;
i < nread;
i++)
839 if( buf[
i] < 32 || buf[
i] > 128 ) std::cerr <<
'.';
840 else std::cerr <<
buf[
i];
846 std::cerr << std::dec <<
"\n";
849 static void my_test()
855 printAllocation(
p0,20);
864 printAllocation(
p1,32);
867 void *
p2=realloc(
p1, 48);
869 std::cerr <<
"realloc : " <<
p2 <<
" " <<
p1 <<
"\n";
870 printAllocation(
p2,48);
875 std::cerr <<
"realloc : " <<
p2 <<
" " <<
p1 <<
"\n";
876 printAllocation(
p1,32);
881 std::cerr <<
"realloc : " <<
p2 <<
" " <<
p0 <<
"\n";
882 printAllocation(
p2,48);
887 std::cerr <<
"free :\n";
888 printAllocation(
pd,32);
898 mallopt(M_MMAP_MAX, 0);
900 mallopt(M_MXFAST, 0);
904 std::cerr <<
"INIT : redzone before / after " <<
deltaLow <<
" / " <<
deltaHigh <<
" bytes\n";
906 std::cerr <<
"INIT : sizes " <<
sizeof(
node) <<
" / " <<
sizeof(
redzone) <<
"\n";
908 std::cerr <<
"INIT : s_first / s_last " << &s_first <<
" / " << &s_last <<
"\n";
916 #if HAVE_MALLOC_HOOKS
918 old_malloc_hook = __malloc_hook;
919 old_realloc_hook = __realloc_hook;
920 old_memalign_hook = __memalign_hook;
921 old_free_hook = __free_hook;
923 __malloc_hook = my_malloc_hook;
924 __realloc_hook = my_realloc_hook;
925 __memalign_hook = my_memalign_hook;
926 __free_hook = my_free_hook;
937 # pragma GCC diagnostic pop