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

Improve internal reporting of errors in vm/vmgen #268

Merged
merged 1 commit into from
Apr 8, 2022

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Apr 6, 2022

Reporting of guest code related errors was done in an inconsistent
manner via either stackTrace or globalReport, where stackTrace used
localReport and an injected return statement to terminate the
execution loop. stackTrace was sometimes used in nested functions,
thus not terminating execution at all.

Changes

Both the execution engine and vmgen now use exceptions for error
propagation internally, translating them into a result value at their
respective interface edges. In case of an error, the result value stores the
error report. Otherwise, it stores the actual result.

In addition, some usages of globalReport are also replaced with the
new error propagation mechanism.

Depending on whether or not a VM invocation is expected to return
something, errors reported from VM execution and/or vmgen are either
turned into a nkError node or handled directly via handleReport

Fixes #160


This implements an adjusted/simplified version of the proposal from the VM discussion thread. Error reporting should be a bit more consistent and streamlined now, making future changes to it a bit easier.

Notes for Reviewers

  • I'm not sure if I got the details regarding newError usage right
  • I was also not sure what the best approach to putting the guest stack-trace into the nkError was, so the stack-traces are simply dropped right now

@zerbina zerbina force-pushed the vm-error-handling branch 3 times, most recently from 8f68d9d to 6d66bfd Compare April 6, 2022 21:10
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is looking much improved, the more uniform handling is very welcome.

I suggested a possible way to handle putting the exception trace in the nkError node, on the phone so it's a bit light on details. I can dig in more on the weekend (slim chance earlier) if it comes up short.

The minor style remarks aren't do or die, shortly evolving the code style, eventually as it cements it can go into the linter.

compiler/sem/sem.nim Outdated Show resolved Hide resolved
proc rawExecute(c: var TCtx, start: int, sframe: StackFrameIndex): TFullReg =
assert sframe == c.sframes.high
proc rawExecute(c: var TCtx, pc: var int, tos: var StackFrameIndex): TFullReg =
## Runs the execution loop, starting in frame `tos` at program counter `pc`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@@ -0,0 +1,44 @@
## This module contains the definitions for error reporting inside
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks

compiler/vm/vmgen.nim Show resolved Hide resolved
@@ -152,25 +152,6 @@ when defined(nimHasInvariant):
of MultipleValueSetting.cincludes: copySeq(conf.cIncludes)
of MultipleValueSetting.clibs: copySeq(conf.cLibs)

proc stackTrace2(c: PCtx, report: SemReport, n: PNode) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill it, kill it! :D

# TODO: put the stack-trace into the error somehow, nothing happens
# with it right now. Maybe use report's `context`?

result = newError(config, node, errKind, rId, instLoc())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, error node now has specialized storage so it might be possible to put a report object in there, in which case creates report object to store the trace. Otherwise, serialize to a string, shove it in there like a son.

(on my phone, so hard to look things up)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, should I add the stack-trace's report ID as a nkIntLit to the error node then? Serializing it to a string, would require going through cli_reporter

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense. Adding it in as an additional argument is likely the way to go.

compiler/vm/vm.nim Outdated Show resolved Hide resolved
@zerbina zerbina force-pushed the vm-error-handling branch from 6d66bfd to 8850e11 Compare April 8, 2022 15:24
@zerbina
Copy link
Collaborator Author

zerbina commented Apr 8, 2022

Requested style adjustments are applied. In addition, the stack-trace's report id is now added to the error node as a nkIntLit.

In the future, VM execution related errors should use their own report type (e.g. VmReport)

Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bors r+

Reporting of guest code related errors was done in an inconsistent
manner via either `stackTrace` or `globalReport`, where `stackTrace` used
`localReport` and an injected `return` statement to terminate the
execution loop. `stackTrace` was sometimes used in nested functions,
thus not terminating execution at all.

## Changes
Both the execution engine and `vmgen` now use exceptions for error
propagation internally, translating them into a result value at their
respective interface edges. In case of an error, the result value stores the
error report. Otherwise, it stores the actual result.

In addition, some usages of `globalReport` are also replaced with the
 new error propagation mechanism.

Depending on whether or not a VM invocation is expected to return
something, errors reported from VM execution and/or `vmgen` are either
 turned into a `nkError` node or handled directly via `handleReport`
@bors
Copy link
Contributor

bors bot commented Apr 8, 2022

Build succeeded:

@bors bors bot merged commit acdb2ca into nim-works:devel Apr 8, 2022
@zerbina zerbina deleted the vm-error-handling branch April 8, 2022 18:07
@haxscramper haxscramper added this to the VM backend refactoring milestone Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor stackTrace in the vm implementation
3 participants