Skip to content

Commit 28835fd

Browse files
committed
wasm: add Boehm GC support
This adds support for `-gc=boehm` on `-target=wasip1` and `-target=wasm` (in a browser or NodeJS). Notably it does *not* add Boehm GC support for `-target=wasip2`, since that target doesn't have a real libc.
1 parent 9ca6f72 commit 28835fd

File tree

21 files changed

+121
-60
lines changed

21 files changed

+121
-60
lines changed

builder/bdwgc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var BoehmGC = Library{
1414
name: "bdwgc",
1515
cflags: func(target, headerPath string) []string {
1616
libdir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/bdwgc")
17-
return []string{
17+
flags := []string{
1818
// use a modern environment
1919
"-DUSE_MMAP", // mmap is available
2020
"-DUSE_MUNMAP", // return memory to the OS using munmap
@@ -30,6 +30,7 @@ var BoehmGC = Library{
3030
// Use a minimal environment.
3131
"-DNO_MSGBOX_ON_ERROR", // don't call MessageBoxA on Windows
3232
"-DDONT_USE_ATEXIT",
33+
"-DNO_GETENV",
3334

3435
// Special flag to work around the lack of __data_start in ld.lld.
3536
// TODO: try to fix this in LLVM/lld directly so we don't have to
@@ -49,12 +50,13 @@ var BoehmGC = Library{
4950

5051
"-I" + libdir + "/include",
5152
}
53+
return flags
5254
},
5355
needsLibc: true,
5456
sourceDir: func() string {
5557
return filepath.Join(goenv.Get("TINYGOROOT"), "lib/bdwgc")
5658
},
57-
librarySources: func(target string) ([]string, error) {
59+
librarySources: func(target string, _ bool) ([]string, error) {
5860
sources := []string{
5961
"allchblk.c",
6062
"alloc.c",

builder/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ var libCompilerRT = Library{
226226
// Development build.
227227
return filepath.Join(goenv.Get("TINYGOROOT"), "lib/compiler-rt-builtins")
228228
},
229-
librarySources: func(target string) ([]string, error) {
229+
librarySources: func(target string, _ bool) ([]string, error) {
230230
builtins := append([]string{}, genericBuiltins...) // copy genericBuiltins
231231
switch compileopts.CanonicalArchName(target) {
232232
case "arm":

builder/library.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Library struct {
3838
sourceDir func() string
3939

4040
// The source files, relative to sourceDir.
41-
librarySources func(target string) ([]string, error)
41+
librarySources func(target string, libcNeedsMalloc bool) ([]string, error)
4242

4343
// The source code for the crt1.o file, relative to sourceDir.
4444
crt1Source string
@@ -226,7 +226,7 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
226226

227227
// Create jobs to compile all sources. These jobs are depended upon by the
228228
// archive job above, so must be run first.
229-
paths, err := l.librarySources(target)
229+
paths, err := l.librarySources(target, config.LibcNeedsMalloc())
230230
if err != nil {
231231
return nil, nil, err
232232
}

builder/mingw-w64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var libMinGW = Library{
4848
}
4949
return flags
5050
},
51-
librarySources: func(target string) ([]string, error) {
51+
librarySources: func(target string, _ bool) ([]string, error) {
5252
// These files are needed so that printf and the like are supported.
5353
var sources []string
5454
if strings.Split(target, "-")[0] == "i386" {

builder/musl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ var libMusl = Library{
121121
return cflags
122122
},
123123
sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/musl/src") },
124-
librarySources: func(target string) ([]string, error) {
124+
librarySources: func(target string, _ bool) ([]string, error) {
125125
arch := compileopts.MuslArchitecture(target)
126126
globs := []string{
127127
"conf/*.c",

builder/picolibc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var libPicolibc = Library{
4343
}
4444
},
4545
sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib") },
46-
librarySources: func(target string) ([]string, error) {
46+
librarySources: func(target string, _ bool) ([]string, error) {
4747
sources := append([]string(nil), picolibcSources...)
4848
if !strings.HasPrefix(target, "avr") {
4949
// Small chips without long jumps can't compile many files (printf,

builder/wasilibc.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ var libWasiLibc = Library{
120120
return nil
121121
},
122122
sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc") },
123-
librarySources: func(target string) ([]string, error) {
123+
librarySources: func(target string, libcNeedsMalloc bool) ([]string, error) {
124124
type filePattern struct {
125125
glob string
126126
exclude []string
@@ -169,6 +169,11 @@ var libWasiLibc = Library{
169169
{glob: "libc-bottom-half/sources/*.c"},
170170
}
171171

172+
// We're using the Boehm GC, so we need a heap implementation in the libc.
173+
if libcNeedsMalloc {
174+
globs = append(globs, filePattern{glob: "dlmalloc/src/dlmalloc.c"})
175+
}
176+
172177
// See: LIBC_TOP_HALF_MUSL_SOURCES in the Makefile
173178
sources := []string{
174179
"libc-top-half/musl/src/misc/a64l.c",

builder/wasmbuiltins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var libWasmBuiltins = Library{
4141
}
4242
},
4343
sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/wasi-libc") },
44-
librarySources: func(target string) ([]string, error) {
44+
librarySources: func(target string, _ bool) ([]string, error) {
4545
return []string{
4646
// memory builtins needed for llvm.memcpy.*, llvm.memmove.*, and
4747
// llvm.memset.* LLVM intrinsics.

compileopts/config.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
// builder.Library struct but that's hard to do since we want to know the
2323
// library path in advance in several places).
2424
var libVersions = map[string]int{
25-
"musl": 3,
25+
"musl": 3,
26+
"bdwgc": 2,
2627
}
2728

2829
// Config keeps all configuration affecting the build in a single struct.
@@ -133,7 +134,7 @@ func (c *Config) GC() string {
133134
// that can be traced by the garbage collector.
134135
func (c *Config) NeedsStackObjects() bool {
135136
switch c.GC() {
136-
case "conservative", "custom", "precise":
137+
case "conservative", "custom", "precise", "boehm":
137138
for _, tag := range c.BuildTags() {
138139
if tag == "tinygo.wasm" {
139140
return true
@@ -258,6 +259,15 @@ func MuslArchitecture(triple string) string {
258259
return CanonicalArchName(triple)
259260
}
260261

262+
// Returns true if the libc needs to include malloc, for the libcs where this
263+
// matters.
264+
func (c *Config) LibcNeedsMalloc() bool {
265+
if c.GC() == "boehm" && c.Target.Libc == "wasi-libc" {
266+
return true
267+
}
268+
return false
269+
}
270+
261271
// LibraryPath returns the path to the library build directory. The path will be
262272
// a library path in the cache directory (which might not yet be built).
263273
func (c *Config) LibraryPath(name string) string {
@@ -281,9 +291,14 @@ func (c *Config) LibraryPath(name string) string {
281291
archname += "-v" + strconv.Itoa(v)
282292
}
283293

294+
options := ""
295+
if c.LibcNeedsMalloc() {
296+
options += "+malloc"
297+
}
298+
284299
// No precompiled library found. Determine the path name that will be used
285300
// in the build cache.
286-
return filepath.Join(goenv.Get("GOCACHE"), name+"-"+archname)
301+
return filepath.Join(goenv.Get("GOCACHE"), name+options+"-"+archname)
287302
}
288303

289304
// DefaultBinaryExtension returns the default extension for binaries, such as

compileopts/target.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
470470
return nil, fmt.Errorf("unknown GOOS=%s", options.GOOS)
471471
}
472472

473+
if spec.GC == "boehm" {
474+
// Add this file only when needed. This fixes a build failure on
475+
// Windows.
476+
spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/gc_boehm.c")
477+
}
478+
473479
// Target triples (which actually have four components, but are called
474480
// triples for historical reasons) have the form:
475481
// arch-vendor-os-environment

0 commit comments

Comments
 (0)