Skip to content

Commit

Permalink
Attempt to fix unwinder errors from lua JIT stacks that aren't on top
Browse files Browse the repository at this point in the history
  • Loading branch information
gnurizen committed Aug 7, 2024
1 parent 000ef13 commit 21ab75a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bpf/unwinders/lua.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ static __always_inline lua_State *fish_for_L(u64 sp, int cur_L_offset) {
struct ngx_http_lua_co_ctx_s *lua_co_ctx;
lua_State *Lmaybe = NULL;

for (int i = 1; i <= 4; i++) {
for (int i = 1; i <= 6; i++) {
bpf_probe_read_user(&lua_co_ctx, sizeof(lua_co_ctx), (void **)sp - i);
if (lua_co_ctx == NULL) {
continue;
Expand Down
21 changes: 21 additions & 0 deletions bpf/unwinders/native.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ SEC("perf_event")
int native_unwind(struct bpf_perf_event_data *ctx) {
u64 pid_tgid = bpf_get_current_pid_tgid();
int per_process_id = pid_tgid >> 32;
int per_thread_id = pid_tgid;

int err = 0;
bool reached_bottom_of_stack = false;
Expand Down Expand Up @@ -932,6 +933,26 @@ int native_unwind(struct bpf_perf_event_data *ctx) {

if (unwind_table_result == FIND_UNWIND_JITTED) {
LOG("[debug] Unwinding JITed stacks");
if (unwind_state->unwinder_type == RUNTIME_UNWINDER_TYPE_LUA) {
LOG("[info] lua: JIT pc 0x%llx encountered, tail calling lua unwinder", unwind_state->ip);
lua_uprobe_state_t *ls = bpf_map_lookup_elem(&tid_to_lua_state, &per_thread_id);
if (ls != NULL && ls->sp != 0) {
LOG("[info] lua: saved lua entry state found, tail calling native unwinder from there");
// copy saved lua state to native unwind state and tail call native unwinder
unwind_state->bp = ls->bp;
unwind_state->sp = ls->sp;
unwind_state->ip = ls->ip;
ls->bp = 0;
ls->sp = 0;
ls->ip = 0;
// remove luajit frame
unwind_state->stack.len = 0;
bpf_tail_call(ctx, &programs, NATIVE_UNWINDER_PROGRAM_ID);
} else {
LOG("[info] lua: no saved lua entry state found, JIT'd stacks won't be connected to native stack, enable LUA uprobes");
bpf_tail_call(ctx, &programs, LUA_UNWINDER_PROGRAM_ID);
}
}

unwind_state->unwinding_jit = true;
if (dwarf_to_jit) {
Expand Down

0 comments on commit 21ab75a

Please sign in to comment.