-
Notifications
You must be signed in to change notification settings - Fork 130
Generate proper stacktrace when calling reraise in Elixir #1804
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
Open
mat-hek
wants to merge
4
commits into
atomvm:main
Choose a base branch
from
mat-hek:mf/fix-reraise-stacktrace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1170,7 +1170,7 @@ static void destroy_extended_registers(Context *ctx, unsigned int live) | |
| #define PROCESS_MAYBE_TRAP_RETURN_VALUE(return_value) \ | ||
| if (term_is_invalid_term(return_value)) { \ | ||
| if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ | ||
| HANDLE_ERROR(); \ | ||
| HANDLE_ERROR_MAYBE_STACKTRACE(); \ | ||
| } else { \ | ||
| SCHEDULE_WAIT(mod, pc); \ | ||
| } \ | ||
|
|
@@ -1180,7 +1180,7 @@ static void destroy_extended_registers(Context *ctx, unsigned int live) | |
| if (term_is_invalid_term(return_value)) { \ | ||
| if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ | ||
| pc = rest_pc; \ | ||
| HANDLE_ERROR(); \ | ||
| HANDLE_ERROR_MAYBE_STACKTRACE(); \ | ||
| } else { \ | ||
| SCHEDULE_WAIT(mod, pc); \ | ||
| } \ | ||
|
|
@@ -1189,7 +1189,7 @@ static void destroy_extended_registers(Context *ctx, unsigned int live) | |
| #define PROCESS_MAYBE_TRAP_RETURN_VALUE_LAST(return_value) \ | ||
| if (term_is_invalid_term(return_value)) { \ | ||
| if (UNLIKELY(!context_get_flags(ctx, Trap))) { \ | ||
| HANDLE_ERROR(); \ | ||
| HANDLE_ERROR_MAYBE_STACKTRACE(); \ | ||
| } else { \ | ||
| DO_RETURN(); \ | ||
| SCHEDULE_WAIT(mod, pc); \ | ||
|
|
@@ -1216,6 +1216,10 @@ static void destroy_extended_registers(Context *ctx, unsigned int live) | |
| x_regs[2] = stacktrace_create_raw(ctx, mod, pc - code, x_regs[0]); \ | ||
| goto handle_error; | ||
|
|
||
| #define HANDLE_ERROR_MAYBE_STACKTRACE() \ | ||
| x_regs[2] = x_regs[2] == term_nil() ? stacktrace_create_raw(ctx, mod, pc - code, x_regs[0]) : x_regs[2]; \ | ||
| goto handle_error; | ||
|
|
||
| #define VERIFY_IS_INTEGER(t, opcode_name, label) \ | ||
| if (UNLIKELY(!term_is_integer(t))) { \ | ||
| TRACE(opcode_name ": " #t " is not an integer\n"); \ | ||
|
|
@@ -1411,6 +1415,10 @@ static bool sort_kv_pairs(struct kv_pair *kv, int size, GlobalContext *global) | |
| return true; | ||
| } | ||
|
|
||
| static bool is_exception_class(term t) { | ||
| return t == ERROR_ATOM || t == LOWERCASE_EXIT_ATOM || t == THROW_ATOM; | ||
| } | ||
|
|
||
| static term maybe_alloc_boxed_integer_fragment(Context *ctx, avm_int64_t value) | ||
| { | ||
| #if BOXED_TERMS_REQUIRED_FOR_INT64 > 1 | ||
|
|
@@ -3741,9 +3749,19 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb) | |
|
|
||
| #ifdef IMPL_EXECUTE_LOOP | ||
| TRACE("raise/2 stacktrace=0x%lx exc_value=0x%lx\n", stacktrace, exc_value); | ||
| x_regs[0] = stacktrace_exception_class(stacktrace); | ||
| x_regs[1] = exc_value; | ||
| x_regs[2] = stacktrace_create_raw(ctx, mod, saved_pc - code, x_regs[0]); | ||
| if (stacktrace_is_built(stacktrace)) { | ||
| // FIXME: This is a temporary solution as in some niche cases of reraise the x_regs[0] is | ||
| // overwritten and it does not represent exception class | ||
| if (!is_exception_class(x_regs[0])) { | ||
| x_regs[0] = ERROR_ATOM; | ||
| } | ||
| x_regs[1] = exc_value; | ||
| x_regs[2] = stacktrace; | ||
| } else { | ||
| x_regs[0] = stacktrace_exception_class(stacktrace); | ||
| x_regs[1] = exc_value; | ||
| x_regs[2] = stacktrace_create_raw(ctx, mod, saved_pc - code, x_regs[0]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I don't understand why we create a stacktrace, while it's already created and we use it to retrieve the exception class and value 🤔 |
||
| } | ||
| goto handle_error; | ||
| #endif | ||
|
|
||
|
|
@@ -3780,7 +3798,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb) | |
| break; | ||
|
|
||
| case ERROR_ATOM_INDEX: { | ||
| x_regs[2] = stacktrace_build(ctx, &x_regs[2], 3); | ||
| x_regs[2] = stacktrace_ensure_built(ctx, &x_regs[2], 3); | ||
| // MEMORY_CAN_SHRINK because catch_end is classified as gc in beam_ssa_codegen.erl | ||
| if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2) * 2, 2, x_regs + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) { | ||
| RAISE_ERROR(OUT_OF_MEMORY_ATOM); | ||
|
|
@@ -6249,7 +6267,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb) | |
|
|
||
| #ifdef IMPL_EXECUTE_LOOP | ||
|
|
||
| x_regs[0] = stacktrace_build(ctx, &x_regs[0], 1); | ||
| x_regs[0] = stacktrace_ensure_built(ctx, &x_regs[0], 1); | ||
|
|
||
| #endif | ||
| break; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| -module(reraise_raiser). | ||
|
|
||
| -export([raise_error/0]). | ||
|
|
||
| raise_error() -> | ||
| error("foo"). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| -module(reraise_reraiser). | ||
|
|
||
| -export([reraise_error/0]). | ||
|
|
||
| reraise_error() -> | ||
| try | ||
| reraise_raiser:raise_error() | ||
| catch | ||
| Class:Reason:Stacktrace -> | ||
| erlang:error(erlang:raise(Class, Reason, Stacktrace)) | ||
| end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| -module(test_reraise). | ||
|
|
||
| -export([start/0]). | ||
|
|
||
| start() -> | ||
| try | ||
| reraise_reraiser:reraise_error() | ||
| catch | ||
| Class:Reason:Stacktrace -> | ||
| error = Class, | ||
| "foo" = Reason, | ||
|
|
||
| [ | ||
| {reraise_raiser, raise_error, 0, _Meta1}, | ||
| {reraise_reraiser, reraise_error, 0, _Meta2} | ||
| | _Rest | ||
| ] = Stacktrace | ||
| end, | ||
| 0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As described in the comment, the exception class may not be in
x_regs[0]at this point, and then the error/exception is effectively lost. To avoid that, we set the exception class toerror, but it may have beenexitorthrowas well. We can't get the exception class from the stacktrace, as it's already built, so I'm not sure where to get it from. Generally, keeping the exception class as part of the raw stacktrace feels a bit hacky to me, but I'm not sure where to keep it instead @bettio @pguyot