fix(runtime): segmentation fault when calling interface private methods cross-package#1387
Conversation
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
Code Review SummaryThis 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:
Overall: Good work. Please address the validation issue in |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
cebc16f to
49ba5a8
Compare
a586c4f to
3913997
Compare
runtime/internal/runtime/z_face.go
Outdated
| 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()) { |
There was a problem hiding this comment.
Name_ 本身格式为 Name 或者是 pkg.Name ( 不导出时 )所以如果 Name_ 相同,最后两个判断也一定是相同的。
There was a problem hiding this comment.
明白,那么基于当前的匹配逻辑来说,把所有abi.Type对应的Method都进入对比是安全的,并不会额外添加一些不预期的函数。当前的 mthds := methods(u, inter.PkgPath_) 是提高itab 填充func数组效率的一个方式,但是由于当前interfaceType.PkgPath 设定为了使用包,而非定义包,导致这个优化过程把应该保留的私有方法过滤了,当前Pr将移除当前提前过滤private methods的过程。
27343bb to
4da6525
Compare
…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>
4da6525 to
4e374a9
Compare
Fixes #1370 #1357
Problem
LLGo previously used an optimization to pre-filter methods before itab matching:
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:
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:
All test cases in the issue now pass.