Skip to content

Bound binary operation values in ValueContentAnalysis - #57337

Open
Evangelink wants to merge 3 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/value-analysis-memory
Open

Evangelink wants to merge 3 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/value-analysis-memory

Conversation

@Evangelink

@Evangelink Evangelink commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: add5b50113bfa24ce10fc4b842baa6f3d15891b4

Dimension Assessment Rationale
Change confidence 8/10 ↑ 370 focused tests passed, including all three self-merge cases, with one skip.
↑ Both affected rules completed post-merge performance checks.
↓ Prior CI also reported an unresolved telemetry failure; new-head CI has not been inspected.
↓ Full security suite was not run; widening can reduce precision.
Diagnosis confidence 9.5/10 ↑ Binary operations did not apply the existing literal bound; the matching baseline build reproduced the resource growth.
↑ The bounded implementation completed the same workloads.
Evidence fit 9/10 ↑ The constant-seed xorshift reproduced excessive memory with the affected rules.
↓ The matched-source comparison adds a reachable sink because current source prunes the original sink-free example.

Constant-seed xorshift code can cause CA5390 and CA5403 to consume excessive memory as binary operations multiply the tracked literal values. Bound distinct binary results using the existing 10-literal limit, widening to unknown on the 11th result; duplicate results do not count against the limit. Dispose the pooled set on all paths. This can reduce analysis precision for operations with more than 10 distinct results.

Fixes #57335

Measurements

On Windows, the SDK 11.0.100 compiler (csc, no shared compilation) analyzed the constant-seed xorshift with a reachable key or certificate sink using Debug analyzer builds from the baseline and this change. Private memory was sampled every 100 ms; baseline runs were stopped at 120 seconds or 4096 MiB. The following comparison was measured before merging main:

Rule Baseline Fixed (5 runs)
CA5390 Stopped after 120.3 s at 2477 MiB peak private memory 4.3-8.1 s, 54-56 MiB, exit 0
CA5403 Stopped after 120.5 s at 2470 MiB peak private memory 3.2-5.2 s, 53-55 MiB, exit 0

Baseline times are censored, not completed-build times. Compiler /reportanalyzer output confirmed the targeted rules executed in the fixed runs. The current-source comparison includes a reachable sink because the current analyzer prunes the original sink-free example; a published analyzer separately reproduced the original issue without that sink.

After merging main, one fixed-analyzer run per rule completed with exit 0: CA5390 in 2.313 s at 54.13 MiB peak private memory and CA5403 in 2.404 s at 55.67 MiB. These are not a matched comparison with the earlier baseline.

Validation

  • Focused CA5390, CA5403 and binary-bound tests: 76 passed.
  • CA1508 AvoidDeadConditionalCode tests: 267 passed, 1 skipped.
  • After merging main: 354 focused CA5390, CA5403, binary-bound and CA1508 tests passed, 1 skipped.
  • After correcting the upstream self-merge test for early widening: 370 focused tests passed, including all three self-merge cases; 1 skipped.
  • Prior CI also reported a telemetry persistence test failure; the same test previously failed on main (#56957). The cause of the current telemetry failure remains unconfirmed.
  • Full security suite and release-branch/backport validation were not performed.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Evangelink
Evangelink requested a review from a team as a code owner November 24, 2026 10:30
Copilot AI balanced review requested due to automatic review settings November 24, 2026 10:30
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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.

🟢 Approval recommended

No active review issues are reported for the change that bounds binary-operation literal tracking to limit memory growth.

No open issues 🎉

What changed in this PR

This PR addresses excessive memory growth in CA5390 and CA5403 by applying ValueContentAnalysis’s existing 10-literal limit to binary-operation results. Operations producing more than 10 distinct values now widen to unknown, trading some analysis precision for bounded resource use.

Changes:

  • Bound distinct binary results during construction and dispose the pooled set on every exit path.
  • Add boundary, duplicate-result, and constant-seed xorshift regression tests.
  • Document the literal limit and its precision tradeoff.
File Description
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​ValueContentAbstractValueTests.cs Tests the result limit, widening, and duplicate handling.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​Security/​TaintedDataAnalyzerTestBase.cs Adds a shared xorshift reproduction with a configurable sink.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​Security/​DoNotHardCodeEncryptionKeyTests.cs Adds the CA5390 xorshift regression test.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​Security/​DoNotHardCodeCertificateTests.cs Adds the CA5403 xorshift regression test.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​src/​Utilities/​FlowAnalysis/​FlowAnalysis/​Analysis/​ValueContentAnalysis/​ValueContentAbstractValue.cs Bounds binary results and guarantees pooled-set disposal.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​docs/​writing-dataflow-analysis-based-analyzers.md Documents bounded literal tracking and widening behavior.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding

Copy link
Copy Markdown
Member

I think this conflicts with some of the cleanup and perf improvements landed in #57054

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Evangelink

Copy link
Copy Markdown
Member Author

Thanks for flagging this. I checked the ValueContentAnalysis changes from #57054: its Merge fast path still reuses identical values when they remain within the 10-literal bound, and widens oversized values to unknown. This change applies that same bound earlier, while MergeBinaryOperation constructs its result, so the self-merge optimization remains intact and oversized Cartesian products are avoided. I updated SelfMergePreservesBinaryProductLiteralBound to assert the earlier widening; all three cases pass in the focused run (370 passed, 1 skipped).

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA5390 / CA5403: ValueContentAnalysis uses unbounded memory on a xorshift generator with a constant seed

3 participants