Skip to content

Commit c7d0946

Browse files
authored
Merge pull request #17 from AjaniBilby/dynamic-allocation-patch
[Bug] Large enough BNFs can cause memory overflow while compiling
2 parents 2d183d3 + 860dbcf commit c7d0946

7 files changed

Lines changed: 60 additions & 3 deletions

File tree

dist/bnf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/source/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 4.1.1
4+
5+
### Fixes:
6+
- [x] Fixed an issue where wasm memory usage exceeded predicted usage - the module now checks if it's about to run out of memory on the start of every rule being parsed.
7+
38
## Version 4.1.0
49

510
### Added:

docs/source/static/dist/bnf-parser.js

Lines changed: 13 additions & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnf-parser",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Deterministic BNF compiler/parser",
55
"homepage": "https://bnf-parser.ajanibilby.com",
66
"main": "./bin/index.js",

source/wasm/rule.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,34 @@ export function CompileRule(m: binaryen.Module, literals: LiteralMapping, rule:
943943
ctx.m.block(null, [
944944
ctx.m.local.set(error, ctx.m.i32.const(0)),
945945

946+
// Auto grow if 1kb from end of memory
947+
ctx.m.if(
948+
ctx.m.i32.lt_s(
949+
ctx.m.i32.mul(
950+
ctx.m.memory.size(),
951+
ctx.m.i32.const(65_536) // bytes per page
952+
),
953+
ctx.m.i32.add(
954+
ctx.m.global.get("heap", binaryen.i32),
955+
ctx.m.i32.const(1024) // 1kb
956+
)
957+
),
958+
ctx.m.block(null, [
959+
rule.name === "expr_arg"
960+
? ctx.m.call("print_i32", [
961+
ctx.m.i32.sub(
962+
ctx.m.i32.mul(
963+
ctx.m.memory.size(),
964+
ctx.m.i32.const(65_536) // bytes per page
965+
),
966+
ctx.m.global.get("heap", binaryen.i32)
967+
)
968+
], binaryen.none)
969+
: ctx.m.nop(),
970+
ctx.m.drop( ctx.m.memory.grow(ctx.m.i32.const(1)) )
971+
])
972+
),
973+
946974
innerWasm,
947975

948976
ctx.m.return(ctx.m.local.get(error, binaryen.i32))

test/bnf/long-syntax.bnf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
program ::= "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
2+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
3+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
4+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
5+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
6+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
7+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
8+
"a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" ;
9+
10+
# Remove a single "..." from this line and it will compile
11+
wordI ::= "b" "c" ;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0 commit comments

Comments
 (0)