Skip to content

[rocprofiler-sdk-rocattach] Fix wrong library base in find_library() - #5413

Merged
adjordje-amd merged 1 commit into
developfrom
users/adjordje-amd/rocattach-symbol-lookup-fix
Apr 27, 2026
Merged

adjordje-amd merged 1 commit into
developfrom
users/adjordje-amd/rocattach-symbol-lookup-fix

Conversation

@adjordje-amd

@adjordje-amd adjordje-amd commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

find_library() returns the wrong load base when multiple /proc/<pid>/maps lines mention the library. The current code takes the first match. By definition, the load base is the line whose file offset is 0.

When the runtime adds extra file-backed mappings of the same library at non-zero file offsets at addresses below the original load (observed during attach setup of librocprofiler-register.so), symbol resolution computes wrong_base + symbol_offset, lands in unrelated memory, and SIGSEGVs the injected ptrace call. The dying thread leaves the ptrace state machine unrecoverable, PTraceSession::~PTraceSession throws during cleanup, and the attacher aborts.

Technical Details

Filter the /proc/<pid>/maps scan to entries whose file offset is 0. This is the unique discriminator for the library load base, robust to any number of extra shadow mappings at other offsets.

Replace the implicit if (!maps) no-match detection with an explicit found flag. The new filter would otherwise let a substring-matching but offset-nonzero line reach std::stoull(line, ...) and silently return a wrong address.

JIRA

N/A

Test Plan

Manual repro with rocprof-sys-attach -p <PID> -d 5 against a HIP target launched with ROCP_TOOL_ATTACH=1.

Test Result

  • Without patch: rocattach_detach SIGSEGVs in target on first detach, attacher dies with SIGABRT.
  • With patch: attach + detach complete with rc=0, target survives.

Checklist

  • Fix verified locally
  • Reviewed by rocattach maintainers

find_library() scans /proc/<pid>/maps and returns the start address of
the first line whose path contains the library name. A shared library
normally has multiple maps entries (one per page-protection segment), of
which only the segment whose file offset is 0 is the actual library load
base.

When the runtime adds extra writable file-backed mappings of the same
library at non-zero file offsets (e.g. for code patching) at addresses
below the original load mapping, those entries appear first in the maps
file (which is sorted by address) and the linear scan returns the wrong
base. Symbol resolution then computes target_addr = wrong_base + offset,
which lands in unrelated memory and SIGSEGVs the injected call.

Filter the scan to lines whose file offset (the third whitespace-
separated field) is 0. There is exactly one such mapping per loaded
library, regardless of any extra shadow mappings at other offsets.
@adjordje-amd
adjordje-amd force-pushed the users/adjordje-amd/rocattach-symbol-lookup-fix branch from e310e10 to 70783d9 Compare April 24, 2026 11:17
@adjordje-amd
adjordje-amd marked this pull request as ready for review April 24, 2026 14:15
@adjordje-amd
adjordje-amd requested review from a team as code owners April 24, 2026 14:15

@meserve-amd meserve-amd left a comment

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.

Seems sensible. The old code assumed the entries would be in order but that is not a guarantee. Good find.

CI failure in rhel is intermittent and unrelated; I reran it to get a pass.

@adjordje-amd
adjordje-amd merged commit adcc766 into develop Apr 27, 2026
36 of 46 checks passed
@adjordje-amd
adjordje-amd deleted the users/adjordje-amd/rocattach-symbol-lookup-fix branch April 27, 2026 20:24
adjordje-amd added a commit that referenced this pull request Oct 12, 2026
…h/re-attach) (#5414)

## Motivation

`rocprof-sys-attach` (the rocattach codepath) had no automated test
coverage. This PR adds a parametrized pytest exercising attach, detach,
and re-attach against a long-running HIP target.

## Technical Details

- `tests/pytest/test_rocattach.py` (new): spawns `transpose` in infinite
mode, runs `n_sessions` attach sessions (`n_sessions = 1, 2, 3`) via
`rocprof-sys-attach`, asserts each session produces an output directory
containing a perfetto trace and that the target survives.
- `tests/pytest/rocprofsys/config.py`: adds optional `rocprofsys_attach:
Path` field; tests that need it skip when the binary is not built.
- `source/bin/rocprof-sys-attach/rocprof-sys-attach.cpp`: adds
`--detach-after <seconds>` flag for non-interactive use; sleeps instead
of waiting for ENTER on stdin.
- `examples/transpose/transpose.cpp`: passing `0` for `NUM_ITERATION`
enables infinite mode; `SIGTERM`/`SIGINT` stops the loop cleanly.

## Dependencies

- #5413 - `find_library()` fix in `rocprofiler-sdk-rocattach`. Required
for `n_sessions = 1` to pass.
- An additional ptrace state-machine failure in the rocattach detach
path that prevents `n_sessions = 2` and `n_sessions = 3` from passing.
Root cause not yet identified; tracking separately.

## JIRA

N/A

## Test Plan

```bash
ctest --test-dir <BUILD_DIR> -R rocattach -V
```
Verify all three parametrized cases (`n_sessions = 1, 2, 3`) pass.

## Test Result

## Checklist

- [x] Test added under `tests/pytest/test_rocattach.py`
- [x] CHANGELOG entry
- [x] Test skips gracefully when GPU is unavailable, `ptrace_scope` is
non-zero, or the `rocprof-sys-attach` binary is not built
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants