Skip to content

Fix #1804: Fix use-after-free in graph-related APIs#2083

Merged
kkraus14 merged 6 commits into
NVIDIA:mainfrom
mdboom:use-after-free
May 19, 2026
Merged

Fix #1804: Fix use-after-free in graph-related APIs#2083
kkraus14 merged 6 commits into
NVIDIA:mainfrom
mdboom:use-after-free

Conversation

@mdboom
Copy link
Copy Markdown
Contributor

@mdboom mdboom commented May 13, 2026

See #1804 for more information. Close #1804.

There are actually two different array semantics at play here, both of which were handled incorrectly.

  1. cuStreamGetCaptureInfo returns "driver owned" arrays that are not allocated by the caller, but are only valid until the next call.

  2. The remainder of the functions use a caller-provided buffer to write into.

These have both been solved by copying the array used to communicate with the API into the Python object itself.

  1. Will probably have to be handled this way forever.
  2. Can probably go away when we move to the new binding generator which handles allocating Python objects that own array data (not just single elements).

But we can leave that last thing as an optimization for the future. This fixes the immediate and serious bug.

Cc: @Andy-Jost for viz since this may have implications for cuda.core.graph. It passed the tests locally for me, but I did not do a thorough review of any implications it might have.

@mdboom mdboom requested review from Andy-Jost and leofang May 13, 2026 21:08
@github-actions github-actions Bot added the cuda.bindings Everything related to the cuda.bindings module label May 13, 2026
@mdboom mdboom added bug Something isn't working P0 High priority - Must do! labels May 13, 2026
@mdboom mdboom added this to the cuda.bindings 13.3.0 & 12.9.7 milestone May 13, 2026
@mdboom mdboom changed the title Fix #1806: Fix use-after-tree in graph-related APIs Fix #1806: Fix use-after-free in graph-related APIs May 13, 2026
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

@kkraus14 kkraus14 left a comment

Choose a reason for hiding this comment

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

Suggested using typed assignment for the deep copy while preserving the use-after-free fix.

pydependencies_out = [cudaGraphNode_t(init_value=<void_ptr>cydependencies_out[idx]) for idx in range(numDependencies_out)]
pydependencies_out = [cudaGraphNode_t() for _ in range(numDependencies_out)]
for idx in range(numDependencies_out):
string.memcpy((<cudaGraphNode_t>pydependencies_out[idx])._pvt_ptr, &cydependencies_out[idx], sizeof(cyruntime.cudaGraphNode_t))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we use typed assignment for these wrapper-owned copies instead of raw memcpy? It keeps the use-after-free fix, while letting the generated Cython/C types define the copy size. Same pattern applies to the other generated deep-copy sites in this PR.

Suggested change
string.memcpy((<cudaGraphNode_t>pydependencies_out[idx])._pvt_ptr, &cydependencies_out[idx], sizeof(cyruntime.cudaGraphNode_t))
(<cudaGraphNode_t>pydependencies_out[idx])._pvt_ptr[0] = cydependencies_out[idx]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That does seem to work. What is this magical thing called C++ 😆

@mdboom mdboom requested a review from kkraus14 May 16, 2026 23:14
pydependencies_out = [CUgraphNode(init_value=<void_ptr>cydependencies_out[idx]) for idx in range(numDependencies_out)]
pydependencies_out = [CUgraphNode() for _ in range(numDependencies_out)]
for idx in range(numDependencies_out):
string.memcpy((<CUgraphNode>pydependencies_out[idx])._pvt_ptr, &cydependencies_out[idx], sizeof(cydriver.CUgraphNode))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This and the remaining memcpys should also use typed copy assignment I think? Does this need to be rerun with the latest codegen changes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Was working from my laptop which has an older version of doxygen and I asked my agent to filter out those changes. Looks like it messed up.

@mdboom mdboom requested a review from kkraus14 May 19, 2026 11:53
@kkraus14 kkraus14 added the to-be-backported Trigger the bot to raise a backport PR upon merge label May 19, 2026
@kkraus14 kkraus14 enabled auto-merge (squash) May 19, 2026 17:25
@kkraus14 kkraus14 merged commit 87ee176 into NVIDIA:main May 19, 2026
168 of 169 checks passed
@github-actions
Copy link
Copy Markdown

Backport failed for 12.9.x, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin 12.9.x
git worktree add -d .worktree/backport-2083-to-12.9.x origin/12.9.x
cd .worktree/backport-2083-to-12.9.x
git switch --create backport-2083-to-12.9.x
git cherry-pick -x 87ee176ba58547c69cc72d97aee3f68b8f47b409

@github-actions
Copy link
Copy Markdown

Doc Preview CI
Preview removed because the pull request was closed or merged.

@leofang leofang changed the title Fix #1806: Fix use-after-free in graph-related APIs Fix #1804: Fix use-after-free in graph-related APIs May 19, 2026
@leofang
Copy link
Copy Markdown
Member

leofang commented May 19, 2026

For posterity: Mike backported the PR in #2110.

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

Labels

bug Something isn't working cuda.bindings Everything related to the cuda.bindings module P0 High priority - Must do! to-be-backported Trigger the bot to raise a backport PR upon merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: cudaGraphGetEdges() suffers use-after-free for cudaGraphEdgeData

4 participants