Commit Graph

43 Commits

Author SHA1 Message Date
Andrew Trick
19c8bd625f Add FIXMEs for calls to liveness that fail to check pointer escapes. 2025-03-02 23:51:34 -08:00
Michael Gottesman
f01d27514d [rbi] Convert an assert to an if check.
This also fixes a case where we would have inferred the wrong isolation (which
the assert caught). I think we missed this since it only comes up with final
classes.

rdar://142568522
2025-02-25 13:39:09 -08:00
Doug Gregor
37d71f362e Add StrictSendableMetatypes to require Sendable requirements on metatypes
Introduce a new experimental feature StrictSendableMetatypes that stops
treating all metatypes as `Sendable`. Instead, metatypes of generic
parameters and existentials are only considered Sendable if their
corresponding instance types are guaranteed to be Sendable.

Start with enforcing this property within region isolation. Track
metatype creation instructions and put them in the task's isolation
domain, so that transferring them into another isolation domain
produces a diagnostic. As an example:

    func f<T: P>(_: T.Type) {
      let x: P.Type = T.self
      Task.detached {
        x.someStaticMethod() // oops, T.Type is not Sendable
      }
    }
2025-02-12 20:21:57 -08:00
Michael Gottesman
7e350bb4ce Revert "[concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated."
This reverts commit 0cb64638d0.
2025-02-06 14:05:06 -08:00
Michael Gottesman
649952e3d9 Revert "[concurrency] Convert ActorIsolation::isConcurrentUnsafe -> isUnsafe."
This reverts commit 95623c691f.
2025-02-06 14:04:29 -08:00
Michael Gottesman
95623c691f [concurrency] Convert ActorIsolation::isConcurrentUnsafe -> isUnsafe.
This ensures it works for both nonisolated(unsafe) and concurrent(unsafe).
2025-02-03 10:56:08 -08:00
Michael Gottesman
0cb64638d0 [concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated.
This is just the first part of a larger transition.
2025-02-03 10:56:06 -08:00
Michael Gottesman
c061ee72ec [rbi] Ensure that we infer isolation correctly for direct sending results.
I did the correct thing for indirect parameters, but did not do the correct
thing for direct parameters. This is now fixed with some tests to boot.

rdar://141631655
2025-01-07 14:33:39 -08:00
Michael Gottesman
b74be1433a [concurrency] Fix a small bug in RBI that caused us to not recognize that an @MainActor SILIsolationInfo is from an @MainActor. 2025-01-02 13:18:55 -08:00
Michael Gottesman
067dbadfef [region-isolation] Add a print command that emits errors of the form "*-isolated code" or "code in the current task"
This makes it so that one does not need to deal with the differences in text in
between the task isolated case and the actor isolated case. This is done by
swallowing the entire part of this message in one method rather than having the
caller do the work.
2024-10-25 16:57:55 -07:00
Michael Gottesman
a0088327d4 [concurrency] Represent a SILFunction without isolation as std::optional<ActorIsolation> instead of ActorIsolation::Unspecified.
The reason why is that we want to distinguish inbetween SILFunction's that are
marked as unspecified by SILGen and those that are parsed from textual SIL that
do not have any specified isolation. This will make it easier to write nice
FileCheck tests against SILGen output on what is the inferred isolation for
various items.

NFCI.
2024-09-18 11:23:22 -07:00
Slava Pestov
f2fff10cc9 SIL: Synchronous local functions don't need to capture the isolation parameter
Also, move this rule from the computation of lowered captures in SIL,
to the computation of AST captures in Sema. This allows us to
correctly handle the case where an async function nests inside a
sync function. It also removes a special case that was added recently
to cope with a generic `self` type.

Fixes rdar://129366819.
2024-08-28 17:03:49 -04:00
Michael Gottesman
00519669af Merge pull request #76076 from gottesmm/rdar134623227
[region-isolation] Treat sending indirect_results as disconnected even if it is a return value of an actor isolated function.
2024-08-25 08:02:25 -04:00
Michael Gottesman
3e27bfc03b [region-isolation] Treat sending indirect_results as disconnected even if it is a return value of an actor isolated function.
rdar://134623227
2024-08-24 14:02:41 -04:00
Michael Gottesman
49eee05647 [region-isolation] Treat as Sendable values that are meant to be ignored since they are marked with preconcurrency
rdar://133531625
2024-08-24 13:14:39 -04:00
Michael Gottesman
a75501e985 [region-isolation] Eliminate the last non-SIL test case usage of SILIsolationInfo::getWithIsolationCrossing().
The reason that I am changing this code is that getWithIsolationCrossing is a
bad API that was being used to infer actor isolation straight from an ApplyExpr
without adding an actor instance. This can cause us to reject programs
unnecessarily if we in other parts of the code correctly infer the SILValue
actor instance for the isolation.

Rather than allow for that, I am removing this code and I improved the rest of
the pattern matching here to ensure that we handled that with the normal actor
instance inferring code. This will prevent this type of mismerge from happening
by mistake. I fixed up the changes in the test cases.

The only usage of this left is for ApplyIsolationCrossings parsed straight from
SIL that we use only when testing. This is safe since if a test writer is using
the parsed SIL in this manner, they can make sure that mismerges do not happen.
2024-07-31 13:10:01 -07:00
Nate Chandler
0cad62521e [Test] Underscored sil_isolation_info_inference.
Use underscores rather than hyphens so that text editors understand the
name as a single word.
2024-07-25 13:52:21 -07:00
Michael Gottesman
75152e7834 [region-isolation] Teach region isolation how to handle cases where due to compiler bugs, we have a function isolated to self without an isolated parameter.
Closures generally only inherit actor instance isolation if they directly
capture state from the actor instance. In this case, for some reason that is not
true, so we hit an assert that assumes that we will only see a global actor
isolated isolation.

Region Isolation should be able to handle code even if the closure isolation
invariant is violated by the frontend. So to do this, I am introducing a new
singleton actor instance to represent the isolation of a defer or closure
created in an actor instance isolated method. The reason why I am using a
singleton is that closures and defer are not methods so we do not actually know
which parameter is 'self' since it isn't in the abi. But we still need some
value to represent the captured values as belonging to. To square this circle, I
just did what we have done in a similar situation where we did not have a value:
(ActorAccessorInit). In that case, we just use a sentinel to represent the
instance (NOTE: This is represented just via a kind so ActorInstances that are
operator== equal will not &value equal since we are just using a kind).
2024-07-18 21:28:22 -07:00
Michael Gottesman
c986af7695 [region-isolation] Be more aggressive about not looking through Sendable values when getting underlying objects.
Otherwise, in cases like the following, we look through the load to x.boolean
and think that the closure is actually capturing x instead of y:

```swift
func testBooleanCapture(_ x: inout NonSendableKlass) {
  let y = x.boolean
  Task.detached { @MainActor [z = y] in
    print(z)
  }
}
```

rdar://131369987
2024-07-09 14:38:47 -07:00
Michael Gottesman
20acc503c2 Fix three typos in comments. NFC. 2024-07-08 11:01:44 -07:00
Michael Gottesman
9f8e5e8998 Merge pull request #75016 from gottesmm/pr-d4640c7e2919faea7bc7eb96ebd8af643108e2d8
[region-isolation] When determining isolation from a class_method, check for global actor isolation first.
2024-07-07 09:58:24 -07:00
Michael Gottesman
62c6de7713 [region-isolation] Reuse ActorInstance::lookThroughInsts when computing the actor instance for SILIsolationInfo purposes.
We are already using this routine in other parts of TransferNonSendable to
ensure that we look through common insts that SILGen inserts that do not change
the actual underlying actor instance that we are using. In this case, I added
support for casts, optional formation, optional extraction, existential ref
initialization.

As an example of where this came up is the following test case where we fail to
look through an init_existential_ref.

```swift
public actor MyActor {
  private var intDict: [Int: Int] = [:]

  public func test() async {
    await withTaskGroup(of: Void.self) { taskGroup in
      for (_, _) in intDict {}
      await taskGroup.waitForAll() // Isolation merge failure happens here
    }
  }
}
```

I also added the ability to at the SIL level actual test out this merge
condition using the analysis test runner. I used this to validate that this
functionality works as expected in a precise way.

rdar://130113744
2024-07-06 23:02:11 -07:00
Michael Gottesman
6129f4e833 [region-isolation] Print out SILIsolationInfo's options on all printing routines for SILIsolationInfo.
Before we wouldn't print them in all situations and even more so a few of the
printing routines did not have it at all. This just adds a centralized
SILIsolationInfo::dumpOptions() method and then goes through all of the printing
helpers and changes them to use them as appropriate.
2024-07-06 23:02:11 -07:00
Michael Gottesman
0c254807bf [region-isolation] Allow for unapplied isolated parameter ownership.
Given a function or a partial_apply with an isolated parameter, we do not know
immediately what the actual isolation is of the function or partial_apply since
we do not know which instance will be applied to the function or partial_apply.

In this commit, I introduce a new bit into SILIsolationInfo that tracks this
information upon construction and allows for it to merge with ownership that has
the appropriate type and a specific instance. Since the values that created the
two isolations, will be in the same region this should ensure that the value is
only ever in a flow sensitive manner in a region with only one actor instance
(since regions with isolations with differing actor instances are illegal).
2024-07-06 23:02:11 -07:00
Michael Gottesman
0a06c00f47 [region-isolation] When determining isolation from a class_method, check for global actor isolation first.
Before this change in the following code, we would say that message is isolated to the actor instead of the global actor isolation of the actor's method:

```swift
class Message { ... }

actor MessageHolder {
  @MainActor func hold(_ message: Message) { ... }
}

@MainActor
func sendMessage() async {
    let messageHolder = MessageHolder()
    let message = Message()
    // We identified messageHolder.hold as being MessageHolder isolated
    // instead of main actor isolated.
    messageHolder.hold(message)
    Task { @MainActor in print(message) }
}
```

I also used this as an opportunity to simplify the logic in this part of the
code. Specifically, I had made it so that multiple interesting cases were
handled in the same conditional statement in a manner that it made it hard to
know which cases were actually being handled and why it was correct. Now I split
that into two separate if statements with comments that make it clear what we
are actually trying to pattern match against.

rdar://130980933
2024-07-06 23:01:21 -07:00
Michael Gottesman
6fe749626f [region-isolation] Add 'inout sending' diagnostics.
Specifically:

1. We error now if one transfers an 'inout sending' parameter and does not
reinitialize it before the end of the function.

2. We error now if one merges an 'inout sending' parameter into an actor
isolated region and do not reinitialize it with a non-actor isolated value
before the end of the function.

rdar://126303739
2024-07-02 16:21:44 -07:00
Michael Gottesman
f43911b30f [region-isolation] Given a .none #isolation parameter, infer the isolation from the callee rather than the isolated parameter.
Otherwise, we will have differing isolation from other parameters since
the isolations will look different since one will have the .none value
as an instance and the other will not have one and instead will rely on
the AST isolation info. That is the correct behavior here since we do
not actually have an actor here.

I also removed some undefined behavior in the merging code. The way the
code should work is that we should check if the merge fails and in such
a case emit an unknown pattern error... instead of not checking
appropriately on the next iteration and hitting undefined behavior.

rdar://130396399
2024-06-25 14:12:15 -07:00
Michael Gottesman
c01f551ebe [transfer-non-sendable] Teach SILIsolationInfo how to handle "look through instructions" when finding actor instances.
From the perspective of the IR, we are changing SILIsolationInfo such that
inferring an actor instance means looking at equivalence classes of values where
we consider operands to look through instructions to be equivalent to their dest
value. The result is that cases where the IR maybe puts in a copy_value or the
like, we consider the copy_value to have the same isolation info as using the
actor directly. This prevents a class of crashes due to merge failings. Example:

```swift
actor MyActor {
  init() async {

  init(ns: NonSendableKlass) async {
    self.k = NonSendableKlass()
    self.helper(ns)
  }

  func helper(_ newK: NonSendableKlass) {}
}
```

Incidently, we already had a failing test case from this behavior rather than
the one that was the original genesis. Specifically:

1. If a function's SILIsolationInfo is the same as the isolation info of a
SILValue, we assume that no transfer actually occurs.

2. Since we were taking too static of a view of actor instances when comparing,
we would think that a SILIsolationInfo of a #isolation parameter to as an
argument would be different than the ambient's function isolation which is also
that same one. So we would emit a transfer non transferrable error if we pass in
any parameters of the ambient function into another isolated function. Example:

```swift
actor Test {

  @TaskLocal static var local: Int?

  func withTaskLocal(isolation: isolated (any Actor)? = #isolation,
                     _ body: (consuming NonSendableValue, isolated (any Actor)?) -> Void) async {
    Self.$local.withValue(12) {

      // We used to get these errors here since we thought that body's isolation
      // was different than the body's isolation.
      //
      //  warning: sending 'body' risks causing data races
      //  note: actor-isolated 'body' is captured by a actor-isolated closure...
      body(NonSendableValue(), isolation)
    }
  }
}
```

rdar://129400019
2024-06-24 18:16:11 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Michael Gottesman
d032bc033a [region-isolation] Check if we have a self param before attempting to get the self type.
Otherwise in asserts builds we get an assert and in no-asserts we get a crash.

rdar://129975097
2024-06-18 13:39:20 -07:00
Michael Gottesman
f035590784 [region-isolation] Dont crash when processing global actor isolated init accessors.
This just means that I stopped treating it like an actor instance isolated
thing. This was fun to track down since ActorIsolation has a union in it that
was being misinterpreted, leading to memory corruption... my favorite! = ).

