Browse Source

[malloc] Report caller address as soon as memory corruption is detected

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
35c5379760
1 changed files with 21 additions and 1 deletions
  1. 21
    1
      src/core/malloc.c

+ 21
- 1
src/core/malloc.c View File

517
 		VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
517
 		VALGRIND_FREELIKE_BLOCK ( old_ptr, 0 );
518
 	}
518
 	}
519
 
519
 
520
+	if ( ASSERTED ) {
521
+		DBGC ( &heap, "Possible memory corruption detected from %p\n",
522
+		       __builtin_return_address ( 0 ) );
523
+	}
520
 	return new_ptr;
524
 	return new_ptr;
521
 }
525
 }
522
 
526
 
530
  * will be aligned to at least a multiple of sizeof(void*).
534
  * will be aligned to at least a multiple of sizeof(void*).
531
  */
535
  */
532
 void * malloc ( size_t size ) {
536
 void * malloc ( size_t size ) {
533
-	return realloc ( NULL, size );
537
+	void *ptr;
538
+
539
+	ptr = realloc ( NULL, size );
540
+	if ( ASSERTED ) {
541
+		DBGC ( &heap, "Possible memory corruption detected from %p\n",
542
+		       __builtin_return_address ( 0 ) );
543
+	}
544
+	return ptr;
534
 }
545
 }
535
 
546
 
536
 /**
547
 /**
544
  * If @c ptr is NULL, no action is taken.
555
  * If @c ptr is NULL, no action is taken.
545
  */
556
  */
546
 void free ( void *ptr ) {
557
 void free ( void *ptr ) {
558
+
547
 	realloc ( ptr, 0 );
559
 	realloc ( ptr, 0 );
560
+	if ( ASSERTED ) {
561
+		DBGC ( &heap, "Possible memory corruption detected from %p\n",
562
+		       __builtin_return_address ( 0 ) );
563
+	}
548
 }
564
 }
549
 
565
 
550
 /**
566
 /**
564
 	data = malloc ( size );
580
 	data = malloc ( size );
565
 	if ( data )
581
 	if ( data )
566
 		memset ( data, 0, size );
582
 		memset ( data, 0, size );
583
+	if ( ASSERTED ) {
584
+		DBGC ( &heap, "Possible memory corruption detected from %p\n",
585
+		       __builtin_return_address ( 0 ) );
586
+	}
567
 	return data;
587
 	return data;
568
 }
588
 }
569
 
589
 

Loading…
Cancel
Save