...
Run Format

Source file src/runtime/signal_darwin_amd64.go

Documentation: runtime

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import "unsafe"
     8  
     9  type sigctxt struct {
    10  	info *siginfo
    11  	ctxt unsafe.Pointer
    12  }
    13  
    14  //go:nosplit
    15  //go:nowritebarrierrec
    16  func (c *sigctxt) regs() *regs64 { return &(*ucontext)(c.ctxt).uc_mcontext.ss }
    17  
    18  func (c *sigctxt) rax() uint64 { return c.regs().rax }
    19  func (c *sigctxt) rbx() uint64 { return c.regs().rbx }
    20  func (c *sigctxt) rcx() uint64 { return c.regs().rcx }
    21  func (c *sigctxt) rdx() uint64 { return c.regs().rdx }
    22  func (c *sigctxt) rdi() uint64 { return c.regs().rdi }
    23  func (c *sigctxt) rsi() uint64 { return c.regs().rsi }
    24  func (c *sigctxt) rbp() uint64 { return c.regs().rbp }
    25  func (c *sigctxt) rsp() uint64 { return c.regs().rsp }
    26  func (c *sigctxt) r8() uint64  { return c.regs().r8 }
    27  func (c *sigctxt) r9() uint64  { return c.regs().r9 }
    28  func (c *sigctxt) r10() uint64 { return c.regs().r10 }
    29  func (c *sigctxt) r11() uint64 { return c.regs().r11 }
    30  func (c *sigctxt) r12() uint64 { return c.regs().r12 }
    31  func (c *sigctxt) r13() uint64 { return c.regs().r13 }
    32  func (c *sigctxt) r14() uint64 { return c.regs().r14 }
    33  func (c *sigctxt) r15() uint64 { return c.regs().r15 }
    34  
    35  //go:nosplit
    36  //go:nowritebarrierrec
    37  func (c *sigctxt) rip() uint64 { return c.regs().rip }
    38  
    39  func (c *sigctxt) rflags() uint64  { return c.regs().rflags }
    40  func (c *sigctxt) cs() uint64      { return c.regs().cs }
    41  func (c *sigctxt) fs() uint64      { return c.regs().fs }
    42  func (c *sigctxt) gs() uint64      { return c.regs().gs }
    43  func (c *sigctxt) sigcode() uint64 { return uint64(c.info.si_code) }
    44  func (c *sigctxt) sigaddr() uint64 { return c.info.si_addr }
    45  
    46  func (c *sigctxt) set_rip(x uint64)     { c.regs().rip = x }
    47  func (c *sigctxt) set_rsp(x uint64)     { c.regs().rsp = x }
    48  func (c *sigctxt) set_sigcode(x uint64) { c.info.si_code = int32(x) }
    49  func (c *sigctxt) set_sigaddr(x uint64) { c.info.si_addr = x }
    50  
    51  func (c *sigctxt) fixsigcode(sig uint32) {
    52  	switch sig {
    53  	case _SIGTRAP:
    54  		// OS X sets c.sigcode() == TRAP_BRKPT unconditionally for all SIGTRAPs,
    55  		// leaving no way to distinguish a breakpoint-induced SIGTRAP
    56  		// from an asynchronous signal SIGTRAP.
    57  		// They all look breakpoint-induced by default.
    58  		// Try looking at the code to see if it's a breakpoint.
    59  		// The assumption is that we're very unlikely to get an
    60  		// asynchronous SIGTRAP at just the moment that the
    61  		// PC started to point at unmapped memory.
    62  		pc := uintptr(c.rip())
    63  		// OS X will leave the pc just after the INT 3 instruction.
    64  		// INT 3 is usually 1 byte, but there is a 2-byte form.
    65  		code := (*[2]byte)(unsafe.Pointer(pc - 2))
    66  		if code[1] != 0xCC && (code[0] != 0xCD || code[1] != 3) {
    67  			// SIGTRAP on something other than INT 3.
    68  			c.set_sigcode(_SI_USER)
    69  		}
    70  
    71  	case _SIGSEGV:
    72  		// x86-64 has 48-bit virtual addresses. The top 16 bits must echo bit 47.
    73  		// The hardware delivers a different kind of fault for a malformed address
    74  		// than it does for an attempt to access a valid but unmapped address.
    75  		// OS X 10.9.2 mishandles the malformed address case, making it look like
    76  		// a user-generated signal (like someone ran kill -SEGV ourpid).
    77  		// We pass user-generated signals to os/signal, or else ignore them.
    78  		// Doing that here - and returning to the faulting code - results in an
    79  		// infinite loop. It appears the best we can do is rewrite what the kernel
    80  		// delivers into something more like the truth. The address used below
    81  		// has very little chance of being the one that caused the fault, but it is
    82  		// malformed, it is clearly not a real pointer, and if it does get printed
    83  		// in real life, people will probably search for it and find this code.
    84  		// There are no Google hits for b01dfacedebac1e or 0xb01dfacedebac1e
    85  		// as I type this comment.
    86  		if c.sigcode() == _SI_USER {
    87  			c.set_sigcode(_SI_USER + 1)
    88  			c.set_sigaddr(0xb01dfacedebac1e)
    89  		}
    90  	}
    91  }
    92  

View as plain text