Skip to content

Commit 8cd6206

Browse files
authored
Merge pull request #38203 from mikeash/fix-iterate-async-task-allocations
[Reflection] Fix iterateAsyncTaskAllocations.
2 parents 6a1e58a + c0e6e1d commit 8cd6206

File tree

5 files changed

+77
-16
lines changed

5 files changed

+77
-16
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,7 @@ class ReflectionContext
13611361
if (!AsyncTaskObj)
13621362
return std::string("failure reading async task");
13631363

1364-
auto *Allocator = reinterpret_cast<const StackAllocator *>(
1365-
&AsyncTaskObj->AllocatorPrivate);
1366-
StoredPointer SlabPtr = Allocator->FirstSlab;
1364+
StoredPointer SlabPtr = AsyncTaskObj->PrivateStorage.Allocator.FirstSlab;
13671365
while (SlabPtr) {
13681366
auto SlabBytes = getReader().readBytes(
13691367
RemoteAddress(SlabPtr), sizeof(typename StackAllocator::Slab));

include/swift/Reflection/RuntimeInternals.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ struct HeapObject {
6868

6969
template <typename Runtime>
7070
struct Job {
71-
typename Runtime::StoredPointer Opaque[4];
71+
HeapObject<Runtime> HeapObject;
72+
typename Runtime::StoredPointer SchedulerPrivate[2];
73+
uint32_t Flags;
74+
uint32_t Id;
75+
typename Runtime::StoredPointer Reserved[2];
76+
typename Runtime::StoredPointer RunJob;
7277
};
7378

7479
template <typename Runtime>
@@ -86,12 +91,19 @@ struct StackAllocator {
8691
};
8792

8893
template <typename Runtime>
89-
struct AsyncTask {
90-
HeapObject<Runtime> HeapObject;
91-
Job<Runtime> Job;
92-
typename Runtime::StoredPointer ResumeContext;
94+
struct AsyncTaskPrivateStorage {
9395
typename Runtime::StoredSize Status;
94-
typename Runtime::StoredPointer AllocatorPrivate[4];
96+
StackAllocator<Runtime> Allocator;
97+
typename Runtime::StoredPointer Local;
98+
};
99+
100+
template <typename Runtime>
101+
struct AsyncTask: Job<Runtime> {
102+
// On 64-bit, there's a Reserved64 after ResumeContext.
103+
typename Runtime::StoredPointer ResumeContextAndReserved[
104+
sizeof(typename Runtime::StoredPointer) == 8 ? 2 : 1];
105+
106+
AsyncTaskPrivateStorage<Runtime> PrivateStorage;
95107
};
96108

97109
} // end namespace reflection

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@
2626

2727
#include <stdlib.h>
2828

29-
/// Major version changes when there are ABI or source incompatible changes.
30-
#define SWIFT_REFLECTION_VERSION_MAJOR 3
31-
32-
/// Minor version changes when new APIs are added in ABI- and source-compatible
33-
/// way.
34-
#define SWIFT_REFLECTION_VERSION_MINOR 0
35-
3629
#ifdef __cplusplus
3730
extern "C" {
3831
#endif
@@ -43,6 +36,14 @@ __attribute__((__weak_import__))
4336
#endif
4437
extern unsigned long long swift_reflection_classIsSwiftMask;
4538

39+
/// An arbitrary version number for this library. Incremented to indicate the
40+
/// presence of a bug fix or feature that can't be detected from the outside
41+
/// otherwise. The currently used version numbers are:
42+
///
43+
/// 0 - Indicates that swift_reflection_iterateAsyncTaskAllocations has been
44+
/// fixed to use the right AsyncTask layout.
45+
SWIFT_REMOTE_MIRROR_LINKAGE extern uint32_t swift_reflection_libraryVersion;
46+
4647
/// Get the metadata version supported by the Remote Mirror library.
4748
SWIFT_REMOTE_MIRROR_LINKAGE
4849
uint16_t swift_reflection_getSupportedMetadataVersion(void);

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
extern "C" {
1818
SWIFT_REMOTE_MIRROR_LINKAGE
1919
unsigned long long swift_reflection_classIsSwiftMask = 2;
20+
21+
SWIFT_REMOTE_MIRROR_LINKAGE uint32_t swift_reflection_libraryVersion = 0;
2022
}
2123

2224
#include "swift/Demangling/Demangler.h"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -Xfrontend -enable-experimental-concurrency -parse-stdlib -parse-as-library -lswiftSwiftReflectionTest %s -o %t/reflect_task
3+
// RUN: %target-codesign %t/reflect_task
4+
5+
// RUN: %target-run %target-swift-reflection-test %t/reflect_task | %FileCheck %s --dump-input=fail
6+
7+
// REQUIRES: reflection_test_support
8+
// REQUIRES: executable_test
9+
// REQUIRES: concurrency
10+
// UNSUPPORTED: use_os_stdlib
11+
12+
import Swift
13+
import _Concurrency
14+
15+
import SwiftReflectionTest
16+
17+
@_silgen_name("swift_task_getCurrent")
18+
func _getCurrentAsyncTask() -> UInt
19+
20+
func add(_ a: UInt, _ b: UInt) async -> UInt {
21+
if b == 0 {
22+
reflect(asyncTask: _getCurrentAsyncTask())
23+
// CHECK: Reflecting an async task.
24+
// CHECK: Async task {{0x[0-9a-fA-F]*}}
25+
26+
// The actual number of chunks we'll get depends on internal implementation
27+
// details that we don't want this test to depend on. We'll just make sure
28+
// we get at least two, and ignore the details.
29+
// CHECK: Allocation block {{0x[0-9a-fA-F]*}}
30+
// CHECK: Chunk at {{0x[0-9a-fA-F]*}} length {{[0-9]*}} kind {{[0-9]*}}
31+
// CHECK: Allocation block {{0x[0-9a-fA-F]*}}
32+
// CHECK: Chunk at {{0x[0-9a-fA-F]*}} length {{[0-9]*}} kind {{[0-9]*}}
33+
return a
34+
} else {
35+
return await add(a, b - 1) + 1
36+
}
37+
}
38+
39+
@main struct Main {
40+
static func main() async {
41+
let n = await add(100, 100)
42+
reflect(any: n)
43+
44+
doneReflecting()
45+
}
46+
}
47+
48+
// CHECK: Done.

0 commit comments

Comments
 (0)