Source file
src/runtime/signal_windows.go
1
2
3
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/runtime/sys"
10 "internal/runtime/syscall/windows"
11 "unsafe"
12 )
13
14 func preventErrorDialogs() {
15 errormode := stdcall(_GetErrorMode)
16 stdcall(_SetErrorMode, errormode|windows.SEM_FAILCRITICALERRORS|windows.SEM_NOGPFAULTERRORBOX|windows.SEM_NOOPENFILEERRORBOX)
17
18
19
20
21
22 var werflags uintptr
23 stdcall(_WerGetFlags, windows.CurrentProcess, uintptr(unsafe.Pointer(&werflags)))
24 stdcall(_WerSetFlags, werflags|windows.WER_FAULT_REPORTING_NO_UI)
25 }
26
27
28 func enableWER() {
29
30 errormode := stdcall(_GetErrorMode)
31 if errormode&windows.SEM_NOGPFAULTERRORBOX != 0 {
32 stdcall(_SetErrorMode, errormode^windows.SEM_NOGPFAULTERRORBOX)
33 }
34 }
35
36
37 func exceptiontramp()
38 func firstcontinuetramp()
39 func lastcontinuetramp()
40 func sehtramp()
41 func sigresume()
42
43 func initExceptionHandler() {
44 stdcall(_AddVectoredExceptionHandler, 1, abi.FuncPCABI0(exceptiontramp))
45 if GOARCH == "386" {
46
47
48 stdcall(_SetUnhandledExceptionFilter, abi.FuncPCABI0(lastcontinuetramp))
49 } else {
50 stdcall(_AddVectoredContinueHandler, 1, abi.FuncPCABI0(firstcontinuetramp))
51 stdcall(_AddVectoredContinueHandler, 0, abi.FuncPCABI0(lastcontinuetramp))
52 }
53 }
54
55
56
57
58
59 func isAbort(r *windows.Context) bool {
60 pc := r.PC()
61 if GOARCH == "386" || GOARCH == "amd64" {
62
63
64 pc--
65 }
66 return isAbortPC(pc)
67 }
68
69
70
71
72
73
74
75
76 func isgoexception(info *windows.ExceptionRecord, r *windows.Context) bool {
77
78
79
80 if r.PC() < firstmoduledata.text || firstmoduledata.etext < r.PC() {
81 return false
82 }
83
84
85 switch info.ExceptionCode {
86 default:
87 return false
88 case windows.EXCEPTION_ACCESS_VIOLATION:
89 case windows.EXCEPTION_IN_PAGE_ERROR:
90 case windows.EXCEPTION_INT_DIVIDE_BY_ZERO:
91 case windows.EXCEPTION_INT_OVERFLOW:
92 case windows.EXCEPTION_FLT_DENORMAL_OPERAND:
93 case windows.EXCEPTION_FLT_DIVIDE_BY_ZERO:
94 case windows.EXCEPTION_FLT_INEXACT_RESULT:
95 case windows.EXCEPTION_FLT_OVERFLOW:
96 case windows.EXCEPTION_FLT_UNDERFLOW:
97 case windows.EXCEPTION_BREAKPOINT:
98 case windows.EXCEPTION_ILLEGAL_INSTRUCTION:
99 }
100 return true
101 }
102
103 const (
104 callbackVEH = iota
105 callbackFirstVCH
106 callbackLastVCH
107 )
108
109
110
111
112
113
114 func sigFetchGSafe() *g
115
116 func sigFetchG() *g {
117 if GOARCH == "386" {
118 return sigFetchGSafe()
119 }
120 return getg()
121 }
122
123
124
125
126
127
128
129
130
131 func sigtrampgo(ep *windows.ExceptionPointers, kind int) int32 {
132 gp := sigFetchG()
133 if gp == nil {
134 return windows.EXCEPTION_CONTINUE_SEARCH
135 }
136
137
138
139
140 if gp != gp.m.g0 && gp.stack.lo != 0 && sys.GetCallerSP() < gp.stack.lo {
141 throw("exception dispatched below goroutine stack bottom")
142 }
143
144 var fn func(info *windows.ExceptionRecord, r *windows.Context, gp *g) int32
145 switch kind {
146 case callbackVEH:
147 fn = exceptionhandler
148 case callbackFirstVCH:
149 fn = firstcontinuehandler
150 case callbackLastVCH:
151 fn = lastcontinuehandler
152 default:
153 throw("unknown sigtramp callback")
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 var ret int32
171 if gp != gp.m.g0 {
172 systemstack(func() {
173 ret = fn(ep.Record, ep.Context, gp)
174 })
175 } else {
176 ret = fn(ep.Record, ep.Context, gp)
177 }
178 if ret == windows.EXCEPTION_CONTINUE_SEARCH {
179 return ret
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 if ep.Context.PC() == abi.FuncPCABI0(sigresume) {
194
195 return ret
196 }
197 prepareContextForSigResume(ep.Context)
198 ep.Context.SetSP(gp.m.g0.sched.sp)
199 ep.Context.SetPC(abi.FuncPCABI0(sigresume))
200 return ret
201 }
202
203
204
205
206
207
208
209
210
211 func exceptionhandler(info *windows.ExceptionRecord, r *windows.Context, gp *g) int32 {
212 if !isgoexception(info, r) {
213 return windows.EXCEPTION_CONTINUE_SEARCH
214 }
215
216 if gp.throwsplit || isAbort(r) {
217
218
219
220
221 winthrow(info, r, gp)
222 }
223
224
225
226
227
228
229
230 gp.sig = info.ExceptionCode
231 gp.sigcode0 = info.ExceptionInformation[0]
232 gp.sigcode1 = info.ExceptionInformation[1]
233 gp.sigpc = r.PC()
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 if r.PC() != 0 && r.PC() != abi.FuncPCABI0(asyncPreempt) {
249 r.PushCall(abi.FuncPCABI0(sigpanic0), r.PC())
250 } else {
251
252 r.SetPC(abi.FuncPCABI0(sigpanic0))
253 }
254 return windows.EXCEPTION_CONTINUE_EXECUTION
255 }
256
257
258
259
260
261
262 func sehhandler(_ *windows.ExceptionRecord, _ uint64, _ *windows.Context, dctxt *windows.DISPATCHER_CONTEXT) int32 {
263 g0 := getg()
264 if g0 == nil || g0.m.curg == nil {
265
266 return windows.EXCEPTION_CONTINUE_SEARCH_SEH
267 }
268
269
270
271
272
273
274
275
276
277
278 gp := g0.m.curg
279 ctxt := dctxt.Ctx()
280 var base, sp uintptr
281 for {
282 entry := stdcall(_RtlLookupFunctionEntry, ctxt.PC(), uintptr(unsafe.Pointer(&base)), 0)
283 if entry == 0 {
284 break
285 }
286 stdcall(_RtlVirtualUnwind, 0, base, ctxt.PC(), entry, uintptr(unsafe.Pointer(ctxt)), 0, uintptr(unsafe.Pointer(&sp)), 0)
287 if sp < gp.stack.lo || gp.stack.hi <= sp {
288 break
289 }
290 }
291 return windows.EXCEPTION_CONTINUE_SEARCH_SEH
292 }
293
294
295
296
297
298
299
300
301
302 func firstcontinuehandler(info *windows.ExceptionRecord, r *windows.Context, gp *g) int32 {
303 if !isgoexception(info, r) {
304 return windows.EXCEPTION_CONTINUE_SEARCH
305 }
306 return windows.EXCEPTION_CONTINUE_EXECUTION
307 }
308
309
310
311
312
313
314
315 func lastcontinuehandler(info *windows.ExceptionRecord, r *windows.Context, gp *g) int32 {
316 if islibrary || isarchive {
317
318
319
320 return windows.EXCEPTION_CONTINUE_SEARCH
321 }
322
323
324
325
326
327
328
329 if GOARCH == "arm64" && info.ExceptionCode == windows.EXCEPTION_ILLEGAL_INSTRUCTION &&
330 (r.PC() < firstmoduledata.text || firstmoduledata.etext < r.PC()) {
331 return windows.EXCEPTION_CONTINUE_SEARCH
332 }
333
334 winthrow(info, r, gp)
335 return 0
336 }
337
338
339
340
341 func winthrow(info *windows.ExceptionRecord, r *windows.Context, gp *g) {
342 g0 := getg()
343
344 if panicking.Load() != 0 {
345 exit(2)
346 }
347 panicking.Store(1)
348
349
350
351
352 g0.stack.lo = 0
353 g0.stackguard0 = g0.stack.lo + stackGuard
354 g0.stackguard1 = g0.stackguard0
355
356 print("Exception ", hex(info.ExceptionCode), " ", hex(info.ExceptionInformation[0]), " ", hex(info.ExceptionInformation[1]), " ", hex(r.PC()), "\n")
357
358 print("PC=", hex(r.PC()), "\n")
359 if g0.m.incgo && gp == g0.m.g0 && g0.m.curg != nil {
360 if iscgo {
361 print("signal arrived during external code execution\n")
362 }
363 gp = g0.m.curg
364 }
365 print("\n")
366
367 g0.m.throwing = throwTypeRuntime
368 g0.m.caughtsig.set(gp)
369
370 level, _, docrash := gotraceback()
371 if level > 0 {
372 tracebacktrap(r.PC(), r.SP(), r.LR(), gp)
373 tracebackothers(gp)
374 dumpregs(r)
375 }
376
377 if docrash {
378 dieFromException(info, r)
379 }
380
381 exit(2)
382 }
383
384 func sigpanic() {
385 gp := getg()
386 if !canpanic() {
387 throw("unexpected signal during runtime execution")
388 }
389
390 switch gp.sig {
391 case windows.EXCEPTION_ACCESS_VIOLATION, windows.EXCEPTION_IN_PAGE_ERROR:
392 if gp.sigcode1 < 0x1000 {
393 panicmem()
394 }
395 if gp.paniconfault {
396 panicmemAddr(gp.sigcode1)
397 }
398 if inUserArenaChunk(gp.sigcode1) {
399
400
401
402 print("accessed data from freed user arena ", hex(gp.sigcode1), "\n")
403 } else {
404 print("unexpected fault address ", hex(gp.sigcode1), "\n")
405 }
406 throw("fault")
407 case windows.EXCEPTION_INT_DIVIDE_BY_ZERO:
408 panicdivide()
409 case windows.EXCEPTION_INT_OVERFLOW:
410 panicoverflow()
411 case windows.EXCEPTION_FLT_DENORMAL_OPERAND,
412 windows.EXCEPTION_FLT_DIVIDE_BY_ZERO,
413 windows.EXCEPTION_FLT_INEXACT_RESULT,
414 windows.EXCEPTION_FLT_OVERFLOW,
415 windows.EXCEPTION_FLT_UNDERFLOW:
416 panicfloat()
417 }
418 throw("fault")
419 }
420
421
422
423 func initsig(preinit bool) {
424 }
425
426 func sigenable(sig uint32) {
427 }
428
429 func sigdisable(sig uint32) {
430 }
431
432 func sigignore(sig uint32) {
433 }
434
435 func signame(sig uint32) string {
436 return ""
437 }
438
439
440 func crash() {
441 dieFromException(nil, nil)
442 }
443
444
445
446
447
448 func dieFromException(info *windows.ExceptionRecord, r *windows.Context) {
449 if info == nil {
450 gp := getg()
451 if gp.sig != 0 {
452
453
454 info = &windows.ExceptionRecord{
455 ExceptionAddress: gp.sigpc,
456 ExceptionCode: gp.sig,
457 NumberParameters: 2,
458 }
459 info.ExceptionInformation[0] = gp.sigcode0
460 info.ExceptionInformation[1] = gp.sigcode1
461 } else {
462
463
464 info = &windows.ExceptionRecord{
465 ExceptionCode: 2,
466 }
467 }
468 }
469 stdcall(_RaiseFailFastException, uintptr(unsafe.Pointer(info)), uintptr(unsafe.Pointer(r)), windows.FAIL_FAST_GENERATE_EXCEPTION_ADDRESS)
470 }
471
472
473 type gsignalStack struct{}
474
View as plain text