Skip to content

Commit 5c443b6

Browse files
authored
Merge pull request #5 from 0-x-2-2/master
Implementation of Unsafe#putByte(long,byte).
2 parents 882e018 + 5047fef commit 5c443b6

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/main/java/dev/xdark/ssvm/natives/UnsafeNatives.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private static void init(VirtualMachine vm, InstanceJavaClass unsafe, UnsafeHelp
163163
throw new PanicException("Segfault");
164164
}
165165
val value = (ObjectValue) obj;
166-
val offset = (int) locals.load(2).asLong();
166+
val offset = locals.load(2).asLong();
167167
val expected = locals.load(4).asInt();
168168
val x = locals.load(5).asInt();
169169
val memoryManager = vm.getMemoryManager();
@@ -189,7 +189,7 @@ private static void init(VirtualMachine vm, InstanceJavaClass unsafe, UnsafeHelp
189189
throw new PanicException("Segfault");
190190
}
191191
val value = (ObjectValue) obj;
192-
val offset = (int) locals.load(2).asLong();
192+
val offset = locals.load(2).asLong();
193193
val expected = locals.<ObjectValue>load(4);
194194
val x = locals.<ObjectValue>load(5);
195195
val memoryManager = vm.getMemoryManager();
@@ -212,7 +212,7 @@ private static void init(VirtualMachine vm, InstanceJavaClass unsafe, UnsafeHelp
212212
throw new PanicException("Segfault");
213213
}
214214
val value = (ObjectValue) $value;
215-
val offset = (int) locals.load(2).asLong();
215+
val offset = locals.load(2).asLong();
216216
val expected = locals.load(4).asLong();
217217
val x = locals.load(6).asLong();
218218
val memoryManager = vm.getMemoryManager();
@@ -282,20 +282,28 @@ private static void init(VirtualMachine vm, InstanceJavaClass unsafe, UnsafeHelp
282282
ctx.setResult(LongValue.M_ONE);
283283
return Result.ABORT;
284284
});
285+
vmi.setInvoker(unsafe, "putByte", "(JB)V", ctx -> {
286+
val memoryManager = vm.getMemoryManager();
287+
val locals = ctx.getLocals();
288+
val address = locals.load(1).asLong();
289+
val block = nonNull(memoryManager.getMemory(address));
290+
block.getData().writeByte(address - block.getAddress(), locals.load(3).asByte());
291+
return Result.ABORT;
292+
});
285293
vmi.setInvoker(unsafe, "putLong", "(JJ)V", ctx -> {
286294
val memoryManager = vm.getMemoryManager();
287295
val locals = ctx.getLocals();
288296
val address = locals.load(1).asLong();
289297
val block = nonNull(memoryManager.getMemory(address));
290-
block.getData().writeLong((int) (address - block.getAddress()), locals.load(3).asLong());
298+
block.getData().writeLong(address - block.getAddress(), locals.load(3).asLong());
291299
return Result.ABORT;
292300
});
293301
vmi.setInvoker(unsafe, "getByte", "(J)B", ctx -> {
294302
val memoryManager = vm.getMemoryManager();
295303
val locals = ctx.getLocals();
296304
val address = locals.load(1).asLong();
297305
val block = nonNull(memoryManager.getMemory(address));
298-
ctx.setResult(IntValue.of(block.getData().readByte((int) (address - block.getAddress()))));
306+
ctx.setResult(IntValue.of(block.getData().readByte(address - block.getAddress())));
299307
return Result.ABORT;
300308
});
301309
vmi.setInvoker(unsafe, "putInt", "(Ljava/lang/Object;JI)V", ctx -> {
@@ -346,7 +354,7 @@ private static void init(VirtualMachine vm, InstanceJavaClass unsafe, UnsafeHelp
346354
vmi.setInvoker(unsafe, "getLong", "(J)J", ctx -> {
347355
val address = ctx.getLocals().load(1).asLong();
348356
val block = nonNull(vm.getMemoryManager().getMemory(address));
349-
ctx.setResult(LongValue.of(block.getData().readLong((int) (address - block.getAddress()))));
357+
ctx.setResult(LongValue.of(block.getData().readLong(address - block.getAddress())));
350358
return Result.ABORT;
351359
});
352360
vmi.setInvoker(unsafe, "defineAnonymousClass", "(Ljava/lang/Class;[B[Ljava/lang/Object;)Ljava/lang/Class;", ctx -> {

src/test/java/dev/xdark/ssvm/enhanced/UnsafeTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ private static void testLong2() {
3838
}
3939
}
4040

41+
@VMTest
42+
private static void testLongWithByte() {
43+
val address = U.allocateMemory(8L);
44+
val v = System.currentTimeMillis();
45+
U.putByte(address, (byte) v);
46+
U.putByte(address + 1, (byte) (v >> 8));
47+
U.putByte(address + 2, (byte) (v >> 16));
48+
U.putByte(address + 3, (byte) (v >> 24));
49+
U.putByte(address + 4, (byte) (v >> 32));
50+
U.putByte(address + 5, (byte) (v >> 40));
51+
U.putByte(address + 6, (byte) (v >> 48));
52+
U.putByte(address + 7, (byte) (v >> 56));
53+
val read = ((long) U.getByte(address + 7) << 56)
54+
| ((long) U.getByte(address + 6) & 0xff) << 48
55+
| ((long) U.getByte(address + 5) & 0xff) << 40
56+
| ((long) U.getByte(address + 4) & 0xff) << 32
57+
| ((long) U.getByte(address + 3) & 0xff) << 24
58+
| ((long) U.getByte(address + 2) & 0xff) << 16
59+
| ((long) U.getByte(address + 1) & 0xff) << 8
60+
| ((long) U.getByte(address) & 0xff);
61+
if (v != read || v != U.getLong(address)) {
62+
throw new IllegalStateException();
63+
}
64+
}
65+
4166
@VMTest
4267
private static void testArray() {
4368
val unsafe = U;

0 commit comments

Comments
 (0)