1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
55
55
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
56
56
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapperLibrary ;
57
57
import com .oracle .graal .python .builtins .objects .ints .PInt ;
58
+ import com .oracle .graal .python .nodes .ErrorMessages ;
58
59
import com .oracle .graal .python .runtime .GilNode ;
60
+ import com .oracle .graal .python .runtime .PythonContext ;
59
61
import com .oracle .graal .python .util .OverflowException ;
60
62
import com .oracle .truffle .api .CompilerAsserts ;
61
63
import com .oracle .truffle .api .CompilerDirectives ;
@@ -175,7 +177,6 @@ long asPointer(
175
177
}
176
178
return interopLibrary .asPointer (nativePointer );
177
179
}
178
-
179
180
}
180
181
181
182
/**
@@ -190,24 +191,24 @@ public CStringWrapper(String delegate) {
190
191
super (delegate );
191
192
}
192
193
193
- public final String getString (PythonNativeWrapperLibrary lib ) {
194
+ public String getString (PythonNativeWrapperLibrary lib ) {
194
195
return ((String ) lib .getDelegate (this ));
195
196
}
196
197
197
198
@ ExportMessage
198
- final long getArraySize (
199
+ long getArraySize (
199
200
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ) {
200
201
return ((String ) lib .getDelegate (this )).length ();
201
202
}
202
203
203
204
@ ExportMessage
204
205
@ SuppressWarnings ("static-method" )
205
- final boolean hasArrayElements () {
206
+ boolean hasArrayElements () {
206
207
return true ;
207
208
}
208
209
209
210
@ ExportMessage
210
- final byte readArrayElement (long index ,
211
+ byte readArrayElement (long index ,
211
212
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ) throws InvalidArrayIndexException {
212
213
try {
213
214
int idx = PInt .intValueExact (index );
@@ -225,19 +226,19 @@ final byte readArrayElement(long index,
225
226
}
226
227
227
228
@ ExportMessage
228
- final boolean isArrayElementReadable (long identifier ,
229
+ boolean isArrayElementReadable (long identifier ,
229
230
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ) {
230
231
return 0 <= identifier && identifier < getArraySize (lib );
231
232
}
232
233
233
234
@ ExportMessage
234
235
@ SuppressWarnings ("static-method" )
235
- protected boolean hasNativeType () {
236
+ boolean hasNativeType () {
236
237
return true ;
237
238
}
238
239
239
240
@ ExportMessage
240
- protected Object getNativeType (
241
+ Object getNativeType (
241
242
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ,
242
243
@ Exclusive @ Cached PCallCapiFunction callByteArrayTypeIdNode ) {
243
244
return callByteArrayTypeIdNode .call (FUN_GET_BYTE_ARRAY_TYPE_ID , ((String ) lib .getDelegate (this )).length ());
@@ -247,6 +248,10 @@ protected Object getNativeType(
247
248
void toNative (
248
249
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ,
249
250
@ Exclusive @ Cached InvalidateNativeObjectsAllManagedNode invalidateNode ) {
251
+ if (!PythonContext .get (lib ).isNativeAccessAllowed ()) {
252
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
253
+ throw new RuntimeException (ErrorMessages .NATIVE_ACCESS_NOT_ALLOWED );
254
+ }
250
255
invalidateNode .execute ();
251
256
if (!lib .isNative (this )) {
252
257
setNativePointer (stringToNativeUtf8Bytes (getString (lib )));
@@ -313,13 +318,13 @@ boolean isArrayElementReadable(long identifier,
313
318
314
319
@ ExportMessage
315
320
@ SuppressWarnings ("static-method" )
316
- protected boolean hasNativeType () {
321
+ boolean hasNativeType () {
317
322
return true ;
318
323
}
319
324
320
325
@ ExportMessage
321
326
@ SuppressWarnings ("static-method" )
322
- protected Object getNativeType (
327
+ Object getNativeType (
323
328
@ Cached GetLLVMType getLLVMType ) {
324
329
return getLLVMType .execute (LLVMType .int8_ptr_t );
325
330
}
@@ -328,6 +333,10 @@ protected Object getNativeType(
328
333
void toNative (
329
334
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ,
330
335
@ Exclusive @ Cached InvalidateNativeObjectsAllManagedNode invalidateNode ) {
336
+ if (!PythonContext .get (lib ).isNativeAccessAllowed ()) {
337
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
338
+ throw new RuntimeException (ErrorMessages .NATIVE_ACCESS_NOT_ALLOWED );
339
+ }
331
340
invalidateNode .execute ();
332
341
if (!lib .isNative (this )) {
333
342
setNativePointer (byteArrayToNativeInt8 (getByteArray (lib ), true ));
@@ -390,12 +399,15 @@ boolean isArrayElementReadable(long identifier,
390
399
void toNative (
391
400
@ CachedLibrary ("this" ) PythonNativeWrapperLibrary lib ,
392
401
@ Exclusive @ Cached InvalidateNativeObjectsAllManagedNode invalidateNode ) {
402
+ if (!PythonContext .get (lib ).isNativeAccessAllowed ()) {
403
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
404
+ throw new RuntimeException (ErrorMessages .NATIVE_ACCESS_NOT_ALLOWED );
405
+ }
393
406
invalidateNode .execute ();
394
407
if (!lib .isNative (this )) {
395
408
int [] data = getIntArray (lib );
396
409
setNativePointer (intArrayToNativeInt32 (data ));
397
410
}
398
411
}
399
-
400
412
}
401
413
}
0 commit comments