|
| 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 | +} |
0 commit comments