Skip to content

Commit 0567b14

Browse files
committed
Consistently use PythonContext.isNativeAccessAllowed
1 parent a2bf4c0 commit 0567b14

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ public static CApiContext ensureCapiWasLoaded(Node node, PythonContext context,
969969
*/
970970
throw e.getExceptionForReraise();
971971
} catch (RuntimeException e) {
972-
if (!context.getEnv().isNativeAccessAllowed()) {
972+
if (!context.isNativeAccessAllowed()) {
973973
throw new ImportException(null, name, path, ErrorMessages.NATIVE_ACCESS_NOT_ALLOWED);
974974
}
975975
throw new ApiInitException(wrapJavaException(e, node), name, ErrorMessages.CAPI_LOAD_ERROR, capiFile.getAbsoluteFile().getPath());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CArrayWrappers.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -55,7 +55,9 @@
5555
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper;
5656
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapperLibrary;
5757
import com.oracle.graal.python.builtins.objects.ints.PInt;
58+
import com.oracle.graal.python.nodes.ErrorMessages;
5859
import com.oracle.graal.python.runtime.GilNode;
60+
import com.oracle.graal.python.runtime.PythonContext;
5961
import com.oracle.graal.python.util.OverflowException;
6062
import com.oracle.truffle.api.CompilerAsserts;
6163
import com.oracle.truffle.api.CompilerDirectives;
@@ -175,7 +177,6 @@ long asPointer(
175177
}
176178
return interopLibrary.asPointer(nativePointer);
177179
}
178-
179180
}
180181

181182
/**
@@ -190,24 +191,24 @@ public CStringWrapper(String delegate) {
190191
super(delegate);
191192
}
192193

193-
public final String getString(PythonNativeWrapperLibrary lib) {
194+
public String getString(PythonNativeWrapperLibrary lib) {
194195
return ((String) lib.getDelegate(this));
195196
}
196197

197198
@ExportMessage
198-
final long getArraySize(
199+
long getArraySize(
199200
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
200201
return ((String) lib.getDelegate(this)).length();
201202
}
202203

203204
@ExportMessage
204205
@SuppressWarnings("static-method")
205-
final boolean hasArrayElements() {
206+
boolean hasArrayElements() {
206207
return true;
207208
}
208209

209210
@ExportMessage
210-
final byte readArrayElement(long index,
211+
byte readArrayElement(long index,
211212
@CachedLibrary("this") PythonNativeWrapperLibrary lib) throws InvalidArrayIndexException {
212213
try {
213214
int idx = PInt.intValueExact(index);
@@ -225,19 +226,19 @@ final byte readArrayElement(long index,
225226
}
226227

227228
@ExportMessage
228-
final boolean isArrayElementReadable(long identifier,
229+
boolean isArrayElementReadable(long identifier,
229230
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
230231
return 0 <= identifier && identifier < getArraySize(lib);
231232
}
232233

233234
@ExportMessage
234235
@SuppressWarnings("static-method")
235-
protected boolean hasNativeType() {
236+
boolean hasNativeType() {
236237
return true;
237238
}
238239

239240
@ExportMessage
240-
protected Object getNativeType(
241+
Object getNativeType(
241242
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
242243
@Exclusive @Cached PCallCapiFunction callByteArrayTypeIdNode) {
243244
return callByteArrayTypeIdNode.call(FUN_GET_BYTE_ARRAY_TYPE_ID, ((String) lib.getDelegate(this)).length());
@@ -247,6 +248,10 @@ protected Object getNativeType(
247248
void toNative(
248249
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
249250
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
251+
if (!PythonContext.get(lib).isNativeAccessAllowed()) {
252+
CompilerDirectives.transferToInterpreterAndInvalidate();
253+
throw new RuntimeException(ErrorMessages.NATIVE_ACCESS_NOT_ALLOWED);
254+
}
250255
invalidateNode.execute();
251256
if (!lib.isNative(this)) {
252257
setNativePointer(stringToNativeUtf8Bytes(getString(lib)));
@@ -313,13 +318,13 @@ boolean isArrayElementReadable(long identifier,
313318

314319
@ExportMessage
315320
@SuppressWarnings("static-method")
316-
protected boolean hasNativeType() {
321+
boolean hasNativeType() {
317322
return true;
318323
}
319324

320325
@ExportMessage
321326
@SuppressWarnings("static-method")
322-
protected Object getNativeType(
327+
Object getNativeType(
323328
@Cached GetLLVMType getLLVMType) {
324329
return getLLVMType.execute(LLVMType.int8_ptr_t);
325330
}
@@ -328,6 +333,10 @@ protected Object getNativeType(
328333
void toNative(
329334
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
330335
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
336+
if (!PythonContext.get(lib).isNativeAccessAllowed()) {
337+
CompilerDirectives.transferToInterpreterAndInvalidate();
338+
throw new RuntimeException(ErrorMessages.NATIVE_ACCESS_NOT_ALLOWED);
339+
}
331340
invalidateNode.execute();
332341
if (!lib.isNative(this)) {
333342
setNativePointer(byteArrayToNativeInt8(getByteArray(lib), true));
@@ -390,12 +399,15 @@ boolean isArrayElementReadable(long identifier,
390399
void toNative(
391400
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
392401
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
402+
if (!PythonContext.get(lib).isNativeAccessAllowed()) {
403+
CompilerDirectives.transferToInterpreterAndInvalidate();
404+
throw new RuntimeException(ErrorMessages.NATIVE_ACCESS_NOT_ALLOWED);
405+
}
393406
invalidateNode.execute();
394407
if (!lib.isNative(this)) {
395408
int[] data = getIntArray(lib);
396409
setNativePointer(intArrayToNativeInt32(data));
397410
}
398411
}
399-
400412
}
401413
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private Object getFunction(PythonContext context, Object lib, NativeFunction fun
209209

210210
private Object loadLibrary(PythonContext context) {
211211
CompilerAsserts.neverPartOfCompilation();
212-
if (context.getEnv().isNativeAccessAllowed()) {
212+
if (context.isNativeAccessAllowed()) {
213213
String path = getLibPath(context, name);
214214
String src = String.format("%sload (RTLD_LOCAL) \"%s\"", nfiBackend.withClause, path);
215215
if (LOGGER.isLoggable(Level.FINE)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ public final Assumption getNativeObjectsAllManagedAssumption() {
17951795
}
17961796

17971797
public boolean isExecutableAccessAllowed() {
1798-
return getEnv().isHostLookupAllowed() || getEnv().isNativeAccessAllowed();
1798+
return getEnv().isHostLookupAllowed() || isNativeAccessAllowed();
17991799
}
18001800

18011801
/**

0 commit comments

Comments
 (0)