rdar://129256560
2024-06-11 22:15:37 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Michael Gottesman
c7124e431a [sending] Fix recent alloc_stack as indirect result isolation inference to infer disconnected if the alloc stack is used as a sending indirect result.
I also fixed an issue that I found where we were not substituting SILResultInfo
flags which was causing us to drop when substituting sil_sending. I added a
SILVerifier check to make sure that we do not break this again.
2024-06-01 23:25:16 -07:00
Michael Gottesman
0cd0868f52 [region-isolation] Recognize sil_isolated parameters that are global actors as global actors rather than actor instance.
The specific example I ran into was in sendable_continuation.swift where we were
passing in the @MainActor instance as a sil_isolated parameter. We were thinking
it was an actor instance so we emitted the wrong message.
2024-06-01 23:25:16 -07:00
Michael Gottesman
1d8ea84fa3 [region-isolation] When determining isolation of a full apply site... use the isolated parameter, not self.
This was ok in the small since most of the time we were processing a self
parameter as isolated... but that isn't always true...
2024-06-01 23:25:16 -07:00
Michael Gottesman
06c32d74ff [region-isolation] Teach SIL isolation inference how to infer applies isolation from their callee's isolation.
This fixes a few issues I missed in the past bit of commits.

I need to fix one issue around async let, but I am going to fix it when I do a
sweep across async let.
2024-05-28 17:31:09 -07:00
Michael Gottesman
74ac12c9d2 [region-isolation] Make temporary alloc_stack that we form for returning values from a non-final class field take on the class method's isolation.
The reason why we are doing this is that otherwise, we have that the alloc_stack
formed for the result is disconnected and despite the fact that we merge it into
the actor region of the class method, we do not have that the alloc_stack
specifically is marked when we attempt to squelch Please.

