github.com/rafaeltorres324/go/src@v0.0.0-20210519164414-9fdf653a9838/runtime/signal_windows.go (about) 1 // Copyright 2011 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 ( 8 "unsafe" 9 ) 10 11 func disableWER() { 12 // do not display Windows Error Reporting dialogue 13 const ( 14 SEM_FAILCRITICALERRORS = 0x0001 15 SEM_NOGPFAULTERRORBOX = 0x0002 16 SEM_NOALIGNMENTFAULTEXCEPT = 0x0004 17 SEM_NOOPENFILEERRORBOX = 0x8000 18 ) 19 errormode := uint32(stdcall1(_SetErrorMode, SEM_NOGPFAULTERRORBOX)) 20 stdcall1(_SetErrorMode, uintptr(errormode)|SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX) 21 } 22 23 // in sys_windows_386.s and sys_windows_amd64.s 24 func exceptiontramp() 25 func firstcontinuetramp() 26 func lastcontinuetramp() 27 28 func initExceptionHandler() { 29 stdcall2(_AddVectoredExceptionHandler, 1, funcPC(exceptiontramp)) 30 if _AddVectoredContinueHandler == nil || GOARCH == "386" { 31 // use SetUnhandledExceptionFilter for windows-386 or 32 // if VectoredContinueHandler is unavailable. 33 // note: SetUnhandledExceptionFilter handler won't be called, if debugging. 34 stdcall1(_SetUnhandledExceptionFilter, funcPC(lastcontinuetramp)) 35 } else { 36 stdcall2(_AddVectoredContinueHandler, 1, funcPC(firstcontinuetramp)) 37 stdcall2(_AddVectoredContinueHandler, 0, funcPC(lastcontinuetramp)) 38 } 39 } 40 41 // isAbort returns true, if context r describes exception raised 42 // by calling runtime.abort function. 43 // 44 //go:nosplit 45 func isAbort(r *context) bool { 46 // In the case of an abort, the exception IP is one byte after 47 // the INT3 (this differs from UNIX OSes). 48 return isAbortPC(r.ip() - 1) 49 } 50 51 // isgoexception reports whether this exception should be translated 52 // into a Go panic. 53 // 54 // It is nosplit to avoid growing the stack in case we're aborting 55 // because of a stack overflow. 56 // 57 //go:nosplit 58 func isgoexception(info *exceptionrecord, r *context) bool { 59 // Only handle exception if executing instructions in Go binary 60 // (not Windows library code). 61 // TODO(mwhudson): needs to loop to support shared libs 62 if r.ip() < firstmoduledata.text || firstmoduledata.etext < r.ip() { 63 return false 64 } 65 66 if isAbort(r) { 67 // Never turn abort into a panic. 68 return false 69 } 70 71 // Go will only handle some exceptions. 72 switch info.exceptioncode { 73 default: 74 return false 75 case _EXCEPTION_ACCESS_VIOLATION: 76 case _EXCEPTION_INT_DIVIDE_BY_ZERO: 77 case _EXCEPTION_INT_OVERFLOW: 78 case _EXCEPTION_FLT_DENORMAL_OPERAND: 79 case _EXCEPTION_FLT_DIVIDE_BY_ZERO: 80 case _EXCEPTION_FLT_INEXACT_RESULT: 81 case _EXCEPTION_FLT_OVERFLOW: 82 case _EXCEPTION_FLT_UNDERFLOW: 83 case _EXCEPTION_BREAKPOINT: 84 } 85 return true 86 } 87 88 // Called by sigtramp from Windows VEH handler. 89 // Return value signals whether the exception has been handled (EXCEPTION_CONTINUE_EXECUTION) 90 // or should be made available to other handlers in the chain (EXCEPTION_CONTINUE_SEARCH). 91 // 92 // This is the first entry into Go code for exception handling. This 93 // is nosplit to avoid growing the stack until we've checked for 94 // _EXCEPTION_BREAKPOINT, which is raised if we overflow the g0 stack, 95 // 96 //go:nosplit 97 func exceptionhandler(info *exceptionrecord, r *context, gp *g) int32 { 98 if !isgoexception(info, r) { 99 return _EXCEPTION_CONTINUE_SEARCH 100 } 101 102 // After this point, it is safe to grow the stack. 103 104 if gp.throwsplit { 105 // We can't safely sigpanic because it may grow the 106 // stack. Let it fall through. 107 return _EXCEPTION_CONTINUE_SEARCH 108 } 109 110 // Make it look like a call to the signal func. 111 // Have to pass arguments out of band since 112 // augmenting the stack frame would break 113 // the unwinding code. 114 gp.sig = info.exceptioncode 115 gp.sigcode0 = uintptr(info.exceptioninformation[0]) 116 gp.sigcode1 = uintptr(info.exceptioninformation[1]) 117 gp.sigpc = r.ip() 118 119 // Only push runtime·sigpanic if r.ip() != 0. 120 // If r.ip() == 0, probably panicked because of a 121 // call to a nil func. Not pushing that onto sp will 122 // make the trace look like a call to runtime·sigpanic instead. 123 // (Otherwise the trace will end at runtime·sigpanic and we 124 // won't get to see who faulted.) 125 // Also don't push a sigpanic frame if the faulting PC 126 // is the entry of asyncPreempt. In this case, we suspended 127 // the thread right between the fault and the exception handler 128 // starting to run, and we have pushed an asyncPreempt call. 129 // The exception is not from asyncPreempt, so not to push a 130 // sigpanic call to make it look like that. Instead, just 131 // overwrite the PC. (See issue #35773) 132 if r.ip() != 0 && r.ip() != funcPC(asyncPreempt) { 133 sp := unsafe.Pointer(r.sp()) 134 sp = add(sp, ^(unsafe.Sizeof(uintptr(0)) - 1)) // sp-- 135 r.set_sp(uintptr(sp)) 136 switch GOARCH { 137 default: 138 panic("unsupported architecture") 139 case "386", "amd64": 140 *((*uintptr)(sp)) = r.ip() 141 case "arm": 142 *((*uintptr)(sp)) = r.lr() 143 r.set_lr(r.ip()) 144 } 145 } 146 r.set_ip(funcPC(sigpanic)) 147 return _EXCEPTION_CONTINUE_EXECUTION 148 } 149 150 // It seems Windows searches ContinueHandler's list even 151 // if ExceptionHandler returns EXCEPTION_CONTINUE_EXECUTION. 152 // firstcontinuehandler will stop that search, 153 // if exceptionhandler did the same earlier. 154 // 155 // It is nosplit for the same reason as exceptionhandler. 156 // 157 //go:nosplit 158 func firstcontinuehandler(info *exceptionrecord, r *context, gp *g) int32 { 159 if !isgoexception(info, r) { 160 return _EXCEPTION_CONTINUE_SEARCH 161 } 162 return _EXCEPTION_CONTINUE_EXECUTION 163 } 164 165 var testingWER bool 166 167 // lastcontinuehandler is reached, because runtime cannot handle 168 // current exception. lastcontinuehandler will print crash info and exit. 169 // 170 // It is nosplit for the same reason as exceptionhandler. 171 // 172 //go:nosplit 173 func lastcontinuehandler(info *exceptionrecord, r *context, gp *g) int32 { 174 if islibrary || isarchive { 175 // Go DLL/archive has been loaded in a non-go program. 176 // If the exception does not originate from go, the go runtime 177 // should not take responsibility of crashing the process. 178 return _EXCEPTION_CONTINUE_SEARCH 179 } 180 if testingWER { 181 return _EXCEPTION_CONTINUE_SEARCH 182 } 183 184 _g_ := getg() 185 186 if panicking != 0 { // traceback already printed 187 exit(2) 188 } 189 panicking = 1 190 191 // In case we're handling a g0 stack overflow, blow away the 192 // g0 stack bounds so we have room to print the traceback. If 193 // this somehow overflows the stack, the OS will trap it. 194 _g_.stack.lo = 0 195 _g_.stackguard0 = _g_.stack.lo + _StackGuard 196 _g_.stackguard1 = _g_.stackguard0 197 198 print("Exception ", hex(info.exceptioncode), " ", hex(info.exceptioninformation[0]), " ", hex(info.exceptioninformation[1]), " ", hex(r.ip()), "\n") 199 200 print("PC=", hex(r.ip()), "\n") 201 if _g_.m.lockedg != 0 && _g_.m.ncgo > 0 && gp == _g_.m.g0 { 202 if iscgo { 203 print("signal arrived during external code execution\n") 204 } 205 gp = _g_.m.lockedg.ptr() 206 } 207 print("\n") 208 209 // TODO(jordanrh1): This may be needed for 386/AMD64 as well. 210 if GOARCH == "arm" { 211 _g_.m.throwing = 1 212 _g_.m.caughtsig.set(gp) 213 } 214 215 level, _, docrash := gotraceback() 216 if level > 0 { 217 tracebacktrap(r.ip(), r.sp(), r.lr(), gp) 218 tracebackothers(gp) 219 dumpregs(r) 220 } 221 222 if docrash { 223 crash() 224 } 225 226 exit(2) 227 return 0 // not reached 228 } 229 230 func sigpanic() { 231 g := getg() 232 if !canpanic(g) { 233 throw("unexpected signal during runtime execution") 234 } 235 236 switch g.sig { 237 case _EXCEPTION_ACCESS_VIOLATION: 238 if g.sigcode1 < 0x1000 { 239 panicmem() 240 } 241 if g.paniconfault { 242 panicmemAddr(g.sigcode1) 243 } 244 print("unexpected fault address ", hex(g.sigcode1), "\n") 245 throw("fault") 246 case _EXCEPTION_INT_DIVIDE_BY_ZERO: 247 panicdivide() 248 case _EXCEPTION_INT_OVERFLOW: 249 panicoverflow() 250 case _EXCEPTION_FLT_DENORMAL_OPERAND, 251 _EXCEPTION_FLT_DIVIDE_BY_ZERO, 252 _EXCEPTION_FLT_INEXACT_RESULT, 253 _EXCEPTION_FLT_OVERFLOW, 254 _EXCEPTION_FLT_UNDERFLOW: 255 panicfloat() 256 } 257 throw("fault") 258 } 259 260 var ( 261 badsignalmsg [100]byte 262 badsignallen int32 263 ) 264 265 func setBadSignalMsg() { 266 const msg = "runtime: signal received on thread not created by Go.\n" 267 for i, c := range msg { 268 badsignalmsg[i] = byte(c) 269 badsignallen++ 270 } 271 } 272 273 // Following are not implemented. 274 275 func initsig(preinit bool) { 276 } 277 278 func sigenable(sig uint32) { 279 } 280 281 func sigdisable(sig uint32) { 282 } 283 284 func sigignore(sig uint32) { 285 } 286 287 func badsignal2() 288 289 func raisebadsignal(sig uint32) { 290 badsignal2() 291 } 292 293 func signame(sig uint32) string { 294 return "" 295 } 296 297 //go:nosplit 298 func crash() { 299 // TODO: This routine should do whatever is needed 300 // to make the Windows program abort/crash as it 301 // would if Go was not intercepting signals. 302 // On Unix the routine would remove the custom signal 303 // handler and then raise a signal (like SIGABRT). 304 // Something like that should happen here. 305 // It's okay to leave this empty for now: if crash returns 306 // the ordinary exit-after-panic happens. 307 } 308 309 // gsignalStack is unused on Windows. 310 type gsignalStack struct{}