Skip to content

Commit

Permalink
[GR-57292] TruffleStackTrace.fillIn doesn't work during context pre-i…
Browse files Browse the repository at this point in the history
…nit.

PullRequest: graal/18592
  • Loading branch information
tzezula committed Aug 17, 2024
2 parents a6157ee + 87233ce commit 3149f62
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -86,6 +87,10 @@
import java.util.stream.Collectors;

import com.oracle.truffle.api.InternalResource;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.test.ReflectionUtils;
import org.graalvm.collections.Pair;
import org.graalvm.options.OptionCategory;
Expand Down Expand Up @@ -2907,6 +2912,44 @@ public void testPatchNotCalledOnNonAllowedLanguage() throws ReflectiveOperationE
}
}

@Test
public void testGR57292() throws Exception {
String message = "Test exception";
BaseLanguage.registerAction(ContextPreInitializationTestFirstLanguage.class, ActionKind.ON_INITIALIZE_CONTEXT, (env) -> {
if (env.getContext().getParent() == null) {
try (TruffleContext innerContext = env.newInnerContextBuilder(FIRST).build()) {
try {
innerContext.evalPublic(null, com.oracle.truffle.api.source.Source.newBuilder(FIRST, "", "test").build());
fail("Should not reach here.");
} catch (Exception e) {
InteropLibrary exceptions = InteropLibrary.getUncached();
assertTrue(exceptions.isException(e));
try {
assertEquals(message, exceptions.asString(exceptions.getExceptionMessage(e)));
} catch (UnsupportedMessageException um) {
throw CompilerDirectives.shouldNotReachHere(um);
}
}
}
} else {
TruffleExceptionImpl truffleException = new TruffleExceptionImpl(message);
TruffleStackTrace.fillIn(truffleException);
throw truffleException;
}
});
setPatchable(FIRST);
doContextPreinitialize(FIRST);
Context.create(FIRST).close();
}

@SuppressWarnings("serial")
private static final class TruffleExceptionImpl extends AbstractTruffleException {

TruffleExceptionImpl(String message) {
super(message);
}
}

private static boolean executedWithXCompOptions() {
Properties props = System.getProperties();
return props.containsKey("polyglot.engine.CompileImmediately") || props.containsKey("polyglot.engine.BackgroundCompilation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Objects;

import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractHostLanguageService;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.Frame;
Expand Down Expand Up @@ -270,7 +271,12 @@ public static TruffleStackTrace fillIn(Throwable throwable) {

private static boolean isHostException(Throwable throwable) {
Object polyglotEngine = LanguageAccessor.ENGINE.getCurrentPolyglotEngine();
return polyglotEngine != null && LanguageAccessor.ENGINE.getHostService(polyglotEngine).isHostException(throwable);
if (polyglotEngine == null) {
return false;
}
AbstractHostLanguageService hostService = LanguageAccessor.ENGINE.getHostService(polyglotEngine);
// hostService is null during context pre-initialization
return hostService != null && hostService.isHostException(throwable);
}

private static final class TracebackElement {
Expand Down

0 comments on commit 3149f62

Please sign in to comment.