Skip to content

Commit

Permalink
fakecgo: C functions require nosplit (#295)
Browse files Browse the repository at this point in the history
* fakecgo: C functions require nosplit

fakecgo runs on systemstack so we shouldn't ever split the stack.

Fixes #287
  • Loading branch information
TotallyGamerJet authored Jan 6, 2025
1 parent ca651c3 commit a32290b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
# Compile without optimization to check potential stack overflow.
# The option '-gcflags=all=-N -l' is often used at Visual Studio Code.
# See also https://go.googlesource.com/vscode-go/+/HEAD/docs/debugging.md#launch.
# TODO: CGO_ENABLED=0 might cause SEGV. Fix this issue.
env CGO_ENABLED=0 go test "-gcflags=all=-N -l" -v ./...
env CGO_ENABLED=1 go test "-gcflags=all=-N -l" -v ./...
- name: go test (Linux arm64)
Expand Down
1 change: 1 addition & 0 deletions internal/fakecgo/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func setg_trampoline(setg uintptr, G uintptr)
func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr
{{ range . -}}
//go:nosplit
func {{.Name}}(
{{- range .Args -}}
{{- if .Name -}}
Expand Down
9 changes: 6 additions & 3 deletions internal/fakecgo/go_libinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe
var err int

for tries = 0; tries < 20; tries++ {
err = int(pthread_create(thread, attr, pfn, unsafe.Pointer(arg)))
// inlined this call because it ran out of stack when inlining was disabled
err = int(call5(pthread_createABI0, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(pfn), uintptr(unsafe.Pointer(arg)), 0))
if err == 0 {
pthread_detach(*thread)
// inlined this call because it ran out of stack when inlining was disabled
call5(pthread_detachABI0, uintptr(*thread), 0, 0, 0, 0)
return 0
}
if err != int(syscall.EAGAIN) {
return err
}
ts.Sec = 0
ts.Nsec = (tries + 1) * 1000 * 1000 // Milliseconds.
nanosleep(&ts, nil)
// inlined this call because it ran out of stack when inlining was disabled
call5(nanosleepABI0, uintptr(unsafe.Pointer(&ts)), 0, 0, 0, 0)
}
return int(syscall.EAGAIN)
}
6 changes: 5 additions & 1 deletion internal/fakecgo/libcgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
package fakecgo

type (
size_t uintptr
size_t uintptr
// Sources:
// Darwin (32 bytes) - https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/_types.h#L74
// FreeBSD (32 bytes) - https://github.com/DoctorWkt/xv6-freebsd/blob/d2a294c2a984baed27676068b15ed9a29b06ab6f/include/signal.h#L98C9-L98C21
// Linux (128 bytes) - https://github.com/torvalds/linux/blob/ab75170520d4964f3acf8bb1f91d34cbc650688e/arch/x86/include/asm/signal.h#L25
sigset_t [128]byte
pthread_attr_t [64]byte
pthread_t int
Expand Down
20 changes: 20 additions & 0 deletions internal/fakecgo/symbols.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a32290b

Please sign in to comment.