src/share/vm/runtime/os.cpp

Print this page
rev 6504 : 6311046: -Xcheck:jni should support checking of GetPrimitiveArrayCritical.
Summary: Wrapped memory with standard bounds checking "GuardedMemory".
Reviewed-by: zgu, fparain, dcubed


  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/vtableStubs.hpp"
  32 #include "gc_implementation/shared/vmGCOperations.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "memory/allocation.inline.hpp"



  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvm.h"
  37 #include "prims/jvm_misc.hpp"
  38 #include "prims/privilegedStack.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/interfaceSupport.hpp"
  42 #include "runtime/java.hpp"
  43 #include "runtime/javaCalls.hpp"
  44 #include "runtime/mutexLocker.hpp"
  45 #include "runtime/os.hpp"
  46 #include "runtime/stubRoutines.hpp"
  47 #include "runtime/thread.inline.hpp"
  48 #include "services/attachListener.hpp"
  49 #include "services/memTracker.hpp"
  50 #include "services/threadService.hpp"
  51 #include "utilities/defaultStream.hpp"
  52 #include "utilities/events.hpp"
  53 #ifdef TARGET_OS_FAMILY_linux
  54 # include "os_linux.inline.hpp"


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


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


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

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

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


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


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


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

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

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




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/javaClasses.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/vtableStubs.hpp"
  32 #include "gc_implementation/shared/vmGCOperations.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "memory/allocation.inline.hpp"
  35 #ifdef ASSERT
  36 #include "memory/guardedMemory.hpp"
  37 #endif
  38 #include "oops/oop.inline.hpp"
  39 #include "prims/jvm.h"
  40 #include "prims/jvm_misc.hpp"
  41 #include "prims/privilegedStack.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/frame.inline.hpp"
  44 #include "runtime/interfaceSupport.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/javaCalls.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "runtime/os.hpp"
  49 #include "runtime/stubRoutines.hpp"
  50 #include "runtime/thread.inline.hpp"
  51 #include "services/attachListener.hpp"
  52 #include "services/memTracker.hpp"
  53 #include "services/threadService.hpp"
  54 #include "utilities/defaultStream.hpp"
  55 #include "utilities/events.hpp"
  56 #ifdef TARGET_OS_FAMILY_linux
  57 # include "os_linux.inline.hpp"


 510     agent_lib->set_valid();
 511     agent_lib->set_static_lib(true);
 512     return true;
 513   }
 514   agent_lib->set_os_lib(save_handle);
 515   return false;
 516 }
 517 
 518 // --------------------- heap allocation utilities ---------------------
 519 
 520 char *os::strdup(const char *str, MEMFLAGS flags) {
 521   size_t size = strlen(str);
 522   char *dup_str = (char *)malloc(size + 1, flags);
 523   if (dup_str == NULL) return NULL;
 524   strcpy(dup_str, str);
 525   return dup_str;
 526 }
 527 
 528 
 529 
















 530 #define paranoid                 0  /* only set to 1 if you suspect checking code has bug */
 531 
 532 #ifdef ASSERT
 533 static void verify_memory(void* ptr) {
 534   GuardedMemory guarded(ptr);
 535   if (!guarded.verify_guards()) {





































































 536     tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
 537     tty->print_cr("## memory stomp:");
 538     guarded.print_on(tty);
 539     fatal("memory stomping error");

















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


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

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




 611   }
 612   // Wrap memory with guard
 613   GuardedMemory guarded(ptr, size);
 614   ptr = guarded.get_user_ptr();
 615 #endif
 616   if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
 617     tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);

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






 693   inc_stat_counter(&free_bytes, size);
 694   memblock = guarded.release_for_freeing();
 695   if (PrintMalloc && tty != NULL) {





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



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