Skip to content

Commit 7a7a839

Browse files
Panic at all syscalls in unix
1 parent 6fb913b commit 7a7a839

42 files changed

Lines changed: 2605 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/fork.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Fork
2+
on:
3+
schedule:
4+
- cron: "0 0 * * *"
5+
push:
6+
branches:
7+
- main
8+
workflow_dispatch:
9+
10+
jobs:
11+
fork:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: write
15+
16+
steps:
17+
- uses: actions/checkout@v5
18+
19+
- uses: actions/setup-go@v6
20+
21+
- name: Clean working tree
22+
run: |
23+
shopt -s extglob
24+
rm -rf !( .git | .github )
25+
26+
- name: Fetch upstream
27+
run: |
28+
git remote add upstream https://go.googlesource.com/sys || true
29+
git fetch upstream
30+
git reset --hard upstream/master
31+
git checkout origin/main .github/
32+
33+
- name: Modify
34+
working-directory: .github/workflows
35+
run: go run main.go ../../unix
36+
37+
- name: Commit
38+
run: |
39+
git config user.name "github-actions[bot]"
40+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
41+
git add -A
42+
git commit -m "Panic at all syscalls in unix"
43+
git tag v0.0.1
44+
45+
- name: Push
46+
run: |
47+
git push --force
48+
git push --force origin v0.0.1

.github/workflows/main.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"go/ast"
7+
"go/format"
8+
"go/parser"
9+
"go/token"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
)
14+
15+
func main() {
16+
if len(os.Args) < 2 {
17+
fmt.Println("Usage: go run main.go <directory>")
18+
os.Exit(1)
19+
}
20+
21+
dir := os.Args[1]
22+
if err := processDirectory(dir); err != nil {
23+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
24+
os.Exit(1)
25+
}
26+
}
27+
28+
func processDirectory(dir string) error {
29+
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
30+
if err != nil {
31+
return err
32+
}
33+
34+
if !info.IsDir() && strings.HasSuffix(path, ".go") {
35+
if err := processFile(path); err != nil {
36+
return fmt.Errorf("processing %s: %w", path, err)
37+
}
38+
fmt.Printf("Processed: %s\n", path)
39+
}
40+
41+
return nil
42+
})
43+
}
44+
45+
func processFile(filename string) error {
46+
fset := token.NewFileSet()
47+
content, err := os.ReadFile(filename)
48+
if err != nil {
49+
return err
50+
}
51+
52+
node, err := parser.ParseFile(fset, filename, content, parser.ParseComments)
53+
if err != nil {
54+
return err
55+
}
56+
57+
syscallFuncs := map[string]bool{
58+
"Syscall": true,
59+
"Syscall6": true,
60+
"RawSyscall": true,
61+
"RawSyscall6": true,
62+
"SyscallNoError": true,
63+
"RawSyscallNoError": true,
64+
}
65+
66+
type stmtInfo struct {
67+
pos token.Pos
68+
call *ast.CallExpr
69+
funcName string
70+
}
71+
var stmts []stmtInfo
72+
73+
ast.Inspect(node, func(n ast.Node) bool {
74+
switch stmt := n.(type) {
75+
case *ast.ExprStmt:
76+
// Handle direct calls like: SyscallNoError(...)
77+
if call, ok := stmt.X.(*ast.CallExpr); ok {
78+
if ident, ok := call.Fun.(*ast.Ident); ok {
79+
if syscallFuncs[ident.Name] {
80+
stmts = append(stmts, stmtInfo{
81+
pos: stmt.Pos(),
82+
call: call,
83+
funcName: ident.Name,
84+
})
85+
}
86+
}
87+
}
88+
case *ast.AssignStmt:
89+
// Handle assignments like: _, _, e1 := Syscall6(...)
90+
for _, expr := range stmt.Rhs {
91+
if call, ok := expr.(*ast.CallExpr); ok {
92+
if ident, ok := call.Fun.(*ast.Ident); ok {
93+
if syscallFuncs[ident.Name] {
94+
stmts = append(stmts, stmtInfo{
95+
pos: stmt.Pos(),
96+
call: call,
97+
funcName: ident.Name,
98+
})
99+
}
100+
}
101+
}
102+
}
103+
}
104+
return true
105+
})
106+
107+
if len(stmts) == 0 {
108+
return nil
109+
}
110+
111+
lines := bytes.Split(content, []byte("\n"))
112+
offset := 0
113+
114+
for _, stmt := range stmts {
115+
pos := fset.Position(stmt.pos)
116+
lineIdx := pos.Line - 1 + offset
117+
118+
if lineIdx < 0 || lineIdx >= len(lines) {
119+
continue
120+
}
121+
122+
if lineIdx > 0 && bytes.Contains(lines[lineIdx-1], []byte("panic(\"syscall not supported in wasm:")) {
123+
continue
124+
}
125+
126+
line := lines[lineIdx]
127+
indent := getIndentBytes(line)
128+
129+
callText := extractCallFromAST(stmt.call, fset, content)
130+
131+
panicLine := append(indent, []byte(fmt.Sprintf("panic(\"syscall not supported in wasm: %s\")", callText))...)
132+
133+
lines = insertLineBytes(lines, lineIdx, panicLine)
134+
offset++
135+
}
136+
137+
modified := bytes.Join(lines, []byte("\n"))
138+
formatted, err := format.Source(modified)
139+
if err != nil {
140+
fmt.Printf("Warning: could not format %s: %v\n", filename, err)
141+
return os.WriteFile(filename, modified, 0644)
142+
}
143+
144+
return os.WriteFile(filename, formatted, 0644)
145+
}
146+
147+
func extractCallFromAST(call *ast.CallExpr, fset *token.FileSet, content []byte) string {
148+
start := fset.Position(call.Pos()).Offset
149+
end := fset.Position(call.End()).Offset
150+
151+
if start >= 0 && end <= len(content) && start < end {
152+
return string(content[start:end])
153+
}
154+
155+
// Fallback (shouldn't happen)
156+
return "syscall"
157+
}
158+
159+
func getIndentBytes(line []byte) []byte {
160+
for i := 0; i < len(line); i++ {
161+
if line[i] != ' ' && line[i] != '\t' {
162+
return line[:i]
163+
}
164+
}
165+
return []byte{}
166+
}
167+
168+
func insertLineBytes(lines [][]byte, index int, newLine []byte) [][]byte {
169+
result := make([][]byte, 0, len(lines)+1)
170+
result = append(result, lines[:index]...)
171+
result = append(result, newLine)
172+
result = append(result, lines[index:]...)
173+
return result
174+
}

