Skip to content

Properly handle nulls in runtime upcast checks #2042

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

Merged
merged 2 commits into from
Sep 4, 2021
Merged
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
31 changes: 24 additions & 7 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10532,14 +10532,31 @@ export class Compiler extends DiagnosticEmitter {
var temp = flow.getTempLocal(type);
var instanceofInstance = this.program.instanceofInstance;
assert(this.compileFunction(instanceofInstance));
expr = module.if(
module.call(instanceofInstance.internalName, [
if (!toType.isNullableReference || flow.isNonnull(expr, type)) {
// Simplify if the value cannot be `null`. If toType is non-nullable, a
// null-check would have been emitted separately so is not necessary here.
expr = module.if(
module.call(instanceofInstance.internalName, [
module.local_tee(temp.index, expr, type.isManaged),
module.i32(toType.classReference!.id)
], TypeRef.I32),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected upcast"), reportNode) // TODO: throw
);
} else {
expr = module.if(
module.local_tee(temp.index, expr, type.isManaged),
module.i32(toType.classReference!.id)
], TypeRef.I32),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected upcast"), reportNode) // TODO: throw
);
module.if(
module.call(instanceofInstance.internalName, [
module.local_get(temp.index, type.toRef()),
module.i32(toType.classReference!.id)
], TypeRef.I32),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected upcast"), reportNode) // TODO: throw
),
module.usize(0)
);
}
flow.freeTempLocal(temp);
this.currentType = toType;
return expr;
Expand Down
173 changes: 90 additions & 83 deletions tests/compiler/managed-cast.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1468,17 +1468,17 @@
i32.const 1344
global.set $~lib/rt/itcms/fromSpace
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $1
local.get $0
i32.store
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
Expand All @@ -1489,7 +1489,7 @@
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store
local.get $1
local.get $0
i32.eqz
if
i32.const 1456
Expand All @@ -1500,30 +1500,30 @@
unreachable
end
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $1
local.get $0
i32.store
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.get $1
local.get $0
i32.store
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
Expand All @@ -1535,29 +1535,29 @@
i32.const 0
i32.store
block $__inlined_func$~lib/rt/__instanceof (result i32)
local.get $1
local.get $0
i32.const 20
i32.sub
i32.load offset=12
local.tee $0
local.tee $1
i32.const 1632
i32.load
i32.le_u
if
loop $do-continue|0
i32.const 1
local.get $0
local.get $1
i32.const 3
i32.eq
br_if $__inlined_func$~lib/rt/__instanceof
drop
local.get $0
local.get $1
i32.const 3
i32.shl
i32.const 1636
i32.add
i32.load offset=4
local.tee $0
local.tee $1
br_if $do-continue|0
end
end
Expand All @@ -1573,20 +1573,20 @@
unreachable
end
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 8
i32.sub
global.set $~lib/memory/__stack_pointer
Expand All @@ -1595,10 +1595,10 @@
i32.lt_s
br_if $folding-inner0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.tee $1
i64.const 0
i64.store
local.get $1
local.get $0
i32.eqz
if
i32.const 1456
Expand All @@ -1608,11 +1608,12 @@
call $~lib/builtins/abort
unreachable
end
local.get $0
local.get $1
local.get $0
local.tee $1
i32.store offset=4
block $__inlined_func$~lib/rt/__instanceof11 (result i32)
local.get $1
local.get $0
i32.const 20
i32.sub
i32.load offset=12
Expand Down Expand Up @@ -1658,12 +1659,12 @@
i32.add
global.set $~lib/memory/__stack_pointer
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
Expand All @@ -1676,29 +1677,29 @@
i32.const 0
i32.store
block $__inlined_func$~lib/rt/__instanceof14 (result i32)
local.get $1
local.get $0
i32.const 20
i32.sub
i32.load offset=12
local.tee $0
local.tee $1
i32.const 1632
i32.load
i32.le_u
if
loop $do-continue|015
i32.const 1
local.get $0
local.get $1
i32.const 3
i32.eq
br_if $__inlined_func$~lib/rt/__instanceof14
drop
local.get $0
local.get $1
i32.const 3
i32.shl
i32.const 1636
i32.add
i32.load offset=4
local.tee $0
local.tee $1
br_if $do-continue|015
end
end
Expand All @@ -1714,19 +1715,19 @@
unreachable
end
local.get $2
local.get $1
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 4
i32.add
global.set $~lib/memory/__stack_pointer
call $managed-cast/Cat#constructor
local.set $1
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $0
local.get $1
i32.store
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 4
i32.sub
global.set $~lib/memory/__stack_pointer
Expand All @@ -1738,46 +1739,52 @@
local.tee $2
i32.const 0
i32.store
block $__inlined_func$~lib/rt/__instanceof17 (result i32)
local.get $1
i32.const 20
i32.sub
i32.load offset=12
local.tee $0
i32.const 1632
i32.load
i32.le_u
if
loop $do-continue|018
i32.const 1
local.get $0
i32.const 3
i32.eq
br_if $__inlined_func$~lib/rt/__instanceof17
drop
local.get $0
i32.const 3
i32.shl
i32.const 1636
i32.add
i32.load offset=4
local.tee $0
br_if $do-continue|018
local.get $0
if
block $__inlined_func$~lib/rt/__instanceof17 (result i32)
local.get $0
i32.const 20
i32.sub
i32.load offset=12
local.tee $1
i32.const 1632
i32.load
i32.le_u
if
loop $do-continue|018
i32.const 1
local.get $1
i32.const 3
i32.eq
br_if $__inlined_func$~lib/rt/__instanceof17
drop
local.get $1
i32.const 3
i32.shl
i32.const 1636
i32.add
i32.load offset=4
local.tee $1
br_if $do-continue|018
end
end
i32.const 0
end
i32.eqz
if
i32.const 1584
i32.const 1520
i32.const 47
i32.const 30
call $~lib/builtins/abort
unreachable
end
else
i32.const 0
end
i32.eqz
if
i32.const 1584
i32.const 1520
i32.const 47
i32.const 30
call $~lib/builtins/abort
unreachable
local.set $0
end
local.get $2
local.get $1
local.get $0
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 4
Expand Down
Loading