Repository navigation
Expand file tree
/
Copy pathJavaMarshalGCBridge.cs
More file actions
569 lines (500 loc) · 20.8 KB
/
Copy pathJavaMarshalGCBridge.cs
File metadata and controls
569 lines (500 loc) · 20.8 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Java;
using System.Threading;
using Android.Runtime;
using Java.Interop;
namespace Microsoft.Android.Runtime;
/// <summary>
/// Processes GC bridges between managed and Java peers.
/// </summary>
/// <remarks>
/// <para>
/// <see cref="Initialize"/> performs process-global, one-shot GC-bridge initialization
/// through the native initializer supplied in <c>JNIEnvInit.JnienvInitializeArgs</c>.
/// The native bridge starts a detached processing thread and aborts the process if it is
/// initialized more than once. <see cref="Initialize"/> is idempotent: subsequent calls
/// return immediately. It must be called after the <c>JniRuntime</c> has been created and
/// published, because bridge processing uses its JNI reference manager.
/// </para>
/// <para>
/// The GC-bridge registration lives for the entire lifetime of the process and is never torn
/// down: stopping the detached bridge-processing thread is not supported by the runtime.
/// </para>
/// </remarks>
static class JavaMarshalGCBridge
{
static readonly Lock initializeLock = new ();
static bool initialized;
static int bridgeProcessingGeneration;
static readonly Lazy<JavaMarshalGCBridgeJni> jni = new (CreateJni);
internal static int BridgeProcessingGeneration => Volatile.Read (ref bridgeProcessingGeneration);
static JavaMarshalGCBridgeJni BridgeJni
=> jni.Value;
static JavaMarshalGCBridgeJni CreateJni ()
{
ManagedObjectReferenceManager.BeginGCBridgeReferenceOperation (GCBridgeReferenceOperation.Initialize);
try {
return new JavaMarshalGCBridgeJni ();
} finally {
ManagedObjectReferenceManager.EndGCBridgeReferenceOperation ();
}
}
/// <safety>
/// <paramref name="args"/> must contain the native host's process-lifetime initializer with the declared
/// unmanaged ABI. The JNI runtime must already be published; its bridge graphs remain live for each callback.
/// The supplied initializer must not have been invoked outside this one-shot initialization path.
/// Retain its native callback and all CLR-supplied descriptor/array storage for each full callback, including
/// failure. Initialization can start the native processing thread before a later managed operation throws;
/// an exception does not undo that registration or authorize releasing its code/storage.
/// </safety>
internal static unsafe void Initialize (JNIEnvInit.JnienvInitializeArgs args)
{
lock (initializeLock) {
if (initialized)
return;
unsafe {
// SAFETY: the host retains the initializer and ProcessBridge for the process lifetime; the lock and guard prevent duplicate initialization.
var initializeGCBridge = args.initializeGCBridge;
ArgumentNullException.ThrowIfNull (initializeGCBridge);
if (RuntimeFeature.EventSourceSupport) {
RuntimeEventSource.Initialize ();
}
var mark_cross_references_ftn = initializeGCBridge (&ProcessBridge);
if (mark_cross_references_ftn == null)
throw new InvalidOperationException ("The native GC bridge initializer did not return a bridge callback.");
JavaMarshal.Initialize (mark_cross_references_ftn);
}
initialized = true;
}
}
internal static bool RequiresTemporaryPeer (nuint componentCount)
=> componentCount == 0;
[UnmanagedCallersOnly]
static void ProcessBridge (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the registered CLR callback supplies args and all counted arrays until FinishCrossReferenceProcessing; identity checks do not validate their native bounds, and failures terminate the process.
try {
ArgumentNullException.ThrowIfNull (args);
bool gcBridgeEventEnabled = RuntimeFeature.EventSourceSupport && RuntimeEventSource.GCBridgeStart ();
HandleContext.EnsureAllContextsAreOurs (args);
Process (args);
ReadOnlySpan<GCHandle> handlesToFree = ProcessCollectedContexts (args);
JavaMarshal.FinishCrossReferenceProcessing (args, handlesToFree);
Interlocked.Increment (ref bridgeProcessingGeneration);
// Keep the switch here so ILLink removes the EventSource when it is disabled.
if (RuntimeFeature.EventSourceSupport) {
if (gcBridgeEventEnabled) {
RuntimeEventSource.GCBridgeStop ();
}
}
} catch (Exception e) {
Environment.FailFast ("Managed GC bridge processing failed.", e);
}
}
}
static ReadOnlySpan<GCHandle> ProcessCollectedContexts (MarkCrossReferencesArgs* mcr)
{
unsafe {
// SAFETY: the CLR retains mcr's counted components and contexts for this callback; collected contexts are queued for later free, after their associated handles are passed back to the CLR.
List<GCHandle> handlesToFree = [];
for (int i = 0; (nuint)i < mcr->ComponentCount; i++) {
StronglyConnectedComponent component = mcr->Components [i];
for (int j = 0; (nuint)j < component.Count; j++) {
ProcessContext ((HandleContext*)component.Contexts [j]);
}
}
void ProcessContext (HandleContext* context)
{
if (context == null) {
throw new ArgumentNullException (nameof (context), "HandleContext should never be null.");
}
// Ignore contexts which were not collected
if (!context->IsCollected) {
return;
}
GCHandle handle = HandleContext.GetAssociatedGCHandle (context);
// Note: modifying the RegisteredInstances dictionary while processing the collected contexts
// is tricky and can lead to deadlocks, so we remember which contexts were collected and we will free
// them later outside of the bridge processing loop.
JavaMarshalRegisteredPeers.QueueCollectedContext ((IntPtr)context);
// important: we must not free the handle before passing it to JavaMarshal.FinishCrossReferenceProcessing
handlesToFree.Add (handle);
}
return CollectionsMarshal.AsSpan (handlesToFree);
}
}
static void Process (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the CLR owns args and its arrays throughout this synchronous prepare/GC/cleanup sequence; validation checks null/count consistency, not arbitrary native allocation provenance.
ValidateArguments (args);
if (RuntimeFeature.GCBridgeLogging) {
LogArguments (args);
}
PrepareForJavaCollection (args);
BridgeJni.TriggerJavaGC ();
CleanupAfterJavaCollection (args);
if (RuntimeFeature.GCBridgeLogging) {
LogSummary (args);
}
}
}
static void ValidateArguments (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: args is the CLR-supplied readable descriptor; these null/count checks do not dereference its component arrays or establish their bounds.
if (args->ComponentCount > 0 && args->Components == null)
throw new InvalidOperationException ("Components must not be null when ComponentCount is greater than zero.");
if (args->CrossReferenceCount > 0 && args->CrossReferences == null)
throw new InvalidOperationException ("CrossReferences must not be null when CrossReferenceCount is greater than zero.");
}
}
static void PrepareForJavaCollection (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the CLR retains the graph for its declared counts; references are linked before each existing global is replaced with a weak global.
PrepareComponentsAndCrossReferences (args);
for (nuint i = 0; i < args->ComponentCount; i++) {
StronglyConnectedComponent component = args->Components [i];
for (nuint j = 0; j < component.Count; j++) {
TakeWeakGlobalReference (GetContext (component, j));
}
}
}
}
static void PrepareComponentsAndCrossReferences (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: component and cross-reference arrays are CLR-owned for this callback; TemporaryPeerMap owns its extra JNI locals until the using scope exits, including on failure.
using var temporaryPeers = new TemporaryPeerMap (args);
for (nuint i = 0; i < args->ComponentCount; i++) {
StronglyConnectedComponent component = args->Components [i];
if (RequiresTemporaryPeer (component.Count)) {
temporaryPeers.Add (i);
} else if (component.Count > 1) {
AddCircularReferences (component);
}
}
for (nuint i = 0; i < args->CrossReferenceCount; i++) {
ComponentCrossReference crossReference = args->CrossReferences [i];
AddCrossReference (
args,
crossReference.SourceGroupIndex,
crossReference.DestinationGroupIndex,
temporaryPeers);
}
}
}
static void AddCircularReferences (StronglyConnectedComponent component)
{
unsafe {
// SAFETY: only components with Count > 1 reach this path; previousIndex and nextIndex stay within the CLR-provided context array while the cycle is linked.
nuint previousIndex = component.Count - 1;
for (nuint nextIndex = 0; nextIndex < component.Count; nextIndex++) {
var previous = GetContext (component, previousIndex);
var next = GetContext (component, nextIndex);
if (!BridgeJni.AddReference (previous->PeerReference, next->PeerReference, knownGCUserPeer: false)) {
throw new InvalidOperationException ("Failed to add a reference between objects in a strongly connected component.");
}
previous->RefsAdded = 1;
previousIndex = nextIndex;
}
}
}
static void AddCrossReference (
MarkCrossReferencesArgs* args,
nuint sourceIndex,
nuint destinationIndex,
TemporaryPeerMap temporaryPeers)
{
unsafe {
// SAFETY: SelectCrossReferenceTarget checks each component index before access; source/destination references and temporary peers remain owned by this bridge callback.
var source = SelectCrossReferenceTarget (args, sourceIndex, temporaryPeers);
var destination = SelectCrossReferenceTarget (args, destinationIndex, temporaryPeers);
if (BridgeJni.AddReference (source.Reference, destination.Reference, source.IsGCUserPeerKnown)) {
source.MarkRefsAdded ();
}
}
}
static CrossReferenceTarget SelectCrossReferenceTarget (
MarkCrossReferencesArgs* args,
nuint componentIndex,
TemporaryPeerMap temporaryPeers)
{
unsafe {
// SAFETY: componentIndex is checked before array access; the CLR retains its components and TemporaryPeerMap retains any borrowed temporary peer.
if (componentIndex >= args->ComponentCount)
throw new InvalidOperationException ("A GC bridge cross-reference component index is out of range.");
JniObjectReference temporaryPeer = temporaryPeers.Get (componentIndex);
if (temporaryPeer.IsValid)
return new CrossReferenceTarget (temporaryPeer);
StronglyConnectedComponent component = args->Components [componentIndex];
if (component.Count == 0)
throw new InvalidOperationException ("An empty component does not have a temporary peer.");
return new CrossReferenceTarget (GetContext (component, 0));
}
}
static void CleanupAfterJavaCollection (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the CLR retains the counted contexts while weak references are promoted, links cleared and component liveness compared; no context is freed here.
for (nuint i = 0; i < args->ComponentCount; i++) {
StronglyConnectedComponent component = args->Components [i];
for (nuint j = 0; j < component.Count; j++) {
var context = GetContext (component, j);
TakeGlobalReference (context);
ClearReferencesIfNeeded (context);
}
EnsureAllCollectedOrAllAlive (component);
}
}
}
static void ClearReferencesIfNeeded (HandleContext* context)
{
unsafe {
// SAFETY: context remains registered during bridge processing; only a live global reference with added links is passed to monodroidClearReferences.()V.
if (context->IsCollected)
return;
if (context->PeerReference.Type != JniObjectReferenceType.Global)
throw new InvalidOperationException ("A live GC bridge peer must have a global reference.");
if (context->RefsAdded == 0)
return;
BridgeJni.ClearReferences (context->PeerReference);
context->RefsAdded = 0;
}
}
static void TakeWeakGlobalReference (HandleContext* context)
{
unsafe {
// SAFETY: the registered context owns global; weak is created before global is released, and failure to create a valid weak reference terminates rather than publishing it.
JniObjectReference global = context->PeerReference;
if (global.Type != JniObjectReferenceType.Global)
throw new InvalidOperationException ("Expected a global reference before Java GC bridge processing.");
ManagedObjectReferenceManager.BeginGCBridgeReferenceOperation (GCBridgeReferenceOperation.GlobalToWeak);
JniObjectReference weak;
try {
weak = global.NewWeakGlobalRef ();
} finally {
ManagedObjectReferenceManager.EndGCBridgeReferenceOperation ();
}
if (!weak.IsValid) {
FailFastOnPendingJavaException ("Failed to create a weak global reference during GC bridge processing.");
Environment.FailFast ("Failed to create a weak global reference during GC bridge processing.");
}
context->SetPeerReference (weak);
ManagedObjectReferenceManager.BeginGCBridgeReferenceOperation (GCBridgeReferenceOperation.GlobalToWeak);
try {
JniObjectReference.Dispose (ref global);
} finally {
ManagedObjectReferenceManager.EndGCBridgeReferenceOperation ();
}
}
}
static void TakeGlobalReference (HandleContext* context)
{
unsafe {
// SAFETY: the registered context owns weak; promotion supplies global or the collected/null result before weak is released, and unexpected JNI failure terminates.
JniObjectReference weak = context->PeerReference;
if (weak.Type != JniObjectReferenceType.WeakGlobal)
throw new InvalidOperationException ("Expected a weak global reference after Java GC bridge processing.");
ManagedObjectReferenceManager.BeginGCBridgeReferenceOperation (GCBridgeReferenceOperation.WeakToGlobal);
JniObjectReference global;
try {
global = weak.NewGlobalRef ();
} finally {
ManagedObjectReferenceManager.EndGCBridgeReferenceOperation ();
}
if (!global.IsValid) {
FailFastOnPendingJavaException ("Failed to promote a weak global reference during GC bridge processing.");
if (RuntimeFeature.EventSourceSupport) {
RuntimeEventSource.WeakGlobalReferenceCollected (weak.Handle);
}
}
context->SetPeerReference (global);
ManagedObjectReferenceManager.BeginGCBridgeReferenceOperation (GCBridgeReferenceOperation.WeakToGlobal);
try {
JniObjectReference.Dispose (ref weak);
} finally {
ManagedObjectReferenceManager.EndGCBridgeReferenceOperation ();
}
}
}
static void EnsureAllCollectedOrAllAlive (StronglyConnectedComponent component)
{
unsafe {
// SAFETY: the CLR retains component.Contexts for Count entries; zero count returns before access, and the loop checks every remaining context against the first.
if (component.Count == 0)
return;
bool collected = GetContext (component, 0)->IsCollected;
for (nuint i = 1; i < component.Count; i++) {
if (GetContext (component, i)->IsCollected != collected)
throw new InvalidOperationException ("Cannot have a mix of collected and alive contexts in a strongly connected component.");
}
}
}
static HandleContext* GetContext (StronglyConnectedComponent component, nuint index)
{
unsafe {
// SAFETY: callers bound index by component.Count in the CLR-owned context array; the checks reject null arrays/elements but cannot validate arbitrary pointer extents.
if (component.Contexts == null)
throw new InvalidOperationException ("A non-empty component must have contexts.");
var context = (HandleContext*) component.Contexts [index];
if (context == null)
throw new InvalidOperationException ("A GC bridge context must not be null.");
return context;
}
}
static void FailFastOnPendingJavaException (string message)
{
IntPtr exception = JNIEnv.ExceptionOccurred ();
if (exception == IntPtr.Zero)
return;
JNIEnv.ExceptionDescribe ();
JNIEnv.ExceptionClear ();
unsafe {
// SAFETY: ExceptionOccurred returned this owned local throwable reference, used only before its release.
JNIEnv.DeleteLocalRef (exception);
}
Environment.FailFast (message);
}
static void LogArguments (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the CLR retains args and its counted arrays during diagnostic iteration; class-name lookup borrows only references reported live by their registered contexts.
if (!Logger.LogGC)
return;
Logger.Log (
LogLevel.Info,
"DOTNET-gc",
$"cross references callback invoked with {args->ComponentCount} sccs and {args->CrossReferenceCount} xrefs.");
for (nuint i = 0; i < args->ComponentCount; i++) {
StronglyConnectedComponent component = args->Components [i];
Logger.Log (LogLevel.Info, "DOTNET-gc", $"group {i} with {component.Count} objects");
for (nuint j = 0; j < component.Count; j++) {
JniObjectReference reference = GetContext (component, j)->PeerReference;
string? className = reference.IsValid ? JniEnvironment.Types.GetJniTypeNameFromInstance (reference) : null;
Logger.Log (
LogLevel.Info,
"DOTNET-gc",
string.Create (
CultureInfo.InvariantCulture,
$"gref 0x{unchecked((nuint)reference.Handle):x} [{className ?? "unknown class"}]"));
}
}
for (nuint i = 0; i < args->CrossReferenceCount; i++) {
ComponentCrossReference crossReference = args->CrossReferences [i];
Logger.Log (
LogLevel.Info,
"DOTNET-gc",
$"xref [{i}] {crossReference.SourceGroupIndex} -> {crossReference.DestinationGroupIndex}");
}
}
}
static void LogSummary (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: the CLR retains args and its counted contexts during this summary; only IsCollected is read before the callback releases or queues contexts.
if (!Logger.LogGC)
return;
nuint total = 0;
nuint alive = 0;
for (nuint i = 0; i < args->ComponentCount; i++) {
StronglyConnectedComponent component = args->Components [i];
for (nuint j = 0; j < component.Count; j++) {
total++;
if (!GetContext (component, j)->IsCollected)
alive++;
}
}
Logger.Log (LogLevel.Info, "DOTNET-gc", $"GC cleanup summary: {total} objects tested - resurrecting {alive}.");
}
}
readonly struct CrossReferenceTarget
{
readonly HandleContext* context;
public CrossReferenceTarget (JniObjectReference temporaryPeer)
{
unsafe {
// SAFETY: only a null context address is stored here; Reference borrows the TemporaryPeerMap-owned local until that map is disposed.
Reference = temporaryPeer;
context = null;
IsGCUserPeerKnown = true;
}
}
public CrossReferenceTarget (HandleContext* context)
{
unsafe {
// SAFETY: context comes from the current CLR graph and remains registered; Reference snapshots its peer without taking ownership or extending its lifetime.
this.context = context;
Reference = context->PeerReference;
IsGCUserPeerKnown = context->RefsAdded != 0;
}
}
public JniObjectReference Reference { get; }
public bool IsGCUserPeerKnown { get; }
public void MarkRefsAdded ()
{
unsafe {
// SAFETY: a nonnull context is still registered for this bridge callback; only its refs_added marker is written, and temporary-peer targets have no context to update.
if (context != null)
context->RefsAdded = 1;
}
}
}
struct TemporaryPeerMap : IDisposable
{
const int LocalReferenceSlack = 16;
readonly JniObjectReference[] peers;
public TemporaryPeerMap (MarkCrossReferencesArgs* args)
{
unsafe {
// SAFETY: ProcessBridge rejects null args and Process validates the component array/count pair;
// the CLR retains that storage through this callback, and checked conversion bounds the managed peers array.
int count = checked((int)args->ComponentCount);
int temporaryPeerCount = 0;
for (nuint i = 0; i < args->ComponentCount; i++) {
if (RequiresTemporaryPeer (args->Components [i].Count))
temporaryPeerCount++;
}
if (temporaryPeerCount == 0) {
peers = [];
return;
}
peers = new JniObjectReference [count];
try {
JniEnvironment.References.EnsureLocalCapacity (checked(temporaryPeerCount + LocalReferenceSlack));
} catch (Exception e) when (e is JavaException || e is InvalidOperationException) {
Logger.Log (
LogLevel.Warn,
"DOTNET-gc",
$"Failed to reserve JNI local reference capacity for {temporaryPeerCount} temporary peers: {e.Message}");
}
}
}
public void Add (nuint componentIndex)
{
int index = checked((int)componentIndex);
if (peers [index].IsValid)
throw new InvalidOperationException ("A temporary peer already exists for this component.");
JniObjectReference peer = BridgeJni.CreateTemporaryPeer ();
if (!peer.IsValid)
throw new InvalidOperationException ("Failed to create a temporary peer during GC bridge processing.");
peers [index] = peer;
}
public JniObjectReference Get (nuint componentIndex)
{
return peers.Length == 0 ? default : peers [checked((int)componentIndex)];
}
public void Dispose ()
{
for (int i = 0; i < peers.Length; i++) {
JniObjectReference.Dispose (ref peers [i]);
}
}
}
}