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

zhengxiaolinX duke at openjdk.java.net
Mon Nov 29 06:47: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

This pull request has now been integrated.

Changeset: 5c1cbe08
Author:    yunyao.zxl <yunyao.zxl at alibaba-inc.com>
Committer: Fei Yang <fyang at openjdk.org>
URL:       https://git.openjdk.java.net/riscv-port/commit/5c1cbe08946a940e46b91183ea7d21638f245e1f
Stats:     7 lines in 1 file changed: 7 ins; 0 del; 0 mod

8277884: riscv: Fix cmpxchg_narrow_value that needs to sign-extend non-bool results

Reviewed-by: fyang

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

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


More information about the riscv-port-dev mailing list