Skip to content

pkg/dyninst: Fix context implementation capture#53746

Open
grantseltzer wants to merge 2 commits into
mainfrom
grantseltzer/fix-context-implementation-capture
Open

pkg/dyninst: Fix context implementation capture#53746
grantseltzer wants to merge 2 commits into
mainfrom
grantseltzer/fix-context-implementation-capture

Conversation

@grantseltzer

Copy link
Copy Markdown
Member

What does this PR do?

When the debugger captures a value that implements Go's context.Context but only forwards those methods elsewhere (a "delegating" wrapper, with no real parent context to follow), it now captures the value's own fields normally instead of mistaking it for a trace-carrying context and trying to walk a chain that isn't there.

It also fixes a deeper bug that made this behavior unreliable: after the debugger re-labeled these context types internally, it left some references pointing at stale copies of the same type. That mismatch caused the "how to capture this" instructions to occasionally go missing, so the value's fields would silently drop out and the snapshot would show garbage type names. Which runs hit it varied from run to run and between Go versions. The fix re-links all references to the single correct copy right after re-labeling, so the mismatch can't happen.

Motivation

Snapshots for these values were intermittently broken: fields showed up as UnknownType(...) out of bounds / missing type information, or the whole value was reported as not captured. Because it depended on internal ordering that shuffles each run, it looked random and was hard to trust. This makes the capture correct and stable, and gives the type a descriptive internal label instead of pretending it's a plain struct.

Describe how you validated your changes

  • Reproduced the failure on demand first and confirmed it was tied to run-to-run internal ordering, not to a specific input.
  • Ran the previously-flaky sample test 40 times on the Go version that used to fail about half the time: now 40/40 pass.
  • Ran the full four-toolchain sample suite 8 times and the complete TestDyninst (all services): all green, with the expected-output golden files unchanged.
  • Ran the irgen, compiler, decode, and symdb package tests, including the snapshot tests. Regenerated only the irgen snapshots, whose sole change is the new descriptive label.

Additional Notes

While fixing the reference re-linking, one downstream routine (the SymDB memory-layout code) had a hand-written list of type shapes it understood and hadn't been told about the newly-reaching wrapper type; it now treats that wrapper like the plain struct it wraps. I audited the other similar spots and confirmed the rest either run before the labeling step or already handle it.

@grantseltzer
grantseltzer force-pushed the grantseltzer/fix-context-implementation-capture branch from 82b651f to d1b0d10 Compare July 16, 2026 14:49
@grantseltzer grantseltzer changed the title Grantseltzer/fix context implementation capture pkg/dyninst: Fix context implementation capture Jul 16, 2026
@grantseltzer grantseltzer added changelog/no-changelog No changelog entry needed qa/done QA done before merge and regressions are covered by tests team/debugger labels Jul 16, 2026
@dd-octo-sts dd-octo-sts Bot added the internal Identify a non-fork PR label Jul 16, 2026
@grantseltzer
grantseltzer marked this pull request as ready for review July 16, 2026 14:50
@grantseltzer
grantseltzer requested a review from a team as a code owner July 16, 2026 14:50
@github-actions github-actions Bot added the long review PR is complex, plan time to review it label Jul 16, 2026
When the dynamic instrumentation debugger captured a Go value that implements the context.Context interface, it always applied its context handling: it walked the value's internal context chain to pull out the trace and span IDs and, in doing so, replaced the capture of the value's own fields. That is correct for real context plumbing, but many ordinary structs implement the interface only through methods that delegate elsewhere (for example rapid.Context, a web request wrapper) and are captured to inspect their fields. For those, the field capture was swapped for the chain walk, which produced bogus type information for the struct's interface fields (errors like "UnknownType(0x...) out of bounds") and, because the value delegates its context behavior, recovered no trace information anyway. This change applies the context handling only to values that are actually a link in a walkable chain, meaning they have a parent context to follow; a value that implements the interface but is really just a struct is now captured normally so its fields decode correctly, while trace and span IDs from genuine contexts are still extracted and promoted to the top of the snapshot as before. Validated with a new reproduction test that captures such a struct across Go 1.23 through 1.26 and by confirming the existing context trace-correlation output is unchanged.
@grantseltzer
grantseltzer force-pushed the grantseltzer/fix-context-implementation-capture branch from d1b0d10 to f351926 Compare July 16, 2026 14:51

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

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread pkg/dyninst/compiler/generate.go
…ImplementationType

Concrete context.Context implementations are wrapped in the type catalog as GoContextImplementationType after finalizeTypes has already bound every PointerType.Pointee and struct field to the pre-wrap StructureType, so the wrapper and the orphaned raw struct end up as two distinct objects sharing one type ID; because the compiler keys each type's ProcessType handler by object identity while deduping by ID, whichever object it happened to process first won the enqueue_pc registration, and the loader's lookup for the wrapper instance intermittently returned 0, which dropped the impl's field-descent program entirely and surfaced as flaky UnknownType-out-of-bounds or depth output depending on map iteration order. This wraps delegating impls (those that satisfy the interface only by forwarding, with no parent context or key/value payload) as GoContextImplementationType too so the IR label is descriptive, gates the chain-walk machinery in the compiler and loader behind a HasChainData predicate so delegating impls are captured as ordinary structs, and adds a rebindTypeReferences pass after annotateSpecialGoTypes so pointees, fields, and variables point at the wrapper instances, removing the object-identity race for good. Because that rebind now routes the wrapper types into the SymDB memory-layout path as well, typeMemoryLayout learns to treat the context-impl and dd-trace-span wrappers like the struct they wrap. Validated by regenerating the irgen snapshots and running the irgen, compiler, decode and symdb package tests plus TestDyninst for the sample service forty times on the previously flaky go1.25.0 build and the full four-toolchain suite eight times, all green, with the integration decoded golden unchanged.
@grantseltzer
grantseltzer force-pushed the grantseltzer/fix-context-implementation-capture branch from f351926 to ba9c1b6 Compare July 16, 2026 15:05
@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Bits has a CI fix ready

🟢 Investigated · 🟢 Fix prepared · ⚪ Validation skipped · 🟠 Ready

pkg/dyninst/testprogs/progs/sample/BUILD.bazel now includes context_impl_capture.go in sample_lib, so the function invoked by main.go is compiled on all platforms and Bazel-generated source lists are current.

Commit fix to this PR


View in Datadog | Reviewed commit ba9c1b6

@dd-octo-sts

dd-octo-sts Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Files inventory check summary

File checks results against ancestor c24fada1:

Results for datadog-agent_7.83.0~devel.git.103.ba9c1b6.pipeline.125039327-1_amd64.deb:

No change detected

@dd-octo-sts

dd-octo-sts Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Static quality checks

✅ Please find below the results from static quality gates
Comparison made with ancestor c24fada
📊 Static Quality Gates Dashboard
🔗 SQG Job

Successful checks

Info

Quality gate Change Size (prev → curr → max)
agent_deb_amd64 +4.0 KiB (0.00% increase, -0.05% of buffer) 751.007 → 751.011 → 758.200
agent_deb_amd64_fips +4.0 KiB (0.00% increase, -0.10% of buffer) 705.916 → 705.920 → 709.840
agent_rpm_amd64 +4.0 KiB (0.00% increase, -0.05% of buffer) 750.991 → 750.995 → 758.170
agent_rpm_amd64_fips +4.0 KiB (0.00% increase, -0.10% of buffer) 705.899 → 705.903 → 709.840
agent_rpm_arm64 +4.0 KiB (0.00% increase, -0.11% of buffer) 726.071 → 726.075 → 729.660
agent_suse_amd64 +4.0 KiB (0.00% increase, -0.05% of buffer) 750.991 → 750.995 → 758.170
agent_suse_amd64_fips +4.0 KiB (0.00% increase, -0.10% of buffer) 705.899 → 705.903 → 709.840
agent_suse_arm64 +4.0 KiB (0.00% increase, -0.11% of buffer) 726.071 → 726.075 → 729.660
docker_agent_amd64 +4.0 KiB (0.00% increase, -0.10% of buffer) 809.706 → 809.710 → 813.790
docker_agent_arm64 +4.0 KiB (0.00% increase, -0.07% of buffer) 809.799 → 809.803 → 815.030
docker_agent_jmx_amd64 +4.0 KiB (0.00% increase, -0.10% of buffer) 1000.603 → 1000.607 → 1004.550
docker_agent_jmx_arm64 +3.99 KiB (0.00% increase, -0.07% of buffer) 989.349 → 989.353 → 994.710
20 successful checks with minimal change (< 2 KiB)
Quality gate Current Size
agent_heroku_amd64 307.721 MiB
agent_rpm_arm64_fips 684.661 MiB
agent_suse_arm64_fips 684.661 MiB
docker_cluster_agent_amd64 209.897 MiB
docker_cluster_agent_arm64 222.952 MiB
docker_cws_instrumentation_amd64 7.447 MiB
docker_cws_instrumentation_arm64 6.877 MiB
docker_dogstatsd_amd64 39.224 MiB
docker_dogstatsd_arm64 37.368 MiB
docker_host_profiler_amd64 303.095 MiB
docker_host_profiler_arm64 314.592 MiB
dogstatsd_deb_amd64 29.959 MiB
dogstatsd_deb_arm64 28.002 MiB
dogstatsd_rpm_amd64 29.959 MiB
dogstatsd_suse_amd64 29.959 MiB
iot_agent_deb_amd64 46.210 MiB
iot_agent_deb_arm64 42.894 MiB
iot_agent_deb_armhf 43.658 MiB
iot_agent_rpm_amd64 46.211 MiB
iot_agent_suse_amd64 46.210 MiB

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Regression Detector

