Skip to content

fix(runtime): segmentation fault when calling interface private methods cross-package#1387

Merged
xushiwei merged 2 commits intogoplus:mainfrom
luoliwoshang:runtime/iface/func
Nov 10, 2025
Merged

fix(runtime): segmentation fault when calling interface private methods cross-package#1387
xushiwei merged 2 commits intogoplus:mainfrom
luoliwoshang:runtime/iface/func

Conversation

@luoliwoshang
Copy link
Member

@luoliwoshang luoliwoshang commented Nov 6, 2025

Fixes #1370 #1357

Problem

LLGo previously used an optimization to pre-filter methods before itab matching:

mthds := methods(u, inter.PkgPath_)

func methods(u *abi.UncommonType, from string) []abi.Method {
    if u.PkgPath_ == from {
        return u.Methods()          // All methods if same package
    }
    return u.ExportedMethods()      // Only exported if different package
}

This optimization attempted to improve itab filling performance by filtering out private methods when the interface and type are from different packages.

However, this optimization relies on interfaceType.PkgPath being set to the interface definition package. Currently, interfaceType.PkgPath is set to the
package where the interface is used (where the interface conversion happens) rather than the package where the interface is defined. This causes private
methods to be incorrectly filtered out, leaving itab slots as NULL and causing segmentation faults.

Solution

Remove the pre-filtering optimization entirely. Since method names (Name_) already contain package path information:

  • Exported methods: "MethodName"
  • Unexported methods: "package/path.methodName"

When Name_ matches, the method is guaranteed to be the correct one. The additional package checks are redundant, and comparing all methods is safe without
risk of adding unexpected functions.

This PR removes the methods() filtering function and directly uses u.Methods() for matching, bypassing the interfaceType.PkgPath issue entirely.

Impact

✅ Fixes segfaults for:

  • Standard library: go/types.Object, go/ast.Expr, etc.
  • User-defined interfaces with private methods
  • Cross-package interface conversions returning concrete type pointers

All test cases in the issue now pass.

@luoliwoshang luoliwoshang changed the title Runtime/iface/func fix(ssa): use empty PkgPath for anonymous interfaces in cross-package conversions Nov 6, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @luoliwoshang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily refactors the LLVM backend's handling of Go interfaces, focusing on their representation and method lookup mechanisms. The changes streamline how interface types are generated in LLVM IR by omitting explicit package path strings during runtime.Interface calls. Additionally, the method visibility logic has been enhanced to support retrieving all methods when a package path is not specified. The PR also includes new demonstration code for Go's token and types packages and a new interface-related test case to ensure correctness.

Highlights

  • Go Interface Representation: The way Go interfaces are represented and initialized in the generated LLVM IR has been updated. Specifically, the package path string is no longer passed to the runtime.Interface function, instead using a zero-initialized string.
  • Method Visibility Logic: The logic for retrieving methods from an UncommonType has been refined. The methods function in the runtime now returns all methods (not just exported ones) if the from package path is an empty string.
  • New Go Tooling Demos: New demonstration files have been added for the go/token and go/types packages, showcasing their functionalities related to tokenizing, parsing, and type information in Go.
  • Interface Test Case: A new test case (geometry1370) has been introduced to validate the updated interface handling, including a Go interface, a struct implementing it, and its corresponding LLVM IR output.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes an issue with cross-package interface conversions, particularly when interfaces contain unexported methods. The core of the fix is in ssa/abitype.go, where anonymous interfaces are now correctly generated with an empty package path. This is supported by a change in runtime/internal/runtime/z_face.go to adjust method visibility checks for such interfaces. The new test case interface1370 effectively demonstrates and validates the fix. The PR also includes updates to numerous LLVM IR output files, which are consistent with the logic changes, and adds new comprehensive demo/test files for the go/token and go/types packages. The changes are correct and well-implemented.

pkg := b.Pkg
methods := make([]Expr, n)
for i := 0; i < n; i++ {
m := t.Method(i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation: Consider adding a comment explaining why unexported methods need fully qualified names:

// For unexported methods, use the fully qualified name with package path
// to distinguish methods with the same name from different packages
if !token.IsExported(mName) {
    mName = abi.FullName(m.Pkg(), mName)
}

This helps maintainers understand the cross-package interface method resolution logic.

@xgopilot
Copy link
Contributor

xgopilot bot commented Nov 6, 2025

Code Review Summary

This PR correctly fixes PkgPath handling for interface methods, particularly for anonymous interfaces and cross-package conversions. The implementation is solid with well-structured demo programs.

Key findings:

  • One security issue in ssa/interface.go:255 regarding potential out-of-bounds access
  • Documentation could be improved to explain cross-package interface method resolution
  • No performance regressions introduced
  • Comprehensive test coverage with excellent demo programs

Overall: Good work. Please address the validation issue in Imethod function.

@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.98%. Comparing base (d2a2225) to head (4e374a9).
⚠️ Report is 37 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1387      +/-   ##
==========================================
+ Coverage   90.18%   90.98%   +0.79%     
==========================================
  Files          43       43              
  Lines       12752    11287    -1465     
==========================================
- Hits        11501    10269    -1232     
+ Misses       1092      859     -233     
  Partials      159      159              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@luoliwoshang luoliwoshang changed the title fix(ssa): use empty PkgPath for anonymous interfaces in cross-package conversions fix(runtime): segmentation fault when calling interface private methods cross-package Nov 6, 2025
@luoliwoshang luoliwoshang force-pushed the runtime/iface/func branch 2 times, most recently from a586c4f to 3913997 Compare November 6, 2025 09:51
mName := m.Name_
if mName >= imName {
if mName == imName && m.Mtyp_ == im.Typ_ {
if mName == imName && m.Mtyp_ == im.Typ_ && (m.Exported() || im.PkgPath() == m.PkgPath()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name_ 本身格式为 Name 或者是 pkg.Name ( 不导出时 )所以如果 Name_ 相同,最后两个判断也一定是相同的。

Copy link
Member Author

@luoliwoshang luoliwoshang Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明白,那么基于当前的匹配逻辑来说,把所有abi.Type对应的Method都进入对比是安全的,并不会额外添加一些不预期的函数。当前的 mthds := methods(u, inter.PkgPath_) 是提高itab 填充func数组效率的一个方式,但是由于当前interfaceType.PkgPath 设定为了使用包,而非定义包,导致这个优化过程把应该保留的私有方法过滤了,当前Pr将移除当前提前过滤private methods的过程。

luoliwoshang and others added 2 commits November 6, 2025 21:00
…ds cross-package

Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: Claude <noreply@anthropic.com>
@luoliwoshang
Copy link
Member Author

@xushiwei

@xushiwei xushiwei merged commit a676ba2 into goplus:main Nov 10, 2025
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation Fault When Calling Interface Private Methods Cross-Package

3 participants