Skip to content

[SILGen] Make sure enum element is always cleaned up. #31779

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zoecarver
Copy link
Contributor

Makes the source enum value a "managed value" so that it is cleaned up after the data is extracted.

The original issue was exposed in #31077 and I attempted to fix this incorrectly in #31157.

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming the old behavior leads to a miscompile and a runtime crash. Can you add an execution test to test/Interpreter/switch.swift that uses LifetimeTracked, and verify that it crashes with your change reverted?

@zoecarver
Copy link
Contributor Author

@slavapestov not really. The old behavior doesn't destroy the enum for trivial types. It still deallocates it, though. LifetimeTracked isn't trivial so it is still destroyed.

The reason I'm fixing this is so that the verifier doesn't error when the dead unchecked_take_enum_data_addr is removed.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @zoecarver for following through on this! This is the right fix = ).

@gottesmm
Copy link
Contributor

@swift-ci test

@zoecarver
Copy link
Contributor Author

@gottesmm of course :)

I agree, this is a better solution.


Sorry, should have posted something here earlier. When running the interpreter tests I discovered that this sometimes destroys the enum twice: once when destroying the data and once when destroying the enum itself. I'm working on a fix right now.

@gottesmm
Copy link
Contributor

The problem is that there are code paths that are assuming the value is forwarded.

@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from bb456f4 to a14d76c Compare May 15, 2020 06:56
@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - bb456f463e9d5dcd9b5d3c6cf2ec8e7f938c85ed

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - bb456f463e9d5dcd9b5d3c6cf2ec8e7f938c85ed

@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from a14d76c to c65fe19 Compare May 15, 2020 17:06
@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@zoecarver
Copy link
Contributor Author

@slavapestov thanks. I found a case where it's a pretty obvious leak. Is there a way to run this program through a sanitizer or something so that we hit a crash/error? I would use something like LifetimeTracked but that won't work for enums because they can't have a custom destructor.

@zoecarver
Copy link
Contributor Author

zoecarver commented May 15, 2020

I found the sanitizer tests and am trying to get one of them to fail on master. The issue is the parent object always seems to be destroyed in the scope it's created in so not destroying one of its elements seems to be OK.

I think I can get this to fail, though. If nothing else with a sil test (but ideally a swift one).

The OS X bot failed because of an LLDB issue. Seems unrelated. I'll re-trigger it after I find a test that crashes on master.

@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from c65fe19 to a84b4fd Compare May 16, 2020 00:11
@zoecarver
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - bb456f463e9d5dcd9b5d3c6cf2ec8e7f938c85ed

@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from a84b4fd to 662b5da Compare May 16, 2020 00:13
@@ -0,0 +1,45 @@
// RUN: %target-swift-emit-silgen -module-name switch %s > %t.sil
// RUN: %target-sil-opt %t.sil -sil-combine -enable-sil-verify-all | %FileCheck %s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test crashes on master. It's a bit unconventional so, I want to get this re-approved before I merge anything.

@swift-ci

This comment has been minimized.

@zoecarver
Copy link
Contributor Author

@swift-ci please test linux platform

@zoecarver
Copy link
Contributor Author

@slavapestov and @gottesmm thanks for bearing with me on this. Sorry for all the back and forth. I ran the tests locally and I think this patch is now good (both in terms of logic and tests).

@swift-ci

This comment has been minimized.

@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from 662b5da to af1462a Compare May 16, 2020 18:49
@zoecarver
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@zoecarver
Copy link
Contributor Author

@swift-ci please test

@zoecarver
Copy link
Contributor Author

@swift-ci please test windows platform

@zoecarver
Copy link
Contributor Author

Do I need re-approval to commit this?

@zoecarver
Copy link
Contributor Author

@gottesmm and @slavapestov friendly ping. Can one of you verify this is still good to go?

Makes the source enum value a "managed value" so that it is cleaned up after the data is extracted.
@zoecarver zoecarver force-pushed the fix/enum-take-guaranteed branch from af1462a to 92b4185 Compare August 9, 2020 17:16
@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

@zoecarver zoecarver changed the base branch from master to main October 2, 2020 04:07
gottesmm added a commit to gottesmm/swift that referenced this pull request Oct 19, 2020
…formal access cleanups.

This has been a long standing issue that we have been hacking around in various
points in SILGen. Now CleanupCloner just does the right thing.

I was unable to cause any issues to pop up in tree (since I believe we hacked
around this and converged on correctness). But it does come up in combination
with new code in swiftlang#31779.

rdar://63514765
@zoecarver
Copy link
Contributor Author

@gottesmm can you take another look at this when you have a minute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants