Skip to content

bug in p28-kill:set killed=1 on unexpected user-space traps#355

Open
manvendrarajpurohit wants to merge 1 commit intocodenet:p28-killfrom
manvendrarajpurohit:p28-kill
Open

bug in p28-kill:set killed=1 on unexpected user-space traps#355
manvendrarajpurohit wants to merge 1 commit intocodenet:p28-killfrom
manvendrarajpurohit:p28-kill

Conversation

@manvendrarajpurohit
Copy link
Copy Markdown

What I noticed

While working through p28-kill, I wrote a small user program that runs an illegal instruction (ud2) to see how the kernel handles it. I expected the process to get killed, since that's what this branch is about. Instead the shell just froze.

The bug

In trap.c, the default case of the trap switch only does something for kernel-mode traps:

'''
default:
if(myproc() == 0 || (tf->cs&3) == 0){
cprintf("unexpected trap %d ...");
panic("trap");
}
'''

For a user-mode trap (divide-by-zero, illegal opcode, page fault, etc.), the if is false and the whole block is skipped. Of the three checks sitting below the switch, two only fire if myproc()->killed is set and the third only fires on the timer interrupt — so for an unexpected user trap, none of them do anything. trap() returns, iret goes back to the faulting instruction, and the process spins on the same fault forever.

The fix

Match xv6-public: after the kernel-panic block, print a --kill proc message and set myproc()->killed = 1. The existing if(myproc() && myproc()->killed && (tf->cs&3) == DPL_USER) exit(); check below the switch then calls exit() on the way back to user mode. 6 new lines, no control-flow changes.

Verifying

  • Builds clean with -Werror -Wall, no new warnings.
  • Normal programs (ls, cat, echo) still work.
  • My ud2 test program now prints the --kill proc message and the shell comes back, instead of hanging.

Scope

Same default case exists unchanged on p28-kill through p31-umalloc. Opening this against p28-kill since that's the earliest branch where the killed-enforcement machinery exists and the fix applies.

Copilot AI review requested due to automatic review settings April 23, 2026 16:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a hang where unexpected user-space traps (e.g., illegal instruction) would repeatedly re-trap without terminating the offending process.

Changes:

  • In trap()’s default: trap handler, add a user-space path that logs the trap and sets myproc()->killed = 1.
  • Rely on the existing post-switch “killed in user space” check to trigger exit() on return to user mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants