-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotoc.go
More file actions
440 lines (377 loc) · 11.4 KB
/
protoc.go
File metadata and controls
440 lines (377 loc) · 11.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package protoc
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"io/fs"
"os/exec"
"sync"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
// Protoc wraps a protoc WASI reactor module providing a high-level API
// for Protocol Buffer compilation.
type Protoc struct {
runtime wazero.Runtime
mod api.Module
// Memory management
malloc api.Function
free api.Function
// Protoc reactor functions
protocInit api.Function
protocRun api.Function
protocDestroy api.Function
// Plugin handler for spawning native plugin processes
pluginHandler PluginHandler
// Mutex for thread-safe Run calls (WASI is single-threaded)
mu sync.Mutex
// State
initialized bool
}
// PluginHandler handles spawning and communicating with protoc plugins.
// The default implementation uses os/exec to spawn native processes.
type PluginHandler interface {
// Communicate spawns a plugin process and handles IPC.
// program: plugin program name (e.g., "protoc-gen-go")
// searchPath: if true, search PATH for the program
// input: serialized CodeGeneratorRequest
// Returns: serialized CodeGeneratorResponse, or error
Communicate(ctx context.Context, program string, searchPath bool, input []byte) ([]byte, error)
}
// DefaultPluginHandler spawns plugin processes using os/exec.
type DefaultPluginHandler struct{}
// Communicate spawns a plugin and communicates via stdin/stdout.
func (h *DefaultPluginHandler) Communicate(ctx context.Context, program string, searchPath bool, input []byte) ([]byte, error) {
var cmd *exec.Cmd
if searchPath {
cmd = exec.CommandContext(ctx, program)
} else {
cmd = exec.CommandContext(ctx, program)
}
cmd.Stdin = bytes.NewReader(input)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
if stderr.Len() > 0 {
return nil, fmt.Errorf("%s: %w: %s", program, err, stderr.String())
}
return nil, fmt.Errorf("%s: %w", program, err)
}
return stdout.Bytes(), nil
}
// Config holds configuration for creating a new Protoc instance.
type Config struct {
// Stdin is the standard input for protoc. Default: empty.
Stdin io.Reader
// Stdout is the standard output for protoc. Default: discard.
Stdout io.Writer
// Stderr is the standard error for protoc. Default: discard.
Stderr io.Writer
// FS is the filesystem for reading .proto files and writing output.
// Default: no filesystem access.
FS fs.FS
// FSConfig allows configuring the wazero filesystem.
// If set, FS is ignored.
FSConfig wazero.FSConfig
// PluginHandler handles spawning plugin processes.
// Default: DefaultPluginHandler (uses os/exec).
PluginHandler PluginHandler
}
// CompileProtoc compiles the embedded protoc WASM module.
// The compiled module can be reused across multiple Protoc instances.
func CompileProtoc(ctx context.Context, r wazero.Runtime) (wazero.CompiledModule, error) {
return r.CompileModule(ctx, ProtocWASM)
}
// NewProtoc creates a new Protoc instance using the embedded WASM reactor.
// Call Close() when done to release resources.
func NewProtoc(ctx context.Context, r wazero.Runtime, cfg *Config) (*Protoc, error) {
// Compile the module
compiled, err := CompileProtoc(ctx, r)
if err != nil {
return nil, err
}
return NewProtocWithModule(ctx, r, compiled, cfg)
}
// NewProtocWithModule creates a new Protoc instance using a pre-compiled module.
func NewProtocWithModule(ctx context.Context, r wazero.Runtime, compiled wazero.CompiledModule, cfg *Config) (*Protoc, error) {
if cfg == nil {
cfg = &Config{}
}
// Set up plugin handler
pluginHandler := cfg.PluginHandler
if pluginHandler == nil {
pluginHandler = &DefaultPluginHandler{}
}
// Create the Protoc instance first so we can reference it in host functions
p := &Protoc{
runtime: r,
pluginHandler: pluginHandler,
}
// Register host functions for plugin communication
_, err := r.NewHostModuleBuilder(ImportModuleProtoc).
NewFunctionBuilder().
WithGoModuleFunction(api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) {
p.hostPluginCommunicate(ctx, mod, stack)
}), []api.ValueType{
api.ValueTypeI32, // program_ptr
api.ValueTypeI32, // program_len
api.ValueTypeI32, // search_path
api.ValueTypeI32, // input_ptr
api.ValueTypeI32, // input_len
api.ValueTypeI32, // output_ptr (pointer to pointer)
api.ValueTypeI32, // output_len (pointer to uint32)
api.ValueTypeI32, // error_ptr (pointer to pointer)
api.ValueTypeI32, // error_len (pointer to uint32)
}, []api.ValueType{api.ValueTypeI32}).
Export(ImportPluginCommunicate).
Instantiate(ctx)
if err != nil {
return nil, fmt.Errorf("failed to register host functions: %w", err)
}
// Instantiate WASI
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
return nil, fmt.Errorf("failed to instantiate WASI: %w", err)
}
// Build module config
modCfg := wazero.NewModuleConfig().WithName(ProtocWASMFilename)
if cfg.Stdin != nil {
modCfg = modCfg.WithStdin(cfg.Stdin)
}
if cfg.Stdout != nil {
modCfg = modCfg.WithStdout(cfg.Stdout)
}
if cfg.Stderr != nil {
modCfg = modCfg.WithStderr(cfg.Stderr)
}
if cfg.FSConfig != nil {
modCfg = modCfg.WithFSConfig(cfg.FSConfig)
} else if cfg.FS != nil {
modCfg = modCfg.WithFSConfig(wazero.NewFSConfig().WithFSMount(cfg.FS, "/"))
}
// Instantiate the module (reactor mode - no _start)
mod, err := r.InstantiateModule(ctx, compiled, modCfg)
if err != nil {
return nil, fmt.Errorf("failed to instantiate module: %w", err)
}
// Call _initialize if present
if initFn := mod.ExportedFunction("_initialize"); initFn != nil {
if _, err := initFn.Call(ctx); err != nil {
mod.Close(ctx)
return nil, fmt.Errorf("_initialize failed: %w", err)
}
}
p.mod = mod
p.malloc = mod.ExportedFunction(ExportMalloc)
p.free = mod.ExportedFunction(ExportFree)
p.protocInit = mod.ExportedFunction(ExportProtocInit)
p.protocRun = mod.ExportedFunction(ExportProtocRun)
p.protocDestroy = mod.ExportedFunction(ExportProtocDestroy)
// Validate required exports
if p.malloc == nil {
mod.Close(ctx)
return nil, errors.New("missing export: " + ExportMalloc)
}
if p.free == nil {
mod.Close(ctx)
return nil, errors.New("missing export: " + ExportFree)
}
if p.protocInit == nil {
mod.Close(ctx)
return nil, errors.New("missing export: " + ExportProtocInit)
}
if p.protocRun == nil {
mod.Close(ctx)
return nil, errors.New("missing export: " + ExportProtocRun)
}
if p.protocDestroy == nil {
mod.Close(ctx)
return nil, errors.New("missing export: " + ExportProtocDestroy)
}
return p, nil
}
// hostPluginCommunicate handles plugin subprocess communication from WASM.
func (p *Protoc) hostPluginCommunicate(ctx context.Context, mod api.Module, stack []uint64) {
programPtr := uint32(stack[0])
programLen := uint32(stack[1])
searchPath := int32(stack[2]) != 0
inputPtr := uint32(stack[3])
inputLen := uint32(stack[4])
outputPtrPtr := uint32(stack[5])
outputLenPtr := uint32(stack[6])
errorPtrPtr := uint32(stack[7])
errorLenPtr := uint32(stack[8])
mem := mod.Memory()
// Read program name
programBytes, ok := mem.Read(programPtr, programLen)
if !ok {
stack[0] = api.EncodeI32(-1)
return
}
program := string(programBytes)
// Read input data
inputData, ok := mem.Read(inputPtr, inputLen)
if !ok {
stack[0] = api.EncodeI32(-1)
return
}
// Call the plugin handler
output, err := p.pluginHandler.Communicate(ctx, program, searchPath, inputData)
if err != nil {
// Write error message
errMsg := err.Error()
errPtr, allocErr := p.allocBytes(ctx, []byte(errMsg))
if allocErr == nil {
p.writePtr(mem, errorPtrPtr, errPtr)
p.writeUint32(mem, errorLenPtr, uint32(len(errMsg)))
}
p.writePtr(mem, outputPtrPtr, 0)
p.writeUint32(mem, outputLenPtr, 0)
stack[0] = api.EncodeI32(1)
return
}
// Write output
outPtr, allocErr := p.allocBytes(ctx, output)
if allocErr != nil {
stack[0] = api.EncodeI32(-1)
return
}
p.writePtr(mem, outputPtrPtr, outPtr)
p.writeUint32(mem, outputLenPtr, uint32(len(output)))
p.writePtr(mem, errorPtrPtr, 0)
p.writeUint32(mem, errorLenPtr, 0)
stack[0] = 0
}
// Init initializes the protoc reactor.
// This must be called before Run.
func (p *Protoc) Init(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.initialized {
return nil
}
results, err := p.protocInit.Call(ctx)
if err != nil {
return fmt.Errorf("protoc_init failed: %w", err)
}
if int32(results[0]) != 0 {
return errors.New("protoc_init returned error")
}
p.initialized = true
return nil
}
// Run runs protoc with the given arguments.
// Init() must be called first.
// Returns the protoc exit code (0 on success).
func (p *Protoc) Run(ctx context.Context, args []string) (int, error) {
p.mu.Lock()
defer p.mu.Unlock()
if !p.initialized {
return 1, errors.New("protoc not initialized, call Init() first")
}
if len(args) == 0 {
args = []string{"protoc"}
}
// Allocate argv
argc := len(args)
argPtrs := make([]uint32, argc)
for i, arg := range args {
ptr, err := p.allocString(ctx, arg)
if err != nil {
// Free already allocated
for j := 0; j < i; j++ {
p.freePtr(ctx, argPtrs[j])
}
return 1, err
}
argPtrs[i] = ptr
}
// Allocate argv array
argvPtr, err := p.allocArgv(ctx, argPtrs)
if err != nil {
for _, ptr := range argPtrs {
p.freePtr(ctx, ptr)
}
return 1, err
}
// Call protoc_run
results, err := p.protocRun.Call(ctx, uint64(argc), uint64(argvPtr))
// Free memory
p.freePtr(ctx, argvPtr)
for _, ptr := range argPtrs {
p.freePtr(ctx, ptr)
}
if err != nil {
return 1, fmt.Errorf("protoc_run failed: %w", err)
}
return int(int32(results[0])), nil
}
// Close destroys the protoc reactor and releases resources.
func (p *Protoc) Close(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.initialized && p.protocDestroy != nil {
p.protocDestroy.Call(ctx)
p.initialized = false
}
if p.mod != nil {
return p.mod.Close(ctx)
}
return nil
}
// Memory helpers
func (p *Protoc) allocString(ctx context.Context, s string) (uint32, error) {
bytes := append([]byte(s), 0) // null-terminated
return p.allocBytes(ctx, bytes)
}
func (p *Protoc) allocBytes(ctx context.Context, data []byte) (uint32, error) {
results, err := p.malloc.Call(ctx, uint64(len(data)))
if err != nil {
return 0, err
}
ptr := uint32(results[0])
if ptr == 0 {
return 0, errors.New("malloc returned null")
}
if !p.mod.Memory().Write(ptr, data) {
p.free.Call(ctx, uint64(ptr))
return 0, errors.New("failed to write to memory")
}
return ptr, nil
}
func (p *Protoc) allocArgv(ctx context.Context, ptrs []uint32) (uint32, error) {
size := len(ptrs) * 4
results, err := p.malloc.Call(ctx, uint64(size))
if err != nil {
return 0, err
}
argvPtr := uint32(results[0])
if argvPtr == 0 {
return 0, errors.New("malloc returned null for argv")
}
for i, ptr := range ptrs {
ptrBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(ptrBytes, ptr)
p.mod.Memory().Write(argvPtr+uint32(i*4), ptrBytes)
}
return argvPtr, nil
}
func (p *Protoc) freePtr(ctx context.Context, ptr uint32) {
if ptr != 0 {
p.free.Call(ctx, uint64(ptr))
}
}
func (p *Protoc) writePtr(mem api.Memory, addr, value uint32) {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, value)
mem.Write(addr, buf)
}
func (p *Protoc) writeUint32(mem api.Memory, addr, value uint32) {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, value)
mem.Write(addr, buf)
}