Repository navigation
Expand file tree
/
Copy pathbuild_test.go
More file actions
571 lines (533 loc) · 16.4 KB
/
Copy pathbuild_test.go
File metadata and controls
571 lines (533 loc) · 16.4 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package main
import (
"bytes"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
"github.com/tinygo-org/tinygo/builder"
"github.com/tinygo-org/tinygo/compileopts"
"github.com/tinygo-org/tinygo/diagnostics"
"github.com/tinygo-org/tinygo/goenv"
)
func TestBuild(t *testing.T) {
t.Parallel()
tests := []string{
"alias.go",
"atomic.go",
"binop.go",
"buildinfo.go",
"calls.go",
"cgo/",
"channel.go",
"coreassign.go",
"embed/",
"finalizer.go",
"finalizerbits.go",
"finalizeridle.go",
"finalizerinvariants.go",
"finalizerlarge.go",
"float.go",
"gc.go",
"generics.go",
"go1.21.go",
"go1.22/",
"go1.23/",
"goroutines.go",
"init.go",
"init_multi.go",
"interface-blank.go",
"interface.go",
"json.go",
"localtypes/",
"localtypes.go",
"map.go",
"map_bigkey.go",
"math.go",
"oldgo/",
"print.go",
"reflect.go",
"signal.go",
"signalnotify.go",
"slice.go",
"sort.go",
"stdlib.go",
"string.go",
"structs.go",
"testing.go",
"timers.go",
"typealias.go",
"zeroalloc.go",
}
if *testTarget != "" {
// This makes it possible to run one specific test (instead of all),
// which is especially useful to quickly check whether some changes
// affect a particular target architecture.
options := optionsFromTarget(*testTarget, sema)
runPlatTests(options, tests, t)
if *testTarget == "wasip1" {
t.Run("cgo-realloc", func(t *testing.T) {
runTest("cgo-realloc/", options, t, nil, nil)
})
}
return
}
t.Run("Host", func(t *testing.T) {
t.Parallel()
hostOptions := optionsFromTarget("", sema)
runPlatTests(hostOptions, tests, t)
t.Run("testing.go-verbose", func(t *testing.T) {
t.Parallel()
runTest("testing-verbose.go", hostOptions, t, nil, nil)
})
// scheduler.threads needs threadID, which exists only on Linux and Darwin.
// scheduler.none does not link on Windows.
switch runtime.GOOS {
case "darwin", "linux":
for _, scheduler := range []string{"threads", "none"} {
t.Run("finalizerinvariants.go-gc-conservative-scheduler-"+scheduler, func(t *testing.T) {
t.Parallel()
options := compileopts.Options(hostOptions)
options.GC = "conservative"
options.Scheduler = scheduler
options.Tags = append(append([]string(nil), hostOptions.Tags...), "runtime_asserts")
runTest("finalizerinvariants.go", options, t, nil, nil)
})
}
}
})
t.Run("boehm-finalizers", func(t *testing.T) {
t.Parallel()
options := optionsFromTarget("wasm", sema)
options.GC = "boehm"
t.Run("finalizerlarge.go-graph", func(t *testing.T) {
t.Parallel()
testOptions := compileopts.Options(options)
testOptions.GlobalValues = map[string]map[string]string{
"main": {"finalizerLargeMode": "graph"},
}
runTest("finalizerlarge.go", testOptions, t, nil, nil)
})
for _, name := range []string{
"finalizer.go",
"finalizerbits.go",
"finalizeridle.go",
"finalizerlarge.go",
"finalizerinvariants.go",
} {
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
testOptions := compileopts.Options(options)
if name == "finalizerlarge.go" {
testOptions.GlobalValues = map[string]map[string]string{
"main": {"finalizerLargeMode": "runner-payload"},
}
}
runTest(name, testOptions, t, nil, nil)
})
}
})
// Test a few build options.
t.Run("build-options", func(t *testing.T) {
t.Parallel()
// Test with few optimizations enabled (no inlining, etc).
t.Run("opt=1", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("", sema)
opts.Opt = "1"
runTestWithConfig("stdlib.go", t, opts, nil, nil)
})
// Test with only the bare minimum of optimizations enabled.
// TODO: fix this for stdlib.go, which currently fails.
t.Run("opt=0", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("", sema)
opts.Opt = "0"
runTestWithConfig("print.go", t, opts, nil, nil)
})
t.Run("opt=0-gc=boehm", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("", sema)
opts.Opt = "0"
opts.GC = "boehm"
runTestWithConfig("gc-boehm-opt0.go", t, opts, nil, nil)
})
// Regression test: at -opt=0 the compiler does not always remove a
// local escaping through a pointer cast, so printitf used to
// allocate on the panic path. printitf is //go:noheap, so a
// regression here makes the build fail with a linker error.
t.Run("opt=0-printitf-cortex-m-qemu", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("cortex-m-qemu", sema)
opts.Opt = "0"
config, err := builder.NewConfig(&opts)
if err != nil {
t.Fatal(err)
}
err = Build("testdata/panic-value.go", t.TempDir()+"/panic-value", config)
if err != nil {
w := &bytes.Buffer{}
diagnostics.CreateDiagnostics(err).WriteTo(w, "")
t.Fatal(w.String())
}
})
// Regression test: at -opt=0 the compiler does not always remove a
// local escaping through Queue.Push, so runGC used to allocate while
// marking the runqueue. runGC is //go:noheap, so a regression here
// makes the build fail with a linker error instead of silently
// hanging like it used to.
t.Run("opt=0-gc-cortex-m-qemu", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("cortex-m-qemu", sema)
opts.Opt = "0"
emuCheck(t, opts)
runTestWithConfig("gc.go", t, opts, nil, nil)
})
t.Run("gc=none-runtime-panic", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("cortex-m-qemu", sema)
opts.GC = "none"
opts.Scheduler = "none"
config, err := builder.NewConfig(&opts)
if err != nil {
t.Fatal(err)
}
err = Build("testdata/trivialpanic.go", t.TempDir()+"/trivialpanic", config)
if err != nil {
w := &bytes.Buffer{}
diagnostics.CreateDiagnostics(err).WriteTo(w, "")
t.Fatal(w.String())
}
})
t.Run("scheduler=none-uefi", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("uefi-amd64", sema)
opts.Scheduler = "none"
config, err := builder.NewConfig(&opts)
if err != nil {
t.Fatal(err)
}
err = Build("testdata/trivialpanic.go", t.TempDir()+"/trivialpanic", config)
if err != nil {
w := &bytes.Buffer{}
diagnostics.CreateDiagnostics(err).WriteTo(w, "")
t.Fatal(w.String())
}
})
t.Run("ldflags", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("", sema)
opts.GlobalValues = map[string]map[string]string{
"main": {
"someGlobal": "foobar",
},
}
runTestWithConfig("ldflags.go", t, opts, nil, nil)
})
// Same as ldflags, but the global has a default value in source.
// -ldflags -X must override it, matching standard Go behaviour.
t.Run("ldflags-initialized", func(t *testing.T) {
t.Parallel()
opts := optionsFromTarget("", sema)
opts.GlobalValues = map[string]map[string]string{
"main": {
"someGlobal": "foobar",
},
}
runTestWithConfig("ldflags-initialized.go", t, opts, nil, nil)
})
})
// Short mode skips the targets that every host can test. Linux CI tests
// them without -short.
if !testing.Short() {
t.Run("EmulatedCortexM3", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromTarget("cortex-m-qemu", sema), tests, t)
})
t.Run("EmulatedRISCV", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromTarget("riscv-qemu", sema), tests, t)
})
t.Run("AVR", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromTarget("simavr", sema), tests, t)
})
t.Run("WebAssembly", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromTarget("wasm", sema), tests, t)
runGCLivenessTest(optionsFromTarget("wasm", sema), t)
// Test with -gc=boehm.
t.Run("gc.go-boehm", func(t *testing.T) {
t.Parallel()
optionsBoehm := optionsFromTarget("wasm", sema)
optionsBoehm.GC = "boehm"
runTest("gc.go", optionsBoehm, t, nil, nil)
runTest("gc-boehm.go", optionsBoehm, t, nil, nil)
})
})
t.Run("WASIp1", func(t *testing.T) {
t.Parallel()
options := optionsFromTarget("wasip1", sema)
runPlatTests(options, tests, t)
runGCLivenessTest(options, t)
t.Run("cgo-realloc", func(t *testing.T) {
runTest("cgo-realloc/", options, t, nil, nil)
})
// Test with -gc=boehm.
t.Run("gc.go-boehm", func(t *testing.T) {
t.Parallel()
optionsBoehm := optionsFromTarget("wasip1", sema)
optionsBoehm.GC = "boehm"
runTest("gc.go", optionsBoehm, t, nil, nil)
runTest("gc-boehm.go", optionsBoehm, t, nil, nil)
})
})
t.Run("WASIp2", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromTarget("wasip2", sema), tests, t)
runGCLivenessTest(optionsFromTarget("wasip2", sema), t)
})
}
// Linux CI also tests the other Linux architectures without -short, so
// short mode skips them. Windows CI runs only in short mode, so
// Windows386 continues to run.
if runtime.GOOS == "linux" && !testing.Short() {
for name, osArch := range supportedLinuxArches {
options := optionsFromOSARCH(osArch, sema)
if options.GOARCH != runtime.GOARCH { // Native architecture already run above.
t.Run(name, func(t *testing.T) {
runPlatTests(options, tests, t)
})
}
}
t.Run("MIPS little-endian", func(t *testing.T) {
// Run a single test for GOARCH=mipsle to see whether it works at
// all. It is already mostly tested because GOARCH=mips and
// GOARCH=mipsle are so similar, but it's good to have an extra test
// to be sure.
t.Parallel()
options := optionsFromOSARCH("linux/mipsle/softfloat", sema)
emuCheck(t, options)
runTest("cgo/", options, t, nil, nil)
})
} else if runtime.GOOS == "windows" {
if runtime.GOARCH != "386" {
t.Run("Windows386", func(t *testing.T) {
t.Parallel()
runPlatTests(optionsFromOSARCH("windows/386", sema), tests, t)
})
}
}
}
func TestESP32QEMU(t *testing.T) {
t.Parallel()
options := optionsFromTarget("esp32-qemu", sema)
emuCheck(t, options)
machines, err := exec.Command("qemu-system-xtensa", "-machine", "help").Output()
if err != nil {
t.Fatal("failed to list qemu-system-xtensa machines:", err)
}
if !regexp.MustCompile(`(?m)^esp32\s`).Match(machines) {
t.Skip("qemu-system-xtensa does not support the ESP32 machine")
}
for _, name := range []string{"print.go", "recover.go"} {
t.Run(name, func(t *testing.T) {
t.Parallel()
runTest(name, options, t, nil, nil)
})
}
}
func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
emuCheck(t, options)
spec, err := compileopts.LoadTarget(&options)
if err != nil {
t.Fatal("failed to load target spec:", err)
}
// FIXME: this should really be:
// isWebAssembly := strings.HasPrefix(spec.Triple, "wasm")
isWASI := strings.HasPrefix(options.Target, "wasi")
isWebAssembly := isWASI || strings.HasPrefix(options.Target, "wasm") || (options.Target == "" && strings.HasPrefix(options.GOARCH, "wasm"))
isBaremetal := options.Target == "simavr" || options.Target == "cortex-m-qemu" || options.Target == "riscv-qemu"
_, goMinor, err := goenv.GetGorootVersion()
if err != nil {
t.Fatal("could not get version:", goMinor)
}
for _, name := range tests {
if name == "goroutines.go" && (spec.Scheduler == "threads" || spec.Scheduler == "cores") {
// This test intentionally checks concurrent scheduling by comparing
// output order, so only run it with non-threaded schedulers.
continue
}
if name == "coreassign.go" && spec.Scheduler != "cores" {
// Core assignment needs the cores scheduler.
continue
}
if options.GOOS == "linux" && (options.GOARCH == "arm" || options.GOARCH == "386") {
switch name {
case "timers.go":
// Timer tests do not work because syscall.seek is implemented
// as Assembly in mainline Go and causes linker failure
continue
}
}
if options.GOOS == "linux" && (options.GOARCH == "mips" || options.GOARCH == "mipsle") {
if name == "atomic.go" || name == "timers.go" {
// 64-bit atomic operations aren't currently supported on MIPS.
continue
}
}
if options.GOOS == "linux" && options.GOARCH == "mips" {
if name == "cgo/" {
// CGo isn't supported yet on big-endian systems (needs updates
// to bitfield access methods).
continue
}
}
if options.Target == "cortex-m-qemu" && goMinor >= 27 && name == "json.go" {
// Go 1.27 jsonv2 exceeds the LM3S6965's 256KiB flash. json.go
// is still covered by larger targets such as riscv-qemu.
continue
}
if options.Target == "simavr" {
// Not all tests are currently supported on AVR.
// Skip the ones that aren't.
switch name {
case "reflect.go":
// Reflect tests do not run correctly, probably because of the
// limited amount of memory.
continue
case "map_bigkey.go":
// Compiler generates many large stack temporaries for [256]byte
// map keys, overflowing the goroutine stack (384 bytes).
continue
case "gc.go":
// Does not pass due to high mark false positive rate.
continue
case "buildinfo.go", "json.go", "stdlib.go", "testing.go":
// Too big for AVR. Doesn't fit in flash/RAM.
continue
case "cgo/":
// CGo function pointers don't work on AVR (needs LLVM 16 and
// some compiler changes).
continue
case "timers.go":
// Crashes starting with Go 1.23.
// Bug: https://github.com/llvm/llvm-project/issues/104032
continue
default:
}
}
if options.Target == "wasip2" {
switch name {
case "cgo/":
// waisp2 use our own libc; cgo tests fail
continue
}
}
if options.GOOS == "windows" {
switch name {
case "signal.go", "signalnotify.go":
// os/signal does not link on Windows.
continue
}
}
if isWebAssembly || isBaremetal {
switch name {
case "signal.go":
// Signals only work on POSIX-like systems.
continue
}
}
if options.Target != "wasm" {
switch name {
case "finalizer.go", "finalizerbits.go", "finalizeridle.go", "finalizerlarge.go":
// These tests require deterministic finalization on target wasm.
// finalizerinvariants.go covers other block GC targets.
continue
}
}
if options.Target == "simavr" {
switch name {
case "finalizerinvariants.go":
// Skip because runtime.GC does not return. See the gc.go exclusion above.
continue
}
}
name := name // redefine to avoid race condition
t.Run(name, func(t *testing.T) {
t.Parallel()
testOptions := compileopts.Options(options)
switch name {
case "finalizerinvariants.go", "finalizerlarge.go", "interface.go", "interface-blank.go":
testOptions.Tags = append(append([]string(nil), options.Tags...), "runtime_asserts")
}
if testOptions.Target == "simavr" && (name == "math.go" || name == "string.go") {
// These tests exceed simavr's default 384-byte goroutine stack.
testOptions.StackSize = 512
}
runTest(name, testOptions, t, nil, nil)
})
}
if !strings.HasPrefix(spec.Emulator, "simavr ") {
t.Run("env.go", func(t *testing.T) {
t.Parallel()
runTest("env.go", options, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
})
}
if isWebAssembly {
t.Run("alias.go-scheduler-none", func(t *testing.T) {
t.Parallel()
options := compileopts.Options(options)
options.Scheduler = "none"
runTest("alias.go", options, t, nil, nil)
})
}
if options.Target == "" || isWASI {
t.Run("filesystem.go", func(t *testing.T) {
t.Parallel()
runTest("filesystem.go", options, t, nil, nil)
})
}
if options.Target == "wasm" || isBaremetal {
t.Run("devnull.go", func(t *testing.T) {
t.Parallel()
runTest("devnull.go", options, t, nil, nil)
})
}
if options.Target == "" || options.Target == "wasm" || isWASI {
t.Run("rand.go", func(t *testing.T) {
t.Parallel()
runTest("rand.go", options, t, nil, nil)
})
}
t.Run("recover.go", func(t *testing.T) {
t.Parallel()
runTest("recover.go", options, t, nil, nil)
})
if isWebAssembly {
t.Run("recover-explicit.go", func(t *testing.T) {
t.Parallel()
options := compileopts.Options(options)
options.Scheduler = "none"
options.PanicUnwind = "explicit"
runTest("recover-explicit.go", options, t, nil, nil)
})
t.Run("calls.go-unwind-none", func(t *testing.T) {
t.Parallel()
options := compileopts.Options(options)
options.PanicUnwind = "none"
runTest("calls.go", options, t, nil, nil)
})
}
}
// runGCLivenessTest runs testdata/gc-liveness-repro.go. Only targets with
// stack objects can lose a GC root the way it reproduces, so it is registered
// per wasm target instead of being part of the shared test list.
func runGCLivenessTest(options compileopts.Options, t *testing.T) {
t.Run("gc-liveness-repro.go", func(t *testing.T) {
t.Parallel()
runTest("gc-liveness-repro.go", options, t, nil, nil)
})
}