-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerSessionState.cs
More file actions
267 lines (245 loc) · 8.59 KB
/
Copy pathWorkerSessionState.cs
File metadata and controls
267 lines (245 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using DotPython.Runtime.Managed.Execution;
using DotPython.Runtime.Native;
namespace DotPython.Worker.Host;
internal sealed class WorkerSessionState : IAsyncDisposable
{
private readonly NativeExecutionLane _nativeLane = new();
private readonly Dictionary<long, StableAbiModule> _nativeModules = [];
private readonly List<long> _nativeOrder = [];
private readonly Dictionary<string, StableAbiModuleCatalogEntry> _stableAbiModules;
private readonly Dictionary<string, long> _boundNativeObjectIds = new(StringComparer.Ordinal);
private long _nextObjectId;
private int _disposed;
internal WorkerSessionState(
IReadOnlyList<string> packageRoots,
IReadOnlyList<StableAbiModuleCatalogEntry> stableAbiModules
)
{
ArgumentNullException.ThrowIfNull(packageRoots);
ArgumentNullException.ThrowIfNull(stableAbiModules);
_stableAbiModules = stableAbiModules.ToDictionary(
entry => entry.Manifest.ModuleName,
StringComparer.Ordinal
);
foreach (var entry in stableAbiModules)
{
ValidateQualifiedPackage(packageRoots, entry.Configuration, entry.Manifest);
}
Engine =
packageRoots.Count == 0
? new ManagedPythonEngine()
: new ManagedPythonEngine(
new ManagedModuleDiscoveryOptions
{
SearchPaths = packageRoots,
NativeExtensionResolver = ResolveNativeExtension,
}
);
}
internal ManagedPythonEngine Engine { get; }
private Task<long> LoadStableAbiModuleAsync(
StableAbiModuleConfiguration configuration,
CancellationToken cancellationToken
) =>
_nativeLane.InvokeAsync(
() =>
{
var module = StableAbiModuleLoader.Load(configuration);
var objectId = checked(++_nextObjectId);
_nativeModules.Add(objectId, module);
_nativeOrder.Add(objectId);
return objectId;
},
cancellationToken
);
public async ValueTask DisposeAsync()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
{
return;
}
_ = await _nativeLane
.InvokeAsync(
() =>
{
for (var index = _nativeOrder.Count - 1; index >= 0; index--)
{
_nativeModules[_nativeOrder[index]].Dispose();
}
_nativeModules.Clear();
_nativeOrder.Clear();
return true;
},
CancellationToken.None
)
.ConfigureAwait(false);
await _nativeLane.DisposeAsync().ConfigureAwait(false);
}
private StableAbiModule GetModule(long objectId)
{
if (!_nativeModules.TryGetValue(objectId, out var module))
{
throw new StableAbiLoadException(
"DPY8106",
StableAbiLoadPhase.Invocation,
"The native module handle is not active.",
artifactPath: null,
artifactSha256: null,
missingSymbol: null
);
}
return module;
}
internal T InvokeNative<T>(Func<T> operation) =>
_nativeLane.InvokeAsync(operation, CancellationToken.None).GetAwaiter().GetResult();
private Action<PythonGlobalNamespace>? ResolveNativeExtension(string name, string path)
{
if (!_stableAbiModules.TryGetValue(name, out var entry))
{
return null;
}
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
if (
!string.Equals(
Path.GetFullPath(path),
Path.GetFullPath(entry.Configuration.ModulePath),
comparison
)
)
{
throw new InvalidDataException(
$"Qualified native module '{name}' resolved to an artifact other than its configured path."
);
}
return globals => InitializeNativeExtension(entry, globals);
}
private void InitializeNativeExtension(
StableAbiModuleCatalogEntry entry,
PythonGlobalNamespace globals
)
{
if (_boundNativeObjectIds.ContainsKey(entry.Manifest.ModuleName))
{
throw new PythonRuntimeException(
"DPY4029",
"The qualified native module cannot be initialized in this session.",
default,
"ImportError"
);
}
try
{
var objectId = LoadStableAbiModuleAsync(entry.Configuration, CancellationToken.None)
.GetAwaiter()
.GetResult();
_boundNativeObjectIds.Add(entry.Manifest.ModuleName, objectId);
var exports = InvokeNative(() =>
{
var module = GetModule(objectId);
return module
.GetAttributeNames()
.Where(name =>
entry.Manifest.AllowedMethods.Contains(name, StringComparer.Ordinal)
)
.Select(name => new KeyValuePair<string, PythonValue>(
name,
QualifiedStableAbiObjectProtocol.ToManaged(this, module.GetAttribute(name))
))
.ToArray();
});
foreach (var export in exports)
{
globals.SetValue(export.Key, export.Value);
}
}
catch (StableAbiLoadException exception)
{
throw new PythonRuntimeException(
exception.Code,
exception.Message,
default,
"ImportError"
);
}
}
private static void ValidateQualifiedPackage(
IReadOnlyList<string> packageRoots,
StableAbiModuleConfiguration configuration,
StableAbiSymbolManifest manifest
)
{
if (
manifest.NativeEntry is null
|| manifest.PackageInitializer is null
|| manifest.PackageInitializerSha256 is null
|| manifest.PackageMetadata is null
|| manifest.PackageMetadataSha256 is null
)
{
return;
}
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
var packageRoot = packageRoots.FirstOrDefault(root =>
string.Equals(
Path.GetFullPath(Path.Combine(root, manifest.NativeEntry)),
Path.GetFullPath(configuration.ModulePath),
comparison
)
);
if (packageRoot is null)
{
return;
}
ValidatePackageFile(
packageRoot,
manifest.PackageInitializer,
manifest.PackageInitializerSha256
);
ValidatePackageFile(packageRoot, manifest.PackageMetadata, manifest.PackageMetadataSha256);
}
private static void ValidatePackageFile(
string packageRoot,
string relativePath,
string expectedSha256
)
{
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(packageRoot));
var path = Path.GetFullPath(Path.Combine(root, relativePath));
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
if (!path.StartsWith(root + Path.DirectorySeparatorChar, comparison) || !File.Exists(path))
{
throw PackagePolicyFailure(path, "A qualified package file is missing.");
}
var file = new FileInfo(path);
if (
file.LinkTarget is not null
|| (file.Attributes & FileAttributes.ReparsePoint) != 0
|| !string.Equals(
StableAbiModuleLoader.ComputeSha256(path),
expectedSha256,
StringComparison.Ordinal
)
)
{
throw PackagePolicyFailure(
path,
"A qualified package file failed identity validation."
);
}
}
private static StableAbiLoadException PackagePolicyFailure(string path, string message) =>
new(
"DPY8001",
StableAbiLoadPhase.Policy,
message,
path,
artifactSha256: null,
missingSymbol: null
);
}