| 1 |
|
| 2 |
|
| 3 |
package internal |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/binary" |
| 7 |
"runtime" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"sync/atomic" |
| 11 |
|
| 12 |
wasm2go "github.com/betterleaks/go-re2/internal/wasm" |
| 13 |
) |
| 14 |
|
| 15 |
var ( |
| 16 |
hostMemory *wasm2go.HostMemory |
| 17 |
hostWASI *wasm2go.HostWASI |
| 18 |
hostEnv *wasm2go.HostEnv |
| 19 |
rootMod *wasm2go.Module |
| 20 |
|
| 21 |
modPoolOnce sync.Once |
| 22 |
modPool sync.Pool |
| 23 |
modCreateMu sync.Mutex |
| 24 |
) |
| 25 |
|
| 26 |
type libre2ABI struct{} |
| 27 |
|
| 28 |
type wasmPtr uint32 |
| 29 |
|
| 30 |
var nilWasmPtr = wasmPtr(0) |
| 31 |
|
| 32 |
var prevTID uint32 |
| 33 |
|
| 34 |
type childModule struct { |
| 35 |
mod *wasm2go.Module |
| 36 |
tlsBasePtr uint32 |
| 37 |
} |
| 38 |
|
| 39 |
func createChildModule(root *wasm2go.Module) *childModule { |
| 40 |
stackPointer := uint32(*root.X__stack_pointer()) |
| 41 |
tlsBase := uint32(*root.X__tls_base()) |
| 42 |
size := stackPointer - tlsBase |
| 43 |
|
| 44 |
ptr := uint32(root.Xmalloc(int32(size))) |
| 45 |
|
| 46 |
child := wasm2go.New(hostWASI, hostEnv) |
| 47 |
child.X__wasm_init_tls(int32(ptr)) |
| 48 |
|
| 49 |
tid := atomic.AddUint32(&prevTID, 1) |
| 50 |
hostMemory.WriteUint32Le(ptr, ptr) |
| 51 |
hostMemory.WriteUint32Le(ptr+20, tid) |
| 52 |
*child.X__stack_pointer() = int32(ptr + size) |
| 53 |
|
| 54 |
ret := &childModule{mod: child, tlsBasePtr: ptr} |
| 55 |
runtime.SetFinalizer(ret, func(obj interface{}) { |
| 56 |
if cm, ok := obj.(*childModule); ok { |
| 57 |
cm.mod.Xfree(int32(cm.tlsBasePtr)) |
| 58 |
} |
| 59 |
}) |
| 60 |
return ret |
| 61 |
} |
| 62 |
|
| 63 |
func getChildModule() *childModule { |
| 64 |
modPoolOnce.Do(func() { |
| 65 |
initWASM() |
| 66 |
}) |
| 67 |
return modPool.Get().(*childModule) |
| 68 |
} |
| 69 |
|
| 70 |
func putChildModule(cm *childModule) { |
| 71 |
modPool.Put(cm) |
| 72 |
} |
| 73 |
|
| 74 |
func initWASM() { |
| 75 |
hostMemory = wasm2go.NewHostMemory(3) |
| 76 |
hostWASI = wasm2go.NewHostWASI(hostMemory) |
| 77 |
hostEnv = wasm2go.NewHostEnv(hostMemory) |
| 78 |
rootMod = wasm2go.New(hostWASI, hostEnv) |
| 79 |
rootMod.X_initialize() |
| 80 |
modPool = sync.Pool{ |
| 81 |
New: func() any { |
| 82 |
modCreateMu.Lock() |
| 83 |
defer modCreateMu.Unlock() |
| 84 |
return createChildModule(rootMod) |
| 85 |
}, |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func newABI() *libre2ABI { |
| 90 |
return &libre2ABI{} |
| 91 |
} |
| 92 |
|
| 93 |
func (abi *libre2ABI) startOperation(memorySize int) allocation { |
| 94 |
return abi.reserve(uint32(memorySize)) |
| 95 |
} |
| 96 |
|
| 97 |
func (abi *libre2ABI) endOperation(a allocation) { |
| 98 |
a.free() |
| 99 |
} |
| 100 |
|
| 101 |
func withModule(fn func(*wasm2go.Module) uint64) uint64 { |
| 102 |
modH := getChildModule() |
| 103 |
defer putChildModule(modH) |
| 104 |
return fn(modH.mod) |
| 105 |
} |
| 106 |
|
| 107 |
func withModuleNoResult(fn func(*wasm2go.Module)) { |
| 108 |
modH := getChildModule() |
| 109 |
defer putChildModule(modH) |
| 110 |
fn(modH.mod) |
| 111 |
} |
| 112 |
|
| 113 |
func newRE(abi *libre2ABI, pattern cString, opts CompileOptions) wasmPtr { |
| 114 |
_ = abi |
| 115 |
optPtr := uint32(withModule(func(m *wasm2go.Module) uint64 { |
| 116 |
return uint64(m.Xcre2_opt_new()) |
| 117 |
})) |
| 118 |
|
| 119 |
defer func() { |
| 120 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 121 |
m.Xcre2_opt_delete(int32(optPtr)) |
| 122 |
}) |
| 123 |
}() |
| 124 |
|
| 125 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 126 |
m.Xcre2_opt_set_max_mem(int32(optPtr), int64(maxSize)) |
| 127 |
}) |
| 128 |
|
| 129 |
if opts.Longest { |
| 130 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 131 |
m.Xcre2_opt_set_longest_match(int32(optPtr), 1) |
| 132 |
}) |
| 133 |
} |
| 134 |
if opts.Posix { |
| 135 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 136 |
m.Xcre2_opt_set_posix_syntax(int32(optPtr), 1) |
| 137 |
}) |
| 138 |
} |
| 139 |
if opts.CaseInsensitive { |
| 140 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 141 |
m.Xcre2_opt_set_case_sensitive(int32(optPtr), 0) |
| 142 |
}) |
| 143 |
} |
| 144 |
if opts.Latin1 { |
| 145 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 146 |
m.Xcre2_opt_set_latin1_encoding(int32(optPtr)) |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 151 |
return uint64(m.Xcre2_new(int32(pattern.ptr), int32(pattern.length), int32(optPtr))) |
| 152 |
}) |
| 153 |
return wasmPtr(res) |
| 154 |
} |
| 155 |
|
| 156 |
func reError(abi *libre2ABI, rePtr wasmPtr) (int, string) { |
| 157 |
_ = abi |
| 158 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 159 |
return uint64(m.Xcre2_error_code(int32(rePtr))) |
| 160 |
}) |
| 161 |
code := int(res) |
| 162 |
if code == 0 { |
| 163 |
return 0, "" |
| 164 |
} |
| 165 |
|
| 166 |
res = withModule(func(m *wasm2go.Module) uint64 { |
| 167 |
return uint64(m.Xcre2_error_arg(int32(rePtr))) |
| 168 |
}) |
| 169 |
msg := copyCString(wasmPtr(res)) |
| 170 |
return code, msg |
| 171 |
} |
| 172 |
|
| 173 |
func numCapturingGroups(abi *libre2ABI, rePtr wasmPtr) int { |
| 174 |
_ = abi |
| 175 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 176 |
return uint64(m.Xcre2_num_capturing_groups(int32(rePtr))) |
| 177 |
}) |
| 178 |
return int(res) |
| 179 |
} |
| 180 |
|
| 181 |
func deleteRE(abi *libre2ABI, rePtr wasmPtr) { |
| 182 |
_ = abi |
| 183 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 184 |
m.Xcre2_delete(int32(rePtr)) |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
func release(re *Regexp) { |
| 189 |
deleteRE(re.abi, re.ptr) |
| 190 |
} |
| 191 |
|
| 192 |
func match(re *Regexp, s cString, matchesPtr wasmPtr, nMatches uint32) bool { |
| 193 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 194 |
return uint64(m.Xcre2_match(int32(re.ptr), int32(s.ptr), int32(s.length), 0, int32(s.length), 0, int32(matchesPtr), int32(nMatches))) |
| 195 |
}) |
| 196 |
|
| 197 |
return res == 1 |
| 198 |
} |
| 199 |
|
| 200 |
func matchFrom(re *Regexp, s cString, startPos int, matchesPtr wasmPtr, nMatches uint32) bool { |
| 201 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 202 |
return uint64(m.Xcre2_match(int32(re.ptr), int32(s.ptr), int32(s.length), int32(startPos), int32(s.length), 0, int32(matchesPtr), int32(nMatches))) |
| 203 |
}) |
| 204 |
|
| 205 |
return res == 1 |
| 206 |
} |
| 207 |
|
| 208 |
func readMatch(alloc *allocation, cs cString, matchPtr wasmPtr, dstCap []int) []int { |
| 209 |
matchBuf := alloc.read(matchPtr, 8) |
| 210 |
subStrPtr := binary.LittleEndian.Uint32(matchBuf) |
| 211 |
sLen := binary.LittleEndian.Uint32(matchBuf[4:]) |
| 212 |
sIdx := subStrPtr - uint32(cs.ptr) |
| 213 |
|
| 214 |
return append(dstCap, int(sIdx), int(sIdx+sLen)) |
| 215 |
} |
| 216 |
|
| 217 |
func readMatches(alloc *allocation, cs cString, matchesPtr wasmPtr, n int, deliver func([]int) bool) { |
| 218 |
var dstCap [2]int |
| 219 |
|
| 220 |
matchesBuf := alloc.read(matchesPtr, 8*n) |
| 221 |
for i := range n { |
| 222 |
subStrPtr := binary.LittleEndian.Uint32(matchesBuf[8*i:]) |
| 223 |
if subStrPtr == 0 { |
| 224 |
if !deliver(append(dstCap[:0], -1, -1)) { |
| 225 |
break |
| 226 |
} |
| 227 |
continue |
| 228 |
} |
| 229 |
sLen := binary.LittleEndian.Uint32(matchesBuf[8*i+4:]) |
| 230 |
sIdx := subStrPtr - uint32(cs.ptr) |
| 231 |
if !deliver(append(dstCap[:0], int(sIdx), int(sIdx+sLen))) { |
| 232 |
break |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func namedGroupsIter(abi *libre2ABI, rePtr wasmPtr) wasmPtr { |
| 238 |
_ = abi |
| 239 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 240 |
return uint64(m.Xcre2_named_groups_iter_new(int32(rePtr))) |
| 241 |
}) |
| 242 |
|
| 243 |
return wasmPtr(res) |
| 244 |
} |
| 245 |
|
| 246 |
func namedGroupsIterNext(abi *libre2ABI, iterPtr wasmPtr) (string, int, bool) { |
| 247 |
_ = abi |
| 248 |
|
| 249 |
|
| 250 |
ptrs := malloc(abi, 8) |
| 251 |
defer free(abi, ptrs) |
| 252 |
namePtrPtr := ptrs |
| 253 |
indexPtr := namePtrPtr + 4 |
| 254 |
|
| 255 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 256 |
return uint64(m.Xcre2_named_groups_iter_next(int32(iterPtr), int32(namePtrPtr), int32(indexPtr))) |
| 257 |
}) |
| 258 |
|
| 259 |
if res == 0 { |
| 260 |
return "", 0, false |
| 261 |
} |
| 262 |
|
| 263 |
namePtr := hostMemory.ReadUint32Le(uint32(namePtrPtr)) |
| 264 |
|
| 265 |
name := copyCString(wasmPtr(namePtr)) |
| 266 |
|
| 267 |
index := hostMemory.ReadUint32Le(uint32(indexPtr)) |
| 268 |
|
| 269 |
return name, int(index), true |
| 270 |
} |
| 271 |
|
| 272 |
func namedGroupsIterDelete(abi *libre2ABI, iterPtr wasmPtr) { |
| 273 |
_ = abi |
| 274 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 275 |
m.Xcre2_named_groups_iter_delete(int32(iterPtr)) |
| 276 |
}) |
| 277 |
} |
| 278 |
|
| 279 |
func newSet(abi *libre2ABI, opts CompileOptions) wasmPtr { |
| 280 |
_ = abi |
| 281 |
optPtr := uint32(withModule(func(m *wasm2go.Module) uint64 { |
| 282 |
return uint64(m.Xcre2_opt_new()) |
| 283 |
})) |
| 284 |
defer func() { |
| 285 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 286 |
m.Xcre2_opt_delete(int32(optPtr)) |
| 287 |
}) |
| 288 |
}() |
| 289 |
|
| 290 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 291 |
m.Xcre2_opt_set_max_mem(int32(optPtr), int64(maxSize)) |
| 292 |
}) |
| 293 |
|
| 294 |
if opts.Longest { |
| 295 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 296 |
m.Xcre2_opt_set_longest_match(int32(optPtr), 1) |
| 297 |
}) |
| 298 |
} |
| 299 |
if opts.Posix { |
| 300 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 301 |
m.Xcre2_opt_set_posix_syntax(int32(optPtr), 1) |
| 302 |
}) |
| 303 |
} |
| 304 |
if opts.CaseInsensitive { |
| 305 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 306 |
m.Xcre2_opt_set_case_sensitive(int32(optPtr), 0) |
| 307 |
}) |
| 308 |
} |
| 309 |
if opts.Latin1 { |
| 310 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 311 |
m.Xcre2_opt_set_latin1_encoding(int32(optPtr)) |
| 312 |
}) |
| 313 |
} |
| 314 |
|
| 315 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 316 |
return uint64(m.Xcre2_set_new(int32(optPtr), 0)) |
| 317 |
}) |
| 318 |
return wasmPtr(res) |
| 319 |
} |
| 320 |
|
| 321 |
func setAdd(set *Set, s cString) string { |
| 322 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 323 |
return uint64(m.Xcre2_set_add(int32(set.ptr), int32(s.ptr), int32(s.length))) |
| 324 |
}) |
| 325 |
if res == 0 { |
| 326 |
return unknownCompileError |
| 327 |
} |
| 328 |
msgPtr := wasmPtr(res) |
| 329 |
msg := copyCString(msgPtr) |
| 330 |
if msg != "ok" { |
| 331 |
free(set.abi, msgPtr) |
| 332 |
return "error parsing regexp: " + msg |
| 333 |
} |
| 334 |
return "" |
| 335 |
} |
| 336 |
|
| 337 |
func setCompile(set *Set) int32 { |
| 338 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 339 |
return uint64(m.Xcre2_set_compile(int32(set.ptr))) |
| 340 |
}) |
| 341 |
return int32(res) |
| 342 |
} |
| 343 |
|
| 344 |
func setMatch(set *Set, cs cString, matchedPtr wasmPtr, nMatch int) int { |
| 345 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 346 |
return uint64(m.Xcre2_set_match(int32(set.ptr), int32(cs.ptr), int32(cs.length), int32(matchedPtr), int32(nMatch))) |
| 347 |
}) |
| 348 |
return int(res) |
| 349 |
} |
| 350 |
|
| 351 |
func deleteSet(abi *libre2ABI, setPtr wasmPtr) { |
| 352 |
_ = abi |
| 353 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 354 |
m.Xcre2_set_delete(int32(setPtr)) |
| 355 |
}) |
| 356 |
} |
| 357 |
|
| 358 |
type cString struct { |
| 359 |
ptr wasmPtr |
| 360 |
length int |
| 361 |
} |
| 362 |
|
| 363 |
type cStringArray struct { |
| 364 |
ptr wasmPtr |
| 365 |
} |
| 366 |
|
| 367 |
func (a cStringArray) free() { |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
func malloc(abi *libre2ABI, size uint32) wasmPtr { |
| 372 |
_ = abi |
| 373 |
res := withModule(func(m *wasm2go.Module) uint64 { |
| 374 |
return uint64(m.Xmalloc(int32(size))) |
| 375 |
}) |
| 376 |
return wasmPtr(res) |
| 377 |
} |
| 378 |
|
| 379 |
func free(abi *libre2ABI, ptr wasmPtr) { |
| 380 |
_ = abi |
| 381 |
withModuleNoResult(func(m *wasm2go.Module) { |
| 382 |
m.Xfree(int32(ptr)) |
| 383 |
}) |
| 384 |
} |
| 385 |
|
| 386 |
func copyCString(ptr wasmPtr) string { |
| 387 |
res := strings.Builder{} |
| 388 |
for { |
| 389 |
b := hostMemory.ReadByte(uint32(ptr)) |
| 390 |
if b == 0 { |
| 391 |
break |
| 392 |
} |
| 393 |
res.WriteByte(b) |
| 394 |
ptr++ |
| 395 |
} |
| 396 |
return res.String() |
| 397 |
} |
| 398 |
|
| 399 |
type allocation struct { |
| 400 |
size uint32 |
| 401 |
bufPtr wasmPtr |
| 402 |
nextIdx uint32 |
| 403 |
abi *libre2ABI |
| 404 |
} |
| 405 |
|
| 406 |
func (abi *libre2ABI) reserve(size uint32) allocation { |
| 407 |
ptr := malloc(abi, size) |
| 408 |
return allocation{ |
| 409 |
size: size, |
| 410 |
bufPtr: ptr, |
| 411 |
nextIdx: 0, |
| 412 |
abi: abi, |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
func (a *allocation) free() { |
| 417 |
free(a.abi, a.bufPtr) |
| 418 |
} |
| 419 |
|
| 420 |
func (a *allocation) allocate(size uint32) wasmPtr { |
| 421 |
if a.nextIdx+size > a.size { |
| 422 |
panic("not enough reserved shared memory") |
| 423 |
} |
| 424 |
|
| 425 |
ptr := uint32(a.bufPtr) + a.nextIdx |
| 426 |
a.nextIdx += size |
| 427 |
return wasmPtr(ptr) |
| 428 |
} |
| 429 |
|
| 430 |
func (a *allocation) read(ptr wasmPtr, size int) []byte { |
| 431 |
return hostMemory.Read(uint32(ptr), uint32(size)) |
| 432 |
} |
| 433 |
|
| 434 |
func (a *allocation) write(b []byte) wasmPtr { |
| 435 |
ptr := a.allocate(uint32(len(b))) |
| 436 |
hostMemory.Write(uint32(ptr), b) |
| 437 |
return ptr |
| 438 |
} |
| 439 |
|
| 440 |
func (a *allocation) writeString(s string) wasmPtr { |
| 441 |
ptr := a.allocate(uint32(len(s))) |
| 442 |
hostMemory.WriteString(uint32(ptr), s) |
| 443 |
return ptr |
| 444 |
} |
| 445 |
|
| 446 |
func (a *allocation) newCString(s string) cString { |
| 447 |
ptr := a.writeString(s) |
| 448 |
return cString{ |
| 449 |
ptr: ptr, |
| 450 |
length: len(s), |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
func (a *allocation) newCStringFromBytes(s []byte) cString { |
| 455 |
ptr := a.write(s) |
| 456 |
return cString{ |
| 457 |
ptr: ptr, |
| 458 |
length: len(s), |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
func (a *allocation) newCStringArray(n int) cStringArray { |
| 463 |
ptr := a.allocate(uint32(n * 8)) |
| 464 |
return cStringArray{ptr: ptr} |
| 465 |
} |