@@ -218,15 +218,7 @@ func TestEarlySignalHandler(t *testing.T) {
218
218
}
219
219
220
220
func TestSignalForwarding (t * testing.T ) {
221
- switch GOOS {
222
- case "darwin" :
223
- switch GOARCH {
224
- case "arm" , "arm64" :
225
- t .Skipf ("skipping on %s/%s; see https://golang.org/issue/13701" , GOOS , GOARCH )
226
- }
227
- case "windows" :
228
- t .Skip ("skipping signal test on Windows" )
229
- }
221
+ checkSignalForwardingTest (t )
230
222
231
223
defer func () {
232
224
os .Remove ("libgo2.a" )
@@ -251,51 +243,19 @@ func TestSignalForwarding(t *testing.T) {
251
243
cmd = exec .Command (bin [0 ], append (bin [1 :], "1" )... )
252
244
253
245
out , err := cmd .CombinedOutput ()
254
-
255
- if err == nil {
256
- t .Logf ("%s" , out )
257
- t .Error ("test program succeeded unexpectedly" )
258
- } else if ee , ok := err .(* exec.ExitError ); ! ok {
259
- t .Logf ("%s" , out )
260
- t .Errorf ("error (%v) has type %T; expected exec.ExitError" , err , err )
261
- } else if ws , ok := ee .Sys ().(syscall.WaitStatus ); ! ok {
262
- t .Logf ("%s" , out )
263
- t .Errorf ("error.Sys (%v) has type %T; expected syscall.WaitStatus" , ee .Sys (), ee .Sys ())
264
- } else if ! ws .Signaled () || ws .Signal () != syscall .SIGSEGV {
265
- t .Logf ("%s" , out )
266
- t .Errorf ("got %v; expected SIGSEGV" , ee )
267
- }
246
+ t .Logf ("%s" , out )
247
+ expectSignal (t , err , syscall .SIGSEGV )
268
248
269
249
// Test SIGPIPE forwarding
270
250
cmd = exec .Command (bin [0 ], append (bin [1 :], "3" )... )
271
251
272
252
out , err = cmd .CombinedOutput ()
273
-
274
- if err == nil {
275
- t .Logf ("%s" , out )
276
- t .Error ("test program succeeded unexpectedly" )
277
- } else if ee , ok := err .(* exec.ExitError ); ! ok {
278
- t .Logf ("%s" , out )
279
- t .Errorf ("error (%v) has type %T; expected exec.ExitError" , err , err )
280
- } else if ws , ok := ee .Sys ().(syscall.WaitStatus ); ! ok {
281
- t .Logf ("%s" , out )
282
- t .Errorf ("error.Sys (%v) has type %T; expected syscall.WaitStatus" , ee .Sys (), ee .Sys ())
283
- } else if ! ws .Signaled () || ws .Signal () != syscall .SIGPIPE {
284
- t .Logf ("%s" , out )
285
- t .Errorf ("got %v; expected SIGPIPE" , ee )
286
- }
253
+ t .Logf ("%s" , out )
254
+ expectSignal (t , err , syscall .SIGPIPE )
287
255
}
288
256
289
257
func TestSignalForwardingExternal (t * testing.T ) {
290
- switch GOOS {
291
- case "darwin" :
292
- switch GOARCH {
293
- case "arm" , "arm64" :
294
- t .Skipf ("skipping on %s/%s; see https://golang.org/issue/13701" , GOOS , GOARCH )
295
- }
296
- case "windows" :
297
- t .Skip ("skipping signal test on Windows" )
298
- }
258
+ checkSignalForwardingTest (t )
299
259
300
260
defer func () {
301
261
os .Remove ("libgo2.a" )
@@ -363,21 +323,46 @@ func TestSignalForwardingExternal(t *testing.T) {
363
323
continue
364
324
}
365
325
366
- if ee , ok := err .(* exec.ExitError ); ! ok {
367
- t .Errorf ("error (%v) has type %T; expected exec.ExitError" , err , err )
368
- } else if ws , ok := ee .Sys ().(syscall.WaitStatus ); ! ok {
369
- t .Errorf ("error.Sys (%v) has type %T; expected syscall.WaitStatus" , ee .Sys (), ee .Sys ())
370
- } else if ! ws .Signaled () || ws .Signal () != syscall .SIGSEGV {
371
- t .Errorf ("got %v; expected SIGSEGV" , ee )
372
- } else {
373
- // We got the error we expected.
326
+ if expectSignal (t , err , syscall .SIGSEGV ) {
374
327
return
375
328
}
376
329
}
377
330
378
331
t .Errorf ("program succeeded unexpectedly %d times" , tries )
379
332
}
380
333
334
+ // checkSignalForwardingTest calls t.Skip if the SignalForwarding test
335
+ // doesn't work on this platform.
336
+ func checkSignalForwardingTest (t * testing.T ) {
337
+ switch GOOS {
338
+ case "darwin" :
339
+ switch GOARCH {
340
+ case "arm" , "arm64" :
341
+ t .Skipf ("skipping on %s/%s; see https://golang.org/issue/13701" , GOOS , GOARCH )
342
+ }
343
+ case "windows" :
344
+ t .Skip ("skipping signal test on Windows" )
345
+ }
346
+ }
347
+
348
+ // expectSignal checks that err, the exit status of a test program,
349
+ // shows a failure due to a specific signal. Returns whether we found
350
+ // the expected signal.
351
+ func expectSignal (t * testing.T , err error , sig syscall.Signal ) bool {
352
+ if err == nil {
353
+ t .Error ("test program succeeded unexpectedly" )
354
+ } else if ee , ok := err .(* exec.ExitError ); ! ok {
355
+ t .Errorf ("error (%v) has type %T; expected exec.ExitError" , err , err )
356
+ } else if ws , ok := ee .Sys ().(syscall.WaitStatus ); ! ok {
357
+ t .Errorf ("error.Sys (%v) has type %T; expected syscall.WaitStatus" , ee .Sys (), ee .Sys ())
358
+ } else if ! ws .Signaled () || ws .Signal () != sig {
359
+ t .Errorf ("got %v; expected signal %v" , ee , sig )
360
+ } else {
361
+ return true
362
+ }
363
+ return false
364
+ }
365
+
381
366
func TestOsSignal (t * testing.T ) {
382
367
switch GOOS {
383
368
case "windows" :
@@ -592,3 +577,44 @@ func TestSIGPROF(t *testing.T) {
592
577
t .Fatal (err )
593
578
}
594
579
}
580
+
581
+ // TestCompileWithoutShared tests that if we compile code without the
582
+ // -shared option, we can put it into an archive. When we use the go
583
+ // tool with -buildmode=c-archive, it passes -shared to the compiler,
584
+ // so we override that. The go tool doesn't work this way, but Bazel
585
+ // will likely do it in the future. And it ought to work. This test
586
+ // was added because at one time it did not work on PPC GNU/Linux.
587
+ func TestCompileWithoutShared (t * testing.T ) {
588
+ // For simplicity, reuse the signal forwarding test.
589
+ checkSignalForwardingTest (t )
590
+
591
+ defer func () {
592
+ os .Remove ("libgo2.a" )
593
+ os .Remove ("libgo2.h" )
594
+ }()
595
+
596
+ cmd := exec .Command ("go" , "build" , "-buildmode=c-archive" , "-gcflags=-shared=false" , "-o" , "libgo2.a" , "libgo2" )
597
+ cmd .Env = gopathEnv
598
+ t .Log (cmd .Args )
599
+ out , err := cmd .CombinedOutput ()
600
+ t .Logf ("%s" , out )
601
+ if err != nil {
602
+ t .Fatal (err )
603
+ }
604
+
605
+ exe := "./testnoshared" + exeSuffix
606
+ ccArgs := append (cc , "-o" , exe , "main5.c" , "libgo2.a" )
607
+ t .Log (ccArgs )
608
+ out , err = exec .Command (ccArgs [0 ], ccArgs [1 :]... ).CombinedOutput ()
609
+ t .Logf ("%s" , out )
610
+ if err != nil {
611
+ t .Fatal (err )
612
+ }
613
+ defer os .Remove (exe )
614
+
615
+ binArgs := append (cmdToRun (exe ), "3" )
616
+ t .Log (binArgs )
617
+ out , err = exec .Command (binArgs [0 ], binArgs [1 :]... ).CombinedOutput ()
618
+ t .Logf ("%s" , out )
619
+ expectSignal (t , err , syscall .SIGPIPE )
620
+ }
0 commit comments