Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a memory.grow handler in the RT #2755

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions std/assembly/rt/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ declare function __finalize(ptr: usize): void;
declare const ASC_RTRACE: bool;
declare const ASC_PROFILE: bool;

// Memory growth handling
declare function ASC_RT_GROW_HANDLER(): void;

// Incremental GC constants
declare const ASC_GC_GRANULARITY: i32;
declare const ASC_GC_SWEEPFACTOR: i32;
Expand Down
1 change: 1 addition & 0 deletions std/assembly/rt/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function maybeGrowMemory(newOffset: usize): void {
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
}
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
}
offset = newOffset;
}
Expand Down
6 changes: 5 additions & 1 deletion std/assembly/rt/tlsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ function growMemory(root: Root, size: usize): void {
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable();
}
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
let pagesAfter = memory.size();
addMemory(root, <usize>pagesBefore << 16, <u64>pagesAfter << 16);
}
Expand All @@ -468,7 +469,10 @@ function initialize(): void {
let rootOffset = (__heap_base + AL_MASK) & ~AL_MASK;
let pagesBefore = memory.size();
let pagesNeeded = <i32>((((rootOffset + ROOT_SIZE) + 0xffff) & ~0xffff) >>> 16);
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
if (pagesNeeded > pagesBefore) {
if (memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
if (isDefined(ASC_RT_GROW_HANDLER)) ASC_RT_GROW_HANDLER();
}
let root = changetype<Root>(rootOffset);
root.flMap = 0;
SETTAIL(root, changetype<Block>(0));
Expand Down
4 changes: 4 additions & 0 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ async function runTest(basename) {
}
compareStderr.end(SUCCESS);
return prepareResult(SUCCESS);
} else if (error) {
// Don't bother comparing fixtures or doing anything else if the
// compilation failed.
return prepareResult(FAILURE, "compile failed");
}

const afterCompileResult = afterCompile("debug");
Expand Down
20 changes: 11 additions & 9 deletions tests/compiler/bindings/esm.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1338,18 +1338,18 @@
local.get $pagesNeeded
local.get $pagesBefore
i32.gt_s
if (result i32)
if
local.get $pagesNeeded
local.get $pagesBefore
i32.sub
memory.grow $0
i32.const 0
i32.lt_s
else
if
unreachable
end
i32.const 0
end
if
unreachable
drop
end
local.get $rootOffset
local.set $root
Expand Down Expand Up @@ -1470,7 +1470,7 @@
if
i32.const 0
i32.const 672
i32.const 562
i32.const 566
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1799,7 +1799,7 @@
if
i32.const 336
i32.const 672
i32.const 461
i32.const 462
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2095,6 +2095,8 @@
unreachable
end
end
i32.const 0
drop
memory.size $0
local.set $pagesAfter
local.get $root
Expand Down Expand Up @@ -2244,7 +2246,7 @@
if
i32.const 0
i32.const 672
i32.const 499
i32.const 503
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -2264,7 +2266,7 @@
if
i32.const 0
i32.const 672
i32.const 501
i32.const 505
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
18 changes: 8 additions & 10 deletions tests/compiler/bindings/esm.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -852,18 +852,16 @@
local.tee $1
i32.const 0
i32.le_s
if (result i32)
if
i32.const 1
local.get $1
i32.sub
memory.grow $0
i32.const 0
i32.lt_s
else
i32.const 0
end
if
unreachable
if
unreachable
end
end
i32.const 35056
i32.const 0
Expand Down Expand Up @@ -1161,7 +1159,7 @@
if
i32.const 0
i32.const 1696
i32.const 562
i32.const 566
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1396,7 +1394,7 @@
if
i32.const 1360
i32.const 1696
i32.const 461
i32.const 462
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1494,7 +1492,7 @@
if
i32.const 0
i32.const 1696
i32.const 499
i32.const 503
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -1509,7 +1507,7 @@
if
i32.const 0
i32.const 1696
i32.const 501
i32.const 505
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
20 changes: 11 additions & 9 deletions tests/compiler/bindings/noExportRuntime.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1253,18 +1253,18 @@
local.get $pagesNeeded
local.get $pagesBefore
i32.gt_s
if (result i32)
if
local.get $pagesNeeded
local.get $pagesBefore
i32.sub
memory.grow $0
i32.const 0
i32.lt_s
else
if
unreachable
end
i32.const 0
end
if
unreachable
drop
end
local.get $rootOffset
local.set $root
Expand Down Expand Up @@ -1385,7 +1385,7 @@
if
i32.const 0
i32.const 512
i32.const 562
i32.const 566
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1714,7 +1714,7 @@
if
i32.const 176
i32.const 512
i32.const 461
i32.const 462
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2010,6 +2010,8 @@
unreachable
end
end
i32.const 0
drop
memory.size $0
local.set $pagesAfter
local.get $root
Expand Down Expand Up @@ -2159,7 +2161,7 @@
if
i32.const 0
i32.const 512
i32.const 499
i32.const 503
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -2179,7 +2181,7 @@
if
i32.const 0
i32.const 512
i32.const 501
i32.const 505
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
18 changes: 8 additions & 10 deletions tests/compiler/bindings/noExportRuntime.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -785,18 +785,16 @@
local.tee $1
i32.const 0
i32.le_s
if (result i32)
if
i32.const 1
local.get $1
i32.sub
memory.grow $0
i32.const 0
i32.lt_s
else
i32.const 0
end
if
unreachable
if
unreachable
end
end
i32.const 34560
i32.const 0
Expand Down Expand Up @@ -1094,7 +1092,7 @@
if
i32.const 0
i32.const 1536
i32.const 562
i32.const 566
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1329,7 +1327,7 @@
if
i32.const 1200
i32.const 1536
i32.const 461
i32.const 462
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1427,7 +1425,7 @@
if
i32.const 0
i32.const 1536
i32.const 499
i32.const 503
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -1442,7 +1440,7 @@
if
i32.const 0
i32.const 1536
i32.const 501
i32.const 505
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
20 changes: 11 additions & 9 deletions tests/compiler/bindings/raw.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1341,18 +1341,18 @@
local.get $pagesNeeded
local.get $pagesBefore
i32.gt_s
if (result i32)
if
local.get $pagesNeeded
local.get $pagesBefore
i32.sub
memory.grow $0
i32.const 0
i32.lt_s
else
if
unreachable
end
i32.const 0
end
if
unreachable
drop
end
local.get $rootOffset
local.set $root
Expand Down Expand Up @@ -1473,7 +1473,7 @@
if
i32.const 0
i32.const 672
i32.const 562
i32.const 566
i32.const 3
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1802,7 +1802,7 @@
if
i32.const 336
i32.const 672
i32.const 461
i32.const 462
i32.const 29
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2098,6 +2098,8 @@
unreachable
end
end
i32.const 0
drop
memory.size $0
local.set $pagesAfter
local.get $root
Expand Down Expand Up @@ -2247,7 +2249,7 @@
if
i32.const 0
i32.const 672
i32.const 499
i32.const 503
i32.const 16
call $~lib/builtins/abort
unreachable
Expand All @@ -2267,7 +2269,7 @@
if
i32.const 0
i32.const 672
i32.const 501
i32.const 505
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down
Loading