Regression Detector Results

Metrics dashboard
Target profiles
Run ID: e944243f-b88c-4ee5-961f-fd9e6087b747

Baseline: c24fada
Comparison: ba9c1b6
Diff

❌ Experiments with retried target crashes

This is a critical error. One or more replicates failed with a non-zero exit code. These replicates may have been retried. See Replicate Execution Details for more information.

  • quality_gate_idle_all_features

Optimization Goals: ✅ No significant changes detected

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI trials links
quality_gate_logs % cpu utilization +0.36 [-0.62, +1.34] 1 Logs bounds checks dashboard
quality_gate_security_mean_fs_load memory utilization +0.23 [+0.19, +0.27] 1 Logs bounds checks dashboard
quality_gate_idle memory utilization +0.22 [+0.17, +0.27] 1 Logs bounds checks dashboard
quality_gate_security_idle memory utilization +0.14 [+0.08, +0.20] 1 Logs bounds checks dashboard
quality_gate_idle_all_features memory utilization +0.09 [+0.02, +0.16] 1 Logs bounds checks dashboard
quality_gate_security_no_fs_load memory utilization +0.02 [-0.07, +0.12] 1 Logs bounds checks dashboard
quality_gate_metrics_logs memory utilization -0.70 [-0.95, -0.44] 1 Logs bounds checks dashboard

Bounds Checks: ✅ Passed

perf experiment bounds_check_name replicates_passed observed_value links
quality_gate_idle intake_connections 10/10 3 ≤ 4 bounds checks dashboard
quality_gate_idle memory_usage 10/10 148.42MiB ≤ 154MiB bounds checks dashboard
quality_gate_idle total_bytes_received 10/10 733.68KiB ≤ 819.20KiB bounds checks dashboard
quality_gate_idle_all_features intake_connections 10/10 3 ≤ 4 bounds checks dashboard
quality_gate_idle_all_features memory_usage 10/10 492.13MiB ≤ 495MiB bounds checks dashboard
quality_gate_idle_all_features total_bytes_received 10/10 1.12MiB ≤ 1.25MiB bounds checks dashboard
quality_gate_logs intake_connections 10/10 3 ≤ 6 bounds checks dashboard
quality_gate_logs memory_usage 10/10 183.36MiB ≤ 195MiB bounds checks dashboard
quality_gate_logs missed_bytes 10/10 0B = 0B bounds checks dashboard
quality_gate_logs total_bytes_received 10/10 264.15MiB ≤ 292MiB bounds checks dashboard
quality_gate_metrics_logs cpu_usage 10/10 355.29 ≤ 2000 bounds checks dashboard
quality_gate_metrics_logs intake_connections 10/10 3 ≤ 6 bounds checks dashboard
quality_gate_metrics_logs memory_usage 10/10 406.62MiB ≤ 430MiB bounds checks dashboard
quality_gate_metrics_logs missed_bytes 10/10 0B = 0B bounds checks dashboard
quality_gate_metrics_logs total_bytes_received 10/10 0.93GiB ≤ 1.04GiB bounds checks dashboard
quality_gate_security_idle cpu_usage 10/10 29.34 ≤ 40 bounds checks dashboard
quality_gate_security_idle memory_usage 10/10 300.47MiB ≤ 330MiB bounds checks dashboard
quality_gate_security_mean_fs_load cpu_usage 10/10 63.81 ≤ 80 bounds checks dashboard
quality_gate_security_mean_fs_load memory_usage 10/10 281.07MiB ≤ 310MiB bounds checks dashboard
quality_gate_security_no_fs_load cpu_usage 10/10 23.72 ≤ 40 bounds checks dashboard
quality_gate_security_no_fs_load memory_usage 10/10 280.72MiB ≤ 320MiB bounds checks dashboard

Explanation

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

Replicate Execution Details

