[concurrency-interest] JVM crash when using Unsafe to read/write bytes in Java 8 (no crash with same code in Java 7)

Oleksandr Otenko oleksandr.otenko at oracle.com
Thu Sep 18 09:49:18 EDT 2014


Small modification of the loop for bytewise method:

         for (int i = 0; i < numberBits; i++) {
             int oddOrEven = 1 + i & 1;
             long byteAddress = addressAndLength[0] + (i >>> 3);
             byte oldValue = unsafe.getByte(byteAddress);
             System.out.println("iteration " + i + " reading " + 
oldValue + " at address " + byteAddress + (((oldValue | 85) != 85 )? " - 
look, how could it get bad value?!": ""));
             byte newValue = (byte) (oldValue | (oddOrEven << (i & 7)));
             System.out.println("iteration " + i + " putting " + 
newValue + " at address " + byteAddress);
             unsafe.putByte(byteAddress, newValue);
         }


gives this on output (clearly way before the end of array, so nothing to 
do with misalignment):
(and many more lines like these, too)

iteration 61440 reading 0 at address 1482093984
iteration 61440 putting 1 at address 1482093984
iteration 61441 reading 96 at address 1482093984 - look, how could it 
get bad value?!
iteration 61441 putting 96 at address 1482093984
iteration 61442 reading 96 at address 1482093984 - look, how could it 
get bad value?!
iteration 61442 putting 100 at address 1482093984
iteration 61443 reading 0 at address 1482093984
iteration 61443 putting 0 at address 1482093984
iteration 61444 reading 1 at address 1482093984
iteration 61444 putting 17 at address 1482093984
iteration 61445 reading 24 at address 1482093984 - look, how could it 
get bad value?!
iteration 61445 putting 24 at address 1482093984
iteration 61446 reading 96 at address 1482093984 - look, how could it 
get bad value?!
iteration 61446 putting 96 at address 1482093984
iteration 61447 reading 17 at address 1482093984
iteration 61447 putting 17 at address 1482093984


Alex


On 18/09/2014 12:53, Gaëlle Guimezanes wrote:
> Hi Stanimir,
>
> Thanks for the suggestion, but I've tried allocating extra 16 bytes 
> and even extra 1024 bytes, and it still fails.
>
> Gaelle
>
> On Thu, Sep 18, 2014 at 1:34 PM, Stanimir Simeonoff 
> <stanimir at riflexo.com <mailto:stanimir at riflexo.com>> wrote:
>
>     Hi,
>
>     I suppose the problem occurs due to the last few (less than 8)
>     bytes are addressed. Probably getByte(address) won't read just a
>     byte but a 32/64 word and mask+shift it.
>     Try to allocate extra 16 bytes instead of 8.
>
>     Stanimir
>
>     On Thu, Sep 18, 2014 at 1:38 PM, Gaëlle Guimezanes
>     <ggu at quartetfs.com <mailto:ggu at quartetfs.com>> wrote:
>
>         Hello,
>
>         I am sorry this is not really a concurrency problem since it
>         can be reproduced using only one thread, but I didn't really
>         know where to discuss this. Since many of you use Unsafe I
>         thought you might be able to help me understand what's
>         happening or direct me to the correct mailing list.
>
>         I have a strange behaviour using Unsafe in Java 1.8.0_20.
>         When I use getByte/putByte to manipulate bits, my JVM crashes
>         (access violation).
>         But 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.
>
>         I have attached a simple test class (DirectMemoryCrash.java)
>         for reproducing the problem: you can run it with no argument
>         for the getByte/putByte version, or with "useLongs" arguments
>         for the getLong/putLong version.
>
>         I also have attached crash reports :
>         hs_err_pid5480.log for the crash on windows
>         hs_err_pid7210.log for the crash on linux
>
>         Thank you for your attention,
>         Best regards,
>         Gaëlle Guimezanes.
>
>
>         _______________________________________________
>         Concurrency-interest mailing list
>         Concurrency-interest at cs.oswego.edu
>         <mailto:Concurrency-interest at cs.oswego.edu>
>         http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140918/c0b98157/attachment-0001.html>


More information about the Concurrency-interest mailing list