Skip to content

Commit 194396d

Browse files
aykevldeadprogram
authored andcommitted
unix: print a message when a fatal signal happens
Print a message for SIGBUS, SIGSEGV, and SIGILL when they happen. These signals are always fatal, but it's very useful to know which of them happened. Also, it prints the location in the binary which can then be parsed by `tinygo run` (see #4383). While this does add some extra binary size, it's for Linux and MacOS (systems that typically have plenty of RAM/storage) and could be very useful when debugging some low-level crash such as a runtime bug.
1 parent 815784b commit 194396d

File tree

12 files changed

+162
-7
lines changed

12 files changed

+162
-7
lines changed

compileopts/target.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
388388
"-arch", llvmarch,
389389
"-platform_version", "macos", platformVersion, platformVersion,
390390
)
391+
spec.ExtraFiles = append(spec.ExtraFiles,
392+
"src/runtime/runtime_unix.c")
391393
case "linux":
392394
spec.Linker = "ld.lld"
393395
spec.RTLib = "compiler-rt"
@@ -407,6 +409,8 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
407409
// proper threading.
408410
spec.CFlags = append(spec.CFlags, "-mno-outline-atomics")
409411
}
412+
spec.ExtraFiles = append(spec.ExtraFiles,
413+
"src/runtime/runtime_unix.c")
410414
case "windows":
411415
spec.Linker = "ld.lld"
412416
spec.Libc = "mingw-w64"

lib/macos-minimal-sdk

src/runtime/arch_386.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const deferExtraRegs = 0
99

1010
const callInstSize = 5 // "call someFunction" is 5 bytes
1111

12-
const linux_MAP_ANONYMOUS = 0x20
12+
const (
13+
linux_MAP_ANONYMOUS = 0x20
14+
linux_SIGBUS = 7
15+
linux_SIGILL = 4
16+
linux_SIGSEGV = 11
17+
)
1318

1419
// Align on word boundary.
1520
func align(ptr uintptr) uintptr {

src/runtime/arch_amd64.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const deferExtraRegs = 0
99

1010
const callInstSize = 5 // "call someFunction" is 5 bytes
1111

12-
const linux_MAP_ANONYMOUS = 0x20
12+
const (
13+
linux_MAP_ANONYMOUS = 0x20
14+
linux_SIGBUS = 7
15+
linux_SIGILL = 4
16+
linux_SIGSEGV = 11
17+
)
1318

1419
// Align a pointer.
1520
// Note that some amd64 instructions (like movaps) expect 16-byte aligned

src/runtime/arch_arm.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const deferExtraRegs = 0
1111

1212
const callInstSize = 4 // "bl someFunction" is 4 bytes
1313

14-
const linux_MAP_ANONYMOUS = 0x20
14+
const (
15+
linux_MAP_ANONYMOUS = 0x20
16+
linux_SIGBUS = 7
17+
linux_SIGILL = 4
18+
linux_SIGSEGV = 11
19+
)
1520

1621
// Align on the maximum alignment for this platform (double).
1722
func align(ptr uintptr) uintptr {

src/runtime/arch_arm64.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const deferExtraRegs = 0
99

1010
const callInstSize = 4 // "bl someFunction" is 4 bytes
1111

12-
const linux_MAP_ANONYMOUS = 0x20
12+
const (
13+
linux_MAP_ANONYMOUS = 0x20
14+
linux_SIGBUS = 7
15+
linux_SIGILL = 4
16+
linux_SIGSEGV = 11
17+
)
1318

1419
// Align on word boundary.
1520
func align(ptr uintptr) uintptr {

src/runtime/arch_mips.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const deferExtraRegs = 0
99

1010
const callInstSize = 8 // "jal someFunc" is 4 bytes, plus a MIPS delay slot
1111

12-
const linux_MAP_ANONYMOUS = 0x800
12+
const (
13+
linux_MAP_ANONYMOUS = 0x800
14+
linux_SIGBUS = 10
15+
linux_SIGILL = 4
16+
linux_SIGSEGV = 11
17+
)
1318

1419
// It appears that MIPS has a maximum alignment of 8 bytes.
1520
func align(ptr uintptr) uintptr {

src/runtime/arch_mipsle.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const deferExtraRegs = 0
99

1010
const callInstSize = 8 // "jal someFunc" is 4 bytes, plus a MIPS delay slot
1111

12-
const linux_MAP_ANONYMOUS = 0x800
12+
const (
13+
linux_MAP_ANONYMOUS = 0x800
14+
linux_SIGBUS = 10
15+
linux_SIGILL = 4
16+
linux_SIGSEGV = 11
17+
)
1318

1419
// It appears that MIPS has a maximum alignment of 8 bytes.
1520
func align(ptr uintptr) uintptr {

src/runtime/os_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ const (
2222
clock_MONOTONIC_RAW = 4
2323
)
2424

25+
// Source:
26+
// https://opensource.apple.com/source/xnu/xnu-7195.141.2/bsd/sys/signal.h.auto.html
27+
const (
28+
sig_SIGBUS = 10
29+
sig_SIGILL = 4
30+
sig_SIGSEGV = 11
31+
)
32+
2533
// https://opensource.apple.com/source/xnu/xnu-7195.141.2/EXTERNAL_HEADERS/mach-o/loader.h.auto.html
2634
type machHeader struct {
2735
magic uint32

src/runtime/os_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const (
2323
clock_MONOTONIC_RAW = 4
2424
)
2525

26+
const (
27+
sig_SIGBUS = linux_SIGBUS
28+
sig_SIGILL = linux_SIGILL
29+
sig_SIGSEGV = linux_SIGSEGV
30+
)
31+
2632
// For the definition of the various header structs, see:
2733
// https://refspecs.linuxfoundation.org/elf/elf.pdf
2834
// Also useful:

0 commit comments

Comments
 (0)