[SR-8991] Compiling with -O and -Osize causes premature deallocation #51494
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
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
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.
The text was updated successfully, but these errors were encountered: