Skip to content

fix: alignment-safe symbol and relocation reads in the ELF parser (PROF-16551) - #964

Open
kaahos wants to merge 18 commits into
mainfrom
PROF-16551-misaligned-elfsym
Open

kaahos wants to merge 18 commits into
mainfrom
PROF-16551-misaligned-elfsym

Conversation

@kaahos

@kaahos kaahos commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?:

Finishes alignment-safe ELF symbol and relocation reads in symbols_linux.cpp, on top of #974's loadSymbolTable fix:

  • symbolNameAt() and relocationAt() copy st_name and the Elf64_Rel prefix out with memcpy at the four sites that dereferenced cast ElfSymbol* / ElfRelocation*: both parseDynamicSection import paths, addRelocationSymbols and hashSymbolRelocations.
  • The relocation walks skip tables whose DT_RELAENT is smaller than an Elf64_Rel, and advance only while a whole entry fits. That stops reads past a table that ends mid-entry, and the hashSymbolRelocations offset no longer wraps back before the table for a huge DT_RELAENT.

Motivation:

PROF-16551: the asan gtest build (-fsanitize=alignment -fno-sanitize-recover=all) aborts with member access within misaligned address on these casts. Nothing checks the alignment of sh_offset, DT_SYMTAB, DT_JMPREL or DT_RELA, so a crafted or corrupt image can misalign the tables.

Additional Notes:

  • Copies rather than alignment validation: real toolchains align these tables, so copying removes the UB without a new rejection path, at one small memcpy per entry on cold paths. Well-formed libraries parse and fingerprint exactly as before.
  • freeNativeLibsForTest() is only safe in a binary that never patches or updates build ids, since those tables match caches by address; elfparser_ut meets that.
  • Out of scope (pre-existing): ElfSection / ElfProgramHeader / ElfNote / ElfDyn casts and the DT_HASH / DT_GNU_HASH reads still lack alignment handling; parseDynamicSection does not bound the relocation symbol index, skips .dynsym when DT_RELAENT is absent, and takes the .rela.plt stride from DT_RELAENT rather than DT_PLTREL.

How to test the change?:

./utils/run-containers-tests.sh --gtest-task=elfparser_ut --mount

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a security review.
  • This PR doesn't touch any of that.

Symbol-table addresses come from untrusted sh_offset / DT_SYMTAB values with
no alignment guarantee; casting them to ElfSymbol* and dereferencing is
undefined behavior and aborts the ASan+UBSan gtest build at
symbols_linux.cpp:974. Copy entries (or just st_name) into aligned locals
instead. Adds an elfparser_ut regression that places a .symtab at a
misaligned file offset and asserts both symbols and the PLT relocation name
resolve.
@dd-octo-sts

dd-octo-sts Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Scan-Build Report

User:runner@runnervmwuk1x
Working Directory:/home/runner/work/java-profiler/java-profiler/ddprof-lib/src/test/make
Command Line:make -j4 all
Clang Version:Ubuntu clang version 21.1.8 (6ubuntu1)
Date:Thu Nov 12 23:09:56 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs2
Logic error
Dereference of null pointer1
Unused code
Dead initialization1

Filter Results by File

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Unused codeDead initializationreferenceChains.cppapplyOomRamp4971
Logic errorDereference of null pointerperfEvents_linux.cppunregisterThread72026

@kaahos kaahos added test:release Run CI tests with release configuration test:asan Run CI tests with AddressSanitizer configuration test:tsan Run CI tests with ThreadSanitizer configuration test:fuzz test:reliability labels Nov 9, 2026
@dd-octo-sts

dd-octo-sts Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

CI Test Results

Run: #43340155198 | Commit: 1036fb5 | Duration: 20m 59s (longest job)

❌ 1 of 108 test jobs failed

Status Overview

JDK glibc-aarch64/asan glibc-aarch64/debug glibc-aarch64/release glibc-aarch64/tsan glibc-amd64/asan glibc-amd64/debug glibc-amd64/release glibc-amd64/tsan musl-aarch64/debug musl-aarch64/release musl-amd64/debug musl-amd64/release
8 - - - - ✅ ✅ ✅ ✅ - - - -
8-ibm - - - - ✅ ✅ ✅ ✅ - - - -
8-j9 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ❌ - - - -
8-librca - - - - - - - - ✅ ✅ ✅ ✅
8-orcl - - - - ✅ ✅ ✅ ✅ - - - -
11 - - - - ✅ ✅ ✅ ✅ - - - -
11-j9 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
11-librca - - - - - - - - ✅ ✅ ✅ ✅
17 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
17-graal ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
17-j9 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
17-librca - - - - - - - - ✅ ✅ ✅ ✅
21 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
21-graal ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
21-librca - - - - - - - - ✅ ✅ ✅ ✅
25 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
25-graal ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ - - - -
25-librca - - - - - - - - ✅ ✅ ✅ ✅

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Failed Jobs

Summary: Total: 108 | Passed: 107 | Failed: 1


Updated: 2026-11-13 01:15:25 UTC

@dd-octo-sts

dd-octo-sts Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ All 40 integration tests passed

📊 Dashboard · 👷 Pipeline · 📦 7237a466

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Tests

