Skip to content

Commit d2325cc

Browse files
committed
transform: introduce check for method calls on nil interfaces
I ran into an issue where I did a method call on a nil interface and it resulted in a HardFault. Luckily I quickly realized what was going on so I could fix it, but I think undefined behavior is definitely the wrong behavior in this case. This commit therefore changes such calls to cause a nil panic instead of introducing undefined behavior. This does have a code size impact. It's relatively minor, much lower than I expected. When comparing the before and after of the drivers smoke tests (probably the most representative sample available), I found that most did not change at all and those that did change, normally not more than 100 bytes (16 or 32 byte changes are typical). Right now the pattern is the following: switch typecode { case 1: call method 1 case 2: call method 2 default: nil panic } I also tried the following (in the hope that it would be easier to optimize), but it didn't really result in a code size reduction: switch typecode { case 1: call method 1 case 2: call method 2 case 0: nil panic default: unreachable } Some code got smaller, while other code (the majority) got bigger. Maybe this can be improved once range[1] is finally allowed[2] on function parameters, but it'll probably take a while before that is implemented. [1]: https://llvm.org/docs/LangRef.html#range-metadata [2]: rust-lang/rust#50156
1 parent e223a9b commit d2325cc

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

transform/interface-lowering.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,19 @@ func (p *lowerInterfacesPass) createInterfaceMethodFunc(itf *interfaceInfo, sign
616616
// Create entry block.
617617
entry := p.ctx.AddBasicBlock(fn, "entry")
618618

619-
// Create default block and make it unreachable (which it is, because all
620-
// possible types are checked).
619+
// Create default block and call runtime.nilPanic.
620+
// The only other possible value remaining is nil for nil interfaces. We
621+
// could panic with a different message here such as "nil interface" but
622+
// that would increase code size and "nil panic" is close enough. Most
623+
// importantly, it avoids undefined behavior when accidentally calling a
624+
// method on a nil interface.
621625
defaultBlock := p.ctx.AddBasicBlock(fn, "default")
622626
p.builder.SetInsertPointAtEnd(defaultBlock)
627+
nilPanic := p.mod.NamedFunction("runtime.nilPanic")
628+
p.builder.CreateCall(nilPanic, []llvm.Value{
629+
llvm.Undef(llvm.PointerType(p.ctx.Int8Type(), 0)),
630+
llvm.Undef(llvm.PointerType(p.ctx.Int8Type(), 0)),
631+
}, "")
623632
p.builder.CreateUnreachable()
624633

625634
// Create type switch in entry block.

transform/testdata/interface.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare void @runtime.printuint8(i8)
2424
declare void @runtime.printint32(i32)
2525
declare void @runtime.printptr(i32)
2626
declare void @runtime.printnl()
27+
declare void @runtime.nilPanic(i8*, i8*)
2728

2829
define void @printInterfaces() {
2930
call void @printInterface(i32 ptrtoint (%runtime.typeInInterface* @"typeInInterface:reflect/types.type:basic:int" to i32), i8* inttoptr (i32 5 to i8*))

transform/testdata/interface.out.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ declare void @runtime.printptr(i32)
2525

2626
declare void @runtime.printnl()
2727

28+
declare void @runtime.nilPanic(i8*, i8*)
29+
2830
define void @printInterfaces() {
2931
call void @printInterface(i32 4, i8* inttoptr (i32 5 to i8*))
3032
call void @printInterface(i32 16, i8* inttoptr (i8 120 to i8*))
@@ -83,6 +85,7 @@ entry:
8385
]
8486

8587
default: ; preds = %entry
88+
call void @runtime.nilPanic(i8* undef, i8* undef)
8689
unreachable
8790

8891
"reflect/types.type:named:Number": ; preds = %entry

0 commit comments

Comments
 (0)