-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
233 lines (196 loc) · 5.51 KB
/
main.go
File metadata and controls
233 lines (196 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//go:build integration
// +build integration
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/second-state/WasmEdge-go/wasmedge"
)
// processWasmFile processes a single WASM file through all stages
// It never panics - all errors are captured and returned in the result
func processWasmFile(filePath string) (result ExecutionResult) {
result.FilePath = filePath
result.FileName = filepath.Base(filePath)
result.FailureStage = StageNone
// Defer panic recovery to ensure we never crash
defer func() {
if r := recover(); r != nil {
result.Success = false
result.FailureStage = StageExecute
result.ErrorMessage = fmt.Sprintf("panic recovered: %v", r)
}
}()
// Initialize WasmEdge configuration
conf := wasmedge.NewConfigure()
defer conf.Release()
// Stage 1: Load WASM file
loader := wasmedge.NewLoader()
defer loader.Release()
ast, err := loader.LoadFile(filePath)
if err != nil {
result.Success = false
result.FailureStage = StageLoad
result.ErrorMessage = fmt.Sprintf("load failed: %v", err)
return result
}
defer ast.Release()
// Stage 2: Validate WASM module
validator := wasmedge.NewValidator()
defer validator.Release()
err = validator.Validate(ast)
if err != nil {
result.Success = false
result.FailureStage = StageValidate
result.ErrorMessage = fmt.Sprintf("validation failed: %v", err)
return result
}
// Stage 3: Instantiate WASM module
store := wasmedge.NewStore()
defer store.Release()
executor := wasmedge.NewExecutor()
defer executor.Release()
module, err := executor.Instantiate(store, ast)
if err != nil {
result.Success = false
result.FailureStage = StageInstantiate
result.ErrorMessage = fmt.Sprintf("instantiation failed: %v", err)
return result
}
defer module.Release()
// Stage 4: Execute the "process" function with input 1
funcInstance := module.FindFunction("process")
if funcInstance == nil {
result.Success = false
result.FailureStage = StageExecute
result.ErrorMessage = "function 'process' not found in module exports"
return result
}
// Call the function with input value 1
returns, err := executor.Invoke(funcInstance, int32(1))
if err != nil {
result.Success = false
result.FailureStage = StageExecute
result.ErrorMessage = fmt.Sprintf("execution failed: %v", err)
return result
}
// Success - capture return values
result.Success = true
result.ReturnValues = make([]interface{}, len(returns))
for i, v := range returns {
result.ReturnValues[i] = v
}
return result
}
// collectWasmFiles returns all .wasm files in the given directory
func collectWasmFiles(dirPath string) ([]string, error) {
var files []string
entries, err := os.ReadDir(dirPath)
if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
if filepath.Ext(entry.Name()) == ".wasm" {
fullPath := filepath.Join(dirPath, entry.Name())
files = append(files, fullPath)
}
}
return files, nil
}
// runFuzzer processes all WASM files in the directory and generates a report
func runFuzzer(dirPath string) (FuzzingReport, error) {
report := FuzzingReport{
Results: make([]ExecutionResult, 0),
FailureCounts: make(map[FailureStage]int),
}
// Initialize failure counts
report.FailureCounts[StageLoad] = 0
report.FailureCounts[StageValidate] = 0
report.FailureCounts[StageInstantiate] = 0
report.FailureCounts[StageExecute] = 0
// Collect all WASM files
files, err := collectWasmFiles(dirPath)
if err != nil {
return report, err
}
report.TotalFiles = len(files)
// Process each file sequentially (no concurrency)
for _, filePath := range files {
result := processWasmFile(filePath)
report.Results = append(report.Results, result)
if result.Success {
report.Passed++
} else {
report.Failed++
report.FailureCounts[result.FailureStage]++
}
}
return report, nil
}
// outputJSON writes the report as formatted JSON to stdout
func outputJSON(report FuzzingReport) error {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
return encoder.Encode(report)
}
func main() {
// Ensure we never panic from main
defer func() {
if r := recover(); r != nil {
errorResult := map[string]interface{}{
"error": "fatal panic in main",
"details": fmt.Sprintf("%v", r),
}
json.NewEncoder(os.Stderr).Encode(errorResult)
os.Exit(1)
}
}()
// Validate command line arguments
if len(os.Args) < 2 {
errorResult := map[string]string{
"error": "usage: wasm-fuzzer <directory>",
}
json.NewEncoder(os.Stderr).Encode(errorResult)
os.Exit(1)
}
dirPath := os.Args[1]
// Verify directory exists
info, err := os.Stat(dirPath)
if err != nil {
errorResult := map[string]string{
"error": "directory access failed",
"details": err.Error(),
}
json.NewEncoder(os.Stderr).Encode(errorResult)
os.Exit(1)
}
if !info.IsDir() {
errorResult := map[string]string{
"error": "path is not a directory",
"path": dirPath,
}
json.NewEncoder(os.Stderr).Encode(errorResult)
os.Exit(1)
}
// Initialize WasmEdge globally (required before any WasmEdge operations)
wasmedge.SetLogErrorLevel()
// Run the fuzzer
report, err := runFuzzer(dirPath)
if err != nil {
errorResult := map[string]string{
"error": "fuzzer execution failed",
"details": err.Error(),
}
json.NewEncoder(os.Stderr).Encode(errorResult)
os.Exit(1)
}
// Output results as JSON
if err := outputJSON(report); err != nil {
fmt.Fprintf(os.Stderr, "failed to encode JSON output: %v\n", err)
os.Exit(1)
}
}