Skip to Content
marmot

Date: 2026-11-18. Status: design only, no build yet. SPEC lists the race detector as a v1 non-goal. It is ranked the second modern feature.

Goal

marmot build -race and marmot run -race find data races in Go programs and report them as gc does: the same "WARNING: DATA RACE" report, goroutine creation stacks, GORACE options, exit status 66 when a race was found. linux/amd64 first, then linux/arm64 and darwin/arm64. wasm: not supported.

Choice: LLVM's TSan pass with the Go flavor of the TSan runtime

Two runtimes exist in compiler-rt, and our LLVM tree (22.1.8) has the sources of both.

A: Go flavor (compiler-rt/lib/tsan/go, the runtime gc links)B: C/C++ runtime (-fsanitize=thread) with the fiber API
GoroutinesFirst class: __tsan_go_start / __tsan_go_end, one Processor per PFibers: __tsan_create_fiber, __tsan_switch_to_fiber(no_sync) at every switch
InterceptorsNonemalloc, pthread, signals and most of libc
SignalsUntouchedAsynchronous signals wait until the thread calls an intercepted function. Our stop-the-world and preemption signals would wait forever in a loop without calls.
HeapMust lie in 0xc000000000 to 0xe000000000 (linux/amd64), globals below 0x10000000000 (non-PIE)Any address, a managed heap through the Java API (__tsan_java_alloc/free, one contiguous range)
Reportsgc's format, code symbolized through a callback (our pc table)C++ format, needs DWARF or an external symbolizer hook
Atomics__tsan_go_atomic*(thr, cpc, pc, args): gc's argument block__tsan_atomic*(addr, ..., mo)
Glue we writeA shim from the pass's C calls to the Go calls (below)Fiber switching, Java heap calls, annotations

Recommendation: A. The signal problem rules out B for the default scheduler, and A gives gc's reports and options without porting work. B stays a fallback for a quick experiment only.

