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-8248] Optimization mode causes weird bug #50780

Open
swift-ci opened this issue Jul 13, 2018 · 15 comments
Open

[SR-8248] Optimization mode causes weird bug #50780

swift-ci opened this issue Jul 13, 2018 · 15 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-8248
Radar rdar://problem/42181330
Original Reporter lincolnlaw (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode10-beta3

Swift4.2

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

md5: 3de2b9861ae23ca337ae4cf30d30c63f

Issue Description:

As I create a new framework,
APlay: https://github.com/CodeEagle/APlay

I found out that. when I turn the optimization mode to -O or -OSize
It'll not work as none optimization level.

@belkadan
Copy link
Contributor

Sorry, what stops working in optimized mode? I still see the app being launched and the information about the test file printed.

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

Except none optimization mode, it'll play the music, you can hear.
-O / -OSize will cause cpu to 100% because decode function is stall or not make any sound.
You can switch optimization mode to test it

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

-O will output no sound.
-OSize will cause CPU 100%
none just work fine

picture for -OSize

@belkadan
Copy link
Contributor

Ah, I didn't realize the only difference was the sound, and I didn't realize you'd set the default mode to -Onone in both Debug and Release in the APlay framework target. I can reproduce the difference now.

@swift-ci create

@eeckstein
Copy link
Member

I investigated this a bit and found that there is no optimizer problem here. The only difference between a good and bad version is a minor difference in stack layout which should not have any impact.

I also did run the attached sample program with the address sanitizer. It reports a heap buffer overflow when dealing with C-strings (in StreamProvider.URLInfo.localFileHit).
I suggest to fix that first. And then if there is still a problem with -Onone vs -O, please let us know.

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

TestDemo-Fixed-localfileHint.zip

Hi Erik, I fixed (StreamProvider.URLInfo.localFileHit) with address sanitizer in -OSize, but still got error

@eeckstein
Copy link
Member

The new ASAN crash is still a bug in your code. You are taking an unsafe pointer from a swift UInt8 array without keeping the array alive.

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

Hi Erik, that is the point why I file the radar. No crash at -Onone but -OSize

@eeckstein
Copy link
Member

The problem here is that by not keeping the array alive, you are running into undefined behavior. That means it can work in one optimization level but not in another optimization level.
In this concrete example, the optimizer shrinks the lifetime of the object, which is a valid optimization.
If you are working with unsafe pointers you have to explicitly make sure that the referenced data is kept alive.

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

Ok, thanks Erik, I'll check my code later. Thanks for all.

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

Hi Erik, fixed array life time issue, but still got cpu 100% using in -OSize mode, not in -Onone mode

TestDemo-V3.zip

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

@eeckstein,@belkadan Any Help?

@eeckstein
Copy link
Member

There are other problems in the source code, which I found just by inspection. For example, there is no explicit lifetime handling for bufferConverter in decodeloopHandler(). It is passed as unsafe pointer to the callback (Why this is not reported by ASAN is a different question).

Also, even with -Onone, but with ASAN enabled I get the wrong behavior. I don't see a compiler problem here.

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 23, 2019

lincolnlaw (JIRA User), Did you figure out what the problem was? Should we mark this issue as resolved?

@swift-ci
Copy link
Collaborator Author

Comment by Lincoln Law (JIRA)

The function `AudioConverterFillComplexBuffer(converter, AudioBufferConverter.callback(), userinfo, &ioOutputDataPackets, &outputBufferList, nil)`
inside callback `AudioConverterComplexInputDataProc` will trigger twice or more in release mode but once in debug mode
what I mean trigger is when I return once, it'll not exit this callack, but still using this callback to retrieve more data.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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