This patch fixes that problem by detecting when an alloc_stack is being used as
a temporary for an out parameter and makes the alloc_stack initially isolated as
appropriate. It only does this in the specific cases where we can pattern match
it which in my limited testing has handled everything.
2024-05-28 17:31:08 -07:00
Michael Gottesman
2de13df909 [region-isolation] Use SILIsolationInfo::initializeTrackableValue instead of SILIsolationInfo::mergeIsolationRegionInfo to fix last issue
When merging SILIsolationInfo for regions, we want to drop
nonisolated(unsafe). This is important since nonisolated(unsafe) should only
apply to the specific "value" that it belongs to, not the entire region.

This creates a problem since in a few places in the code base we initialize a
value (producing a disconnected value) and then initialize it by merging in an
actor isolation. This no longer work since we will then always have
nonisolated(unsafe) stripped, so no values would ever be considered to be
nonisolated(unsafe). After analyzing the use case, I realized that these were
just initialization patterns and in this commit, I added a specific
initialization operation called SILIsolationInfo::initializeTrackableValue and
eliminated those calls to SILIsolationInfo::mergeIsolationRegionInfo.

Since SILIsolationInfo no longer has any merge operation on it, I then
eliminated that code in this commit. This completes the behavior split that I
put into the type system in the last commit. Specifically, I defined a
composition type called SILDynamicMergedIsolationInfo. It represents a
SILIsolationInfo that has been merged... that is why I called it the
DynamicMergedIsolationInfo. It could probably use a better name = (.

This fixes one of the last weird test case that I wrote where we were not letting through valid
nonisolated(unsafe) code.

At the same time, I discovered an additional issue (which can be seen in the
TODOs in this commit), where we are being too conservative around a non-Sendable
class var field. I am going to fix that in the next commit.

rdar://128299305
2024-05-27 21:42:15 -07:00
Michael Gottesman
741244e16b [region-isolation] Split in the type system SILIsolationInfo that has been merged and those that haven't.
Specifically, I introduced a new composition type called
SILDynamicMergedIsolationInfo that just contains a
SILIsolationInfo. Importantly, whenever one merges a SILIsolationInfo with
another SILIsolationInfo, one gets back a SILDynamicMergedIsolationInfo.

The reason why I am doing this is that we drop nonisolated(unsafe) when merging
so I want to ensure that parts of the code that use merging (where the dropping
occurs) and normal SILIsolationInfo where we do not want to merge is
distinguished.
2024-05-27 21:41:54 -07:00
Michael Gottesman
3a1f58a72a [region-isolation] Make sure that nonisolated(unsafe) works in all cases.
I made sure we match what we get without region isolation by turning off region
isolation in one of the test runs on the test for this.

There is one problem where for non-final classes with nonisolated(unsafe) var
fields, we currently do not properly squelch since I need to do more
infrastructure work. I am going to do that in the next commit.

rdar://128299305
2024-05-27 21:41:32 -07:00
Michael Gottesman
89a2cfce0b [region-isolation] Initialize TrackableValueState's regionInfo with a .none instead of a disconnected region.
The design change here is that instead of just initializing the regionInfo with
disconnected, we set it as .none and if we see .none, just return a newly
construct disconnected isolation region info when getIsolationRegionInfo() is
called.

This enables us to provide a setIsolationRegionInfo() helper for
RegionAnalysisValueMap::getTrackableValue that does not perform a merge. This is
important since for nonisolated(unsafe), we want to not have nonisolated(unsafe)
propagate through merging. So if we use merging to initialize the internal
regionInfo state of a SILIsolationInfo, we will never have a SILIsolationInfo
with that bit set since it will be lost in the merge. So we need some sort of
other assignment operator. Noting that we should only compute a value's
SILIsolationInfo once in RegionAnalysisValueMap before we cache it in the map,
it made sense to just represent it as an optional that way we can guarantee that
the regionInfo is only ever set exactly once by that routine.
2024-05-27 21:28:34 -07:00
Michael Gottesman
3f26d08ee4 [region-isolation] Add the ability in SILIsolationInfo to represent a disconnected value that is nonisolated(unsafe). 2024-05-27 21:25:44 -07:00
Michael Gottesman
3597006200 [region-isolation] Move SILIsolationInfo out of PartitionUtils into its own header/cpp file.
SILIsolationInfo is conceptually a layer that composes with PartitionUtils so it
makes sense for it to be in a different file.
2024-05-27 20:37:08 -07:00