src/share/vm/runtime/os.cpp

Print this page




 504     // Found an entry point like Agent_OnLoad_lib_name so we have a static agent
 505     agent_lib->set_valid();
 506     agent_lib->set_static_lib(true);
 507     return true;
 508   }
 509   agent_lib->set_os_lib(save_handle);
 510   return false;
 511 }
 512 
 513 // --------------------- heap allocation utilities ---------------------
 514 
 515 char *os::strdup(const char *str, MEMFLAGS flags) {
 516   size_t size = strlen(str);
 517   char *dup_str = (char *)malloc(size + 1, flags);
 518   if (dup_str == NULL) return NULL;
 519   strcpy(dup_str, str);
 520   return dup_str;
 521 }
 522 
 523 
 524 
 525 #ifdef ASSERT
 526 #define space_before             (MallocCushion + sizeof(double))
 527 #define space_after              MallocCushion
 528 #define size_addr_from_base(p)   (size_t*)(p + space_before - sizeof(size_t))
 529 #define size_addr_from_obj(p)    ((size_t*)p - 1)
 530 // MallocCushion: size of extra cushion allocated around objects with +UseMallocOnly
 531 // NB: cannot be debug variable, because these aren't set from the command line until
 532 // *after* the first few allocs already happened
 533 #define MallocCushion            16
 534 #else
 535 #define space_before             0
 536 #define space_after              0
 537 #define size_addr_from_base(p)   should not use w/o ASSERT
 538 #define size_addr_from_obj(p)    should not use w/o ASSERT
 539 #define MallocCushion            0
 540 #endif
 541 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 542 
 543 #ifdef ASSERT
 544 inline size_t get_size(void* obj) {
 545   size_t size = *size_addr_from_obj(obj);
 546   if (size < 0) {
 547     fatal(err_msg("free: size field of object #" PTR_FORMAT " was overwritten ("
 548                   SIZE_FORMAT ")", obj, size));
 549   }
 550   return size;
 551 }
 552 
 553 u_char* find_cushion_backwards(u_char* start) {
 554   u_char* p = start;
 555   while (p[ 0] != badResourceValue || p[-1] != badResourceValue ||
 556          p[-2] != badResourceValue || p[-3] != badResourceValue) p--;
 557   // ok, we have four consecutive marker bytes; find start
 558   u_char* q = p - 4;
 559   while (*q == badResourceValue) q--;
 560   return q + 1;
 561 }
 562 
 563 u_char* find_cushion_forwards(u_char* start) {
 564   u_char* p = start;
 565   while (p[0] != badResourceValue || p[1] != badResourceValue ||
 566          p[2] != badResourceValue || p[3] != badResourceValue) p++;
 567   // ok, we have four consecutive marker bytes; find end of cushion
 568   u_char* q = p + 4;
 569   while (*q == badResourceValue) q++;
 570   return q - MallocCushion;
 571 }
 572 
 573 void print_neighbor_blocks(void* ptr) {
 574   // find block allocated before ptr (not entirely crash-proof)
 575   if (MallocCushion < 4) {
 576     tty->print_cr("### cannot find previous block (MallocCushion < 4)");
 577     return;
 578   }
 579   u_char* start_of_this_block = (u_char*)ptr - space_before;
 580   u_char* end_of_prev_block_data = start_of_this_block - space_after -1;
 581   // look for cushion in front of prev. block
 582   u_char* start_of_prev_block = find_cushion_backwards(end_of_prev_block_data);
 583   ptrdiff_t size = *size_addr_from_base(start_of_prev_block);
 584   u_char* obj = start_of_prev_block + space_before;
 585   if (size <= 0 ) {
 586     // start is bad; may have been confused by OS data in between objects
 587     // search one more backwards
 588     start_of_prev_block = find_cushion_backwards(start_of_prev_block);
 589     size = *size_addr_from_base(start_of_prev_block);
 590     obj = start_of_prev_block + space_before;
 591   }
 592 
 593   if (start_of_prev_block + space_before + size + space_after == start_of_this_block) {
 594     tty->print_cr("### previous object: " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", obj, size);
 595   } else {
 596     tty->print_cr("### previous object (not sure if correct): " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", obj, size);
 597   }
 598 
 599   // now find successor block
 600   u_char* start_of_next_block = (u_char*)ptr + *size_addr_from_obj(ptr) + space_after;
 601   start_of_next_block = find_cushion_forwards(start_of_next_block);
 602   u_char* next_obj = start_of_next_block + space_before;
 603   ptrdiff_t next_size = *size_addr_from_base(start_of_next_block);
 604   if (start_of_next_block[0] == badResourceValue &&
 605       start_of_next_block[1] == badResourceValue &&
 606       start_of_next_block[2] == badResourceValue &&
 607       start_of_next_block[3] == badResourceValue) {
 608     tty->print_cr("### next object: " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", next_obj, next_size);
 609   } else {
 610     tty->print_cr("### next object (not sure if correct): " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", next_obj, next_size);
 611   }
 612 }
 613 
 614 
 615 void report_heap_error(void* memblock, void* bad, const char* where) {
 616   tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 617   tty->print_cr("## memory stomp: byte at " PTR_FORMAT " %s object " PTR_FORMAT, bad, where, memblock);
 618   print_neighbor_blocks(memblock);
 619   fatal("memory stomping error");
 620 }
 621 
 622 void verify_block(void* memblock) {
 623   size_t size = get_size(memblock);
 624   if (MallocCushion) {
 625     u_char* ptr = (u_char*)memblock - space_before;
 626     for (int i = 0; i < MallocCushion; i++) {
 627       if (ptr[i] != badResourceValue) {
 628         report_heap_error(memblock, ptr+i, "in front of");
 629       }
 630     }
 631     u_char* end = (u_char*)memblock + size + space_after;
 632     for (int j = -MallocCushion; j < 0; j++) {
 633       if (end[j] != badResourceValue) {
 634         report_heap_error(memblock, end+j, "after");
 635       }
 636     }
 637   }
 638 }

 639 #endif
 640 
 641 //
 642 // This function supports testing of the malloc out of memory
 643 // condition without really running the system out of memory.
 644 //
 645 static u_char* testMalloc(size_t alloc_size) {
 646   assert(MallocMaxTestWords > 0, "sanity check");
 647 
 648   if ((cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) {
 649     return NULL;
 650   }
 651 
 652   u_char* ptr = (u_char*)::malloc(alloc_size);
 653 
 654   if (ptr != NULL) {
 655     Atomic::add(((jint) (alloc_size / BytesPerWord)),
 656                 (volatile jint *) &cur_malloc_words);
 657   }
 658   return ptr;


 667   // since os::malloc can be called when the libjvm.{dll,so} is
 668   // first loaded and we don't have a thread yet.
 669   // try to find the thread after we see that the watcher thread
 670   // exists and has crash protection.
 671   WatcherThread *wt = WatcherThread::watcher_thread();
 672   if (wt != NULL && wt->has_crash_protection()) {
 673     Thread* thread = ThreadLocalStorage::get_thread_slow();
 674     if (thread == wt) {
 675       assert(!wt->has_crash_protection(),
 676           "Can't malloc with crash protection from WatcherThread");
 677     }
 678   }
 679 #endif
 680 
 681   if (size == 0) {
 682     // return a valid pointer if size is zero
 683     // if NULL is returned the calling functions assume out of memory.
 684     size = 1;
 685   }
 686 
 687   const size_t alloc_size = size + space_before + space_after;
 688 


 689   if (size > alloc_size) { // Check for rollover.
 690     return NULL;
 691   }

 692 
 693   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 694 
 695   u_char* ptr;
 696 
 697   if (MallocMaxTestWords > 0) {
 698     ptr = testMalloc(alloc_size);
 699   } else {
 700     ptr = (u_char*)::malloc(alloc_size);
 701   }
 702 
 703 #ifdef ASSERT
 704   if (ptr == NULL) return NULL;
 705   if (MallocCushion) {
 706     for (u_char* p = ptr; p < ptr + MallocCushion; p++) *p = (u_char)badResourceValue;
 707     u_char* end = ptr + space_before + size;
 708     for (u_char* pq = ptr+MallocCushion; pq < end; pq++) *pq = (u_char)uninitBlockPad;
 709     for (u_char* q = end; q < end + MallocCushion; q++) *q = (u_char)badResourceValue;
 710   }
 711   // put size just before data
 712   *size_addr_from_base(ptr) = size;

 713 #endif
 714   u_char* memblock = ptr + space_before;
 715   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 716     tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, memblock);
 717     breakpoint();
 718   }
 719   debug_only(if (paranoid) verify_block(memblock));
 720   if (PrintMalloc && tty != NULL) tty->print_cr("os::malloc " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, memblock);


 721 
 722   // we do not track MallocCushion memory
 723     MemTracker::record_malloc((address)memblock, size, memflags, caller == 0 ? CALLER_PC : caller);
 724 
 725   return memblock;
 726 }
 727 
 728 
 729 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, address caller) {
 730 #ifndef ASSERT
 731   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 732   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 733   MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
 734   void* ptr = ::realloc(memblock, size);
 735   if (ptr != NULL) {
 736     tkr.record((address)memblock, (address)ptr, size, memflags,
 737      caller == 0 ? CALLER_PC : caller);
 738   } else {
 739     tkr.discard();
 740   }
 741   return ptr;
 742 #else
 743   if (memblock == NULL) {
 744     return malloc(size, memflags, (caller == 0 ? CALLER_PC : caller));
 745   }
 746   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 747     tty->print_cr("os::realloc caught " PTR_FORMAT, memblock);
 748     breakpoint();
 749   }
 750   verify_block(memblock);
 751   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 752   if (size == 0) return NULL;


 753   // always move the block
 754   void* ptr = malloc(size, memflags, caller == 0 ? CALLER_PC : caller);
 755   if (PrintMalloc) tty->print_cr("os::remalloc " SIZE_FORMAT " bytes, " PTR_FORMAT " --> " PTR_FORMAT, size, memblock, ptr);


 756   // Copy to new memory if malloc didn't fail
 757   if ( ptr != NULL ) {
 758     memcpy(ptr, memblock, MIN2(size, get_size(memblock)));
 759     if (paranoid) verify_block(ptr);

 760     if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 761       tty->print_cr("os::realloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 762       breakpoint();
 763     }
 764     free(memblock);
 765   }
 766   return ptr;
 767 #endif
 768 }
 769 
 770 
 771 void  os::free(void *memblock, MEMFLAGS memflags) {

 772   NOT_PRODUCT(inc_stat_counter(&num_frees, 1));
 773 #ifdef ASSERT
 774   if (memblock == NULL) return;
 775   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 776     if (tty != NULL) tty->print_cr("os::free caught " PTR_FORMAT, memblock);
 777     breakpoint();
 778   }
 779   verify_block(memblock);
 780   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 781   // Added by detlefs.
 782   if (MallocCushion) {
 783     u_char* ptr = (u_char*)memblock - space_before;
 784     for (u_char* p = ptr; p < ptr + MallocCushion; p++) {
 785       guarantee(*p == badResourceValue,
 786                 "Thing freed should be malloc result.");
 787       *p = (u_char)freeBlockPad;
 788     }
 789     size_t size = get_size(memblock);
 790     inc_stat_counter(&free_bytes, size);
 791     u_char* end = ptr + space_before + size;
 792     for (u_char* q = end; q < end + MallocCushion; q++) {
 793       guarantee(*q == badResourceValue,
 794                 "Thing freed should be malloc result.");
 795       *q = (u_char)freeBlockPad;
 796     }
 797     if (PrintMalloc && tty != NULL)
 798       fprintf(stderr, "os::free " SIZE_FORMAT " bytes --> " PTR_FORMAT "\n", size, (uintptr_t)memblock);
 799   } else if (PrintMalloc && tty != NULL) {
 800     // tty->print_cr("os::free %p", memblock);
 801     fprintf(stderr, "os::free " PTR_FORMAT "\n", (uintptr_t)memblock);
 802   }
 803 #endif
 804   MemTracker::record_free((address)memblock, memflags);
 805 
 806   ::free((char*)memblock - space_before);
 807 }
 808 
 809 void os::init_random(long initval) {
 810   _rand_seed = initval;
 811 }
 812 
 813 
 814 long os::random() {
 815   /* standard, well-known linear congruential random generator with
 816    * next_rand = (16807*seed) mod (2**31-1)
 817    * see
 818    * (1) "Random Number Generators: Good Ones Are Hard to Find",
 819    *      S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
 820    * (2) "Two Fast Implementations of the 'Minimal Standard' Random
 821    *     Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
 822   */
 823   const long a = 16807;
 824   const unsigned long m = 2147483647;
 825   const long q = m / a;        assert(q == 127773, "weird math");
 826   const long r = m % a;        assert(r == 2836, "weird math");




 504     // Found an entry point like Agent_OnLoad_lib_name so we have a static agent
 505     agent_lib->set_valid();
 506     agent_lib->set_static_lib(true);
 507     return true;
 508   }
 509   agent_lib->set_os_lib(save_handle);
 510   return false;
 511 }
 512 
 513 // --------------------- heap allocation utilities ---------------------
 514 
 515 char *os::strdup(const char *str, MEMFLAGS flags) {
 516   size_t size = strlen(str);
 517   char *dup_str = (char *)malloc(size + 1, flags);
 518   if (dup_str == NULL) return NULL;
 519   strcpy(dup_str, str);
 520   return dup_str;
 521 }
 522 
 523 

















 524 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 525 
 526 #ifdef ASSERT
 527 #include "memory/fencedMemory.hpp"





















































 528 
 529 static void verify_memory(void* ptr) {
 530   FencedMemory fenced(ptr);
 531   if (!fenced.verify_fences()) {














 532     tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 533     tty->print_cr("## memory stomp:");
 534     fenced.print_on(tty);
 535     fatal("memory stomping error");

















 536   }
 537 }
 538 
 539 #endif
 540 
 541 //
 542 // This function supports testing of the malloc out of memory
 543 // condition without really running the system out of memory.
 544 //
 545 static u_char* testMalloc(size_t alloc_size) {
 546   assert(MallocMaxTestWords > 0, "sanity check");
 547 
 548   if ((cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) {
 549     return NULL;
 550   }
 551 
 552   u_char* ptr = (u_char*)::malloc(alloc_size);
 553 
 554   if (ptr != NULL) {
 555     Atomic::add(((jint) (alloc_size / BytesPerWord)),
 556                 (volatile jint *) &cur_malloc_words);
 557   }
 558   return ptr;


 567   // since os::malloc can be called when the libjvm.{dll,so} is
 568   // first loaded and we don't have a thread yet.
 569   // try to find the thread after we see that the watcher thread
 570   // exists and has crash protection.
 571   WatcherThread *wt = WatcherThread::watcher_thread();
 572   if (wt != NULL && wt->has_crash_protection()) {
 573     Thread* thread = ThreadLocalStorage::get_thread_slow();
 574     if (thread == wt) {
 575       assert(!wt->has_crash_protection(),
 576           "Can't malloc with crash protection from WatcherThread");
 577     }
 578   }
 579 #endif
 580 
 581   if (size == 0) {
 582     // return a valid pointer if size is zero
 583     // if NULL is returned the calling functions assume out of memory.
 584     size = 1;
 585   }
 586 
 587 #ifndef ASSERT
 588   const size_t alloc_size = size;
 589 #else
 590   const size_t alloc_size = FencedMemory::get_total_size(size);
 591   if (size > alloc_size) { // Check for rollover.
 592     return NULL;
 593   }
 594 #endif
 595 
 596   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 597 
 598   u_char* ptr;

 599   if (MallocMaxTestWords > 0) {
 600     ptr = testMalloc(alloc_size);
 601   } else {
 602     ptr = (u_char*)::malloc(alloc_size);
 603   }
 604 
 605 #ifdef ASSERT
 606   if (ptr == NULL) {
 607     return NULL;




 608   }
 609   // Wrap memory with fence
 610   FencedMemory fenced(ptr, size);
 611   ptr = fenced.get_user_ptr();
 612 #endif
 613   if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 614     tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);

 615     breakpoint();
 616   }
 617   debug_only(if (paranoid) verify_memory(ptr));
 618   if (PrintMalloc && tty != NULL) {
 619     tty->print_cr("os::malloc " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 620   }
 621 
 622   // we do not track fence memory
 623   MemTracker::record_malloc((address)ptr, size, memflags, caller == 0 ? CALLER_PC : caller);
 624 
 625   return ptr;
 626 }
 627 
 628 
 629 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, address caller) {
 630 #ifndef ASSERT
 631   NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
 632   NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
 633   MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
 634   void* ptr = ::realloc(memblock, size);
 635   if (ptr != NULL) {
 636     tkr.record((address)memblock, (address)ptr, size, memflags,
 637      caller == 0 ? CALLER_PC : caller);
 638   } else {
 639     tkr.discard();
 640   }
 641   return ptr;
 642 #else
 643   if (memblock == NULL) {
 644     return os::malloc(size, memflags, (caller == 0 ? CALLER_PC : caller));
 645   }
 646   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 647     tty->print_cr("os::realloc caught " PTR_FORMAT, memblock);
 648     breakpoint();
 649   }
 650   verify_memory(memblock);
 651   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 652   if (size == 0) {
 653     return NULL;
 654   }
 655   // always move the block
 656   void* ptr = os::malloc(size, memflags, caller == 0 ? CALLER_PC : caller);
 657   if (PrintMalloc) {
 658     tty->print_cr("os::remalloc " SIZE_FORMAT " bytes, " PTR_FORMAT " --> " PTR_FORMAT, size, memblock, ptr);
 659   }
 660   // Copy to new memory if malloc didn't fail
 661   if ( ptr != NULL ) {
 662     FencedMemory fenced(memblock);
 663     memcpy(ptr, memblock, MIN2(size, fenced.get_user_size()));
 664     if (paranoid) verify_memory(ptr);
 665     if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 666       tty->print_cr("os::realloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
 667       breakpoint();
 668     }
 669     os::free(memblock);
 670   }
 671   return ptr;
 672 #endif
 673 }
 674 
 675 
 676 void  os::free(void *memblock, MEMFLAGS memflags) {
 677   address trackp = (address) memblock;
 678   NOT_PRODUCT(inc_stat_counter(&num_frees, 1));
 679 #ifdef ASSERT
 680   if (memblock == NULL) return;
 681   if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
 682     if (tty != NULL) tty->print_cr("os::free caught " PTR_FORMAT, memblock);
 683     breakpoint();
 684   }
 685   verify_memory(memblock);
 686   NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
 687 
 688   FencedMemory fenced(memblock);
 689   size_t size = fenced.get_user_size();






 690   inc_stat_counter(&free_bytes, size);
 691   memblock = fenced.release_for_freeing();
 692   if (PrintMalloc && tty != NULL) {





 693       fprintf(stderr, "os::free " SIZE_FORMAT " bytes --> " PTR_FORMAT "\n", size, (uintptr_t)memblock);



 694   }
 695 #endif
 696   MemTracker::record_free(trackp, memflags);
 697 
 698   ::free(memblock);
 699 }
 700 
 701 void os::init_random(long initval) {
 702   _rand_seed = initval;
 703 }
 704 
 705 
 706 long os::random() {
 707   /* standard, well-known linear congruential random generator with
 708    * next_rand = (16807*seed) mod (2**31-1)
 709    * see
 710    * (1) "Random Number Generators: Good Ones Are Hard to Find",
 711    *      S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
 712    * (2) "Two Fast Implementations of the 'Minimal Standard' Random
 713    *     Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
 714   */
 715   const long a = 16807;
 716   const unsigned long m = 2147483647;
 717   const long q = m / a;        assert(q == 127773, "weird math");
 718   const long r = m % a;        assert(r == 2836, "weird math");