Instrumentation

  • frontend: with -race, every function of a package that gc instruments gets the LLVM attribute sanitize_thread. Not instrumented, as gc: runtime, internal/runtime/*, sync/atomic, internal/race, and the runtime's C (runtime/core is never instrumented). The race build tag is set, so the standard library's own annotations (internal/race in os, net, sync and others) compile in.
  • backend: with -race, add ModuleThreadSanitizerPass and ThreadSanitizerPass to the pipeline (Pipeline.cpp builds it with PassBuilder, registerOptimizerLastEPCallback, after the optimizations, as clang does). The pass instruments only functions with sanitize_thread. It skips loads and stores of allocas that do not escape, and atomics become __tsan_atomic* calls.
  • The pass calls the C interface: __tsan_read4(addr), __tsan_write8(addr), __tsan_func_entry(retaddr), __tsan_func_exit(), __tsan_read_range, __tsan_memcpy and the atomics. The Go runtime wants (thr, addr, pc) arguments. runtime/core/src/race.c (new, built only into librtcore_race.a) implements the C interface:
    • thr: the current goroutine's race context, a new field of rt_g (TLS: getg() is one load).
    • Address filter: only heap arena and data/bss addresses go to TSan, as gc's racecalladdr does. Goroutine stacks, libc memory and mmap'd memory outside the arena are skipped. A stack object that two goroutines share must escape to the heap first, so skipping stacks loses no race that gc finds.
    • Atomics: build gc's argument block on the stack and call __tsan_go_atomic*.
    • pc: __builtin_return_address(0), the instrumented instruction's caller address, for the report stack.
  • Link: the race build links librtcore_race.a, the shim and the TSan Go runtime object. The object is built from compiler-rt/lib/tsan/go with buildgo.sh, pinned to our LLVM version. The prebuilt race_linux.syso of gc is a fallback for the first experiment. Race executables are non-PIE (MARMOT_NOPIE=1 path), because the Go mapping wants globals below 0x10000000000.

Runtime (rtcore)

  • Start: __tsan_init(&thr, &proc, callback) before package init, __tsan_map_shadow over the arena and data/bss. Arena: in race builds, reserve at 0xc000000000 (MAP_FIXED_NOREPLACE, 64 GiB, the Go mapping allows 128 GiB).
  • Callback: RaceGetProcCmd (the current P's Processor), RaceSymbolizeCodeCmd (our pc table: function, file, line, inlined frames), RaceSymbolizeDataCmd (globals: symbol and offset, heap: the object's span).
  • Goroutines: rt_go calls __tsan_go_start(parent thr, &child thr, pc of the go statement). Gstart sets the context, rt_goexit calls __tsan_go_end. One __tsan_proc_create per P, destroyed when a P goes away.
  • Allocation: alloc_slow and the fast path call __tsan_malloc(thr, pc, p, size) in race builds (gc's racemalloc), so a reused address starts clean.
  • Sweep: freed objects get __tsan_free(p, size) (gc's racefree in sweep). This makes the sweep visit every freed object in race builds.
  • Regions (region.c) and the stack-to-heap optimizations of codegen are off in race builds. The parallel and concurrent GC stay: the collector is not instrumented.
  • Reports and exit: at exit __tsan_fini. Status 66 if races were found (gc's behavior, GORACE exitcode overrides). GORACE options pass to the runtime as gc does (halt_on_error, history_size, log_path, strip_path_prefix).
  • Stack depth: native goroutine stacks reserve 64 MiB, enough for TSan's report path without switching to g0 as gc does. wasm (64 KiB stacks) is out of scope.

Library (lib/src)

  • runtime: RaceAcquire, RaceRelease, RaceReleaseMerge, RaceRead, RaceWrite, RaceReadRange, RaceWriteRange, RaceDisable, RaceEnable, RaceErrors, as gc exports them for internal/race.
  • Channels (chan.go, chanring.go, select.go are ours): gc's annotations. A send releases on the slot address of a buffered channel and acquires on the receive. An unbuffered exchange acquires and releases both ways. Close releases on the channel, a receive of the close acquires. Len and cap read the channel.
  • sync: Mutex is our overlay over internal/sync and has no race calls now (rwmutex.go and waitgroup.go kept gc's). Add gc's acquire on Lock and release on Unlock. Pool drops a random quarter of Puts in race builds, as gc.
  • Semaphores (sema.go), timers (AfterFunc), finalizers, syscall and os I/O: gc's annotations, mostly through internal/race in the standard library.

Tests

  • Go's runtime/race/testdata (TestRace* must report, TestNoRace* must not): about 900 cases. Port the runner into tests/race and run it in conform as a mode.
  • Go's runtime/race output tests: the report text, with paths and goroutine numbers normalized.
  • The existing tests/run with -race: no reports, the same output (false positives show up here).
  • Cost: gc's race builds run 2 to 20 times slower with 5 to 10 times the memory. perfcmp does not gate race builds. A bench line tracks them.

Plan

  1. Spike (runtime, about two days): a C test links the Go TSan runtime with librtcore, and two goroutines race on a heap word. The report comes back with our symbolization. Proves the mapping (arena at 0xc000000000, non-PIE) and the callbacks.
  2. Codegen flag and pass (frontend, backend): -race, sanitize_thread, the pass, the race tag, the link line.
  3. Shim and runtime hooks: race.c, goroutines, Ps, allocation, sweep, exit status.
  4. Library annotations (runtime and front end): runtime API, channels, select, Mutex, Pool.
  5. Tests: race testdata in conform, output tests, tests/run under -race.
  6. linux/arm64 and darwin/arm64 (their Go mappings), then docs.

Spike result (2026-11-18, linux/amd64)

  • The Go flavor of the TSan runtime builds from our LLVM 22.1.8 source with our clang (buildgo.sh, 648 KB object, compiler-rt's own test passes). Its only dependencies are libc and pthread.
  • A C program links librtcore and starts goroutines with rt_go (GOMAXPROCS=4, M:N threads). It calls the race events by hand at the places where the shim will call them. RACE_MODE=racy: two goroutines increment a shared heap word. RACE_MODE=clean: the same under rt_mutex, with acquire and release around it, joined through a wait group.
  • Racy: one report in the format of gc. It has both accesses with their goroutine stacks (increment called from worker), and the heap block. It has "Goroutine 2 (running) created at" and "Goroutine 3 (finished) created at" with the function of the go statement. It ends with "Found 1 data race(s)" and exit status 66. Clean: no report, exit 0. 10 of 10 runs each.
  • Learned on the way:
    • data and bss need __tsan_map_shadow at start, as gc's racemapshadow. Without it the first acquire on a global faults in TSan's meta region.
    • The main goroutine needs its own __tsan_go_start from the initial context. Then it is "main goroutine", and the others are goroutine 2, 3, as in gc.
    • Every goroutine needs a function-entry event whose caller is the runtime: TSan's Go mode drops the outermost frame of every stack (gc's goexit frame).
    • The symbolize callback must answer for every pc, runtime C frames included. TSan skips an unanswered frame and then drops the user's outermost frame instead.
    • Non-PIE and a heap block in 0xc000000000-0xe000000000 work with MAP_FIXED_NOREPLACE beside libthe runtime's own 64 GiB arena reservation.

Risks and open questions

  • Fixed arena address: MAP_FIXED_NOREPLACE fails if something already maps there (rare at startup, fall back to an error that names the cause).
  • Codegen paths that bypass loads and stores the pass sees (runtime memmove calls, our own memcpy for big values) need __tsan_read_range/__tsan_write_range calls. Frontend lists them.
  • tier0 (the dev backend) has no LLVM pass. Race builds use the LLVM path in every mode, or tier0 learns the calls. Proposal: LLVM path only.
  • License: compiler-rt is Apache 2.0 with the LLVM exception. The object is linked into user binaries, as gc does. A project decision is open: ship our build or the .syso of gc.
  • darwin: the Go mapping differs. The arena reservation at 0xc000000000 must succeed there too.

On this page

Was this page helpful?

Report an issueOpen issues