Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8058781

JVM crash when using Unsafe to read/write bytes in Java 8 (no crash Java 7)

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8u20
    • Fix Version/s: None
    • Component/s: hotspot
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      FULL OS VERSION :
      Microsoft Windows [Version 6.1.7601]

      Linux adc 3.14.17-100.fc19.x86_64 #1 SMP Thu Aug 14 17:17:26 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Crash (access violation) using Unsafe getByte/putByte to manipulate bits.
      When I use getLong/putLong for the same purpose it works correctly (even though I have allocated exactly the same amount of memory).
      Also, when using Java 1.7.0_67 to compile and run the same source code, both the getByte/putByte and the getLong/putLong approaches work correctly.

      THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: Did not try

      THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Did not try

      REGRESSION. Last worked in version 7u67

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Allocate bytes.
      Read a byte, manipulate it and re-write it several times at the same address then move on to the next address... the crash does not always happen at the same iteration.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Should read and write bytes without any crash. Crashes after a certain number of iterations.
      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      #
      # A fatal error has been detected by the Java Runtime Environment:
      #
      # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000028ea723, pid=5480, tid=4072
      #
      # JRE version: Java(TM) SE Runtime Environment (8.0_20-b26) (build 1.8.0_20-b26)
      # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.20-b23 mixed mode windows-amd64 compressed oops)
      # Problematic frame:
      # J 126% C1 DirectMemoryCrash.alternateBitsUsingBytes(I)V (175 bytes) @ 0x00000000028ea723 [0x00000000028ea100+0x623]
      #
      # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
      #
      # If you would like to submit a bug report, please visit:
      # http://bugreport.sun.com/bugreport/crash.jsp
      #

      --------------- T H R E A D ---------------

      Current thread (0x00000000022de800): JavaThread "main" [_thread_in_Java, id=4072, stack(0x00000000023c0000,0x00000000024c0000)]

      siginfo: ExceptionCode=0xc0000005, reading address 0x0000000058d5a4e8

      Registers:
      RAX=0x00000000d7e88328, RBX=0x000000000000f007, RCX=0x0000000058ce42b0, RDX=0x00000000d7e88348
      RSP=0x00000000024bf190, RBP=0x00000000024bf330, RSI=0x00000000d7e88318, RDI=0x000000000000f007
      R8 =0x00000000000186a0, R9 =0x0000000000000001, R10=0x0000000000000000, R11=0x0000000058ce24b0
      R12=0x0000000000000000, R13=0x00000000028e9cd0, R14=0x00000000024bf2e8, R15=0x00000000022de800
      RIP=0x00000000028ea723, EFLAGS=0x0000000000010206

      Top of Stack: (sp=0x00000000024bf190)
      0x00000000024bf190: 00000000d7e88328 00000000028e9cd0
      0x00000000024bf1a0: 000000000000000a 0000000066063157
      0x00000000024bf1b0: 0000000000000000 0000000000000050
      0x00000000024bf1c0: 00000000024bf210 0000000000000000
      0x00000000024bf1d0: 00000000d7e88300 ffffffd60000002a
      0x00000000024bf1e0: 00000000024bf250 00000000027ad6fb
      0x00000000024bf1f0: 0000000002795410 0000000000000000
      0x00000000024bf200: 0000f007024bf290 00000000024bf250
      0x00000000024bf210: 00000000024bf290 00000000d7e88348
      0x00000000024bf220: 00000000d7e88328 00000000000186a0
      0x00000000024bf230: 00000000d7e88318 0000000058ce42b0
      0x00000000024bf240: 00000000d629fa00 00000000d7e88318
      0x00000000024bf250: 0000000000000001 00000000028e9cd0
      0x00000000024bf260: 00000000024bf2e8 0000000057591398
      0x00000000024bf270: 0000000000000000 0000000057590f80
      0x00000000024bf280: 00000000000186a0 00000000024bf2e0

      Instructions: (pc=0x00000000028ea723)
      0x00000000028ea703: 00 00 4c 8b 58 10 48 8b cf c1 e9 03 48 63 c9 49
      0x00000000028ea713: 03 cb 48 89 8c 24 a8 00 00 00 48 3b 02 48 63 df
      0x00000000028ea723: 45 0f be 2c db 48 bb 30 03 a0 80 00 00 00 00 44
      0x00000000028ea733: 8b 73 64 4c 89 b4 24 e0 00 00 00 48 89 94 24 c8


      Register to memory mapping:

      RAX=0x00000000d7e88328 is an oop
      [J
       - klass: {type array long}
       - length: 2
      RBX=0x000000000000f007 is an unknown value
      RCX=0x0000000058ce42b0 is an unknown value
      RDX=0x00000000d7e88348 is an oop
      sun.misc.Unsafe
       - klass: 'sun/misc/Unsafe'
      RSP=0x00000000024bf190 is pointing into the stack for thread: 0x00000000022de800
      RBP=0x00000000024bf330 is pointing into the stack for thread: 0x00000000022de800
      RSI=0x00000000d7e88318 is an oop
      DirectMemoryCrash
       - klass: 'DirectMemoryCrash'
      RDI=0x000000000000f007 is an unknown value
      R8 =0x00000000000186a0 is an unknown value
      R9 =0x0000000000000001 is an unknown value
      R10=0x0000000000000000 is an unknown value
      R11=0x0000000058ce24b0 is an unknown value
      R12=0x0000000000000000 is an unknown value
      R13=0x00000000028e9cd0 is at entry_point+-1072 in (nmethod*)0x00000000028e9cd0
      R14=0x00000000024bf2e8 is pointing into the stack for thread: 0x00000000022de800
      R15=0x00000000022de800 is a thread


      Stack: [0x00000000023c0000,0x00000000024c0000], sp=0x00000000024bf190, free space=1020k
      Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
      C 0x00000000028ea723


      --------------- P R O C E S S ---------------

      Java Threads: ( => current thread )
        0x0000000058cd3000 JavaThread "Service Thread" daemon [_thread_blocked, id=5936, stack(0x000000005a820000,0x000000005a920000)]
        0x0000000058c62800 JavaThread "C1 CompilerThread3" daemon [_thread_blocked, id=5892, stack(0x0000000059b70000,0x0000000059c70000)]
        0x0000000058c4a000 JavaThread "C2 CompilerThread2" daemon [_thread_blocked, id=3708, stack(0x000000005a5d0000,0x000000005a6d0000)]
        0x0000000058c3a800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=2436, stack(0x000000005a350000,0x000000005a450000)]
        0x0000000058c30000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=5268, stack(0x000000005a1e0000,0x000000005a2e0000)]
        0x0000000058c2e000 JavaThread "Attach Listener" daemon [_thread_blocked, id=5228, stack(0x000000005a090000,0x000000005a190000)]
        0x0000000058c2d000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=5280, stack(0x0000000059da0000,0x0000000059ea0000)]
        0x000000000221e800 JavaThread "Finalizer" daemon [_thread_blocked, id=4412, stack(0x0000000059ea0000,0x0000000059fa0000)]
        0x0000000057b74800 JavaThread "Reference Handler" daemon [_thread_blocked, id=5288, stack(0x0000000012700000,0x0000000012800000)]
      =>0x00000000022de800 JavaThread "main" [_thread_in_Java, id=4072, stack(0x00000000023c0000,0x00000000024c0000)]

      Other Threads:
        0x0000000057b6f800 VMThread [stack: 0x0000000059c80000,0x0000000059d80000] [id=5720]
        0x0000000058cd4800 WatcherThread [stack: 0x000000005aa10000,0x000000005ab10000] [id=4368]

      VM state:not at safepoint (normal execution)

      VM Mutex/Monitor currently owned by a thread: None

      Heap:
       PSYoungGen total 38400K, used 10773K [0x00000000d5900000, 0x00000000d8380000, 0x0000000100000000)
        eden space 33280K, 31% used [0x00000000d5900000,0x00000000d63316e8,0x00000000d7980000)
        from space 5120K, 6% used [0x00000000d7e80000,0x00000000d7ed4010,0x00000000d8380000)
        to space 5120K, 0% used [0x00000000d7980000,0x00000000d7980000,0x00000000d7e80000)
       ParOldGen total 87552K, used 16K [0x0000000080a00000, 0x0000000085f80000, 0x00000000d5900000)
        object space 87552K, 0% used [0x0000000080a00000,0x0000000080a04000,0x0000000085f80000)
       Metaspace used 2498K, capacity 4494K, committed 4864K, reserved 1056768K
        class space used 270K, capacity 386K, committed 512K, reserved 1048576K

      Card table byte_map: [0x0000000011b50000,0x0000000011f50000] byte_map_base: 0x000000001174b000

      Marking Bits: (ParMarkBitMap*) 0x000000006662b460
       Begin Bits: [0x0000000012de0000, 0x0000000014db8000)
       End Bits: [0x0000000014db8000, 0x0000000016d90000)

      Polling page: 0x0000000000120000

      CodeCache: size=245760Kb used=1395Kb max_used=1395Kb free=244364Kb
       bounds [0x0000000002790000, 0x0000000002a00000, 0x0000000011790000]
       total_blobs=351 nmethods=127 adapters=139
       compilation: enabled

      Compilation events (10 events):
      Event: 0.260 Thread 0x0000000058c30000 123 4 java.lang.AbstractStringBuilder::append (70 bytes)
      Event: 0.262 Thread 0x0000000058c3a800 nmethod 119 0x00000000028d7990 code [0x00000000028d7b40, 0x00000000028d8890]
      Event: 0.262 Thread 0x0000000058c4a000 nmethod 122 0x00000000028e10d0 code [0x00000000028e1240, 0x00000000028e1728]
      Event: 0.264 Thread 0x0000000058c30000 nmethod 123 0x00000000028caad0 code [0x00000000028cac40, 0x00000000028cb0b8]
      Event: 0.410 Thread 0x0000000058c3a800 124 ! 4 java.io.PrintStream::println (24 bytes)
      Event: 0.410 Thread 0x0000000058c4a000 125 ! 4 java.io.PrintStream::write (83 bytes)
      Event: 0.420 Thread 0x0000000058c4a000 nmethod 125 0x00000000028e2010 code [0x00000000028e21a0, 0x00000000028e2c88]
      Event: 0.422 Thread 0x0000000058c3a800 nmethod 124 0x00000000028e5f50 code [0x00000000028e6100, 0x00000000028e6c50]
      Event: 7.414 Thread 0x0000000058c62800 126 % 3 DirectMemoryCrash::alternateBitsUsingBytes @ 17 (175 bytes)
      Event: 7.416 Thread 0x0000000058c62800 nmethod 126% 0x00000000028e9cd0 code [0x00000000028ea100, 0x00000000028ec7e8]

      GC Heap History (4 events):
      Event: 3.296 GC heap before
      {Heap before GC invocations=1 (full 0):
       PSYoungGen total 38400K, used 33280K [0x00000000d5900000, 0x00000000d8380000, 0x0000000100000000)
        eden space 33280K, 100% used [0x00000000d5900000,0x00000000d7980000,0x00000000d7980000)
        from space 5120K, 0% used [0x00000000d7e80000,0x00000000d7e80000,0x00000000d8380000)
        to space 5120K, 0% used [0x00000000d7980000,0x00000000d7980000,0x00000000d7e80000)
       ParOldGen total 87552K, used 0K [0x0000000080a00000, 0x0000000085f80000, 0x00000000d5900000)
        object space 87552K, 0% used [0x0000000080a00000,0x0000000080a00000,0x0000000085f80000)
       Metaspace used 2490K, capacity 4490K, committed 4864K, reserved 1056768K
        class space used 269K, capacity 386K, committed 512K, reserved 1048576K
      Event: 3.319 GC heap after
      Heap after GC invocations=1 (full 0):
       PSYoungGen total 38400K, used 400K [0x00000000d5900000, 0x00000000d8380000, 0x0000000100000000)
        eden space 33280K, 0% used [0x00000000d5900000,0x00000000d5900000,0x00000000d7980000)
        from space 5120K, 7% used [0x00000000d7980000,0x00000000d79e4010,0x00000000d7e80000)
        to space 5120K, 0% used [0x00000000d7e80000,0x00000000d7e80000,0x00000000d8380000)
       ParOldGen total 87552K, used 8K [0x0000000080a00000, 0x0000000085f80000, 0x00000000d5900000)
        object space 87552K, 0% used [0x0000000080a00000,0x0000000080a02000,0x0000000085f80000)
       Metaspace used 2490K, capacity 4490K, committed 4864K, reserved 1056768K
        class space used 269K, capacity 386K, committed 512K, reserved 1048576K
      }
      Event: 6.496 GC heap before
      {Heap before GC invocations=2 (full 0):
       PSYoungGen total 38400K, used 33680K [0x00000000d5900000, 0x00000000d8380000, 0x0000000100000000)
        eden space 33280K, 100% used [0x00000000d5900000,0x00000000d7980000,0x00000000d7980000)
        from space 5120K, 7% used [0x00000000d7980000,0x00000000d79e4010,0x00000000d7e80000)
        to space 5120K, 0% used [0x00000000d7e80000,0x00000000d7e80000,0x00000000d8380000)
       ParOldGen total 87552K, used 8K [0x0000000080a00000, 0x0000000085f80000, 0x00000000d5900000)
        object space 87552K, 0% used [0x0000000080a00000,0x0000000080a02000,0x0000000085f80000)
       Metaspace used 2493K, capacity 4490K, committed 4864K, reserved 1056768K
        class space used 270K, capacity 386K, committed 512K, reserved 1048576K
      Event: 6.497 GC heap after
      Heap after GC invocations=2 (full 0):
       PSYoungGen total 38400K, used 336K [0x00000000d5900000, 0x00000000d8380000, 0x0000000100000000)
        eden space 33280K, 0% used [0x00000000d5900000,0x00000000d5900000,0x00000000d7980000)
        from space 5120K, 6% used [0x00000000d7e80000,0x00000000d7ed4010,0x00000000d8380000)
        to space 5120K, 0% used [0x00000000d7980000,0x00000000d7980000,0x00000000d7e80000)
       ParOldGen total 87552K, used 16K [0x0000000080a00000, 0x0000000085f80000, 0x00000000d5900000)
        object space 87552K, 0% used [0x0000000080a00000,0x0000000080a04000,0x0000000085f80000)
       Metaspace used 2493K, capacity 4490K, committed 4864K, reserved 1056768K
        class space used 270K, capacity 386K, committed 512K, reserved 1048576K
      }

      Deoptimization events (0 events):
      No events

      Internal exceptions (3 events):
      Event: 0.025 Thread 0x00000000022de800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x00000000d590c6b0) thrown at [D:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u20\1074\hotspot\srÖßD&ˆ\™?
      Event: 0.025 Thread 0x00000000022de800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x00000000d590c940) thrown at [D:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u20\1074\hotspot\src\share\vm\prims\j6DjÕÙ®?
      Event: 0.060 Thread 0x00000000022de800 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d595bba0) thrown at [D:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u20\1074\hotspot\src\share\vm\prims\jvm.cpp, line 1275]

      Events (10 events):
      Event: 0.087 Thread 0x00000000022de800 DEOPT PACKING pc=0x000000000289dfc0 sp=0x00000000024bf100
      Event: 0.087 Thread 0x00000000022de800 DEOPT UNPACKING pc=0x00000000027d75bf sp=0x00000000024bee00 mode 0
      Event: 3.296 Executing VM operation: ParallelGCFailedAllocation
      Event: 3.319 Executing VM operation: ParallelGCFailedAllocation done
      Event: 3.319 loading class sun/misc/Cleaner
      Event: 3.319 loading class sun/misc/Cleaner done
      Event: 4.119 Executing VM operation: EnableBiasedLocking
      Event: 4.119 Executing VM operation: EnableBiasedLocking done
      Event: 6.496 Executing VM operation: ParallelGCFailedAllocation
      Event: 6.497 Executing VM operation: ParallelGCFailedAllocation done


      Dynamic libraries:
      0x000000013f300000 - 0x000000013f334000 C:\devtools\jdks\jdk1.8.0_20\bin\javaw.exe
      0x0000000077580000 - 0x0000000077729000 C:\Windows\SYSTEM32\ntdll.dll
      0x0000000077460000 - 0x000000007757f000 C:\Windows\system32\kernel32.dll
      0x000007fefd3b0000 - 0x000007fefd41c000 C:\Windows\system32\KERNELBASE.dll
      0x000007fefe370000 - 0x000007fefe44b000 C:\Windows\system32\ADVAPI32.dll
      0x000007fefe000000 - 0x000007fefe09f000 C:\Windows\system32\msvcrt.dll
      0x000007fefde70000 - 0x000007fefde8f000 C:\Windows\SYSTEM32\sechost.dll
      0x000007fefe240000 - 0x000007fefe36d000 C:\Windows\system32\RPCRT4.dll
      0x0000000077360000 - 0x000000007745a000 C:\Windows\system32\USER32.dll
      0x000007feff380000 - 0x000007feff3e7000 C:\Windows\system32\GDI32.dll
      0x000007feff7f0000 - 0x000007feff7fe000 C:\Windows\system32\LPK.dll
      0x000007feff3f0000 - 0x000007feff4b9000 C:\Windows\system32\USP10.dll
      0x000007fefb6a0000 - 0x000007fefb894000 C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_fa396087175ac9ac\COMCTL32.dll
      0x000007feff800000 - 0x000007feff871000 C:\Windows\system32\SHLWAPI.dll
      0x000007fefe0a0000 - 0x000007fefe0ce000 C:\Windows\system32\IMM32.DLL
      0x000007fefe0d0000 - 0x000007fefe1d9000 C:\Windows\system32\MSCTF.dll
      0x0000000066e60000 - 0x0000000066f32000 C:\devtools\jdks\jdk1.8.0_20\jre\bin\msvcr100.dll
      0x0000000065e50000 - 0x00000000666a8000 C:\devtools\jdks\jdk1.8.0_20\jre\bin\server\jvm.dll
      0x000007fef8700000 - 0x000007fef8709000 C:\Windows\system32\WSOCK32.dll
      0x000007feff7a0000 - 0x000007feff7ed000 C:\Windows\system32\WS2_32.dll
      0x000007feff880000 - 0x000007feff888000 C:\Windows\system32\NSI.dll
      0x000007fef9990000 - 0x000007fef99cb000 C:\Windows\system32\WINMM.dll
      0x0000000077740000 - 0x0000000077747000 C:\Windows\system32\PSAPI.DLL
      0x0000000071620000 - 0x000000007162f000 C:\devtools\jdks\jdk1.8.0_20\jre\bin\verify.dll
      0x0000000066fa0000 - 0x0000000066fc8000 C:\devtools\jdks\jdk1.8.0_20\jre\bin\java.dll
      0x00000000715d0000 - 0x00000000715e6000 C:\devtools\jdks\jdk1.8.0_20\jre\bin\zip.dll
      0x000007fefe5f0000 - 0x000007feff378000 C:\Windows\system32\SHELL32.dll
      0x000007fefd930000 - 0x000007fefdb33000 C:\Windows\system32\ole32.dll
      0x000007fefd3a0000 - 0x000007fefd3af000 C:\Windows\system32\profapi.dll
      0x000007fee97d0000 - 0x000007fee98f5000 C:\Windows\system32\dbghelp.dll

      VM Arguments:
      jvm_args: -Dfile.encoding=UTF-8
      java_command: DirectMemoryCrash
      java_class_path (initial): C:\dev\explore\directmemory\target\test-classes;C:\dev\explore\directmemory\target\classes;C:\dev\views\vAP5.1\maven\repo\junit\junit\4.11\junit-4.11.jar;C:\dev\views\vAP5.1\maven\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar
      Launcher Type: SUN_STANDARD

      Environment Variables:
      JAVA_HOME=c:/devtools/jdks/jdk1.8.0_20
      PATH=c:\Users\g.guimezanes\bin;.;C:\devtools\Git\local\bin;C:\devtools\Git\mingw\bin;C:\devtools\Git\bin;c:\ProgramData\Oracle\Java\javapath;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;c:\Windows\system32;c:\Windows;c:\Windows\System32\Wbem;c:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\Intel\WiFi\bin\;c:\Program Files\Common Files\Intel\WirelessCommon\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\devtools\apache-maven-3.2.3\bin;C:\devtools\Git\bin
      USERNAME=g.guimezanes
      OS=Windows_NT
      PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel



      --------------- S Y S T E M ---------------

      OS: Windows 7 , 64 bit Build 7601 Service Pack 1

      CPU:total 8 (4 cores per cpu, 2 threads per core) family 6 model 42 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, aes, clmul, ht, tsc, tscinvbit

      Memory: 4k page, physical 8344628k(5542788k free), swap 16687392k(13269328k free)

      vm_info: Java HotSpot(TM) 64-Bit Server VM (25.20-b23) for windows-amd64 JRE (1.8.0_20-b26), built on Jul 30 2014 13:51:23 by "java_re" with MS VC++ 10.0 (VS2010)

      time: Thu Sep 18 10:37:54 2014
      elapsed time: 7 seconds (0d 0h 0m 7s)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.InvocationTargetException;
      import java.lang.reflect.Method;

      import sun.misc.Unsafe;

      public class DirectMemoryCrash {

      private void reserveMemory(int bytes) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException,
      NoSuchFieldException {
      Class<?> bitsClass = Class.forName("java.nio.Bits");
      Method reserveMemoryMethod = bitsClass.getDeclaredMethod("reserveMemory", long.class, int.class);
      reserveMemoryMethod.setAccessible(true);
      reserveMemoryMethod.invoke(null, (long) bytes, bytes);
      }

      private void unreserveMemory(int bytes) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException,
      NoSuchFieldException {
      Class<?> bitsClass = Class.forName("java.nio.Bits");
      Method unreserveMemoryMethod = bitsClass.getDeclaredMethod("unreserveMemory", long.class, int.class);
      unreserveMemoryMethod.setAccessible(true);
      unreserveMemoryMethod.invoke(null, (long) bytes, bytes);
      }


      private sun.misc.Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
      java.lang.reflect.Field f = sun.misc
      .Unsafe.class.getDeclaredField("theUnsafe");
      f.setAccessible(true);
      sun.misc.Unsafe unsafe = (Unsafe) f.get(null);
      return unsafe;
      }

      private long[] reserveAndAllocateMemory(int numberBits) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException,
      NoSuchMethodException, SecurityException {
      long[] result = new long[2];
      int numberBytes = ((numberBits >>> 6) << 3);
      // make sure we allocate enough to work with longs
      if (numberBits > (numberBytes << 3)) {
      numberBytes += 8;
      }
      sun.misc.Unsafe unsafe = getUnsafe();
      reserveMemory(numberBytes);
      long address = unsafe.allocateMemory(numberBytes);
      unsafe.setMemory(address, numberBytes, (byte) 0);
      result[0] = address;
      result[1] = numberBytes;
      return result;
      }

      private void alternateBitsUsingBytes(int numberBits) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
      SecurityException,
      NoSuchFieldException {

      long[] addressAndLength = reserveAndAllocateMemory(numberBits);

      sun.misc.Unsafe unsafe = getUnsafe();

      for (int i = 0; i < numberBits; i++) {
      int oddOrEven = i & 1;
      long byteAddress = addressAndLength[0] + (i >>> 3);
      byte oldValue = unsafe.getByte(byteAddress);
      System.out.println("iteration " + i + " reading " + oldValue + " at address " + byteAddress);
      byte newValue = (byte) (oldValue | (oddOrEven << (i & 7)));
      System.out.println("iteration " + i + " putting " + newValue + " at address " + byteAddress);
      unsafe.putByte(byteAddress, newValue);
      }

      unsafe.freeMemory(addressAndLength[0]);
      unreserveMemory((int) addressAndLength[1]);
      }

      private void alternateBitsUsingLongs(int numberBits) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
      SecurityException,
      NoSuchFieldException {

      long[] addressAndLength = reserveAndAllocateMemory(numberBits);

      sun.misc.Unsafe unsafe = getUnsafe();
      for (int i = 0; i < numberBits; i++) {
      long oddOrEven = i & 1;
      long longAddress = addressAndLength[0] + ((i >>> 6) << 3);
      long oldValue = unsafe.getLong(longAddress);
      System.out.println("iteration " + i + " reading " + oldValue + " at address " + longAddress);
      long newValue = oldValue | (long) ((oddOrEven << (i & 63)));
      System.out.println("iteration " + i + " putting " + newValue + " at address " + longAddress);
      unsafe.putLong(longAddress, newValue);
      }

      unsafe.freeMemory(addressAndLength[0]);
      unreserveMemory((int) addressAndLength[1]);
      }

      public static void main(String[] args) {
      try {
      int numberBits = 100_000;
      DirectMemoryCrash test = new DirectMemoryCrash();
      if (args.length > 0 && "useLongs".equals(args[0])) {
      test.alternateBitsUsingLongs(numberBits);
      System.out.println("Finished job using longs");
      }
      else {
      test.alternateBitsUsingBytes(numberBits);
      System.out.println("Finished job using bytes");
      }
      } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) {
      System.out.println("There was an exception : " + ex.getClass().getSimpleName());
      }
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use the putLong/getLong methods

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: