diff --git a/builder/musl.go b/builder/musl.go index 8130981e6c..ecae118e47 100644 --- a/builder/musl.go +++ b/builder/musl.go @@ -128,6 +128,7 @@ var libMusl = Library{ "mman/*.c", "math/*.c", "multibyte/*.c", + "signal/" + arch + "/*.s", "signal/*.c", "stdio/*.c", "string/*.c", diff --git a/compileopts/target.go b/compileopts/target.go index 41a7babd91..7c0592368e 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -390,7 +390,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) { ) spec.ExtraFiles = append(spec.ExtraFiles, "src/runtime/os_darwin.c", - "src/runtime/runtime_unix.c") + "src/runtime/runtime_unix.c", + "src/runtime/signal.c") case "linux": spec.Linker = "ld.lld" spec.RTLib = "compiler-rt" @@ -411,7 +412,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) { spec.CFlags = append(spec.CFlags, "-mno-outline-atomics") } spec.ExtraFiles = append(spec.ExtraFiles, - "src/runtime/runtime_unix.c") + "src/runtime/runtime_unix.c", + "src/runtime/signal.c") case "windows": spec.Linker = "ld.lld" spec.Libc = "mingw-w64" diff --git a/main_test.go b/main_test.go index 62eb5c51b9..532ddf30c6 100644 --- a/main_test.go +++ b/main_test.go @@ -75,6 +75,7 @@ func TestBuild(t *testing.T) { "oldgo/", "print.go", "reflect.go", + "signal.go", "slice.go", "sort.go", "stdlib.go", @@ -213,6 +214,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) { // 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" for _, name := range tests { if options.GOOS == "linux" && (options.GOARCH == "arm" || options.GOARCH == "386") { @@ -277,6 +279,13 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) { continue } } + if isWebAssembly || isBaremetal || options.GOOS == "windows" { + switch name { + case "signal.go": + // Signals only work on POSIX-like systems. + continue + } + } name := name // redefine to avoid race condition t.Run(name, func(t *testing.T) { diff --git a/src/os/signal/signal.go b/src/os/signal/signal.go deleted file mode 100644 index 41ceaf4853..0000000000 --- a/src/os/signal/signal.go +++ /dev/null @@ -1,14 +0,0 @@ -// 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. - -package signal - -import ( - "os" -) - -// Just stubbing the functions for now since signal handling is not yet implemented in tinygo -func Reset(sig ...os.Signal) {} -func Ignore(sig ...os.Signal) {} -func Notify(c chan<- os.Signal, sig ...os.Signal) {} diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index ba5d5a5938..47e42a702f 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -3,6 +3,8 @@ package runtime import ( + "math/bits" + "sync/atomic" "unsafe" ) @@ -12,6 +14,14 @@ func libc_write(fd int32, buf unsafe.Pointer, count uint) int //export usleep func usleep(usec uint) int +//export pause +func pause() int32 + +// int sigsuspend(const sigset_t *mask); +// +//export sigsuspend +func sigsuspend(sigset_t unsafe.Pointer) int32 + // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); // Note: off_t is defined as int64 because: // - musl (used on Linux) always defines it as int64 @@ -217,8 +227,22 @@ func nanosecondsToTicks(ns int64) timeUnit { } func sleepTicks(d timeUnit) { + // Check for incoming signals. + if checkSignals() { + // Received a signal, so there's probably at least one goroutine that's + // runnable again. + return + } + + // TODO: there is a race condition here. If a signal arrives between + // checkSignals() and usleep(), the usleep() call will not exit early so the + // signal is delayed until usleep finishes or another signal arrives. + // timeUnit is in nanoseconds, so need to convert to microseconds here. - usleep(uint(d) / 1000) + result := usleep(uint(d) / 1000) + if result != 0 { + checkSignals() + } } func getTime(clock int32) uint64 { @@ -307,3 +331,209 @@ func growHeap() bool { setHeapEnd(heapStart + heapSize) return true } + +func init() { + // Set up a channel to receive signals into. + signalChan = make(chan uint32, 1) +} + +var signalChan chan uint32 + +// Simple boolean that's true when any signals have been registered. +var hasSignals uint32 + +// Mask of signals that have been received. The signal handler atomically ORs +// signals into this value. +var receivedSignals uint32 + +// Bitmap keeping track of enabled signals. +var activeSignals uint32 + +// Bitmap keeping track of masked/disabled signals. +var maskedSignals uint32 + +//go:linkname signal_enable os/signal.signal_enable +func signal_enable(s uint32) { + if s >= 32 { + // TODO: to support higher signal numbers, we need to turn + // receivedSignals into a uint32 array. + runtimePanicAt(returnAddress(0), "unsupported signal number") + } + atomic.StoreUint32(&hasSignals, 1) + + // update the enabled signals bitmap + val := atomic.LoadUint32(&activeSignals) + atomic.CompareAndSwapUint32(&receivedSignals, val, val|(1<= 32 { + // TODO: to support higher signal numbers, we need to turn + // receivedSignals into a uint32 array. + runtimePanicAt(returnAddress(0), "unsupported signal number") + } + tinygo_signal_ignore(s) +} + +//go:linkname signal_disable os/signal.signal_disable +func signal_disable(s uint32) { + if s >= 32 { + // TODO: to support higher signal numbers, we need to turn + // receivedSignals into a uint32 array. + runtimePanicAt(returnAddress(0), "unsupported signal number") + } + tinygo_signal_disable(s) +} + +//go:linkname signal_waitUntilIdle os/signal.signalWaitUntilIdle +func signal_waitUntilIdle() { + // Make sure all signals are sent on the channel. + for atomic.LoadUint32(&receivedSignals) != 0 { + checkSignals() + Gosched() + } + + // Make sure all signals are processed. + for len(signalChan) != 0 { + Gosched() + } +} + +//export tinygo_signal_enable +func tinygo_signal_enable(s uint32) + +//export tinygo_signal_ignore +func tinygo_signal_ignore(s uint32) + +//export tinygo_signal_disable +func tinygo_signal_disable(s uint32) + +//export tinygo_mask_signals +func tinygo_mask_signals(mask uint32) + +//export tinygo_unmask_signals +func tinygo_unmask_signals(mask uint32) + +//export tinygo_sigsuspend +func tinygo_sigsuspend(mask uint32) + +// void tinygo_signal_handler(int sig); +// +//export tinygo_signal_handler +func tinygo_signal_handler(s int32) { + // This loop is essentially the atomic equivalent of the following: + // + // receivedSignals |= 1 << s + // + // TODO: use atomic.Uint32.And once we drop support for Go 1.22 instead of + // this loop. + for { + mask := uint32(1) << uint32(s) + val := atomic.LoadUint32(&receivedSignals) + swapped := atomic.CompareAndSwapUint32(&receivedSignals, val, val|mask) + if swapped { + break + } + } +} + +//go:linkname signal_recv os/signal.signal_recv +func signal_recv() uint32 { + // Function called from os/signal to get the next received signal. + val := <-signalChan + checkSignals() + return val +} + +// Atomically find a signal that previously occurred and send it into the +// signalChan channel. Return true if at least one signal was delivered this +// way, false otherwise. +func checkSignals() bool { + gotSignals := false + for { + // Extract the lowest numbered signal number from receivedSignals. + val := atomic.LoadUint32(&receivedSignals) + if val == 0 { + // There is no signal ready to be received by the program (common + // case). + return gotSignals + } + num := uint32(bits.TrailingZeros32(val)) + + // Do a non-blocking send on signalChan. + select { + case signalChan <- num: + // There was room free in the channel, so remove the signal number + // from the receivedSignals mask. + gotSignals = true + default: + // Could not send the signal number on the channel. This means + // there's still a signal pending. In that case, let it be received + // at which point checkSignals is called again to put the next one + // in the channel buffer. + return gotSignals + } + + // Atomically clear the signal number from receivedSignals. + // TODO: use atomic.Uint32.Or once we drop support for Go 1.22 instead + // of this loop. + for { + newVal := val &^ (1 << num) + swapped := atomic.CompareAndSwapUint32(&receivedSignals, val, newVal) + if swapped { + break + } + val = atomic.LoadUint32(&receivedSignals) + } + } +} + +func sigSuspend() { + signals := atomic.LoadUint32(&activeSignals) + tinygo_sigsuspend(signals) +} + +// mask a set of signals defined by the activeSignals bitmap (32bit) +// returns the C style sigset_t pointer. +func maskActiveSignals() { + signals := atomic.LoadUint32(&activeSignals) + tinygo_mask_signals(signals) +} + +// unmask a set of signals defined by the activeSignals bitmap (32bit) +func unmaskActiveSignals() { + signals := atomic.LoadUint32(&activeSignals) + tinygo_unmask_signals(signals) +} + +func waitForEvents() { + if atomic.LoadUint32(&hasSignals) != 0 { + // TODO: there is a race condition here. If a signal arrives between + // checkSignals() and pause(), pause() will not exit early but instead + // be delayed until the next signal arrives. + // We should use something like this instead to avoid it: + // - mask all active signals + // - run checkSignals() + // - run sigsuspend() with all active signals + // - unmask all active signals + // For a longer explanation of the problem, see: + // https://www.cipht.net/2023/11/30/perils-of-pause.html + + // When a signal arrives while masked by the process, it remains pending + // until the process unmasks it. + maskActiveSignals() + + checkSignals() + sigSuspend() + + checkSignals() + unmaskActiveSignals() + + } else { + // The program doesn't use signals, so this is a deadlock. + runtimePanic("deadlocked: no event source") + } +} diff --git a/src/runtime/signal.c b/src/runtime/signal.c new file mode 100644 index 0000000000..9015364ff7 --- /dev/null +++ b/src/runtime/signal.c @@ -0,0 +1,76 @@ +//go:build none + +// Ignore the //go:build above. This file is manually included on Linux and +// MacOS to provide os/signal support. + +#include +#include +#include + +// Signal handler in the runtime. +void tinygo_signal_handler(int sig); + +// Enable a signal from the runtime. +void tinygo_signal_enable(uint32_t sig) +{ + struct sigaction act = {0}; + act.sa_handler = &tinygo_signal_handler; + sigaction(sig, &act, NULL); +} + +void tinygo_signal_ignore(uint32_t sig) +{ + struct sigaction act = {0}; + act.sa_handler = SIG_IGN; + sigaction(sig, &act, NULL); +} + +void tinygo_signal_disable(uint32_t sig) +{ + struct sigaction act = {0}; + act.sa_handler = SIG_DFL; + sigaction(sig, &act, NULL); +} + +// https://www.cipht.net/2023/11/30/perils-of-pause.html#text-3 +void tinygo_mask_signals(uint32_t mask) +{ + sigset_t set, prev; + sigemptyset(&set); + for (int i = 0; i < 32; i++) + { + if (mask & (1 << i)) + { + sigaddset(&set, i); + } + } + sigprocmask(SIG_BLOCK, &set, &prev); +} + +void tinygo_unmask_signals(uint32_t mask) +{ + sigset_t set, prev; + sigemptyset(&set); + for (int i = 0; i < 32; i++) + { + if (mask & (1 << i)) + { + sigaddset(&set, i); + } + } + sigprocmask(SIG_UNBLOCK, &set, &prev); +} + +void tinygo_sigsuspend(uint32_t mask) +{ + sigset_t set; + sigemptyset(&set); + for (int i = 0; i < 32; i++) + { + if (mask & (1 << i)) + { + sigaddset(&set, i); + } + } + sigsuspend(&set); +} diff --git a/src/runtime/wait_other.go b/src/runtime/wait_other.go index b51d4b64b6..f1487e3969 100644 --- a/src/runtime/wait_other.go +++ b/src/runtime/wait_other.go @@ -1,4 +1,4 @@ -//go:build !tinygo.riscv && !cortexm +//go:build !tinygo.riscv && !cortexm && !(linux && !baremetal && !tinygo.wasm && !nintendoswitch) && !darwin package runtime diff --git a/testdata/signal.go b/testdata/signal.go new file mode 100644 index 0000000000..a82991f086 --- /dev/null +++ b/testdata/signal.go @@ -0,0 +1,42 @@ +package main + +// Test POSIX signals. +// TODO: run `tinygo test os/signal` instead, once CGo errno return values are +// supported. + +import ( + "os" + "os/signal" + "syscall" + "time" +) + +func main() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGUSR1) + + // Wait for signals to arrive. + go func() { + for sig := range c { + if sig == syscall.SIGUSR1 { + println("got expected signal") + } else { + println("got signal:", sig.String()) + } + } + }() + + // Send the signal. + syscall.Kill(syscall.Getpid(), syscall.SIGUSR1) + + time.Sleep(time.Millisecond * 100) + + // Stop notifying. + // (This is just a smoke test, it's difficult to test the default behavior + // in a unit test). + signal.Ignore(syscall.SIGUSR1) + + signal.Stop(c) + + println("exiting signal program") +} diff --git a/testdata/signal.txt b/testdata/signal.txt new file mode 100644 index 0000000000..c4726d7174 --- /dev/null +++ b/testdata/signal.txt @@ -0,0 +1,2 @@ +got expected signal +exiting signal program