fix(ssa): correct PkgPath for interface metadata in cross-package conversions

Fixes #1370 - Segmentation Fault When Calling Interface Private Methods Cross-Package

This commit fixes a critical bug where interface metadata's PkgPath was incorrectly
set when converting concrete type pointers to interfaces with private methods across
package boundaries.

Problem:
- When a concrete type pointer was converted to an interface with private methods
  in a package different from the interface definition package, the compiler
  incorrectly set the interface metadata's PkgPath to the current compilation
  package instead of the interface definition package
- This caused the runtime to only fill exported methods in the itab, leaving
  private method slots as NULL (0x0), resulting in segmentation faults

Solution:
- Modified abiInterfaceOf() in ssa/abitype.go to extract the package path from
  the interface's private methods (if any)
- Use that package path instead of the current compilation package path
- Fall back to current package path only if all methods are exported

Changes:
- ssa/abitype.go: Use abi.PathOf() to get correct package path
- ssa/interface.go: Refactor to simplify interface type handling
- Added comprehensive test cases and demos for go/types, go/token, and go/ast
- Updated all test outputs to reflect correct interface metadata generation

This fix resolves segmentation faults when using Go standard library interfaces
and user-defined interfaces with private methods across package boundaries.

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
This commit is contained in:
xgopilot
2025-10-30 06:24:23 +00:00
parent d2a22252c2
commit 88d32c9930
19 changed files with 2959 additions and 1741 deletions

View File

@@ -43,11 +43,11 @@ func (b Builder) newItab(tintf, typ Expr) Expr {
return b.Call(b.Pkg.rtFunc("NewItab"), tintf, typ)
}
func (b Builder) unsafeInterface(rawIntf *types.Interface, t Expr, data llvm.Value) llvm.Value {
func (b Builder) unsafeInterface(rawIntf *types.Interface, originType types.Type, t Expr, data llvm.Value) llvm.Value {
if rawIntf.Empty() {
return b.unsafeEface(t.impl, data)
}
tintf := b.abiType(rawIntf)
tintf := b.abiType(originType)
itab := b.newItab(tintf, t)
return b.unsafeIface(itab.impl, data)
}
@@ -112,7 +112,7 @@ func (b Builder) MakeInterface(tinter Type, x Expr) (ret Expr) {
case abi.Indirect:
vptr := b.AllocU(typ)
b.Store(vptr, x)
return Expr{b.unsafeInterface(rawIntf, tabi, vptr.impl), tinter}
return Expr{b.unsafeInterface(rawIntf, tinter.raw.Type, tabi, vptr.impl), tinter}
}
ximpl := x.impl
if lvl > 0 {
@@ -121,7 +121,7 @@ func (b Builder) MakeInterface(tinter Type, x Expr) (ret Expr) {
var u llvm.Value
switch kind {
case abi.Pointer:
return Expr{b.unsafeInterface(rawIntf, tabi, ximpl), tinter}
return Expr{b.unsafeInterface(rawIntf, tinter.raw.Type, tabi, ximpl), tinter}
case abi.Integer:
tu := prog.Uintptr()
u = llvm.CreateIntCast(b.impl, ximpl, tu.ll)
@@ -136,7 +136,7 @@ func (b Builder) MakeInterface(tinter Type, x Expr) (ret Expr) {
panic("todo")
}
data := llvm.CreateIntToPtr(b.impl, u, prog.tyVoidPtr())
return Expr{b.unsafeInterface(rawIntf, tabi, data), tinter}
return Expr{b.unsafeInterface(rawIntf, tinter.raw.Type, tabi, data), tinter}
}
func (b Builder) valFromData(typ Type, data llvm.Value) Expr {
@@ -249,7 +249,9 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) Expr {
} else {
if rawIntf, ok := assertedTyp.raw.Type.Underlying().(*types.Interface); ok {
eq = b.InlineCall(b.Pkg.rtFunc("Implements"), tabi, tx)
val = func() Expr { return Expr{b.unsafeInterface(rawIntf, tx, b.faceData(x.impl)), assertedTyp} }
val = func() Expr {
return Expr{b.unsafeInterface(rawIntf, assertedTyp.raw.Type, tx, b.faceData(x.impl)), assertedTyp}
}
} else {
eq = b.BinOp(token.EQL, tx, tabi)
val = func() Expr { return b.valFromData(assertedTyp, b.faceData(x.impl)) }
@@ -305,7 +307,7 @@ func (b Builder) ChangeInterface(typ Type, x Expr) (ret Expr) {
rawIntf := typ.raw.Type.Underlying().(*types.Interface)
tabi := b.faceAbiType(x)
data := b.faceData(x.impl)
return Expr{b.unsafeInterface(rawIntf, tabi, data), typ}
return Expr{b.unsafeInterface(rawIntf, typ.raw.Type, tabi, data), typ}
}
// -----------------------------------------------------------------------------