Skip to content

CPU engines: check the sigaction probe and validate the assigned interval - #1014

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 14 commits into
mainfrom
cozy-dappled-crane
Nov 19, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 14 commits into
mainfrom
cozy-dappled-crane

Conversation

@jbachorik

@jbachorik jbachorik commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?:

Fixes three correctness issues in the CPU-time engines, found during review of PR #537 (PROF-14755):

  1. ITimerJvmti::check() restores an uninitialized sigaction as the process-wide SIGPROF disposition when the probing sigaction() call fails. It now returns an Error before arming the setitimer probe.
  2. ITimerJvmti::start(), ITimer::start() and CTimer::startWithTableSize() validated args._interval but assigned args.cpuSamplerInterval(). A caller setting only cpu with a non-positive _interval was rejected although the assigned value is valid. They now validate the value they actually assign; a non-CPU event reaching a CPU engine (cpuSamplerInterval() == 0) is rejected instead of silently arming a disarmed timer.
  3. ITimerJvmti::stop() does not clear _enabled — verified as correct: Profiler::stop() calls disableEngines() before any engine teardown, so no stale window exists. The lifecycle is now documented at the two points that rely on it; no behavior change.

Motivation:

Three review findings on PR #537 were filed as PROF-14755. Investigation confirmed bug 1 as a real defect (stack-garbage handler installation on a failed syscall), bug 2 as a latent validation/assignment mismatch (not reachable through argument parsing, which rejects interval <= 0, but reachable for programmatically built Arguments), and bug 3's premise as disproven by the current Profiler::stop() ordering — the ticket's suggested enableEvents(false) fix would have introduced a new inconsistency, since no engine writes _enabled from stop(). The SIG_IGN asymmetry between ITimerJvmti::stop() and ITimer::stop() is documented and pinned by tests rather than changed; both behaviors are safe.

