Skip to content

fix: do not incorrectly symbolize addresses beyong gopclntab end#950

Merged
fabled merged 4 commits into
open-telemetry:mainfrom
rogercoll:fix_sort_result
Nov 11, 2025
Merged

fix: do not incorrectly symbolize addresses beyong gopclntab end#950
fabled merged 4 commits into
open-telemetry:mainfrom
rogercoll:fix_sort_result

Conversation

@rogercoll
Copy link
Copy Markdown
Contributor

sort.Seach package docs:

If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) Search calls f(i) only for i in the range [0, n).

index := sort.Search(g.numFuncs, func(i int) bool {
funcPc, _ := g.getFuncMapEntry(i)
return funcPc > pc
}) - 1
Copy link
Copy Markdown
Contributor Author

@rogercoll rogercoll Nov 10, 2025

Choose a reason for hiding this comment

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

Is this subtraction needed? Coredump tests are failing because the Go symbolization does not map anymore, I am wondering if this was added because assuming "if search not found" returns 0

cc @open-telemetry/ebpf-profiler-approvers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As you see, this PR results tests failing and the change is incorrect.

The reason is that sort.Search lambda is designed so that the returned entry is the first entry that has address greater than pc. But we are interested in the entry that is the first below the pc. Thus the -1 is needed to adjust the result to the wanted record.

The search needs to be like due to the way sort.Search works, and how the on-disk map table is sorted.

Hope this clarifies the issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the context, hope 8c266b5 fixes the search result. (Although some MacOS/arm64 tests might need to be fixed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No the fix is incorrect. It breaks tests where the correct match is against the first entry. There's something more in the issue at hand.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this work:

--- a/nativeunwind/elfunwindinfo/elfgopclntab.go
+++ b/nativeunwind/elfunwindinfo/elfgopclntab.go
@@ -541,11 +541,11 @@ func (g *Gopclntab) mapPcval(offs int32, startPc, pc uint) (int32, bool) {
 
 // Symbolize returns the file, line and function information for given PC
 func (g *Gopclntab) Symbolize(pc uintptr) (sourceFile string, line uint, funcName string) {
-       index := sort.Search(g.numFuncs, func(i int) bool {
+       index := sort.Search(g.numFuncs + 1, func(i int) bool {
                funcPc, _ := g.getFuncMapEntry(i)
                return funcPc > pc
        }) - 1
-       if index < 0 {
+       if index < 0 || index >= g.numFuncs {
                return "", 0, ""
        }
 

This is valid because in .gopclntab the function table contains one extra item in the end which is the end-of-the-code entry. This should fix the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If it works, I can prepare a PR with proper explanation and also amend some comments on how the lookup is designed to work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, these changes work. Moving the PR ready for review, but feel free to close it and fix it in another one if preferred.

@rogercoll rogercoll marked this pull request as ready for review November 10, 2025 16:21
@rogercoll rogercoll requested review from a team as code owners November 10, 2025 16:21
@fabled fabled changed the title fix: handle not-found case from sort.Search correctly fix: do not incorrectly symbolize addresses beyong gopclntab end Nov 10, 2025
Comment thread nativeunwind/elfunwindinfo/elfgopclntab.go
Copy link
Copy Markdown
Contributor

@fabled fabled left a comment

Choose a reason for hiding this comment

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

Approving. Perhaps you can commit the suggested additional commit (committing from web ui should also mark me as co-author).

Co-authored-by: Timo Teräs <timo.teras@iki.fi>
@fabled fabled merged commit 40664e6 into open-telemetry:main Nov 11, 2025
26 checks passed
bobrik pushed a commit to bobrik/opentelemetry-ebpf-profiler that referenced this pull request Nov 26, 2025
bobrik pushed a commit to bobrik/opentelemetry-ebpf-profiler that referenced this pull request Nov 26, 2025
bobrik pushed a commit to bobrik/opentelemetry-ebpf-profiler that referenced this pull request Nov 29, 2025
bobrik pushed a commit to bobrik/opentelemetry-ebpf-profiler that referenced this pull request Dec 5, 2025
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.

3 participants