-
Notifications
You must be signed in to change notification settings - Fork 17.9k
/
Copy pathcrash_cgo_test.go
484 lines (416 loc) · 8.15 KB
/
crash_cgo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build cgo
package runtime_test
import (
"os/exec"
"runtime"
"strings"
"testing"
)
func TestCgoCrashHandler(t *testing.T) {
testCrashHandler(t, true)
}
func TestCgoSignalDeadlock(t *testing.T) {
if testing.Short() && runtime.GOOS == "windows" {
t.Skip("Skipping in short mode") // takes up to 64 seconds
}
got := executeTest(t, cgoSignalDeadlockSource, nil)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %q", want, got)
}
}
func TestCgoTraceback(t *testing.T) {
got := executeTest(t, cgoTracebackSource, nil)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %q", want, got)
}
}
func TestCgoCallbackGC(t *testing.T) {
if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
t.Skipf("no pthreads on %s", runtime.GOOS)
}
if testing.Short() && runtime.GOOS == "dragonfly" {
t.Skip("see golang.org/issue/11990")
}
got := executeTest(t, cgoCallbackGCSource, nil)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %q", want, got)
}
}
func TestCgoExternalThreadPanic(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skipf("no pthreads on %s", runtime.GOOS)
}
csrc := cgoExternalThreadPanicC
if runtime.GOOS == "windows" {
csrc = cgoExternalThreadPanicC_windows
}
got := executeTest(t, cgoExternalThreadPanicSource, nil, "main.c", csrc)
want := "panic: BOOM"
if !strings.Contains(got, want) {
t.Fatalf("want failure containing %q. output:\n%s\n", want, got)
}
}
func TestCgoExternalThreadSIGPROF(t *testing.T) {
// issue 9456.
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("no pthreads on %s", runtime.GOOS)
case "darwin":
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" {
// static constructor needs external linking, but we don't support
// external linking on OS X 10.6.
out, err := exec.Command("uname", "-r").Output()
if err != nil {
t.Fatalf("uname -r failed: %v", err)
}
// OS X 10.6 == Darwin 10.x
if strings.HasPrefix(string(out), "10.") {
t.Skipf("no external linking on OS X 10.6")
}
}
}
if runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" {
// TODO(austin) External linking not implemented on
// ppc64 (issue #8912)
t.Skipf("no external linking on ppc64")
}
got := executeTest(t, cgoExternalThreadSIGPROFSource, nil)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %q", want, got)
}
}
func TestCgoExternalThreadSignal(t *testing.T) {
// issue 10139
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("no pthreads on %s", runtime.GOOS)
}
got := executeTest(t, cgoExternalThreadSignalSource, nil)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %q", want, got)
}
}
func TestCgoDLLImports(t *testing.T) {
// test issue 9356
if runtime.GOOS != "windows" {
t.Skip("skipping windows specific test")
}
got := executeTest(t, cgoDLLImportsMainSource, nil, "a/a.go", cgoDLLImportsPkgSource)
want := "OK\n"
if got != want {
t.Fatalf("expected %q, but got %v", want, got)
}
}
const cgoSignalDeadlockSource = `
package main
import "C"
import (
"fmt"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(100)
ping := make(chan bool)
go func() {
for i := 0; ; i++ {
runtime.Gosched()
select {
case done := <-ping:
if done {
ping <- true
return
}
ping <- true
default:
}
func() {
defer func() {
recover()
}()
var s *string
*s = ""
}()
}
}()
time.Sleep(time.Millisecond)
for i := 0; i < 64; i++ {
go func() {
runtime.LockOSThread()
select {}
}()
go func() {
runtime.LockOSThread()
select {}
}()
time.Sleep(time.Millisecond)
ping <- false
select {
case <-ping:
case <-time.After(time.Second):
fmt.Printf("HANG\n")
return
}
}
ping <- true
select {
case <-ping:
case <-time.After(time.Second):
fmt.Printf("HANG\n")
return
}
fmt.Printf("OK\n")
}
`
const cgoTracebackSource = `
package main
/* void foo(void) {} */
import "C"
import (
"fmt"
"runtime"
)
func main() {
C.foo()
buf := make([]byte, 1)
runtime.Stack(buf, true)
fmt.Printf("OK\n")
}
`
const cgoCallbackGCSource = `
package main
import "runtime"
/*
#include <pthread.h>
void go_callback();
static void *thr(void *arg) {
go_callback();
return 0;
}
static void foo() {
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 256 << 10);
pthread_create(&th, &attr, thr, 0);
pthread_join(th, 0);
}
*/
import "C"
import "fmt"
//export go_callback
func go_callback() {
runtime.GC()
grow()
runtime.GC()
}
var cnt int
func grow() {
x := 10000
sum := 0
if grow1(&x, &sum) == 0 {
panic("bad")
}
}
func grow1(x, sum *int) int {
if *x == 0 {
return *sum + 1
}
*x--
sum1 := *sum + *x
return grow1(x, &sum1)
}
func main() {
const P = 100
done := make(chan bool)
// allocate a bunch of stack frames and spray them with pointers
for i := 0; i < P; i++ {
go func() {
grow()
done <- true
}()
}
for i := 0; i < P; i++ {
<-done
}
// now give these stack frames to cgo callbacks
for i := 0; i < P; i++ {
go func() {
C.foo()
done <- true
}()
}
for i := 0; i < P; i++ {
<-done
}
fmt.Printf("OK\n")
}
`
const cgoExternalThreadPanicSource = `
package main
// void start(void);
import "C"
func main() {
C.start()
select {}
}
//export gopanic
func gopanic() {
panic("BOOM")
}
`
const cgoExternalThreadPanicC = `
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
void gopanic(void);
static void*
die(void* x)
{
gopanic();
return 0;
}
void
start(void)
{
pthread_t t;
if(pthread_create(&t, 0, die, 0) != 0)
printf("pthread_create failed\n");
}
`
const cgoExternalThreadPanicC_windows = `
#include <stdlib.h>
#include <stdio.h>
void gopanic(void);
static void*
die(void* x)
{
gopanic();
return 0;
}
void
start(void)
{
if(_beginthreadex(0, 0, die, 0, 0, 0) != 0)
printf("_beginthreadex failed\n");
}
`
const cgoExternalThreadSIGPROFSource = `
package main
/*
#include <stdint.h>
#include <signal.h>
#include <pthread.h>
volatile int32_t spinlock;
static void *thread1(void *p) {
(void)p;
while (spinlock == 0)
;
pthread_kill(pthread_self(), SIGPROF);
spinlock = 0;
return NULL;
}
__attribute__((constructor)) void issue9456() {
pthread_t tid;
pthread_create(&tid, 0, thread1, NULL);
}
*/
import "C"
import (
"runtime"
"sync/atomic"
"unsafe"
)
func main() {
// This test intends to test that sending SIGPROF to foreign threads
// before we make any cgo call will not abort the whole process, so
// we cannot make any cgo call here. See https://golang.org/issue/9456.
atomic.StoreInt32((*int32)(unsafe.Pointer(&C.spinlock)), 1)
for atomic.LoadInt32((*int32)(unsafe.Pointer(&C.spinlock))) == 1 {
runtime.Gosched()
}
println("OK")
}
`
const cgoExternalThreadSignalSource = `
package main
/*
#include <pthread.h>
void **nullptr;
void *crash(void *p) {
*nullptr = p;
return 0;
}
int start_crashing_thread(void) {
pthread_t tid;
return pthread_create(&tid, 0, crash, 0);
}
*/
import "C"
import (
"fmt"
"os"
"os/exec"
"time"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "crash" {
i := C.start_crashing_thread()
if i != 0 {
fmt.Println("pthread_create failed:", i)
// Exit with 0 because parent expects us to crash.
return
}
// We should crash immediately, but give it plenty of
// time before failing (by exiting 0) in case we are
// running on a slow system.
time.Sleep(5 * time.Second)
return
}
out, err := exec.Command(os.Args[0], "crash").CombinedOutput()
if err == nil {
fmt.Println("C signal did not crash as expected\n")
fmt.Printf("%s\n", out)
os.Exit(1)
}
fmt.Println("OK")
}
`
const cgoDLLImportsMainSource = `
package main
/*
#include <windows.h>
DWORD getthread() {
return GetCurrentThreadId();
}
*/
import "C"
import "./a"
func main() {
C.getthread()
a.GetThread()
println("OK")
}
`
const cgoDLLImportsPkgSource = `
package a
/*
#cgo CFLAGS: -mnop-fun-dllimport
#include <windows.h>
DWORD agetthread() {
return GetCurrentThreadId();
}
*/
import "C"
func GetThread() uint32 {
return uint32(C.agetthread())
}
`