...

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  //go:nosplit
    52  func (c *sigctxt) fixsigcode(sig uint32) {
    53  	switch sig {
    54  	case _SIGTRAP:
    55  		// OS X sets c.sigcode() == TRAP_BRKPT unconditionally for all SIGTRAPs,
    56  		// leaving no way to distinguish a breakpoint-induced SIGTRAP
    57  		// from an asynchronous signal SIGTRAP.
    58  		// They all look breakpoint-induced by default.
    59  		// Try looking at the code to see if it's a breakpoint.
    60  		// The assumption is that we're very unlikely to get an
    61  		// asynchronous SIGTRAP at just the moment that the
    62  		// PC started to point at unmapped memory.
    63  		pc := uintptr(c.rip())
    64  		// OS X will leave the pc just after the INT 3 instruction.
    65  		// INT 3 is usually 1 byte, but there is a 2-byte form.
    66  		code := (*[2]byte)(unsafe.Pointer(pc - 2))
    67  		if code[1] != 0xCC && (code[0] != 0xCD || code[1] != 3) {
    68  			// SIGTRAP on something other than INT 3.
    69  			c.set_sigcode(_SI_USER)
    70  		}
    71  
    72  	case _SIGSEGV:
    73  		// x86-64 has 48-bit virtual addresses. The top 16 bits must echo bit 47.
    74  		// The hardware delivers a different kind of fault for a malformed address
    75  		// than it does for an attempt to access a valid but unmapped address.
    76  		// OS X 10.9.2 mishandles the malformed address case, making it look like
    77  		// a user-generated signal (like someone ran kill -SEGV ourpid).
    78  		// We pass user-generated signals to os/signal, or else ignore them.
    79  		// Doing that here - and returning to the faulting code - results in an
    80  		// infinite loop. It appears the best we can do is rewrite what the kernel
    81  		// delivers into something more like the truth. The address used below
    82  		// has very little chance of being the one that caused the fault, but it is
    83  		// malformed, it is clearly not a real pointer, and if it does get printed
    84  		// in real life, people will probably search for it and find this code.
    85  		// There are no Google hits for b01dfacedebac1e or 0xb01dfacedebac1e
    86  		// as I type this comment.
    87  		//
    88  		// Note: if this code is removed, please consider
    89  		// enabling TestSignalForwardingGo for darwin-amd64 in
    90  		// misc/cgo/testcarchive/carchive_test.go.
    91  		if c.sigcode() == _SI_USER {
    92  			c.set_sigcode(_SI_USER + 1)
    93  			c.set_sigaddr(0xb01dfacedebac1e)
    94  		}
    95  	}
    96  }
    97  

View as plain text