[riscv-port] RFR: 8277884: riscv: Fix cmpxchg_narrow_value that needs to sign-extend non-bool results

Fei Yang fyang at openjdk.java.net
Mon Nov 29 05:09:27 UTC 2021


On Mon, 29 Nov 2021 03:40:43 GMT, zhengxiaolinX <duke at openjdk.java.net> wrote:

> Hi team,
> 
> According to https://bugs.openjdk.java.net/browse/JDK-8277884 and [the original patch](https://github.com/riscv-collab/riscv-openjdk/pull/9):
> 
> A very simple program inherited from jcstress could reproduce this:
> 
> 
> import java.lang.invoke.MethodHandles;
> import java.lang.invoke.VarHandle;
> 
> // run with:
> // build/linux-riscv64-server-release/images/jdk/bin/java -ea -XX:-TieredCompilation -XX:CompileCommand=compileonly,jdk.internal.misc.Unsafe::compareAndSetByte TestCASByte
> // assert will fail.
> // if we add '-XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_compareAndExchangeByte' this test will pass.
> 
> public class TestCASByte {
> 
>     byte[] array = new byte[1];
> 
>     static final VarHandle vh;
>     static {
>         try {
>             vh = MethodHandles.arrayElementVarHandle(byte[].class);
>         } catch (Exception e) {
>             throw new RuntimeException(e);
>         }
>     }
> 
>     public void test() {
>         for (int i = 0; i < 100_000; i++) { // To level 4
>             // clear
>             array[0] = 0;
> 
>             byte res1 = (byte)vh.getAndSet(array, 0, (byte)-1);
>             byte res2 = (byte)vh.getAndSet(array, 0, (byte)2);
> 
>             assert res1 == 0 && res2 == -1;
>         }
>     }
> 
>     public static void main(String[] args) {
>         new TestCASByte().test();
>     }
> 
> }
> 
> 
> In this case when we are getting the `-1` value saved in memory, the shift is `0`, the old is `0x00000000000000ff` and the mask is `0x00000000000000ff` at the end of cmpxchg_narrow_value. The final result is `0x00000000000000ff` but it indeed should be `0xffffffffffffffff`. Therefore, if the result is negative, we need to make a sign extension.
> 
> Thanks,
> Xiaolin

Looks good.
I can also reproduce the bug  with following three jcstress tests:
org.openjdk.jcstress.tests.atomicity.varHandles.fields.GetAndSetTest.GetAndSetByte
org.openjdk.jcstress.tests.atomicity.varHandles.fields.GetAndSetTest.GetAndSetShort
org.openjdk.jcstress.tests.atomicity.varHandles.arrays.GetAndSetTest.GetAndSetByte

-------------

Marked as reviewed by fyang (Lead).

PR: https://git.openjdk.java.net/riscv-port/pull/15


More information about the riscv-port-dev mailing list