Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Docker Build
on:
push:
branches: [ main ]
workflow_dispatch:


jobs:
docker:
Expand Down Expand Up @@ -32,4 +34,4 @@ jobs:
platforms: linux/amd64,linux/arm64
tags: |
ghcr.io/${{ env.REPO_LC }}:cibuild
ghcr.io/${{ env.REPO_LC }}:cibuild-${{ github.sha }}
ghcr.io/${{ env.REPO_LC }}:cibuild-${{ github.sha }}
20 changes: 17 additions & 3 deletions library/regex-utilities.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const { RE2 } = require('re2-wasm');
const { RE2 } = require('re2-wasm');

class RegExUtilities {

constructor() {
this._cache = new Map();
}

compile(pattern, flags) {
// RE2 requires the unicode flag; add it if not already present
// re2-wasm has a fixed 16 MB WASM heap with no real free(): every
// new RE2(...) permanently consumes a few KB. Cache by (pattern, flags)
// so the same regex compiles at most once.
// TODO: replace re2-wasm with native re2 to eliminate the underlying leak.
const re2Flags = flags && flags.includes('u') ? flags : (flags || '') + 'u';
return new RE2(pattern, re2Flags);
const key = pattern + '|' + re2Flags;
let compiled = this._cache.get(key);
if (!compiled) {
compiled = new RE2(pattern, re2Flags);
this._cache.set(key, compiled);
}
return compiled;
}

}

module.exports = new RegExUtilities();

37 changes: 26 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions test-scripts/repro-re2-wasm-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Manual reproducer for the re2-wasm WASM heap leak.
// See library/regex-utilities.js for the workaround.
//
// node test-scripts/repro-re2-wasm-leak.js # same-pattern stress
// node test-scripts/repro-re2-wasm-leak.js --unique # unique-pattern stress
//
// Without the cache, same-pattern OOMs at ~2965 iterations.
// With the cache, same-pattern runs indefinitely.
// Unique-pattern still OOMs (each pattern is a real compile and the underlying
// re2-wasm heap leak still applies). A proper fix is to replace re2-wasm with
// the native `re2` package.

const re = require('../library/regex-utilities');

const mode = process.argv.includes('--unique') ? 'unique' : 'same';
const basePattern =
'CYTO|HL7\\.CYTOGEN|HL7\\.GENETICS|^PATH(\\..*)?|^MOLPATH(\\..*)?|NR STATS|H&P\\.HX\\.LAB|CHALSKIN|LABORDERS';

console.log(`mode: ${mode}-pattern`);

let i = 0;
try {
for (;;) {
const pattern = mode === 'unique' ? basePattern + `|UNIQUE${i}` : basePattern;
re.compile(pattern);
i++;
if (i % 100 === 0) console.log(`iter ${i}`);
}
} catch (e) {
console.error(`OOMed at iteration ${i}: ${e.message}`);
process.exit(1);
}

Loading