Skip to content

Commit 1088631

Browse files
committed
Break exception edges when calling C API functions.
1 parent fe6ca92 commit 1088631

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,14 +2459,12 @@ public final Object call(NativeCAPISymbol symbol, Object... args) {
24592459
static Object doIt(NativeCAPISymbol name, Object[] args,
24602460
@Cached ImportCExtSymbolNode importCExtSymbolNode,
24612461
@CachedContext(PythonLanguage.class) PythonContext context,
2462-
@CachedLibrary(limit = "1") InteropLibrary interopLibrary,
2463-
@Cached PRaiseNode raiseNode) {
2462+
@CachedLibrary(limit = "1") InteropLibrary interopLibrary) {
24642463
try {
24652464
return interopLibrary.execute(importCExtSymbolNode.execute(context.getCApiContext(), name), args);
2466-
} catch (UnsupportedTypeException | ArityException e) {
2467-
throw raiseNode.raise(PythonBuiltinClassType.TypeError, e);
2468-
} catch (UnsupportedMessageException e) {
2469-
throw raiseNode.raise(PythonBuiltinClassType.TypeError, ErrorMessages.CAPI_SYM_NOT_CALLABLE, name);
2465+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
2466+
// consider these exceptions to be fatal internal errors
2467+
throw CompilerDirectives.shouldNotReachHere(e);
24702468
}
24712469
}
24722470

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,51 +152,51 @@ public abstract static class ImportCExtSymbolNode extends PNodeWithContext {
152152
public abstract Object execute(CExtContext nativeContext, NativeCExtSymbol symbol);
153153

154154
@Specialization(guards = "cachedSymbol == symbol", limit = "1", assumptions = "singleContextAssumption()")
155-
@SuppressWarnings("unused")
156-
static Object doSymbolCached(CExtContext nativeContext, NativeCExtSymbol symbol,
157-
@Cached("symbol") NativeCExtSymbol cachedSymbol,
158-
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
159-
@Cached("importCAPISymbolUncached(nativeContext, raiseNode, symbol)") Object llvmSymbol) {
155+
static Object doSymbolCached(@SuppressWarnings("unused") CExtContext nativeContext, @SuppressWarnings("unused") NativeCExtSymbol symbol,
156+
@Cached("symbol") @SuppressWarnings("unused") NativeCExtSymbol cachedSymbol,
157+
@Cached("importCAPISymbolUncached(nativeContext, symbol)") Object llvmSymbol) {
160158
return llvmSymbol;
161159
}
162160

163161
// n.b. if 'singleContextAssumption' is valid, we may also cache the native context
164162
@Specialization(guards = "nativeContext == cachedNativeContext", limit = "1", //
165163
assumptions = "singleContextAssumption()", //
166164
replaces = "doSymbolCached")
167-
@SuppressWarnings("unused")
168-
static Object doWithSymbolCacheSingleContext(CExtContext nativeContext, NativeCExtSymbol symbol,
165+
Object doWithSymbolCacheSingleContext(@SuppressWarnings("unused") CExtContext nativeContext, NativeCExtSymbol symbol,
169166
@Cached("nativeContext") CExtContext cachedNativeContext,
170167
@Cached("nativeContext.getSymbolCache()") DynamicObject cachedSymbolCache,
171-
@CachedLibrary("cachedSymbolCache") DynamicObjectLibrary dynamicObjectLib,
172-
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
173-
@Cached("importCAPISymbolUncached(nativeContext, raiseNode, symbol)") Object sym) {
174-
return doWithSymbolCache(cachedNativeContext, symbol, cachedSymbolCache, dynamicObjectLib, raiseNode);
168+
@CachedLibrary("cachedSymbolCache") DynamicObjectLibrary dynamicObjectLib) {
169+
return doWithSymbolCache(cachedNativeContext, symbol, cachedSymbolCache, dynamicObjectLib);
175170
}
176171

177172
@Specialization(replaces = {"doSymbolCached", "doWithSymbolCacheSingleContext"}, limit = "1")
178-
static Object doWithSymbolCache(CExtContext nativeContext, NativeCExtSymbol symbol,
173+
Object doWithSymbolCache(CExtContext nativeContext, NativeCExtSymbol symbol,
179174
@Bind("nativeContext.getSymbolCache()") DynamicObject symbolCache,
180-
@CachedLibrary("symbolCache") DynamicObjectLibrary dynamicObjectLib,
181-
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
175+
@CachedLibrary("symbolCache") DynamicObjectLibrary dynamicObjectLib) {
182176
Object nativeSymbol = dynamicObjectLib.getOrDefault(symbolCache, symbol, PNone.NO_VALUE);
183177
if (nativeSymbol == PNone.NO_VALUE) {
184-
CompilerDirectives.transferToInterpreter();
185-
nativeSymbol = importCAPISymbolUncached(nativeContext, raiseNode, symbol);
186-
dynamicObjectLib.put(symbolCache, symbol, nativeSymbol);
178+
nativeSymbol = importCAPISymbolUncached(nativeContext, symbol, symbolCache, dynamicObjectLib);
187179
}
188180
return nativeSymbol;
189181
}
190182

191-
protected static Object importCAPISymbolUncached(CExtContext nativeContext, PRaiseNode raiseNode, NativeCExtSymbol symbol) {
183+
protected Object importCAPISymbolUncached(CExtContext nativeContext, NativeCExtSymbol symbol) {
184+
CompilerAsserts.neverPartOfCompilation();
185+
return importCAPISymbolUncached(nativeContext, symbol, nativeContext.getSymbolCache(), DynamicObjectLibrary.getUncached());
186+
}
187+
188+
@TruffleBoundary
189+
protected Object importCAPISymbolUncached(CExtContext nativeContext, NativeCExtSymbol symbol, DynamicObject symbolCache, DynamicObjectLibrary dynamicObjectLib) {
192190
Object llvmLibrary = nativeContext.getLLVMLibrary();
193191
String name = symbol.getName();
194192
try {
195-
return InteropLibrary.getUncached().readMember(llvmLibrary, name);
193+
Object nativeSymbol = InteropLibrary.getUncached().readMember(llvmLibrary, name);
194+
dynamicObjectLib.put(symbolCache, symbol, nativeSymbol);
195+
return nativeSymbol;
196196
} catch (UnknownIdentifierException e) {
197-
throw raiseNode.raise(PythonBuiltinClassType.SystemError, ErrorMessages.INVALID_CAPI_FUNC, name);
197+
throw PRaiseNode.raiseUncached(this, PythonBuiltinClassType.SystemError, ErrorMessages.INVALID_CAPI_FUNC, name);
198198
} catch (UnsupportedMessageException e) {
199-
throw raiseNode.raise(PythonBuiltinClassType.SystemError, ErrorMessages.CORRUPTED_CAPI_LIB_OBJ, llvmLibrary);
199+
throw PRaiseNode.raiseUncached(this, PythonBuiltinClassType.SystemError, ErrorMessages.CORRUPTED_CAPI_LIB_OBJ, llvmLibrary);
200200
}
201201
}
202202
}

0 commit comments

Comments
 (0)