Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report AbstractTruffleException as a Panic #5794

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.polyglot;

import com.oracle.truffle.api.exception.AbstractTruffleException;
import org.graalvm.polyglot.PolyglotException;

public final class HostEnsoUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.frame.FrameInstanceVisitor;
import com.oracle.truffle.api.frame.VirtualFrame;
Expand Down Expand Up @@ -244,13 +245,13 @@ public void onReturnExceptional(VirtualFrame frame, Throwable exception) {
} else if (exception instanceof PanicException) {
PanicException panicException = (PanicException) exception;
onReturnValue(frame, new PanicSentinel(panicException, context.getInstrumentedNode()));
} else if (exception instanceof PanicSentinel) {
} else if (exception instanceof AbstractTruffleException) {
onReturnValue(frame, exception);
}
}

private void onExpressionReturn(Object result, Node node, EventContext context) throws ThreadDeath {
boolean isPanic = result instanceof PanicSentinel;
boolean isPanic = result instanceof AbstractTruffleException;
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
UUID nodeId = ((ExpressionNode) node).getId();

String resultType = typeOf(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,74 @@ class RuntimeErrorsTest
context.consumeOut shouldEqual List("3")
}

it should "send updates when NPE is resolved in method" in {
val contextId = UUID.randomUUID()
val requestId = UUID.randomUUID()
val moduleName = "Enso_Test.Test.Main"
val metadata = new Metadata
val xId = metadata.addItem(146, 3, "aaa")

val code =
"""from Standard.Base import all
|polyglot java import java.lang.NullPointerException
|
|foo =
| Panic.throw NullPointerException.new
|
|main =
| x = foo
| x
|""".stripMargin.linesIterator.mkString("\n")
val contents = metadata.appendToCode(code)
val mainFile = context.writeMain(contents)

metadata.assertInCode(xId, code, "foo")

// create context
context.send(Api.Request(requestId, Api.CreateContextRequest(contextId)))
context.receive shouldEqual Some(
Api.Response(requestId, Api.CreateContextResponse(contextId))
)

// Open the new file
context.send(
Api.Request(Api.OpenFileNotification(mainFile, contents))
)
context.receiveNone shouldEqual None

// push main
context.send(
Api.Request(
requestId,
Api.PushContextRequest(
contextId,
Api.StackItem.ExplicitCall(
Api.MethodPointer(moduleName, "Enso_Test.Test.Main", "main"),
None,
Vector()
)
)
)
)
context.receiveNIgnorePendingExpressionUpdates(
3
) should contain theSameElementsAs Seq(
Api.Response(requestId, Api.PushContextResponse(contextId)),
TestMessages.panic(
contextId,
xId,
Api.MethodPointer(moduleName, moduleName, "foo"),
Api.ExpressionUpdate.Payload.Panic(
"java.lang.NullPointerException",
Seq(xId)
),
None
),
context.executionComplete(contextId)
)
context.consumeOut shouldEqual Seq()
}

it should "send updates when dataflow error is resolved in method" in {
val contextId = UUID.randomUUID()
val requestId = UUID.randomUUID()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ object ProgramExecutionSupport {
Api.ExpressionUpdate.Payload.DataflowError(
ErrorResolver.getStackTrace(error).flatMap(_.expressionId)
)
case panic: AbstractTruffleException =>
Api.ExpressionUpdate.Payload
.Panic(
VisualizationResult.findExceptionMessage(panic),
ErrorResolver.getStackTrace(panic).flatMap(_.expressionId)
)
case withWarnings: WithWarnings =>
val warningsCount = withWarnings.getWarningsCount
val warning =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ object TestMessages {
): Api.Response =
panicBuilder(contextId, expressionId, Some(methodPointer), payload, builtin)

/** Create a panic update response.
*
* @param contextId an identifier of the context
* @param expressionId an identifier of the expression
* @param methodPointer a pointer to the method definition
* @param payload the error payload
* @param builtin the type to use
* @return the expression update response
*/
def panic(
contextId: UUID,
expressionId: UUID,
methodPointer: Api.MethodPointer,
payload: Api.ExpressionUpdate.Payload,
builtin: Option[String]
): Api.Response =
panicBuilder(contextId, expressionId, Some(methodPointer), payload, builtin)

/** Create a panic update response.
*
* @param contextId an identifier of the context
Expand All @@ -308,16 +326,30 @@ object TestMessages {
methodPointer: Option[Api.MethodPointer],
payload: Api.ExpressionUpdate.Payload,
builtin: Boolean
): Api.Response = panicBuilder(
contextId,
expressionId,
methodPointer,
payload,
Some(
if (builtin) ConstantsGen.PANIC_BUILTIN else ConstantsGen.PANIC
)
)

private def panicBuilder(
contextId: UUID,
expressionId: UUID,
methodPointer: Option[Api.MethodPointer],
payload: Api.ExpressionUpdate.Payload,
builtin: Option[String]
): Api.Response =
Api.Response(
Api.ExpressionUpdates(
contextId,
Set(
Api.ExpressionUpdate(
expressionId,
Some(
if (builtin) ConstantsGen.PANIC_BUILTIN else ConstantsGen.PANIC
),
builtin,
methodPointer,
Vector(Api.ProfilingInfo.ExecutionTime(0)),
false,
Expand Down