-
Notifications
You must be signed in to change notification settings - Fork 134
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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]); | ||
|
Contributor
Author
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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,20 @@ term stacktrace_create_raw(Context *ctx, Module *mod, int current_offset, term e | |
| return exception_class; | ||
| } | ||
|
|
||
| bool stacktrace_is_built(term stack_info_or_stacktrace) | ||
| { | ||
| UNUSED(stack_info_or_stacktrace); | ||
| return true; | ||
| } | ||
|
|
||
| term stacktrace_ensure_built(Context *ctx, term *stack_info_or_stacktrace, uint32_t live) | ||
| { | ||
| UNUSED(ctx); | ||
| UNUSED(stack_info_or_stacktrace); | ||
| UNUSED(live); | ||
| return UNDEFINED_ATOM; | ||
| } | ||
|
|
||
| term stacktrace_build(Context *ctx, term *stack_info, uint32_t live) | ||
| { | ||
| UNUSED(ctx); | ||
|
|
@@ -257,6 +271,27 @@ static term find_path_created(const void *key, struct ModulePathPair *module_pat | |
| return term_invalid_term(); | ||
| } | ||
|
|
||
| bool stacktrace_is_built(term stack_info_or_stacktrace) | ||
| { | ||
| TERM_DEBUG_ASSERT(term_is_tuple(stack_info_or_stacktrace) || term_is_list(stack_info_or_stacktrace)); | ||
| if (term_is_tuple(stack_info_or_stacktrace)) { | ||
| return false; | ||
| } else if (term_is_list(stack_info_or_stacktrace)) { | ||
| return true; | ||
| } else { | ||
| fprintf(stderr, "Error: invalid stack_info or stacktrace passed to stacktrace_is_built"); | ||
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| term stacktrace_ensure_built(Context *ctx, term *stack_info_or_stacktrace, uint32_t live) | ||
| { | ||
| if (stacktrace_is_built(*stack_info_or_stacktrace)) { | ||
| return *stack_info_or_stacktrace; | ||
| } | ||
| return stacktrace_build(ctx, stack_info_or_stacktrace, live); | ||
| } | ||
|
|
||
| term stacktrace_build(Context *ctx, term *stack_info, uint32_t live) | ||
| { | ||
| GlobalContext *glb = ctx->global; | ||
|
|
||
| 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"). |
| 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. |
| 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. |
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