-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix use-after-free on substituting function type involving conditional ~Escapable with Escapable type #81678
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
Open
meg-gupta
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
meg-gupta:fixsubst
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
@_addressableForDependencies | ||
public struct Something { | ||
public struct View<T: BitwiseCopyable> : ~Copyable, ~Escapable { | ||
var ptr: UnsafeBufferPointer<T> | ||
|
||
@lifetime(borrow ptr) | ||
public init(ptr: borrowing UnsafeBufferPointer<T>) { | ||
self.ptr = copy ptr | ||
} | ||
|
||
public var span: Span<T> { | ||
@lifetime(borrow self) | ||
borrowing get { | ||
Span(_unsafeElements: ptr) | ||
} | ||
} | ||
} | ||
|
||
public struct MutableView<T: BitwiseCopyable> : ~Copyable, ~Escapable { | ||
var ptr: UnsafeMutableBufferPointer<T> | ||
|
||
@lifetime(borrow ptr) | ||
public init(ptr: borrowing UnsafeMutableBufferPointer<T>) { | ||
self.ptr = copy ptr | ||
} | ||
|
||
public var mutableSpan: MutableSpan<T> { | ||
@lifetime(&self) | ||
mutating get { | ||
MutableSpan(_unsafeElements: ptr) | ||
} | ||
} | ||
} | ||
|
||
var ptr: UnsafeMutableRawBufferPointer | ||
public init(ptr: UnsafeMutableRawBufferPointer) { | ||
self.ptr = ptr | ||
} | ||
|
||
@lifetime(borrow self) | ||
public func view<T>(of type: T.Type = T.self) -> View<T> { | ||
let tp = ptr.assumingMemoryBound(to: T.self) | ||
return __overrideLifetime(View(ptr: .init(tp)), borrowing: self) | ||
} | ||
|
||
@lifetime(&self) | ||
public mutating func mutableView<T>(of type: T.Type = T.self) -> MutableView<T> { | ||
let tp = ptr.assumingMemoryBound(to: T.self) | ||
return __overrideLifetime(MutableView(ptr: tp), mutating: &self) | ||
} | ||
} | ||
|
||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(borrow source) | ||
public func __overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, borrowing source: borrowing U | ||
) -> T { | ||
dependent | ||
} | ||
|
||
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return | ||
/// a value identical to `dependent` that inherits all lifetime dependencies from | ||
/// the `source` argument. | ||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(copy source) | ||
public func __overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, copying source: borrowing U | ||
) -> T { | ||
dependent | ||
} | ||
|
||
/// Unsafely discard any lifetime dependency on the `dependent` argument. | ||
/// Return a value identical to `dependent` with a lifetime dependency | ||
/// on the caller's exclusive borrow scope of the `source` argument. | ||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(&source) | ||
public func __overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, | ||
mutating source: inout U | ||
) -> T { | ||
dependent | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/ne_types.swift \ | ||
// RUN: -enable-experimental-feature LifetimeDependence \ | ||
// RUN: -enable-experimental-feature AddressableTypes \ | ||
// RUN: -enable-library-evolution \ | ||
// RUN: -emit-module-path %t/ne_types.swiftmodule | ||
|
||
// RUN: %target-swift-frontend -emit-silgen -I %t %s \ | ||
// RUN: -enable-experimental-feature LifetimeDependence | ||
|
||
// REQUIRES: swift_feature_LifetimeDependence | ||
// REQUIRES: swift_feature_AddressableTypes | ||
|
||
import ne_types | ||
|
||
// Ensure no memory crashes during SILGen | ||
|
||
struct NonEscapableFrameworkThingTests { | ||
func example() async throws { | ||
let ptr = UnsafeMutableRawBufferPointer.allocate(byteCount: 40, alignment: 1) | ||
defer { ptr.deallocate() } | ||
var something = Something(ptr: ptr) | ||
var mutableView = something.mutableView(of: Float32.self) | ||
var mutableSpan = mutableView.mutableSpan | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's best to sink the AllocateCopy down into withLifetimeDependencies()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some rule for when
withLifetimeDependencies
needs its own allocation? Could that at least be commented in the API?We call this in several other places and I don't know which need an allocation and which don't. e.g.
SILFunctionType::getSubstLifetimeDependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs its own allocation if the LifetimeDependencies that you pass in are themselves not in the global AST context arena. It's safest to move the AllocateCopy into withLifetimeDependencies() itself, so that callers don't have to worry about creating an ExtInfo with a dangling pointer in it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not originally want to do this because
withLifetimeDependencies
is called from places with an already arena allocated pointer as the input arg, and proactively allocating within it would not allow sharing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps the LifetimeDependencies objects should always be allocated and uniqued by the ASTContext then, so you'll pass them around by pointer and the pointer will always have global extent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping to use something akin to
llvm::BumpPtrAllocatorImpl::identifyObject
to ensure the passed input arg is safe in assert builds. Not everything is plumbed through to try that out.I added a C++ delete overload for
withLifetimeDependencies
when input argument isSmallVectorImpl
for now.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slavapestov This sounds promising. But maybe a big refactor for release/6.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created rdar://151811669 (LifetimeDependencies objects should always be allocated and uniqued by the ASTContext)