Skip to content

[5.3] Fix a miscompile of global variables #34495

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
Oct 30, 2020

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Oct 29, 2020

This is a cherry pick of #32621

What we didn't know in the original PR is that it's actually fixing a bad miscompile in GlobalOpt, which was introduced in swift 5.1 by #22967.

  • Radar: rdar://problem/70399912, Jira: https://bugs.swift.org/browse/SR-13748

  • Explanation: In case a function is in ownership-SSA form, the global-variable-optimization skipped checking for accesses to global variables. This can lead to constant propagating a global variable (e.g. if it's an Int), even if there are multiple writes with different values to that variable. The weird thing is that we fixed the bug on the main branch since a while - without knowing that the change is actually a bug fix.

  • Scope: The bug was introduced in Swift 5.1. It can result in miscompiles if an internal or private global (or static) variable is set to different constant values at multiple locations in the source code.

  • Risk: Low. The bug was fixed on main since a while and the compiler tested since then. Also, the code change is trivial.

  • Reviewer: @meg-gupta

)

GlobalOpt works mostly on trivial values (there are special cases for ObjectInst and ValueToBridgeObjectInst).
optimizeGlobalAccess is explicitly turned off for non-trivial values. optimizeInitializer calls SILGlobalVariable::isValidStaticInitializerInst which limits it to mostly trivial values except for special cases for ObjectInst and ValueToBridgeObjectInst.

This changes adds GlobalOpt tests for ossa and enables GlobalOpt on ossa
@eeckstein eeckstein requested a review from a team as a code owner October 29, 2020 18:30
@eeckstein
Copy link
Contributor Author

@swift-ci test

@atrick
Copy link
Contributor

atrick commented Oct 30, 2020

@eeckstein this is fine for 5.3, but I would not call the bug "fixed" until SILGlobalOpt::collect() has a function-level comment explaining that it needs to process all functions regardless of whether they should be optimized and why it needs to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍒 release cherry pick Flag: Release branch cherry picks swift 5.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants