/* * GCC can occasionally decide to realign the stack pointer and change * the offset of the stack frame in the prologue of a function called * by head/entry code. Examples: * * <start_secondary>: * push %edi * lea 0x8(%esp),%edi * and $0xfffffff8,%esp * pushl -0x4(%edi) * push %ebp * mov %esp,%ebp * * <x86_64_start_kernel>: * lea 0x8(%rsp),%r10 * and $0xfffffffffffffff0,%rsp * pushq -0x8(%r10) * push %rbp * mov %rsp,%rbp * * After aligning the stack, it pushes a duplicate copy of the return * address before pushing the frame pointer.
*/ return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
}
/* * When unwinding from an ftrace handler of a function called by entry * code, the stack layout of the last frame is: * * bp * parent ret addr * bp * function ret addr * parent ret addr * pt_regs * -----------------
*/ return (state->bp == last_ftrace_bp &&
*state->bp == *(state->bp + 2) &&
*(state->bp + 1) == *(state->bp + 4));
}
/* * This determines if the frame pointer actually contains an encoded pointer to * pt_regs on the stack. See ENCODE_FRAME_POINTER.
*/ #ifdef CONFIG_X86_64 staticstruct pt_regs *decode_frame_pointer(unsignedlong *bp)
{ unsignedlong regs = (unsignedlong)bp;
/* * While walking the stack, KMSAN may stomp on stale locals from other * functions that were marked as uninitialized upon function exit, and * now hold the call frame information for the current function (e.g. the frame * pointer). Because KMSAN does not specifically mark call frames as * initialized, false positive reports are possible. To prevent such reports, * we mark the functions scanning the stack (here and below) with * __no_kmsan_checks.
*/
__no_kmsan_checks staticbool update_stack_state(struct unwind_state *state, unsignedlong *next_bp)
{ struct stack_info *info = &state->stack_info; enum stack_type prev_type = info->type; struct pt_regs *regs; unsignedlong *frame, *prev_frame_end, *addr_p, addr;
size_t len;
/* Is the next frame pointer an encoded pointer to pt_regs? */
regs = decode_frame_pointer(next_bp); if (regs) {
frame = (unsignedlong *)regs;
len = sizeof(*regs);
state->got_irq = true;
} else {
frame = next_bp;
len = FRAME_HEADER_SIZE;
}
/* * If the next bp isn't on the current stack, switch to the next one. * * We may have to traverse multiple stacks to deal with the possibility * that info->next_sp could point to an empty stack and the next bp * could be on a subsequent stack.
*/ while (!on_stack(info, frame, len)) if (get_stack_info(info->next_sp, state->task, info,
&state->stack_mask)) returnfalse;
/* Make sure it only unwinds up and doesn't overlap the prev frame: */ if (state->orig_sp && state->stack_info.type == prev_type &&
frame < prev_frame_end) returnfalse;
/* Move state to the next frame: */ if (regs) {
state->regs = regs;
state->bp = NULL;
} else {
state->bp = next_bp;
state->regs = NULL;
}
/* Save the return address: */ if (state->regs && user_mode(state->regs))
state->ip = 0; else {
addr_p = unwind_get_return_address_ptr(state);
addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
state->ip = unwind_recover_ret_addr(state, addr, addr_p);
}
/* Save the original stack pointer for unwind_dump(): */ if (!state->orig_sp)
state->orig_sp = frame;
/* Have we reached the end? */ if (state->regs && user_mode(state->regs)) goto the_end;
if (is_last_task_frame(state)) {
regs = task_pt_regs(state->task);
/* * kthreads (other than the boot CPU's idle thread) have some * partial regs at the end of their stack which were placed * there by copy_thread(). But the regs don't have any * useful information, so we can skip them. * * This user_mode() check is slightly broader than a PF_KTHREAD * check because it also catches the awkward situation where a * newly forked kthread transitions into a user task by calling * kernel_execve(), which eventually clears PF_KTHREAD.
*/ if (!user_mode(regs)) goto the_end;
/* * We're almost at the end, but not quite: there's still the * syscall regs frame. Entry code doesn't encode the regs * pointer for syscalls, so we have to set it manually.
*/
state->regs = regs;
state->bp = NULL;
state->ip = 0; returntrue;
}
/* Get the next frame pointer: */ if (state->next_bp) {
next_bp = state->next_bp;
state->next_bp = NULL;
} elseif (state->regs) {
next_bp = (unsignedlong *)state->regs->bp;
} else {
next_bp = (unsignedlong *)READ_ONCE_TASK_STACK(state->task, *state->bp);
}
/* Move to the next frame if it's safe: */ if (!update_stack_state(state, next_bp)) goto bad_address;
returntrue;
bad_address:
state->error = true;
/* * When unwinding a non-current task, the task might actually be * running on another CPU, in which case it could be modifying its * stack while we're reading it. This is generally not a problem and * can be ignored as long as the caller understands that unwinding * another task will not always succeed.
*/ if (state->task != current) goto the_end;
/* * Don't warn if the unwinder got lost due to an interrupt in entry * code or in the C handler before the first frame pointer got set up:
*/ if (state->got_irq && in_entry_code(state->ip)) goto the_end; if (state->regs &&
state->regs->sp >= (unsignedlong)last_aligned_frame(state) &&
state->regs->sp < (unsignedlong)task_pt_regs(state->task)) goto the_end;
/* * There are some known frame pointer issues on 32-bit. Disable * unwinder warnings on 32-bit until it gets objtool support.
*/ if (IS_ENABLED(CONFIG_X86_32)) goto the_end;
if (state->task != current) goto the_end;
if (state->regs) {
printk_deferred_once(KERN_WARNING "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
state->regs, state->task->comm,
state->task->pid, next_bp);
unwind_dump(state);
} else {
printk_deferred_once(KERN_WARNING "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
state->bp, state->task->comm,
state->task->pid, next_bp);
unwind_dump(state);
}
the_end:
state->stack_info.type = STACK_TYPE_UNKNOWN; returnfalse;
}
EXPORT_SYMBOL_GPL(unwind_next_frame);
/* Don't even attempt to start from user mode regs: */ if (regs && user_mode(regs)) {
state->stack_info.type = STACK_TYPE_UNKNOWN; return;
}
bp = get_frame_pointer(task, regs);
/* * If we crash with IP==0, the last successfully executed instruction * was probably an indirect function call with a NULL function pointer. * That means that SP points into the middle of an incomplete frame: * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we * would have written a frame pointer if we hadn't crashed. * Pretend that the frame is complete and that BP points to it, but save * the real BP so that we can use it when looking for the next frame.
*/ if (regs && regs->ip == 0 && (unsignedlong *)regs->sp >= first_frame) {
state->next_bp = bp;
bp = ((unsignedlong *)regs->sp) - 1;
}
/* Initialize stack info and make sure the frame data is accessible: */
get_stack_info(bp, state->task, &state->stack_info,
&state->stack_mask);
update_stack_state(state, bp);
/* * The caller can provide the address of the first frame directly * (first_frame) or indirectly (regs->sp) to indicate which stack frame * to start unwinding at. Skip ahead until we reach it.
*/ while (!unwind_done(state) &&
(!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
(state->next_bp == NULL && state->bp < first_frame)))
unwind_next_frame(state);
}
EXPORT_SYMBOL_GPL(__unwind_start);
Messung V0.5
¤ Dauer der Verarbeitung: 0.24 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.