Skip to content

Commit ce4b257

Browse files
committed
8320886: Unsafe_SetMemory0 is not guarded
Reviewed-by: dholmes, fparain
1 parent b270f30 commit ce4b257

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/hotspot/cpu/x86/assembler_x86.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ address Assembler::locate_operand(address inst, WhichOperand which) {
920920
case 0x11: // movups
921921
case 0x12: // movlps
922922
case 0x28: // movaps
923+
case 0x29: // movaps
923924
case 0x2E: // ucomiss
924925
case 0x2F: // comiss
925926
case 0x54: // andps
@@ -969,7 +970,7 @@ address Assembler::locate_operand(address inst, WhichOperand which) {
969970
assert(which == call32_operand, "jcc has no disp32 or imm");
970971
return ip;
971972
default:
972-
ShouldNotReachHere();
973+
fatal("not handled: 0x0F%2X", 0xFF & *(ip-1));
973974
}
974975
break;
975976

src/hotspot/share/prims/unsafe.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ UNSAFE_ENTRY_SCOPED(void, Unsafe_SetMemory0(JNIEnv *env, jobject unsafe, jobject
390390
oop base = JNIHandles::resolve(obj);
391391
void* p = index_oop_from_field_offset_long(base, offset);
392392

393-
Copy::fill_to_memory_atomic(p, sz, value);
393+
{
394+
GuardUnsafeAccess guard(thread);
395+
Copy::fill_to_memory_atomic(p, sz, value);
396+
}
394397
} UNSAFE_END
395398

396399
UNSAFE_ENTRY_SCOPED(void, Unsafe_CopyMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) {

test/hotspot/jtreg/runtime/Unsafe/InternalErrorTest.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.File;
4242
import java.io.IOException;
4343
import java.io.RandomAccessFile;
44+
import java.lang.foreign.MemorySegment;
4445
import java.lang.reflect.Field;
4546
import java.lang.reflect.Method;
4647
import java.nio.MappedByteBuffer;
@@ -60,6 +61,8 @@ public class InternalErrorTest {
6061
private static final String failureMsg1 = "InternalError not thrown";
6162
private static final String failureMsg2 = "Wrong InternalError: ";
6263

64+
private static final int NUM_TESTS = 4;
65+
6366
public static void main(String[] args) throws Throwable {
6467
Unsafe unsafe = Unsafe.getUnsafe();
6568

@@ -71,9 +74,9 @@ public static void main(String[] args) throws Throwable {
7174
s.append("1");
7275
}
7376
Files.write(file.toPath(), s.toString().getBytes());
74-
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
77+
FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
7578
MappedByteBuffer buffer =
76-
fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
79+
fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size());
7780

7881
// Get address of mapped memory.
7982
long mapAddr = 0;
@@ -86,13 +89,13 @@ public static void main(String[] args) throws Throwable {
8689
}
8790
long allocMem = unsafe.allocateMemory(4000);
8891

89-
for (int i = 0; i < 3; i++) {
92+
for (int i = 0; i < NUM_TESTS; i++) {
9093
test(buffer, unsafe, mapAddr, allocMem, i);
9194
}
9295

9396
Files.write(file.toPath(), "2".getBytes());
9497
buffer.position(buffer.position() + pageSize);
95-
for (int i = 0; i < 3; i++) {
98+
for (int i = 0; i < NUM_TESTS; i++) {
9699
try {
97100
test(buffer, unsafe, mapAddr, allocMem, i);
98101
WhiteBox.getWhiteBox().forceSafepoint();
@@ -107,7 +110,7 @@ public static void main(String[] args) throws Throwable {
107110
Method m = InternalErrorTest.class.getMethod("test", MappedByteBuffer.class, Unsafe.class, long.class, long.class, int.class);
108111
WhiteBox.getWhiteBox().enqueueMethodForCompilation(m, 3);
109112

110-
for (int i = 0; i < 3; i++) {
113+
for (int i = 0; i < NUM_TESTS; i++) {
111114
try {
112115
test(buffer, unsafe, mapAddr, allocMem, i);
113116
WhiteBox.getWhiteBox().forceSafepoint();
@@ -121,7 +124,7 @@ public static void main(String[] args) throws Throwable {
121124

122125
WhiteBox.getWhiteBox().enqueueMethodForCompilation(m, 4);
123126

124-
for (int i = 0; i < 3; i++) {
127+
for (int i = 0; i < NUM_TESTS; i++) {
125128
try {
126129
test(buffer, unsafe, mapAddr, allocMem, i);
127130
WhiteBox.getWhiteBox().forceSafepoint();
@@ -143,13 +146,18 @@ public static void test(MappedByteBuffer buffer, Unsafe unsafe, long mapAddr, lo
143146
buffer.get(new byte[8]);
144147
break;
145148
case 1:
146-
// testing Unsafe.copySwapMemory, trying to access next page after truncation.
149+
// testing Unsafe.copySwapMemory, trying to access next page after truncation.
147150
unsafe.copySwapMemory(null, mapAddr + pageSize, new byte[4000], 16, 2000, 2);
148151
break;
149152
case 2:
150-
// testing Unsafe.copySwapMemory, trying to access next page after truncation.
153+
// testing Unsafe.copySwapMemory, trying to access next page after truncation.
151154
unsafe.copySwapMemory(null, mapAddr + pageSize, null, allocMem, 2000, 2);
152155
break;
156+
case 3:
157+
MemorySegment segment = MemorySegment.ofBuffer(buffer);
158+
// testing Unsafe.setMemory, trying to access next page after truncation.
159+
segment.fill((byte) 0xF0);
160+
break;
153161
}
154162
}
155163

0 commit comments

Comments
 (0)