Skip to content

Commit 0b43348

Browse files
committed
Add boundary around copyMemory and setMemory calls
1 parent ec0c78d commit 0b43348

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/CFieldBuiltins.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ static Object z_set(@SuppressWarnings("unused") FieldSet setfunc, Pointer ptr, O
652652
@Shared @Cached PointerNodes.PointerFromLongNode pointerFromLongNode,
653653
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
654654
@Shared @Cached PointerNodes.WritePointerNode writePointerNode,
655-
@Shared @Cached PRaiseNode raiseNode,
656-
@Shared @Cached PythonObjectFactory factory) {
655+
@Shared @Cached PRaiseNode raiseNode) {
657656
if (value == PNone.NONE) {
658657
writePointerNode.execute(inliningTarget, ptr, Pointer.NULL);
659658
return PNone.NONE;
@@ -688,7 +687,7 @@ static Object Z_set(@SuppressWarnings("unused") FieldSet setfunc, Pointer ptr, O
688687
@Cached TruffleString.CopyToByteArrayNode copyToByteArrayNode,
689688
@Shared @Cached PointerNodes.WritePointerNode writePointerNode,
690689
@Shared @Cached PRaiseNode raiseNode,
691-
@Shared @Cached PythonObjectFactory factory) { // CTYPES_UNICODE
690+
@Cached PythonObjectFactory factory) { // CTYPES_UNICODE
692691
if (value == PNone.NONE) {
693692
writePointerNode.execute(inliningTarget, ptr, Pointer.NULL);
694693
return PNone.NONE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/memory/PointerNodes.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ static void doPointerArray(Node inliningTarget, byte[] dst, int dstOffset, Memor
134134

135135
@Specialization
136136
static void doNativeMemory(Node inliningTarget, byte[] dst, int dstOffset, @SuppressWarnings("unused") MemoryBlock srcMemory, LongPointerStorage src, int srcOffset, int size) {
137-
Unsafe unsafe = PythonContext.get(inliningTarget).getUnsafe();
138-
unsafe.copyMemory(null, src.pointer + srcOffset, dst, byteArrayOffset(dstOffset), size);
137+
PythonContext context = PythonContext.get(inliningTarget);
138+
context.copyNativeMemory(dst, dstOffset, src.pointer + srcOffset, size);
139139
}
140140
}
141141

@@ -327,8 +327,8 @@ static void doMemoryView(@SuppressWarnings("unused") MemoryBlock dstMemory, Memo
327327

328328
@Specialization
329329
static void doNativeMemory(Node inliningTarget, @SuppressWarnings("unused") MemoryBlock dstMemory, LongPointerStorage dst, int dstOffset, byte[] src, int srcOffset, int size) {
330-
Unsafe unsafe = PythonContext.get(inliningTarget).getUnsafe();
331-
unsafe.copyMemory(src, byteArrayOffset(srcOffset), null, dst.pointer + dstOffset, size);
330+
PythonContext context = PythonContext.get(inliningTarget);
331+
context.copyNativeMemory(dst.pointer + dstOffset, src, srcOffset, size);
332332
}
333333

334334
@Specialization
@@ -627,7 +627,7 @@ static long doBytes(Node inliningTarget, MemoryBlock memory, ByteArrayStorage st
627627
// We need to copy the whole memory block to keep the pointer offsets consistent
628628
PythonContext context = PythonContext.get(inliningTarget);
629629
long pointer = context.allocateNativeMemory(len);
630-
context.getUnsafe().copyMemory(storage.bytes, byteArrayOffset(0), null, pointer, len);
630+
context.copyNativeMemory(pointer, storage.bytes, 0, len);
631631
memory.storage = new LongPointerStorage(pointer);
632632
return pointer + offset;
633633
}
@@ -637,7 +637,7 @@ static long doZero(Node inliningTarget, MemoryBlock memory, ZeroStorage storage,
637637
int len = storage.size;
638638
PythonContext context = PythonContext.get(inliningTarget);
639639
long pointer = context.allocateNativeMemory(len);
640-
context.getUnsafe().setMemory(pointer, len, (byte) 0);
640+
context.setNativeMemory(pointer, len, (byte) 0);
641641
memory.storage = new LongPointerStorage(pointer);
642642
return pointer + offset;
643643
}
@@ -873,8 +873,4 @@ static ByteArrayStorage convert(Node inliningTarget, MemoryBlock memory, Pointer
873873
return newStorage;
874874
}
875875
}
876-
877-
private static long byteArrayOffset(int offset) {
878-
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + (long) Unsafe.ARRAY_BYTE_INDEX_SCALE * (long) offset;
879-
}
880876
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,16 +2534,50 @@ public Unsafe getUnsafe() {
25342534
return UnsafeWrapper.UNSAFE;
25352535
}
25362536
CompilerDirectives.transferToInterpreterAndInvalidate();
2537-
throw new RuntimeException("NAtive access not allowed, cannot manipulate native memory");
2537+
throw new RuntimeException("Native access not allowed, cannot manipulate native memory");
25382538
}
25392539

2540-
@TruffleBoundary
25412540
public long allocateNativeMemory(long size) {
2542-
return getUnsafe().allocateMemory(size);
2541+
return allocateNativeMemoryBoundary(getUnsafe(), size);
25432542
}
25442543

25452544
@TruffleBoundary
2545+
private static long allocateNativeMemoryBoundary(Unsafe unsafe, long size) {
2546+
return unsafe.allocateMemory(size);
2547+
}
2548+
25462549
public void freeNativeMemory(long address) {
2547-
getUnsafe().freeMemory(address);
2550+
freeNativeMemoryBoundary(getUnsafe(), address);
2551+
}
2552+
2553+
@TruffleBoundary
2554+
private static void freeNativeMemoryBoundary(Unsafe unsafe, long address) {
2555+
unsafe.freeMemory(address);
2556+
}
2557+
2558+
public void copyNativeMemory(long dst, byte[] src, int srcOffset, int size) {
2559+
copyNativeMemoryBoundary(getUnsafe(), null, dst, src, byteArrayOffset(srcOffset), size);
2560+
}
2561+
2562+
public void copyNativeMemory(byte[] dst, int dstOffset, long src, int size) {
2563+
copyNativeMemoryBoundary(getUnsafe(), dst, byteArrayOffset(dstOffset), null, src, size);
2564+
}
2565+
2566+
private static long byteArrayOffset(int offset) {
2567+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + (long) Unsafe.ARRAY_BYTE_INDEX_SCALE * (long) offset;
2568+
}
2569+
2570+
@TruffleBoundary
2571+
private static void copyNativeMemoryBoundary(Unsafe unsafe, Object dst, long dstOffset, Object src, long srcOffset, int size) {
2572+
unsafe.copyMemory(src, srcOffset, dst, dstOffset, size);
2573+
}
2574+
2575+
public void setNativeMemory(long pointer, int size, byte value) {
2576+
setNativeMemoryBoundary(getUnsafe(), pointer, size, value);
2577+
}
2578+
2579+
@TruffleBoundary
2580+
private static void setNativeMemoryBoundary(Unsafe unsafe, long pointer, int size, byte value) {
2581+
unsafe.setMemory(pointer, size, value);
25482582
}
25492583
}

0 commit comments

Comments
 (0)