-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaho_corasick_wazero.go
370 lines (308 loc) · 8.99 KB
/
aho_corasick_wazero.go
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
//go:build !tinygo.wasm && !aho_corasick_cgo
package aho_corasick
import (
"context"
_ "embed"
"encoding/binary"
"errors"
"sync"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
var (
errFailedWrite = errors.New("failed to read from wasm memory")
errFailedRead = errors.New("failed to read from wasm memory")
)
//go:embed wasm/aho_corasick.wasm
var ahocorasickWasm []byte
var (
wasmRT wazero.Runtime
wasmCompiled wazero.CompiledModule
)
type ahoCorasickABI struct {
new_matcher api.Function
delete_matcher api.Function
find_iter api.Function
find_iter_next api.Function
find_iter_delete api.Function
overlapping_iter api.Function
overlapping_iter_next api.Function
overlapping_iter_delete api.Function
matches api.Function
matches_delete api.Function
malloc api.Function
free api.Function
wasmMemory api.Memory
mod api.Module
callStack []uint64
memory sharedMemory
mu sync.Mutex
}
func init() {
ctx := context.Background()
rt := wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
code, err := rt.CompileModule(ctx, ahocorasickWasm)
if err != nil {
panic(err)
}
wasmCompiled = code
wasmRT = rt
}
func newABI() *ahoCorasickABI {
ctx := context.Background()
mod, err := wasmRT.InstantiateModule(ctx, wasmCompiled, wazero.NewModuleConfig().WithName(""))
if err != nil {
panic(err)
}
callStack := make([]uint64, 6)
return &ahoCorasickABI{
new_matcher: mod.ExportedFunction("new_matcher"),
delete_matcher: mod.ExportedFunction("delete_matcher"),
find_iter: mod.ExportedFunction("find_iter"),
find_iter_next: mod.ExportedFunction("find_iter_next"),
find_iter_delete: mod.ExportedFunction("find_iter_delete"),
overlapping_iter: mod.ExportedFunction("overlapping_iter"),
overlapping_iter_next: mod.ExportedFunction("overlapping_iter_next"),
overlapping_iter_delete: mod.ExportedFunction("overlapping_iter_delete"),
matches: mod.ExportedFunction("matches"),
matches_delete: mod.ExportedFunction("matches_delete"),
malloc: mod.ExportedFunction("malloc"),
free: mod.ExportedFunction("free"),
wasmMemory: mod.Memory(),
mod: mod,
callStack: callStack,
}
}
func (abi *ahoCorasickABI) startOperation(memorySize int) {
abi.mu.Lock()
abi.memory.reserve(abi, uint32(memorySize))
}
func (abi *ahoCorasickABI) endOperation() {
abi.mu.Unlock()
}
func (abi *ahoCorasickABI) newMatcher(patterns []string, patternBytes int, asciiCaseInsensitive bool, dfa bool, matchKind int) uintptr {
patternsPtr := abi.memory.allocate(uint32(patternBytes))
lensPtr := abi.memory.allocate(uint32(len(patterns) * 4))
buf, ok := abi.wasmMemory.Read(uint32(patternsPtr), uint32(patternBytes))
if !ok {
panic(errFailedRead)
}
off := 0
for i, p := range patterns {
copy(buf[off:], p)
off += len(p)
abi.wasmMemory.WriteUint32Le(uint32(lensPtr)+uint32(i*4), uint32(len(p)))
}
aci := 0
if asciiCaseInsensitive {
aci = 1
}
d := 0
if dfa {
d = 1
}
callStack := abi.callStack
callStack[0] = uint64(patternsPtr)
callStack[1] = uint64(lensPtr)
callStack[2] = uint64(len(patterns))
callStack[3] = uint64(aci)
callStack[4] = uint64(d)
callStack[5] = uint64(matchKind)
if err := abi.new_matcher.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
return uintptr(callStack[0])
}
func (abi *ahoCorasickABI) deleteMatcher(ptr uintptr) {
callStack := abi.callStack
callStack[0] = uint64(ptr)
if err := abi.delete_matcher.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
}
func (abi *ahoCorasickABI) findIter(acPtr uintptr, value cString) uintptr {
callStack := abi.callStack
callStack[0] = uint64(acPtr)
callStack[1] = uint64(value.ptr)
callStack[2] = uint64(value.length)
if err := abi.find_iter.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
return uintptr(callStack[0])
}
func (abi *ahoCorasickABI) findIterNext(iter uintptr) (int, int, int, bool) {
patternPtr := abi.memory.allocate(4)
startPtr := abi.memory.allocate(4)
endPtr := abi.memory.allocate(4)
callStack := abi.callStack
callStack[0] = uint64(iter)
callStack[1] = uint64(patternPtr)
callStack[2] = uint64(startPtr)
callStack[3] = uint64(endPtr)
if err := abi.find_iter_next.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
if callStack[0] == 0 {
return 0, 0, 0, false
}
pattern, ok := abi.wasmMemory.ReadUint32Le(uint32(patternPtr))
if !ok {
panic(errFailedRead)
}
start, ok := abi.wasmMemory.ReadUint32Le(uint32(startPtr))
if !ok {
panic(errFailedRead)
}
end, ok := abi.wasmMemory.ReadUint32Le(uint32(endPtr))
if !ok {
panic(errFailedRead)
}
return int(pattern), int(start), int(end), true
}
func (abi *ahoCorasickABI) findIterDelete(iter uintptr) {
callStack := abi.callStack
callStack[0] = uint64(iter)
if err := abi.find_iter_delete.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
}
func (abi *ahoCorasickABI) overlappingIter(acPtr uintptr, value cString) uintptr {
callStack := abi.callStack
callStack[0] = uint64(acPtr)
callStack[1] = uint64(value.ptr)
callStack[2] = uint64(value.length)
if err := abi.overlapping_iter.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
return uintptr(callStack[0])
}
func (abi *ahoCorasickABI) overlappingIterNext(iter uintptr) (int, int, int, bool) {
patternPtr := abi.memory.allocate(4)
startPtr := abi.memory.allocate(4)
endPtr := abi.memory.allocate(4)
callStack := abi.callStack
callStack[0] = uint64(iter)
callStack[1] = uint64(patternPtr)
callStack[2] = uint64(startPtr)
callStack[3] = uint64(endPtr)
if err := abi.overlapping_iter_next.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
if callStack[0] == 0 {
return 0, 0, 0, false
}
pattern, _ := abi.wasmMemory.ReadUint32Le(uint32(patternPtr))
start, _ := abi.wasmMemory.ReadUint32Le(uint32(startPtr))
end, _ := abi.wasmMemory.ReadUint32Le(uint32(endPtr))
return int(pattern), int(start), int(end), true
}
func (abi *ahoCorasickABI) overlappingIterDelete(iter uintptr) {
callStack := abi.callStack
callStack[0] = uint64(iter)
if err := abi.overlapping_iter_delete.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
}
func (abi *ahoCorasickABI) findN(iter uintptr, valueStr string, value cString, n int, matchWholeWords bool) []Match {
lenPtr := abi.memory.allocate(4)
callStack := abi.callStack
callStack[0] = uint64(iter)
callStack[1] = uint64(value.ptr)
callStack[2] = uint64(value.length)
callStack[3] = uint64(n)
callStack[4] = uint64(lenPtr)
if err := abi.matches.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
resLen, ok := abi.wasmMemory.ReadUint32Le(uint32(lenPtr))
if !ok {
panic(errFailedRead)
}
resPtr := callStack[0]
defer func() {
callStack[0] = uint64(resPtr)
callStack[1] = uint64(resLen)
if err := abi.matches_delete.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
}()
res, ok := abi.wasmMemory.Read(uint32(resPtr), resLen*4)
if !ok {
panic(errFailedRead)
}
num := resLen / 3
matches := make([]Match, 0, num)
for i := 0; i < int(num); i++ {
start := int(binary.LittleEndian.Uint32(res[i*12+4:]))
end := int(binary.LittleEndian.Uint32(res[i*12+8:]))
if matchWholeWords && isNotWholeWord(valueStr, start, end) {
continue
}
var m Match
m.pattern = int(binary.LittleEndian.Uint32(res[i*12:]))
m.start = start
m.end = end
matches = append(matches, m)
}
return matches
}
type sharedMemory struct {
size uint32
bufPtr uint32
nextIdx uint32
}
func (m *sharedMemory) reserve(abi *ahoCorasickABI, size uint32) {
m.nextIdx = 0
if m.size >= size {
return
}
ctx := context.Background()
callStack := abi.callStack
if m.bufPtr != 0 {
callStack[0] = uint64(m.bufPtr)
if err := abi.free.CallWithStack(ctx, callStack); err != nil {
panic(err)
}
}
callStack[0] = uint64(size)
if err := abi.malloc.CallWithStack(ctx, callStack); err != nil {
panic(err)
}
m.size = size
m.bufPtr = uint32(callStack[0])
}
func (m *sharedMemory) allocate(size uint32) uintptr {
if m.nextIdx+size > m.size {
panic("not enough reserved shared memory")
}
ptr := m.bufPtr + m.nextIdx
m.nextIdx += size
return uintptr(ptr)
}
type cString struct {
ptr uintptr
length int
}
func (abi *ahoCorasickABI) newOwnedCString(s string) cString {
res, err := abi.malloc.Call(context.Background(), uint64(len(s)))
if err != nil {
panic(err)
}
ptr := res[0]
if !abi.wasmMemory.WriteString(uint32(ptr), s) {
panic(errFailedWrite)
}
return cString{
ptr: uintptr(ptr),
length: len(s),
}
}
func (abi *ahoCorasickABI) freeOwnedCStringPtr(ptr uintptr) {
callStack := abi.callStack
callStack[0] = uint64(ptr)
if err := abi.free.CallWithStack(context.Background(), callStack); err != nil {
panic(err)
}
}