Skip to content

Commit

Permalink
Indirect calls on null now either trap with unreachable or, if exce…
Browse files Browse the repository at this point in the history
…ption-handlig is enabled, throws a Wasm exception
  • Loading branch information
jayphelps committed Nov 17, 2019
1 parent b2bb264 commit fef1379
Show file tree
Hide file tree
Showing 281 changed files with 623 additions and 340 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ jobs:
run: npm run clean
- name: Test experimental features
env:
ASC_FEATURES: mutable-globals,threads,reference-types,bigint-integration
ASC_FEATURES: mutable-globals,threads,reference-types,bigint-integration,exception-handling
run: |
npm run test:compiler rt/flags features/js-bigint-integration features/reference-types features/threads
npm run test:compiler rt/flags features/js-bigint-integration features/reference-types features/threads features/exception-handling
test-runtime:
name: "Test runtimes on node: node"
runs-on: ubuntu-latest
Expand Down
17 changes: 9 additions & 8 deletions examples/game-of-life/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
global.get $assembly/config/BGR_DEAD
i32.const 16777215
i32.and
else
else
global.get $assembly/config/BGR_ALIVE
i32.const -16777216
i32.or
Expand Down Expand Up @@ -134,7 +134,7 @@
i32.eq
if (result i32)
local.get $0
else
else
local.get $2
i32.const 1
i32.sub
Expand All @@ -145,7 +145,7 @@
i32.eq
if (result i32)
i32.const 0
else
else
local.get $2
i32.const 1
i32.add
Expand All @@ -165,7 +165,7 @@
i32.eq
if (result i32)
local.get $1
else
else
local.get $5
i32.const 1
i32.sub
Expand All @@ -176,7 +176,7 @@
i32.eq
if (result i32)
i32.const 0
else
else
local.get $5
i32.const 1
i32.add
Expand Down Expand Up @@ -367,7 +367,7 @@
i32.shl
local.get $14
i32.store
else
else
local.get $5
local.set $16
local.get $2
Expand All @@ -388,7 +388,7 @@
local.get $14
i32.store
end
else
else
local.get $9
i32.const 3
i32.eq
Expand All @@ -412,7 +412,7 @@
i32.shl
local.get $8
i32.store
else
else
local.get $5
local.set $15
local.get $2
Expand Down Expand Up @@ -563,5 +563,6 @@
end
)
(func $null (; 4 ;) (type $FUNCSIG$v)
unreachable
)
)
1 change: 1 addition & 0 deletions examples/mandelbrot/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,6 @@
end
)
(func $null (; 3 ;) (type $FUNCSIG$v)
unreachable
)
)
1 change: 1 addition & 0 deletions examples/n-body/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2225,5 +2225,6 @@
global.set $~lib/rt/stub/offset
)
(func $null (; 24 ;) (type $FUNCSIG$v)
unreachable
)
)
1 change: 1 addition & 0 deletions examples/pson/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,6 @@
end
)
(func $null (; 18 ;) (type $FUNCSIG$v)
unreachable
)
)
1 change: 1 addition & 0 deletions lib/i64/build/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,6 @@
global.set $assembly/i64/hi
)
(func $null (; 31 ;) (type $FUNCSIG$v)
unreachable
)
)
40 changes: 35 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ export class Compiler extends DiagnosticEmitter {
module.removeGlobal(BuiltinSymbols.rtti_base);
if (this.runtimeFeatures & RuntimeFeatures.RTTI) compileRTTI(this);

// set up function table
var functionTable = this.functionTable;
module.setFunctionTable(functionTable.length, 0xffffffff, functionTable, module.i32(0));
this.addNullPointerExceptionHandling(module);

// update the heap base pointer
var memoryOffset = this.memoryOffset;
memoryOffset = i64_align(memoryOffset, options.usizeType.byteSize);
Expand Down Expand Up @@ -447,11 +452,6 @@ export class Compiler extends DiagnosticEmitter {
// import memory if requested (default memory is named '0' by Binaryen)
if (options.importMemory) module.addMemoryImport("0", "env", "memory", isSharedMemory);

// set up function table
var functionTable = this.functionTable;
module.setFunctionTable(functionTable.length, 0xffffffff, functionTable, module.i32(0));
module.addFunction("null", this.ensureFunctionType(null, Type.void), null, module.block(null, []));

// import table if requested (default table is named '0' by Binaryen)
if (options.importTable) module.addTableImport("0", "env", "table");

Expand All @@ -462,6 +462,36 @@ export class Compiler extends DiagnosticEmitter {
return module;
}

addNullPointerExceptionHandling(module: Module) {
if (this.options.hasFeature(Feature.EXCEPTION_HANDLING)) {
let message = this.ensureStaticString("null is not a function");
let exceptionType = module.addFunctionType(
"NullPointerException",
NativeType.None,
[NativeType.I32]
);

module.addEvent("NullPointerException", 0, exceptionType);
module.addFunction(
"null",
this.ensureFunctionType(null, Type.void),
null,
module.block(null, [
module.throw("NullPointerException", [message])
])
);
} else {
module.addFunction(
"null",
this.ensureFunctionType(null, Type.void),
null,
module.block(null, [
module.unreachable()
])
);
}
}

// === Exports ==================================================================================

/** Applies the respective module exports for the specified file. */
Expand Down
17 changes: 9 additions & 8 deletions tests/allocators/buddy/untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
local.get $1
i32.const 3
i32.and
else
else
i32.const 0
end
if
Expand Down Expand Up @@ -1293,7 +1293,7 @@
i32.le_u
if (result i32)
i32.const 1
else
else
local.get $5
local.get $3
i32.add
Expand Down Expand Up @@ -1413,7 +1413,7 @@
end
end
end
else
else
local.get $4
i32.const 7
i32.and
Expand Down Expand Up @@ -1554,7 +1554,7 @@
local.get $4
i32.load8_u
i32.eq
else
else
i32.const 0
end
if
Expand All @@ -1581,7 +1581,7 @@
local.get $4
i32.load8_u
i32.sub
else
else
i32.const 0
end
end
Expand Down Expand Up @@ -2019,7 +2019,7 @@
i32.ne
if (result i32)
i32.const 1
else
else
local.get $2
i32.const 0
i32.eq
Expand Down Expand Up @@ -2060,7 +2060,7 @@
i32.div_u
global.get $assembly/buddy/List.SIZE
i32.add
else
else
local.get $3
end
local.set $4
Expand Down Expand Up @@ -2164,7 +2164,7 @@
call $assembly/buddy/parent_is_split
if (result i32)
i32.const 1
else
else
local.get $1
global.get $assembly/buddy/bucket_limit
i32.eq
Expand Down Expand Up @@ -2207,5 +2207,6 @@
call $start:assembly/index
)
(func $null (; 26 ;) (type $FUNCSIG$v)
unreachable
)
)
Loading

0 comments on commit fef1379

Please sign in to comment.