We run multiple replicates for each experiment/variant. However, we allow replicates to be automatically retried if there are any failures, up to 8 times, at which point the replicate is marked dead and we are unable to run analysis for the entire experiment. We call each of these attempts at running replicates a replicate execution. This section lists all replicate executions that failed due to the target crashing or being oom killed.

Note: In the below tables we bucket failures by experiment, variant, and failure type. For each of these buckets we list out the replicate indexes that failed with an annotation signifying how many times said replicate failed with the given failure mode. In the below example the baseline variant of the experiment named experiment_with_failures had two replicates that failed by oom kills. Replicate 0, which failed 8 executions, and replicate 1 which failed 6 executions, all with the same failure mode.

Experiment Variant Replicates Failure Logs Debug Dashboard
experiment_with_failures baseline 0 (x8) 1 (x6) Oom killed Debug Dashboard

The debug dashboard links will take you to a debugging dashboard specifically designed to investigate replicate execution failures.

❌ Retried Normal Replicate Execution Failures (non-profiling)

Experiment Variant Replicates Failure Debug Dashboard
quality_gate_idle_all_features baseline 1 Oom killed Debug Dashboard

❌ Retried Profiling Replicate Execution Failures (ddprof)

Note: Profiling replicas may still be executing. See the debug dashboard for up to date status.

Experiment Variant Replicates Failure Debug Dashboard
quality_gate_idle baseline 10 Oom killed Debug Dashboard
quality_gate_idle comparison 10 Oom killed Debug Dashboard
quality_gate_idle_all_features baseline 10 Oom killed Debug Dashboard
quality_gate_idle_all_features comparison 10 Oom killed Debug Dashboard
quality_gate_logs baseline 10 Oom killed Debug Dashboard
quality_gate_metrics_logs baseline 10 Oom killed Debug Dashboard
quality_gate_metrics_logs comparison 10 Oom killed Debug Dashboard
quality_gate_security_idle baseline 10 Crashed (exit code: 134) Debug Dashboard
quality_gate_security_idle comparison 10 Oom killed Debug Dashboard
quality_gate_security_mean_fs_load comparison 10 Crashed (exit code: 134) Debug Dashboard
quality_gate_security_no_fs_load baseline 10 Crashed (exit code: 134) Debug Dashboard
quality_gate_security_no_fs_load comparison 10 Crashed (exit code: 134) Debug Dashboard

CI Pass/Fail Decision

Passed. All Quality Gates passed.

  • quality_gate_logs, bounds check missed_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_no_fs_load, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_no_fs_load, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_security_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_idle, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_mean_fs_load, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_mean_fs_load, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check missed_bytes: 10/10 replicas passed. Gate passed.

@andreimatei andreimatei 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.

I was very confused by the PR description and the .md note, until I I think I figured out the deal:

a "delegating" wrapper, with no real parent context to follow

but many ordinary structs implement the interface only through methods that delegate elsewhere (for example rapid.Context, a web request wrapper)

It's the other way around, isn't it? The fix is about implementations of context.Context who are... not wrappers. Like, I would consider a context.Context implementation that we recognize fine because it contains a context.Context field to be a wrapper.
If I understand correctly, the fix is for context.Context impls that we don't recognize properly, right? Like, types that implement context.Context but do not have a context.Context field and are also not one of the well known ones (e.g. cancelCtx, valueCtx, etc). Is that right? If so, let's stay away from "wrapper" and talk in terms of "supported implementations of context" and "unsupported ...". The fix would be about the unsupported ones.

// "UnknownType(...) out of bounds" when such a struct was wrongly handled as a
// context.
type contextImpl struct {
logger fmt.Stringer // time.Duration (foreign)

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.

what does "foreign" mean?

@grantseltzer

Copy link
Copy Markdown
Member Author

The distinction is whether the type is a link in a walkable context chain (has a parent Context or key/value, like cancelCtx/valueCtx) or not. A walkable context stores its parent context as a field that eBPF can read at a fixed offset (like cancelCtx), so it can hop parent to parent looking for a trace ID. rapid.Context (what spurred this investigation) has no such field, it reaches its parent by calling a method at runtime, which the walker can't do, so there's nothing to walk and we just capture its fields like a normal struct.

The fix is for the ones that aren't chain links, which we were mishandling. I'll drop "wrapper"/"delegating" and reword everything (PR description, the .md note, and the code comments) in terms of chain-linked vs not.

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

Labels

changelog/no-changelog No changelog entry needed internal Identify a non-fork PR long review PR is complex, plan time to review it qa/done QA done before merge and regressions are covered by tests team/debugger

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants