Skip to content

[AutoDiff] Fix use after free when pullback is used multiple times #64647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023

Conversation

asl
Copy link
Contributor

@asl asl commented Mar 27, 2023

Linear maps are captured in vjp routine via callee-guaranteed partial apply and are passed as @owned references to the enclosing pullback that finally consumes them. Necessary retains are inserted by a partial apply forwarder.

However, this is not the case when the function being differentiated contains loops as heap-allocated context is used and bare pointer is captured by the pullback partial apply. As a result, partial apply forwarder does not retain the linear maps that are owned by a heap-allocated context, however, they are still treated as @owned references and therefore are released in the pullback after the first call. As a result, subsequent pullback calls release linear maps and we'd end with possible use-after-free.

Ensure we retain values when we load values from the context.

Reproducible only when:

  • Loops (so, heap-allocated context)
  • Pullbacks of thick functions (so context is non-zero)
  • Multiple pullback calls

Some cleanup while there

Fixes #64257

@asl asl requested review from rxwei and dan-zheng March 27, 2023 20:25
@asl
Copy link
Contributor Author

asl commented Mar 27, 2023

@swift-ci please test

@asl
Copy link
Contributor Author

asl commented Mar 27, 2023

Tagging @BradLarson @fibrechannelscsi

Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Aside from this change, I think that we should also try to migrate from the underlying bump pointer allocator to StackAllocator that's currently used for Concurrency. StackAllocator allows for the pullback to deallocate things along the way.

@asl
Copy link
Contributor Author

asl commented Mar 27, 2023

Looks good to me.

Aside from this change, I think that we should also try to migrate from the underlying bump pointer allocator to StackAllocator that's currently used for Concurrency. StackAllocator allows for the pullback to deallocate things along the way.

Oh, great, I was not aware of it. Definitely will explore.

@asl asl merged commit 8990a12 into swiftlang:main Mar 28, 2023
@asl asl deleted the 64257-use-after-free branch March 28, 2023 08:36
etcwilde pushed a commit to etcwilde/swift that referenced this pull request Apr 19, 2023
…4647)

Linear maps are captured in vjp routine via callee-guaranteed partial apply and are passed as @owned references to the enclosing pullback that finally consumes them. Necessary retains are inserted by a partial apply forwarder.

However, this is not the case when the function being differentiated contains loops as heap-allocated context is used and bare pointer is captured by the pullback partial apply. As a result, partial apply forwarder does not retain the linear maps that are owned by a heap-allocated context, however, they are still treated as @owned references and therefore are released in the pullback after the first call. As a result, subsequent pullback calls release linear maps and we'd end with possible use-after-free.

Ensure we retain values when we load values from the context.

Reproducible only when:

 * Loops (so, heap-allocated context)
 * Pullbacks of thick functions (so context is non-zero)
 * Multiple pullback calls
 * Some cleanup while there

Fixes swiftlang#64257
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.

[AutoDiff] Runtime crash when evaluating a pullback more than once.
2 participants