🔁 Datadog re-ran test-matrix / test-linux-glibc-amd64 (25-graal, tsan, regular) after it failed. The re-run did not pass. View in Datadog

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 7237a46 | Docs | View more details | Give us feedback!

State that the misaligned member access is unconditionally undefined
behavior (the sanitizer flag only detects it), rename readSymbol to
readSymbolEntry to avoid the VMStructs::readSymbol grep collision, and use
reinterpret_cast instead of a C-style cast in hashSymbolRelocations.
Resource-dependent tests called exit(0)/exit(1) when the cwd-relative
native-libs paths did not resolve, so the whole binary exited before any
test ran from the project-root cwd CI uses; replace the early exits with
GTEST_SKIP()/FAIL() so the resource-independent tests always execute.
Free the CodeCaches that parseLibraries publishes into the tests' stack
CodeCacheArrays (and the ElfReladyn fixture's process-wide array via new
#ifdef UNIT_TEST freeForTest/freeNativeLibsForTest helpers), so the
binary exits LeakSanitizer-clean for the first time. Teardown in the
DelayedUnmap tests happens after the unmapper thread joins.
@kaahos kaahos changed the title fix: read ELF symbol entries through aligned copies (PROF-16551) fix: read ELF symbol entries through aligned copies + run elfparser_ut in sanitizer CI (PROF-16551) Nov 9, 2026
@dd-octo-sts

dd-octo-sts Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Reliability & Chaos Results

✅ All reliability & chaos checks passed Pipeline: https://gitlab.ddbuild.io/DataDog/java-profiler/-/pipelines/152832102

@kaahos kaahos changed the title fix: read ELF symbol entries through aligned copies + run elfparser_ut in sanitizer CI (PROF-16551) fix: alignment-safe ELF symbol entry reads + elfparser_ut under sanitizer CI (PROF-16551) Nov 10, 2026
@kaahos
kaahos marked this pull request as ready for review November 10, 2026 15:27
@kaahos
kaahos requested a review from a team as a code owner November 10, 2026 15:27
@kaahos

kaahos commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

@codex

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T15:29:35.270787Z 07b533d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 07b533db91

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The alignment-safe copies preserve conditional field reads, including skipping zero-name symbols before accessing bytes beyond a readable mapping. Runtime sanitizer behavior remains unverified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 07b533d

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The PR fixes misaligned ELF-symbol reads and test cleanup; the latest update makes the test mapping guard non-copyable. One issue remains: ten generated core dumps add 440 MiB to checkouts and should be removed; runtime sanitizer behavior remains unverified.

P2 Remove the ten generated core dumps

The new commit adds ten generated qemu_lineNumberTableCopy_ut_20261110-*.core dumps, each 44 MiB, increasing every checkout and CI workspace by 440 MiB without providing test fixtures. Remove all ten crash artifacts from the PR.
Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 08846b8

@kaahos
kaahos force-pushed the PROF-16551-misaligned-elfsym branch from 08846b8 to e4e4826 Compare November 10, 2026 19:11

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

The new global-library cleanup breaks repeated full-suite runs: stale inode caches prevent libreladyn from repopulating the emptied library array on the next iteration.

Open Bits AI session

🤖 Bits Code Review · Commit e4e4826

@kaahos
kaahos force-pushed the PROF-16551-misaligned-elfsym branch from 8152ae5 to d6e5c79 Compare November 10, 2026 23:26

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The alignment-safe copies preserve conditional field reads, allowing zero-name dynamic symbol entries to be skipped before reading across an unreadable-memory boundary.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit d6e5c79

@kaahos kaahos changed the title fix: alignment-safe ELF symbol entry reads + elfparser_ut under sanitizer CI (PROF-16551) fix: alignment-safe ELF st_name reads without widening DT_SYMTAB entry reads (PROF-16551) Nov 12, 2026
@kaahos kaahos changed the title fix: alignment-safe ELF st_name reads without widening DT_SYMTAB entry reads (PROF-16551) fix: alignment-safe st_name reads in the remaining ELF symbol paths (PROF-16551) Nov 12, 2026
@kaahos kaahos changed the title fix: alignment-safe st_name reads in the remaining ELF symbol paths (PROF-16551) fix: alignment-safe symbol and relocation reads in the ELF parser (PROF-16551) Nov 12, 2026
@kaahos
kaahos force-pushed the PROF-16551-misaligned-elfsym branch from 57032ed to c3b5d80 Compare November 12, 2026 23:02

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Source review supports that the alignment-safe copies preserve ELF fields used by import parsing and fingerprinting, while the revised bounds consistently exclude incomplete relocation entries. Runtime sanitizer behavior was not independently verified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit c3b5d80

@kaahos

kaahos commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-13 00:20:52 UTC ℹ️ Start processing command /merge


2026-11-13 00:20:56 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in main is approximately 53m (p90).


2026-11-13 00:49:45 UTC ❌ MergeQueue: The checks failed on this merge request

Tests failed on this commit bb10fc2:

What to do next?

  • Investigate the failures and when ready, re-add your pull request to the queue!
  • If your PR checks are green, try to rebase/merge. It might be because the CI run is a bit old.
  • Any question, go check the FAQ.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mergequeue-status: rejected test:asan Run CI tests with AddressSanitizer configuration test:fuzz test:release Run CI tests with release configuration test:reliability test:tsan Run CI tests with ThreadSanitizer configuration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant