Skip to content

Commit fef1379

Browse files
committed
Indirect calls on null now either trap with unreachable or, if exception-handlig is enabled, throws a Wasm exception
1 parent b2bb264 commit fef1379

File tree

281 files changed

+623
-340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+623
-340
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ jobs:
7070
run: npm run clean
7171
- name: Test experimental features
7272
env:
73-
ASC_FEATURES: mutable-globals,threads,reference-types,bigint-integration
73+
ASC_FEATURES: mutable-globals,threads,reference-types,bigint-integration,exception-handling
7474
run: |
75-
npm run test:compiler rt/flags features/js-bigint-integration features/reference-types features/threads
75+
npm run test:compiler rt/flags features/js-bigint-integration features/reference-types features/threads features/exception-handling
7676
test-runtime:
7777
name: "Test runtimes on node: node"
7878
runs-on: ubuntu-latest

examples/game-of-life/build/untouched.wat

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
global.get $assembly/config/BGR_DEAD
6161
i32.const 16777215
6262
i32.and
63-
else
63+
else
6464
global.get $assembly/config/BGR_ALIVE
6565
i32.const -16777216
6666
i32.or
@@ -134,7 +134,7 @@
134134
i32.eq
135135
if (result i32)
136136
local.get $0
137-
else
137+
else
138138
local.get $2
139139
i32.const 1
140140
i32.sub
@@ -145,7 +145,7 @@
145145
i32.eq
146146
if (result i32)
147147
i32.const 0
148-
else
148+
else
149149
local.get $2
150150
i32.const 1
151151
i32.add
@@ -165,7 +165,7 @@
165165
i32.eq
166166
if (result i32)
167167
local.get $1
168-
else
168+
else
169169
local.get $5
170170
i32.const 1
171171
i32.sub
@@ -176,7 +176,7 @@
176176
i32.eq
177177
if (result i32)
178178
i32.const 0
179-
else
179+
else
180180
local.get $5
181181
i32.const 1
182182
i32.add
@@ -367,7 +367,7 @@
367367
i32.shl
368368
local.get $14
369369
i32.store
370-
else
370+
else
371371
local.get $5
372372
local.set $16
373373
local.get $2
@@ -388,7 +388,7 @@
388388
local.get $14
389389
i32.store
390390
end
391-
else
391+
else
392392
local.get $9
393393
i32.const 3
394394
i32.eq
@@ -412,7 +412,7 @@
412412
i32.shl
413413
local.get $8
414414
i32.store
415-
else
415+
else
416416
local.get $5
417417
local.set $15
418418
local.get $2
@@ -563,5 +563,6 @@
563563
end
564564
)
565565
(func $null (; 4 ;) (type $FUNCSIG$v)
566+
unreachable
566567
)
567568
)

examples/mandelbrot/build/untouched.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,6 @@
251251
end
252252
)
253253
(func $null (; 3 ;) (type $FUNCSIG$v)
254+
unreachable
254255
)
255256
)

examples/n-body/build/untouched.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,5 +2225,6 @@
22252225
global.set $~lib/rt/stub/offset
22262226
)
22272227
(func $null (; 24 ;) (type $FUNCSIG$v)
2228+
unreachable
22282229
)
22292230
)

examples/pson/build/untouched.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,5 +484,6 @@
484484
end
485485
)
486486
(func $null (; 18 ;) (type $FUNCSIG$v)
487+
unreachable
487488
)
488489
)

lib/i64/build/untouched.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,5 +749,6 @@
749749
global.set $assembly/i64/hi
750750
)
751751
(func $null (; 31 ;) (type $FUNCSIG$v)
752+
unreachable
752753
)
753754
)

src/compiler.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ export class Compiler extends DiagnosticEmitter {
408408
module.removeGlobal(BuiltinSymbols.rtti_base);
409409
if (this.runtimeFeatures & RuntimeFeatures.RTTI) compileRTTI(this);
410410

411+
// set up function table
412+
var functionTable = this.functionTable;
413+
module.setFunctionTable(functionTable.length, 0xffffffff, functionTable, module.i32(0));
414+
this.addNullPointerExceptionHandling(module);
415+
411416
// update the heap base pointer
412417
var memoryOffset = this.memoryOffset;
413418
memoryOffset = i64_align(memoryOffset, options.usizeType.byteSize);
@@ -447,11 +452,6 @@ export class Compiler extends DiagnosticEmitter {
447452
// import memory if requested (default memory is named '0' by Binaryen)
448453
if (options.importMemory) module.addMemoryImport("0", "env", "memory", isSharedMemory);
449454

450-
// set up function table
451-
var functionTable = this.functionTable;
452-
module.setFunctionTable(functionTable.length, 0xffffffff, functionTable, module.i32(0));
453-
module.addFunction("null", this.ensureFunctionType(null, Type.void), null, module.block(null, []));
454-
455455
// import table if requested (default table is named '0' by Binaryen)
456456
if (options.importTable) module.addTableImport("0", "env", "table");
457457

@@ -462,6 +462,36 @@ export class Compiler extends DiagnosticEmitter {
462462
return module;
463463
}
464464

465+
addNullPointerExceptionHandling(module: Module) {
466+
if (this.options.hasFeature(Feature.EXCEPTION_HANDLING)) {
467+
let message = this.ensureStaticString("null is not a function");
468+
let exceptionType = module.addFunctionType(
469+
"NullPointerException",
470+
NativeType.None,
471+
[NativeType.I32]
472+
);
473+
474+
module.addEvent("NullPointerException", 0, exceptionType);
475+
module.addFunction(
476+
"null",
477+
this.ensureFunctionType(null, Type.void),
478+
null,
479+
module.block(null, [
480+
module.throw("NullPointerException", [message])
481+
])
482+
);
483+
} else {
484+
module.addFunction(
485+
"null",
486+
this.ensureFunctionType(null, Type.void),
487+
null,
488+
module.block(null, [
489+
module.unreachable()
490+
])
491+
);
492+
}
493+
}
494+
465495
// === Exports ==================================================================================
466496

467497
/** Applies the respective module exports for the specified file. */

tests/allocators/buddy/untouched.wat

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
local.get $1
8989
i32.const 3
9090
i32.and
91-
else
91+
else
9292
i32.const 0
9393
end
9494
if
@@ -1293,7 +1293,7 @@
12931293
i32.le_u
12941294
if (result i32)
12951295
i32.const 1
1296-
else
1296+
else
12971297
local.get $5
12981298
local.get $3
12991299
i32.add
@@ -1413,7 +1413,7 @@
14131413
end
14141414
end
14151415
end
1416-
else
1416+
else
14171417
local.get $4
14181418
i32.const 7
14191419
i32.and
@@ -1554,7 +1554,7 @@
15541554
local.get $4
15551555
i32.load8_u
15561556
i32.eq
1557-
else
1557+
else
15581558
i32.const 0
15591559
end
15601560
if
@@ -1581,7 +1581,7 @@
15811581
local.get $4
15821582
i32.load8_u
15831583
i32.sub
1584-
else
1584+
else
15851585
i32.const 0
15861586
end
15871587
end
@@ -2019,7 +2019,7 @@
20192019
i32.ne
20202020
if (result i32)
20212021
i32.const 1
2022-
else
2022+
else
20232023
local.get $2
20242024
i32.const 0
20252025
i32.eq
@@ -2060,7 +2060,7 @@
20602060
i32.div_u
20612061
global.get $assembly/buddy/List.SIZE
20622062
i32.add
2063-
else
2063+
else
20642064
local.get $3
20652065
end
20662066
local.set $4
@@ -2164,7 +2164,7 @@
21642164
call $assembly/buddy/parent_is_split
21652165
if (result i32)
21662166
i32.const 1
2167-
else
2167+
else
21682168
local.get $1
21692169
global.get $assembly/buddy/bucket_limit
21702170
i32.eq
@@ -2207,5 +2207,6 @@
22072207
call $start:assembly/index
22082208
)
22092209
(func $null (; 26 ;) (type $FUNCSIG$v)
2210+
unreachable
22102211
)
22112212
)

0 commit comments

Comments
 (0)