Additional Notes:

  • Intended behavior change: with event=wall cpu=N (a CPU engine is selected because args._cpu >= 0 precedes the EVENT_WALL branch in selectCpuEngine), the CPU engine previously started silently with a zero interval — a disarmed timer producing no samples. It now fails to start with a logged, recoverable error. cpuSamplerInterval() substitutes DEFAULT_CPU_INTERVAL for CPU events, so no legitimate CPU configuration is newly rejected. Counterpart flip, accepted: event=cpu interval=-1 (rejected before) now resolves to DEFAULT_CPU_INTERVAL — consistent with ITimer's pre-existing behavior and with Arguments::parse, which rejects interval <= 0.
  • The check preflight now mirrors the start()-time interval validation for the configurations that run a real CPU engine (cpu set, or a CPU event) and rejects with the same error, so check no longer reports OK for options start() rejects. Engine check() remains pure capability probing: engine selection, fallback order, and the event=wall/alloc/noop-without-cpu surface are unchanged.
  • Agentic review: three fresh-context review rounds over the range and its fixes, plus a five-lane specialized review fanout (signal/syscall semantics, concurrency/lifecycle, caller regression, test quality, conventions/security) with two fix rounds. The final round found no Critical/High/Substantive issues; remaining trivia is listed below. Review marker recorded with base origin/main.
  • Accepted review trivia (not fixed): the CTimer positive-branch test is skipped under ASan/TSan (SKIP_IF_CALLOC_OVERFLOW_ABORTS; the reject branch covers CTimer's validation there), and StartValidatesCpuSamplerInterval leaves CTimer::_interval/_signal set after the forced allocation failure, matching the neighboring pre-existing test.
  • Human review: recommended — the event=wall cpu=N behavior change touches engine selection paths. (human_review:recommended)
  • This PR doesn't touch any of the security-sensitive areas.

How to test the change?:

New gtests pin all changed behavior; each discriminating test fails against the pre-fix code (verified by running with the production fix reverted):

  • ITimerTest.JvmtiCheckFailsWhenSigactionProbeFails — a forwarding sigaction interposer (bound statically in the test binary) fails the probe; asserts the Error return and an unchanged SIGPROF disposition.
  • ITimerTest.JvmtiStartAcceptsCpuOnlyArguments / JvmtiStartUsesIntervalFallback — the value actually assigned is accepted in both cpuSamplerInterval() branches; rejected by pre-fix code.
  • ITimerTest.StartRejectsNonCpuEvent / JvmtiStartRejectsNonCpuEvent (_event=wall, _cpu>0) — rejected now; pre-fix code silently armed a disarmed engine. The static _interval is seeded before the call, so "untouched" is asserted deterministically.
  • CTimerNativeMemTest.StartValidatesCpuSamplerInterval / StartRejectsNonCpuEvent — the CTimer twin.
  • ITimerTest.JvmtiCheckRestoresPreviousDisposition — the probe/restore roundtrip leaves the disposition untouched (masking libc-internal SA_RESTORER bookkeeping that musl reports on read-back).

Verification (candidate: ae607c4):

Check Result
:ddprof-lib:gtestDebug (macOS arm64, debug) passed, 0 failed
:ddprof-lib:gtestDebug_itimer_ut (macOS arm64) passed, 8/8
glibc aarch64 container: :ddprof-lib:gtestDebug_ctimer_ut passed, 40/40
glibc aarch64 container: full C++ gtest suite under ASan / TSan passed (pre-fanout candidate; test-only changes since re-verified per-file)
glibc aarch64 container: gtestAsan_itimer_ut, gtestAsan_ctimer_ut, gtestTsan_itimer_ut, gtestTsan_ctimer_ut (final candidate) passed
musl aarch64 container: :ddprof-lib:gtestDebug_itimer_ut (final candidate) passed, 8/8
:ddprof-lib:qualityChecks passed
:ddprof-test:testDebug -Ptests=SmokeCpuTest (macOS, JDK 25) passed
Pre-fix discrimination runs (itimer_ut, ctimer_ut with the production fix reverted) failed as expected

Known unrelated failure: :ddprof-test:testAsan in the glibc aarch64 container fails in CollapsingSleepTest.testSleep (timing assertion). It fails identically on bare origin/main without this diff; it passed on the pre-rebase candidate. Recorded as pre-existing, not caused by this change.

Note: gtest runs in CI on Linux only; the macOS and musl results above cover the platform where ITimerJvmti is actually selected and the alternate libc.

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 (run the dd:platform-security-review
    skill, or file a request via the PSEC review form).
    bewaire also runs automatically on every PR.
  • This PR doesn't touch any of that.
  • JIRA: PROF-14755

@jbachorik jbachorik added the human_review:recommended Human review recommended — see PR description label Nov 18, 2026
@jbachorik

Copy link
Copy Markdown
Collaborator Author

@DataDog review

@dd-octo-sts

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

Copy link
Copy Markdown
Contributor

Scan-Build Report

User:runner@runnervmccnms
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:Wed Nov 18 12:51:30 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

@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 signal-restoration assertion fails on glibc x86_64 because sigaction adds SA_RESTORER to the installed flags. Comparing against the queried installed disposition avoids falsely failing the gtest suite.

Open Bits AI session

🤖 Bits Code Review · Commit 1bb7828

Comment thread ddprof-lib/src/test/cpp/itimer_ut.cpp Resolved
@datadog-datadog-prod-us1-2

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

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: eb789ac | Docs | Give us feedback!

@dd-octo-sts

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

Copy link
Copy Markdown
Contributor

CI Test Results

Run: #44499064029 | Commit: 004eae2 | Duration: 1h 1m 2s (longest job)

❌ 1 of 32 test jobs failed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
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: 32 | Passed: 31 | Failed: 1


Updated: 2026-11-19 10:48:50 UTC

@jbachorik
jbachorik marked this pull request as ready for review November 18, 2026 11:30
@jbachorik
jbachorik requested a review from a team as a code owner November 18, 2026 11:30
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T11:32:01.527634Z ae607c4 Draft marked ready
ℹ️ 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: ae607c407a

ℹ️ 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".

Comment thread ddprof-lib/src/test/cpp/itimer_ut.cpp Resolved

@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

On glibc x86-64, sigaction() injects the internal SA_RESTORER flag when installing a custom SIGPROF handler, so any assertion that compares the originally-requested struct sigaction against the read-back disposition will fail even when the handler is correctly restored. The fix is to snapshot the installed disposition (post-sigaction) before the probe and compare two read-backs, as the neighboring tests already do.

Open Bits AI session

🤖 Bits Code Review · Commit ae607c4

Comment thread ddprof-lib/src/test/cpp/itimer_ut.cpp Resolved

@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 PR guards failed SIGPROF probes, validates resolved CPU intervals, and documents engine lifecycle behavior. The follow-up fixes the glibc x86_64 restoration-test failure by masking SA_RESTORER; no findings remain open, with full gtests unrun and production telemetry excluded.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 90225b2

@dd-octo-sts

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

Copy link
Copy Markdown
Contributor

✅ All 40 integration tests passed

📊 Dashboard · 👷 Pipeline · 📦 eb789ac2

…rval

ITimerJvmti::check() restored an uninitialized sigaction as the SIGPROF
disposition when the probe failed; return an Error instead. ITimerJvmti,
ITimer and CTimer validated args._interval but assigned
cpuSamplerInterval(); validate the assigned value so a caller setting
only cpu is accepted and a non-CPU event reaching a CPU engine fails
instead of arming a disarmed timer.
itimer_ut covers the ITimer/ITimerJvmti cases with a forwarding sigaction
interposer for the probe-failure path; ctimer_ut covers the CTimer twin.
The non-CPU-event rejection tests now use the default _interval=0, which
the pre-fix validation accepted, so they fail on unfixed code. Replace
stale itimer.cpp line references in ProfilingEngines.md with symbol
references.
Restore SIG_IGN before the saved SIGPROF disposition in itimer_ut
teardown, drop the remaining itimer.cpp line citation from
ProfilingEngines.md, and phrase the test comments in current-behavior
terms.
sameDisposition ignores libc-internal SA_RESTORER in the read-back flags
(musl keeps it set; glibc strips it), which unblocks the musl container.
Cover cpuSamplerInterval()'s _interval fallback branch, pin the stop()
SIG_IGN asymmetry and the untouched _interval on rejected starts, drop
the vacuous enginesReady asserts, and cite both engines for the
setitimer mechanism in ProfilingEngines.md.
Seed the engines' static _interval through test accessors instead of
relying on state leaked by earlier tests, and pin ITimer::stop() to the
exact engine handler. Extend the SA_RESTORER comment to cover glibc.
x86-64 glibc leaves SA_RESTORER (0x04000000) in the read-back sa_flags
without exporting the macro, so the header-guarded mask never engaged
there and the disposition roundtrip test failed on the amd64 CI.

@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

On Linux with CTimer available, check,event=wall,cpu=1ms reports OK even though the same options now fail to start a recording. The PR describes this preflight mismatch as a follow-up.

Open Bits AI session

🤖 Bits Code Review · Commit cb6ce9a

@jbachorik jbachorik added the dog-sitter Actively watched by babysit-pr: fixes pushed through the repo gates label Nov 18, 2026
@jbachorik

jbachorik commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

🐶 babysit-pr is watching this PR — CI breakages and mechanical findings are fixed and pushed automatically; design decisions are always brought to you.

This run
  • 14:27 · started · CI on head cb6ce9a12 — GH test matrix green; dd-gitlab/default-pipeline red via post-pr-comment (1/40 integration configs: musl-arm64-hotspot-jdk25); bits-review/head FAIL on cb6ce9a (preflight check vs start validation — P2, matches the documented out-of-scope follow-up)

  • 14:35 · answered · Bits threads on itimer_ut.cpp:178/171 (SA_RESTORER masking ×3) — fixed by cb6ce9a, replies posted

  • 14:35 · CI red · integration-test-arm64-musl [hotspot, 25] — "No ExecutionSample events" (allow-failure); retry refused (403, not in authorized retry group), ddci request retry also refused

  • 14:55 · asked · operator — Bits P2 fix scope: engine check() doubles as selectCpuEngine capability probing, so in-engine validation would reroute the wall+cpu fallback; refined placement proposed

👀 watching · since 14:27 UTC · gitlab red (retry access denied) · 3 findings answered · Bits FAIL on cb6ce9a — awaiting operator on fix placement

Profiler::check() now rejects cpu/event combinations that start() would
reject with the same error, before JVM capability probing; engine
check() stays pure capability probing, so engine selection and the
event=wall/alloc/noop without cpu surface are unchanged. isCpuEvent is
public for the preflight gate.
@jbachorik jbachorik removed the dog-sitter Actively watched by babysit-pr: fixes pushed through the repo gates label Nov 18, 2026

@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 preflight guard in profiler.cpp rejects configurations with a zero/missing CPU interval before the CPU engine is selected, causing false negatives for engines (OpenJ9 JVMTI wall-clock, PerfEvents cycles) that supply their own default interval at startup. The fix requires moving the interval validation to after engine selection and scoping it only to timer engines that enforce it.

Open Bits AI session

🤖 Bits Code Review · Commit 80e717b

Comment thread ddprof-lib/src/main/cpp/profiler.cpp Resolved
perf_events and the J9 JVMTI sampler accept a zero resolved interval at
start (hardware default period / wall interval), so the uniform preflight
rule over-rejected on OpenJ9 and perf-only hosts. Gate the rule on the
selected engine via Engine::enforcesCpuSamplerInterval(), true only for
the timer family.

@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: WARN

The PR handles failed SIGPROF probes safely and validates the effective CPU timer interval. Engine-specific preflight now preserves CTimer’s rejection without blocking OpenJ9 or PerfEvents, but selecting the engine before the JVM capability check can expose a previously skipped process-wide timer probe on a JVM that preflight would reject.

Open Bits AI session

🤖 Bits Code Review · Commit 1fb2b34

Comment thread ddprof-lib/src/main/cpp/profiler.cpp Resolved
@jbachorik

Copy link
Copy Markdown
Collaborator Author

@DataDog review

@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 preflight rejects mixed CPU and wall-clock configurations that can still start a wall-clock recording. It also probes CPU engines before validating the JVM, so a rejected check can disarm an existing process-wide profiling timer.

Open Bits AI session

🤖 Bits Code Review · Commit 1fb2b34

Comment thread ddprof-lib/src/main/cpp/profiler.cpp Resolved
Comment thread ddprof-lib/src/main/cpp/profiler.cpp Resolved
…overable CPU failure

Engine selection moves back behind checkJvmCapabilities(): the probes
(ITimerJvmti's setitimer clobber, CTimer's timer_create) must not run on
JVMs the gate would reject. A CPU engine rejected for its interval is
skipped like startCpuEngine() skips it on failure, and the preflight
fails only when no other requested mode validates; preflight tests move
to CheckCommandTest on a live JVM.

@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 most critical production-correctness issue is in profiler.cpp: the no-op allocation engine's check passes and causes check to report OK for configurations where start would not actually activate allocation sampling. Separately, the new timer-rejection assertion in CheckCommandTest.java is not guarded by engine type, so it produces false failures on OpenJ9/JVMTI-backed configurations where the command correctly returns OK.

Open Bits AI session

🤖 Bits Code Review · Commit 504b9c7

Comment thread ddprof-lib/src/main/cpp/profiler.cpp Resolved
…penJ9 rejection test

The no-op allocation and wall engines validate but start() skips them, so
they cannot carry a recording; the preflight must not treat them as a
viable alternative. checkPreflightRejectsNonCpuEventWithCpu skips OpenJ9,
whose JVMTI sampler accepts a zero resolved CPU interval.

@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 PR fixes unsafe SIGPROF restoration and validates resolved CPU intervals while preserving backend defaults and viable mixed-mode recordings. The OpenJ9 preflight and test issues are resolved, and no-op engines no longer count as viable fallbacks; no review findings remain open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit eb789ac

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

Labels

human_review:recommended Human review recommended — see PR description mergequeue-status: done

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant