Skip to content

Commit 6d13c76

Browse files
author
Vladimir Ivanov
committed
8253191: C2: Masked byte comparisons with large masks produce wrong result on x86
Reviewed-by: thartmann
1 parent a191c58 commit 6d13c76

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

src/hotspot/cpu/x86/x86_64.ad

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2909,6 +2909,16 @@ operand immI2()
29092909
interface(CONST_INTER);
29102910
%}
29112911

2912+
operand immU7()
2913+
%{
2914+
predicate((0 <= n->get_int()) && (n->get_int() <= 0x7F));
2915+
match(ConI);
2916+
2917+
op_cost(5);
2918+
format %{ %}
2919+
interface(CONST_INTER);
2920+
%}
2921+
29122922
operand immI8()
29132923
%{
29142924
predicate((-0x80 <= n->get_int()) && (n->get_int() < 0x80));
@@ -11743,7 +11753,7 @@ instruct compB_mem_imm(rFlagsReg cr, memory mem, immI8 imm)
1174311753
ins_pipe(ialu_cr_reg_mem);
1174411754
%}
1174511755

11746-
instruct testUB_mem_imm(rFlagsReg cr, memory mem, immU8 imm, immI0 zero)
11756+
instruct testUB_mem_imm(rFlagsReg cr, memory mem, immU7 imm, immI0 zero)
1174711757
%{
1174811758
match(Set cr (CmpI (AndI (LoadUB mem) imm) zero));
1174911759

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Red Hat Inc. All rights reserved.
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,72 +23,67 @@
2323

2424
/*
2525
* @test
26-
* @bug 8204479
27-
* @summary Bitwise AND on byte value sometimes produces wrong result
26+
* @bug 8204479 8253191
2827
*
29-
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
30-
* -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -Xcomp -XX:-Inline
31-
* compiler.c2.TestUnsignedByteCompare
28+
* @library /test/lib
29+
* @modules java.base/jdk.internal.vm.annotation
30+
*
31+
* @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation compiler.c2.TestUnsignedByteCompare
3232
*/
33-
3433
package compiler.c2;
3534

35+
import java.lang.invoke.*;
36+
import jdk.internal.vm.annotation.DontInline;
37+
import jdk.test.lib.Asserts;
38+
3639
public class TestUnsignedByteCompare {
3740

38-
static int p, n;
41+
@DontInline static boolean testByteGT0(byte[] val) { return (val[0] & mask()) > 0; }
42+
@DontInline static boolean testByteGE0(byte[] val) { return (val[0] & mask()) >= 0; }
43+
@DontInline static boolean testByteEQ0(byte[] val) { return (val[0] & mask()) == 0; }
44+
@DontInline static boolean testByteNE0(byte[] val) { return (val[0] & mask()) != 0; }
45+
@DontInline static boolean testByteLE0(byte[] val) { return (val[0] & mask()) <= 0; }
46+
@DontInline static boolean testByteLT0(byte[] val) { return (val[0] & mask()) < 0; }
3947

40-
static void report(byte[] ba, int i, boolean failed) {
41-
// Enable for debugging:
42-
// System.out.println((failed ? "Failed" : "Passed") + " with: " + ba[i] + " at " + i);
48+
static void testValue(byte b) {
49+
byte[] bs = new byte[] { b };
50+
Asserts.assertEquals(((b & mask()) > 0), testByteGT0(bs), errorMessage(b, "GT0"));
51+
Asserts.assertEquals(((b & mask()) >= 0), testByteGE0(bs), errorMessage(b, "GE0"));
52+
Asserts.assertEquals(((b & mask()) == 0), testByteEQ0(bs), errorMessage(b, "EQ0"));
53+
Asserts.assertEquals(((b & mask()) != 0), testByteNE0(bs), errorMessage(b, "NE0"));
54+
Asserts.assertEquals(((b & mask()) <= 0), testByteLE0(bs), errorMessage(b, "LE0"));
55+
Asserts.assertEquals(((b & mask()) < 0), testByteLT0(bs), errorMessage(b, "LT0"));
4356
}
4457

45-
static void m1(byte[] ba) {
46-
for (int i = 0; i < ba.length; i++) {
47-
if ((ba[i] & 0xFF) < 0x10) {
48-
p++;
49-
report(ba, i, true);
50-
} else {
51-
n++;
52-
report(ba, i, false);
58+
public static void main(String[] args) {
59+
for (int mask = 0; mask <= 0xFF; mask++) {
60+
setMask(mask);
61+
for (int i = 0; i < 20_000; i++) {
62+
testValue((byte) i);
5363
}
5464
}
65+
System.out.println("TEST PASSED");
5566
}
5667

57-
static void m2(byte[] ba) {
58-
for (int i = 0; i < ba.length; i++) {
59-
if (((ba[i] & 0xFF) & 0x80) < 0) {
60-
p++;
61-
report(ba, i, true);
62-
} else {
63-
n++;
64-
report(ba, i, false);
65-
}
66-
}
68+
static String errorMessage(byte b, String type) {
69+
return String.format("%s: val=0x%x mask=0x%x", type, b, mask());
6770
}
6871

69-
static public void main(String[] args) {
70-
final int tries = 1_000;
71-
final int count = 1_000;
72+
// Mutable mask as a compile-time constant.
7273

73-
byte[] ba = new byte[count];
74+
private static final CallSite MASK_CS = new MutableCallSite(MethodType.methodType(int.class));
75+
private static final MethodHandle MASK_MH = MASK_CS.dynamicInvoker();
7476

75-
for (int i = 0; i < count; i++) {
76-
int v = -(i % 126 + 1);
77-
ba[i] = (byte)v;
78-
}
79-
80-
for (int t = 0; t < tries; t++) {
81-
m1(ba);
82-
if (p != 0) {
83-
throw new IllegalStateException("m1 error: p = " + p + ", n = " + n);
84-
}
77+
static int mask() {
78+
try {
79+
return (int) MASK_MH.invokeExact();
80+
} catch (Throwable t) {
81+
throw new InternalError(t); // should NOT happen
8582
}
83+
}
8684

87-
for (int t = 0; t < tries; t++) {
88-
m2(ba);
89-
if (p != 0) {
90-
throw new IllegalStateException("m2 error: p = " + p + ", n = " + n);
91-
}
92-
}
85+
static void setMask(int mask) {
86+
MethodHandle constant = MethodHandles.constant(int.class, mask);
87+
MASK_CS.setTarget(constant);
9388
}
9489
}

0 commit comments

Comments
 (0)