Skip to content

Commit dcac4b0

Browse files
author
Fei Gao
committedOct 14, 2024
8341471: Reversed field layout caused by unstable sorting
Reviewed-by: jwaters, jsjolen
1 parent e3f6503 commit dcac4b0

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed
 

‎src/hotspot/share/classfile/fieldLayoutBuilder.hpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, 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
@@ -101,17 +101,13 @@ class LayoutRawBlock : public ResourceObj {
101101
// sort fields in decreasing order.
102102
// Note: with line types, the comparison should include alignment constraint if sizes are equals
103103
static int compare_size_inverted(LayoutRawBlock** x, LayoutRawBlock** y) {
104-
#ifdef _WINDOWS
105-
// qsort() on Windows reverse the order of fields with the same size
106-
// the extension of the comparison function below preserves this order
107104
int diff = (*y)->size() - (*x)->size();
105+
// qsort() may reverse the order of fields with the same size.
106+
// The extension is to ensure stable sort.
108107
if (diff == 0) {
109108
diff = (*x)->field_index() - (*y)->field_index();
110109
}
111110
return diff;
112-
#else
113-
return (*y)->size() - (*x)->size();
114-
#endif // _WINDOWS
115111
}
116112

117113
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2024, Arm Limited. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.lang.reflect.Field;
25+
import jdk.internal.misc.Unsafe;
26+
27+
/*
28+
* @test
29+
* @bug 8341471
30+
* @summary Reversed field layout caused by unstable sorting
31+
* @modules java.base/jdk.internal.misc
32+
* @run main/othervm TestFieldLayout
33+
*/
34+
35+
public class TestFieldLayout {
36+
37+
private static final Unsafe U = Unsafe.getUnsafe();
38+
39+
public static void main(String[] args) throws Exception {
40+
41+
boolean endResult = true;
42+
long previous = 0;
43+
44+
for (Field f : Test.class.getDeclaredFields()) {
45+
long current = U.objectFieldOffset(f);
46+
if (current < previous) {
47+
System.out.printf("FAILED: field %s offset %d previous %d\n",
48+
f.getName(), current, previous);
49+
endResult = false;
50+
}
51+
previous = current;
52+
}
53+
54+
System.out.println(endResult ? "Test PASSES" : "Test FAILS");
55+
if (!endResult) {
56+
throw new Error("Test failed");
57+
}
58+
}
59+
60+
public class Test {
61+
char a000;
62+
char a001;
63+
char a002;
64+
char a003;
65+
char a004;
66+
char a005;
67+
char a006;
68+
char a007;
69+
char a008;
70+
char a009;
71+
char a00a;
72+
char a00b;
73+
}
74+
75+
}
76+

0 commit comments

Comments
 (0)
Please sign in to comment.