bug in p28-kill:set killed=1 on unexpected user-space traps#355
Open
manvendrarajpurohit wants to merge 1 commit intocodenet:p28-killfrom
Open
bug in p28-kill:set killed=1 on unexpected user-space traps#355manvendrarajpurohit wants to merge 1 commit intocodenet:p28-killfrom
manvendrarajpurohit wants to merge 1 commit intocodenet:p28-killfrom
Conversation
There was a problem hiding this comment.
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()’sdefault:trap handler, add a user-space path that logs the trap and setsmyproc()->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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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, thedefaultcase 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
ifis false and the whole block is skipped. Of the three checks sitting below the switch, two only fire ifmyproc()->killedis set and the third only fires on the timer interrupt — so for an unexpected user trap, none of them do anything.trap()returns,iretgoes 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 procmessage and setmyproc()->killed = 1. The existingif(myproc() && myproc()->killed && (tf->cs&3) == DPL_USER) exit();check below the switch then callsexit()on the way back to user mode. 6 new lines, no control-flow changes.Verifying
-Werror -Wall, no new warnings.ls,cat,echo) still work.ud2test program now prints the--kill procmessage and the shell comes back, instead of hanging.Scope
Same default case exists unchanged on
p28-killthroughp31-umalloc. Opening this againstp28-killsince that's the earliest branch where thekilled-enforcement machinery exists and the fix applies.