unix/affinity_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const cpuSetSize = _CPU_SETSIZE / _NCPUBITS
1717
type CPUSet [cpuSetSize]cpuMask
1818

1919
func schedAffinity(trap uintptr, pid int, set *CPUSet) error {
20+
panic("syscall not supported in wasm: RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))")
2021
_, _, e := RawSyscall(trap, uintptr(pid), uintptr(unsafe.Sizeof(*set)), uintptr(unsafe.Pointer(set)))
2122
if e != 0 {
2223
return errnoErr(e)

unix/fcntl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import "unsafe"
1313
var fcntl64Syscall uintptr = SYS_FCNTL
1414

1515
func fcntl(fd int, cmd, arg int) (int, error) {
16+
panic("syscall not supported in wasm: Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))")
1617
valptr, _, errno := Syscall(fcntl64Syscall, uintptr(fd), uintptr(cmd), uintptr(arg))
1718
var err error
1819
if errno != 0 {
@@ -28,6 +29,7 @@ func FcntlInt(fd uintptr, cmd, arg int) (int, error) {
2829

2930
// FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command.
3031
func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error {
32+
panic("syscall not supported in wasm: Syscall(fcntl64Syscall, fd, uintptr(cmd), uintptr(unsafe.Pointer(lk)))")
3133
_, _, errno := Syscall(fcntl64Syscall, fd, uintptr(cmd), uintptr(unsafe.Pointer(lk)))
3234
if errno == 0 {
3335
return nil

unix/ioctl_linux.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "unsafe"
1010
// associated with opened file descriptor fd, and returns a non-negative
1111
// integer that is returned by the ioctl syscall.
1212
func IoctlRetInt(fd int, req uint) (int, error) {
13+
panic("syscall not supported in wasm: Syscall(SYS_IOCTL, uintptr(fd), uintptr(req), 0)")
1314
ret, _, err := Syscall(SYS_IOCTL, uintptr(fd), uintptr(req), 0)
1415
if err != 0 {
1516
return 0, err

unix/syscall_dragonfly.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
163163
_p0 = unsafe.Pointer(&buf[0])
164164
bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
165165
}
166+
panic("syscall not supported in wasm: Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))")
166167
r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
167168
n = int(r0)
168169
if e1 != 0 {

unix/syscall_freebsd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
154154
_p0 = unsafe.Pointer(&buf[0])
155155
bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
156156
}
157+
panic("syscall not supported in wasm: Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))")
157158
r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
158159
n = int(r0)
159160
if e1 != 0 {

unix/syscall_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,7 @@ func Getrandom(buf []byte, flags int) (n int, err error) {
20142014
if len(buf) > 0 {
20152015
p = &buf[0]
20162016
}
2017+
panic("syscall not supported in wasm: Syscall(SYS_GETRANDOM, uintptr(unsafe.Pointer(p)), uintptr(len(buf)), uintptr(flags))")
20172018
r, _, e := Syscall(SYS_GETRANDOM, uintptr(unsafe.Pointer(p)), uintptr(len(buf)), uintptr(flags))
20182019
if e != 0 {
20192020
return 0, errnoErr(e)
@@ -2070,6 +2071,7 @@ func Prlimit(pid, resource int, newlimit, old *Rlimit) error {
20702071
// optional arguments arg2 through arg5 depending on option. It returns a
20712072
// non-negative integer that is returned by the prctl syscall.
20722073
func PrctlRetInt(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (int, error) {
2074+
panic("syscall not supported in wasm: Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0)")
20732075
ret, _, err := Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0)
20742076
if err != 0 {
20752077
return 0, err
@@ -2299,6 +2301,7 @@ func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) {
22992301
p = unsafe.Pointer(&iovs[0])
23002302
}
23012303

2304+
panic("syscall not supported in wasm: Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0)")
23022305
n, _, errno := Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0)
23032306
if errno != 0 {
23042307
return 0, syscall.Errno(errno)
@@ -2486,6 +2489,7 @@ func OpenByHandleAt(mountFD int, handle FileHandle, flags int) (fd int, err erro
24862489
// the value specified by arg and passes a dummy pointer to bufp.
24872490
func Klogset(typ int, arg int) (err error) {
24882491
var p unsafe.Pointer
2492+
panic("syscall not supported in wasm: Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg))")
24892493
_, _, errno := Syscall(SYS_SYSLOG, uintptr(typ), uintptr(p), uintptr(arg))
24902494
if errno != 0 {
24912495
return errnoErr(errno)

unix/syscall_linux_386.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func Shutdown(s, how int) (err error) {
270270
}
271271

272272
func Fstatfs(fd int, buf *Statfs_t) (err error) {
273+
panic("syscall not supported in wasm: Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))")
273274
_, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
274275
if e != 0 {
275276
err = e
@@ -282,6 +283,7 @@ func Statfs(path string, buf *Statfs_t) (err error) {
282283
if err != nil {
283284
return err
284285
}
286+
panic("syscall not supported in wasm: Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))")
285287
_, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
286288
if e != 0 {
287289
err = e

unix/syscall_linux_arm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func Utime(path string, buf *Utimbuf) error {
9797
//sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
9898

9999
func Fadvise(fd int, offset int64, length int64, advice int) (err error) {
100+
panic("syscall not supported in wasm: Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32))")
100101
_, _, e1 := Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32))
101102
if e1 != 0 {
102103
err = errnoErr(e1)
@@ -107,6 +108,7 @@ func Fadvise(fd int, offset int64, length int64, advice int) (err error) {
107108
//sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
108109

109110
func Fstatfs(fd int, buf *Statfs_t) (err error) {
111+
panic("syscall not supported in wasm: Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))")
110112
_, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
111113
if e != 0 {
112114
err = e
@@ -119,6 +121,7 @@ func Statfs(path string, buf *Statfs_t) (err error) {
119121
if err != nil {
120122
return err
121123
}
124+
panic("syscall not supported in wasm: Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))")
122125
_, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
123126
if e != 0 {
124127
err = e

0 commit comments

Comments
 (0)