38
38
#include " logging/log.hpp"
39
39
#include " logging/logStream.hpp"
40
40
#include " memory/allocation.inline.hpp"
41
- #include " memory/guardedMemory.hpp"
42
41
#include " memory/resourceArea.hpp"
43
42
#include " memory/universe.hpp"
44
43
#include " oops/compressedOops.inline.hpp"
@@ -83,13 +82,6 @@ int os::_processor_count = 0;
83
82
int os::_initial_active_processor_count = 0 ;
84
83
os::PageSizes os::_page_sizes;
85
84
86
- #ifndef PRODUCT
87
- julong os::num_mallocs = 0 ; // # of calls to malloc/realloc
88
- julong os::alloc_bytes = 0 ; // # of bytes allocated
89
- julong os::num_frees = 0 ; // # of calls to free
90
- julong os::free_bytes = 0 ; // # of bytes freed
91
- #endif
92
-
93
85
static size_t cur_malloc_words = 0 ; // current size for MallocMaxTestWords
94
86
95
87
DEBUG_ONLY (bool os::_mutex_init_done = false ;)
@@ -636,30 +628,11 @@ char* os::strdup_check_oom(const char* str, MEMFLAGS flags) {
636
628
return p;
637
629
}
638
630
639
-
640
- #define paranoid 0 /* only set to 1 if you suspect checking code has bug */
641
-
642
- #ifdef ASSERT
643
-
644
- static void verify_memory (void * ptr) {
645
- GuardedMemory guarded (ptr);
646
- if (!guarded.verify_guards ()) {
647
- LogTarget (Warning, malloc, free) lt;
648
- ResourceMark rm;
649
- LogStream ls (lt);
650
- ls.print_cr (" ## nof_mallocs = " UINT64_FORMAT " , nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
651
- ls.print_cr (" ## memory stomp:" );
652
- guarded.print_on (&ls);
653
- fatal (" memory stomping error" );
654
- }
655
- }
656
-
657
- #endif
658
-
659
631
//
660
632
// This function supports testing of the malloc out of memory
661
633
// condition without really running the system out of memory.
662
634
//
635
+
663
636
static bool has_reached_max_malloc_test_peak (size_t alloc_size) {
664
637
if (MallocMaxTestWords > 0 ) {
665
638
size_t words = (alloc_size / BytesPerWord);
@@ -672,13 +645,24 @@ static bool has_reached_max_malloc_test_peak(size_t alloc_size) {
672
645
return false ;
673
646
}
674
647
648
+ #ifdef ASSERT
649
+ static void check_crash_protection () {
650
+ assert (!os::ThreadCrashProtection::is_crash_protected (Thread::current_or_null ()),
651
+ " not allowed when crash protection is set" );
652
+ }
653
+ static void break_if_ptr_caught (void * ptr) {
654
+ if (p2i (ptr) == (intptr_t )MallocCatchPtr) {
655
+ log_warning (malloc, free)(" ptr caught: " PTR_FORMAT, p2i (ptr));
656
+ breakpoint ();
657
+ }
658
+ }
659
+ #endif // ASSERT
660
+
675
661
void * os::malloc (size_t size, MEMFLAGS flags) {
676
662
return os::malloc (size, flags, CALLER_PC);
677
663
}
678
664
679
665
void * os::malloc (size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
680
- NOT_PRODUCT (inc_stat_counter (&num_mallocs, 1 ));
681
- NOT_PRODUCT (inc_stat_counter (&alloc_bytes, size));
682
666
683
667
#if INCLUDE_NMT
684
668
{
@@ -689,63 +673,40 @@ void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
689
673
}
690
674
#endif
691
675
692
- // Since os::malloc can be called when the libjvm.{dll,so} is
693
- // first loaded and we don't have a thread yet we must accept NULL also here.
694
- assert (!os::ThreadCrashProtection::is_crash_protected (Thread::current_or_null ()),
695
- " malloc() not allowed when crash protection is set" );
676
+ DEBUG_ONLY (check_crash_protection ());
696
677
697
- if (size == 0 ) {
698
- // return a valid pointer if size is zero
699
- // if NULL is returned the calling functions assume out of memory.
700
- size = 1 ;
678
+ // On malloc(0), implementators of malloc(3) have the choice to return either
679
+ // NULL or a unique non-NULL pointer. To unify libc behavior across our platforms
680
+ // we chose the latter.
681
+ size = MAX2 ((size_t )1 , size);
682
+
683
+ // For the test flag -XX:MallocMaxTestWords
684
+ if (has_reached_max_malloc_test_peak (size)) {
685
+ return NULL ;
701
686
}
702
687
703
- // NMT support
704
- NMT_TrackingLevel level = MemTracker::tracking_level ();
688
+ const NMT_TrackingLevel level = MemTracker::tracking_level ();
705
689
const size_t nmt_overhead =
706
690
MemTracker::malloc_header_size (level) + MemTracker::malloc_footer_size (level);
707
691
708
- // Check for overflow.
709
- if (size + nmt_overhead < size) {
710
- return NULL ;
711
- }
692
+ const size_t outer_size = size + nmt_overhead;
712
693
713
- #ifndef ASSERT
714
- const size_t alloc_size = size + nmt_overhead;
715
- #else
716
- const size_t alloc_size = GuardedMemory::get_total_size (size + nmt_overhead);
717
- if (size + nmt_overhead > alloc_size) { // Check for rollover.
694
+ // Check for overflow.
695
+ if (outer_size < size) {
718
696
return NULL ;
719
697
}
720
- #endif
721
698
722
- // For the test flag -XX:MallocMaxTestWords
723
- if (has_reached_max_malloc_test_peak (size) ) {
699
+ void * const outer_ptr = ( u_char *):: malloc (outer_size);
700
+ if (outer_ptr == NULL ) {
724
701
return NULL ;
725
702
}
726
703
727
- u_char * ptr;
728
- ptr = (u_char *)::malloc (alloc_size);
729
-
730
- #ifdef ASSERT
731
- if (ptr == NULL ) {
732
- return NULL ;
733
- }
734
- // Wrap memory with guard
735
- GuardedMemory guarded (ptr, size + nmt_overhead);
736
- ptr = guarded.get_user_ptr ();
704
+ void * inner_ptr = MemTracker::record_malloc ((address)outer_ptr, size, memflags, stack, level);
737
705
738
- if ((intptr_t )ptr == (intptr_t )MallocCatchPtr) {
739
- log_warning (malloc, free)(" os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, p2i (ptr));
740
- breakpoint ();
741
- }
742
- if (paranoid) {
743
- verify_memory (ptr);
744
- }
745
- #endif
706
+ DEBUG_ONLY (::memset (inner_ptr, uninitBlockPad, size);)
707
+ DEBUG_ONLY (break_if_ptr_caught (inner_ptr);)
746
708
747
- // we do not track guard memory
748
- return MemTracker::record_malloc ((address)ptr, size, memflags, stack, level);
709
+ return inner_ptr;
749
710
}
750
711
751
712
void * os::realloc (void *memblock, size_t size, MEMFLAGS flags) {
@@ -763,59 +724,41 @@ void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCa
763
724
}
764
725
#endif
765
726
727
+ if (memblock == NULL ) {
728
+ return os::malloc (size, memflags, stack);
729
+ }
730
+
731
+ DEBUG_ONLY (check_crash_protection ());
732
+
733
+ // On realloc(p, 0), implementators of realloc(3) have the choice to return either
734
+ // NULL or a unique non-NULL pointer. To unify libc behavior across our platforms
735
+ // we chose the latter.
736
+ size = MAX2 ((size_t )1 , size);
737
+
766
738
// For the test flag -XX:MallocMaxTestWords
767
739
if (has_reached_max_malloc_test_peak (size)) {
768
740
return NULL ;
769
741
}
770
742
771
- if (size == 0 ) {
772
- // return a valid pointer if size is zero
773
- // if NULL is returned the calling functions assume out of memory.
774
- size = 1 ;
775
- }
776
-
777
- #ifndef ASSERT
778
- NOT_PRODUCT (inc_stat_counter (&num_mallocs, 1 ));
779
- NOT_PRODUCT (inc_stat_counter (&alloc_bytes, size));
780
- // NMT support
781
- NMT_TrackingLevel level = MemTracker::tracking_level ();
782
- void * membase = MemTracker::record_free (memblock, level);
743
+ const NMT_TrackingLevel level = MemTracker::tracking_level ();
783
744
const size_t nmt_overhead =
784
745
MemTracker::malloc_header_size (level) + MemTracker::malloc_footer_size (level);
785
- void * ptr = ::realloc (membase, size + nmt_overhead);
786
- return MemTracker::record_malloc (ptr, size, memflags, stack, level);
787
- #else
788
- if (memblock == NULL ) {
789
- return os::malloc (size, memflags, stack);
790
- }
791
- if ((intptr_t )memblock == (intptr_t )MallocCatchPtr) {
792
- log_warning (malloc, free)(" os::realloc caught " PTR_FORMAT, p2i (memblock));
793
- breakpoint ();
794
- }
795
- // NMT support
796
- void * membase = MemTracker::malloc_base (memblock);
797
- verify_memory (membase);
798
- // always move the block
799
- void * ptr = os::malloc (size, memflags, stack);
800
- // Copy to new memory if malloc didn't fail
801
- if (ptr != NULL ) {
802
- GuardedMemory guarded (MemTracker::malloc_base (memblock));
803
- // Guard's user data contains NMT header
804
- NMT_TrackingLevel level = MemTracker::tracking_level ();
805
- const size_t nmt_overhead =
806
- MemTracker::malloc_header_size (level) + MemTracker::malloc_footer_size (level);
807
- size_t memblock_size = guarded.get_user_size () - nmt_overhead;
808
- memcpy (ptr, memblock, MIN2 (size, memblock_size));
809
- if (paranoid) {
810
- verify_memory (MemTracker::malloc_base (ptr));
811
- }
812
- os::free (memblock);
813
- }
814
- return ptr;
815
- #endif
746
+
747
+ const size_t new_outer_size = size + nmt_overhead;
748
+
749
+ // If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
750
+ void * const old_outer_ptr = MemTracker::record_free (memblock, level);
751
+
752
+ void * const new_outer_ptr = ::realloc (old_outer_ptr, new_outer_size);
753
+
754
+ // If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
755
+ void * const new_inner_ptr = MemTracker::record_malloc (new_outer_ptr, size, memflags, stack, level);
756
+
757
+ DEBUG_ONLY (break_if_ptr_caught (new_inner_ptr);)
758
+
759
+ return new_inner_ptr;
816
760
}
817
761
818
- // handles NULL pointers
819
762
void os::free (void *memblock) {
820
763
821
764
#if INCLUDE_NMT
@@ -824,25 +767,17 @@ void os::free(void *memblock) {
824
767
}
825
768
#endif
826
769
827
- NOT_PRODUCT (inc_stat_counter (&num_frees, 1 ));
828
- #ifdef ASSERT
829
- if (memblock == NULL ) return ;
830
- if ((intptr_t )memblock == (intptr_t )MallocCatchPtr) {
831
- log_warning (malloc, free)(" os::free caught " PTR_FORMAT, p2i (memblock));
832
- breakpoint ();
770
+ if (memblock == NULL ) {
771
+ return ;
833
772
}
834
- void * membase = MemTracker::record_free (memblock, MemTracker::tracking_level ());
835
- verify_memory (membase);
836
773
837
- GuardedMemory guarded (membase);
838
- size_t size = guarded.get_user_size ();
839
- inc_stat_counter (&free_bytes, size);
840
- membase = guarded.release_for_freeing ();
841
- ::free (membase);
842
- #else
843
- void * membase = MemTracker::record_free (memblock, MemTracker::tracking_level ());
844
- ::free (membase);
845
- #endif
774
+ DEBUG_ONLY (break_if_ptr_caught (memblock);)
775
+
776
+ const NMT_TrackingLevel level = MemTracker::tracking_level ();
777
+
778
+ // If NMT is enabled, this checks for heap overwrites, then de-accounts the old block.
779
+ void * const old_outer_ptr = MemTracker::record_free (memblock, level);
780
+ ::free (old_outer_ptr);
846
781
}
847
782
848
783
void os::init_random (unsigned int initval) {
1 commit comments
openjdk-notifier[bot] commentedon Jan 10, 2023
Review
Issues