Skip to content
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

[SR-8991] Compiling with -O and -Osize causes premature deallocation #51494

Closed
swift-ci opened this issue Oct 14, 2018 · 2 comments
Closed

[SR-8991] Compiling with -O and -Osize causes premature deallocation #51494

swift-ci opened this issue Oct 14, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8991
Radar rdar://problem/45576014
Original Reporter bryankeller (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)

Target: x86_64-apple-darwin18.0.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Miscompile, OptimizedOnly
Assignee None
Priority Medium

md5: 514fd7f53f9867f34524436cf8e96a33

Issue Description:

Compiling the attached main.swift file with -O or -Osize optimization produces a binary that, when run, has unexpected behavior. This unexpected behavior seems to happen due to premature memory deallocation.

Simply commenting out a print("") statement in the dead code path will cause the produced binary to exhibit expected behavior.

Adding back in the print("") statement in the dead code path will cause the produced binary to exhibit unexpected behavior.

Compiling without optimizations also produces a binary that exhibits expected behavior.


Expected behavior: Running the binary prints "This should always print 6... actually printing 6"

Actual behavior: Running the binary prints "This should always print 6... actually printing 0"


Also attached is a comparison of the disassembly pseudo-code produced by Hopper. The pseudo-code on the right, which is from the binary that includes the print("") statement in the dead code path, appears to immediately deallocate the memory that was just allocated on the previous line.

@belkadan
Copy link
Contributor

@swift-ci create

@eeckstein
Copy link
Member

You are getting an unsafe pointer to an array buffer and return that pointer while the array goes out of scope and is being destroyed.
This is undefined behavior. It's pure coincidence that the program works with -Onone.

The only safe way to get a pointer to the array buffer is with Array.withUnsafeBufferPointer

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

3 participants