Skip to content

Commit

Permalink
Merge pull request #17 from AjaniBilby/dynamic-allocation-patch
Browse files Browse the repository at this point in the history
[Bug] Large enough BNFs can cause memory overflow while compiling
  • Loading branch information
AjaniBilby authored Jan 9, 2024
2 parents 2d183d3 + 860dbcf commit c7d0946
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/bnf.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 4.1.1

### Fixes:
- [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.

## Version 4.1.0

### Added:
Expand Down
14 changes: 13 additions & 1 deletion docs/source/static/dist/bnf-parser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnf-parser",
"version": "4.1.0",
"version": "4.1.1",
"description": "Deterministic BNF compiler/parser",
"homepage": "https://bnf-parser.ajanibilby.com",
"main": "./bin/index.js",
Expand Down
28 changes: 28 additions & 0 deletions source/wasm/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,34 @@ export function CompileRule(m: binaryen.Module, literals: LiteralMapping, rule:
ctx.m.block(null, [
ctx.m.local.set(error, ctx.m.i32.const(0)),

// Auto grow if 1kb from end of memory
ctx.m.if(
ctx.m.i32.lt_s(
ctx.m.i32.mul(
ctx.m.memory.size(),
ctx.m.i32.const(65_536) // bytes per page
),
ctx.m.i32.add(
ctx.m.global.get("heap", binaryen.i32),
ctx.m.i32.const(1024) // 1kb
)
),
ctx.m.block(null, [
rule.name === "expr_arg"
? ctx.m.call("print_i32", [
ctx.m.i32.sub(
ctx.m.i32.mul(
ctx.m.memory.size(),
ctx.m.i32.const(65_536) // bytes per page
),
ctx.m.global.get("heap", binaryen.i32)
)
], binaryen.none)
: ctx.m.nop(),
ctx.m.drop( ctx.m.memory.grow(ctx.m.i32.const(1)) )
])
),

innerWasm,

ctx.m.return(ctx.m.local.get(error, binaryen.i32))
Expand Down
11 changes: 11 additions & 0 deletions test/bnf/long-syntax.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"
"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"
"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"
"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"
"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"
"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"
"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"
"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" ;

# Remove a single "..." from this line and it will compile
wordI ::= "b" "c" ;
1 change: 1 addition & 0 deletions test/sample/long-syntax/correct.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0 comments on commit c7d0946

Please sign in to comment.