Skip to content

Commit ef7c59a

Browse files
TotallyGamerJethajimehoshi
authored andcommitted
fakecgo: C functions require nosplit (#295)
* fakecgo: C functions require nosplit fakecgo runs on systemstack so we shouldn't ever split the stack. Fixes #287
1 parent f719fc5 commit ef7c59a

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ jobs:
9393
run: |
9494
env CGO_ENABLED=0 go test -shuffle=on -v -count=10 ./...
9595
env CGO_ENABLED=1 go test -shuffle=on -v -count=10 ./...
96+
# Compile without optimization to check potential stack overflow.
97+
# The option '-gcflags=all=-N -l' is often used at Visual Studio Code.
98+
# See also https://go.googlesource.com/vscode-go/+/HEAD/docs/debugging.md#launch.
99+
env CGO_ENABLED=0 go test "-gcflags=all=-N -l" -v ./...
100+
env CGO_ENABLED=1 go test "-gcflags=all=-N -l" -v ./...
96101
97102
- name: go test (Linux arm64)
98103
if: runner.os == 'Linux'

internal/fakecgo/gen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func setg_trampoline(setg uintptr, G uintptr)
3636
func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr
3737
3838
{{ range . -}}
39+
//go:nosplit
3940
func {{.Name}}(
4041
{{- range .Args -}}
4142
{{- if .Name -}}

internal/fakecgo/go_libinit.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,20 @@ func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe
5050
var err int
5151

5252
for tries = 0; tries < 20; tries++ {
53-
err = int(pthread_create(thread, attr, pfn, unsafe.Pointer(arg)))
53+
// inlined this call because it ran out of stack when inlining was disabled
54+
err = int(call5(pthread_createABI0, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(pfn), uintptr(unsafe.Pointer(arg)), 0))
5455
if err == 0 {
55-
pthread_detach(*thread)
56+
// inlined this call because it ran out of stack when inlining was disabled
57+
call5(pthread_detachABI0, uintptr(*thread), 0, 0, 0, 0)
5658
return 0
5759
}
5860
if err != int(syscall.EAGAIN) {
5961
return err
6062
}
6163
ts.Sec = 0
6264
ts.Nsec = (tries + 1) * 1000 * 1000 // Milliseconds.
63-
nanosleep(&ts, nil)
65+
// inlined this call because it ran out of stack when inlining was disabled
66+
call5(nanosleepABI0, uintptr(unsafe.Pointer(&ts)), 0, 0, 0, 0)
6467
}
6568
return int(syscall.EAGAIN)
6669
}

internal/fakecgo/libcgo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
package fakecgo
77

88
type (
9-
size_t uintptr
9+
size_t uintptr
10+
// Sources:
11+
// Darwin (32 bytes) - https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
12+
// FreeBSD (32 bytes) - https://github.com/DoctorWkt/xv6-freebsd/blob/d2a294c2a984baed27676068b15ed9a29b06ab6f/include/signal.h#L98C9-L98C21
13+
// Linux (128 bytes) - https://github.com/torvalds/linux/blob/ab75170520d4964f3acf8bb1f91d34cbc650688e/arch/x86/include/asm/signal.h#L25
1014
sigset_t [128]byte
1115
pthread_attr_t [64]byte
1216
pthread_t int

internal/fakecgo/symbols.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)