-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeCoreMirOwnershipAdapterTests.cs
More file actions
634 lines (596 loc) · 30.1 KB
/
Copy pathSafeCoreMirOwnershipAdapterTests.cs
File metadata and controls
634 lines (596 loc) · 30.1 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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
using RustSharp.Semantics;
using RustSharp.Syntax;
namespace RustSharp.Tests;
internal static class SafeCoreMirOwnershipAdapterTests
{
public static IReadOnlyList<TestCase> All { get; } =
[
new("typed MIR ownership adapter materializes scalar constants", ConstantReturnAsync),
new("typed MIR ownership adapter applies local limits per function", PerFunctionLimitAsync),
new("typed MIR ownership adapter bounds materialized constants", MaterializedConstantLimitAsync),
new("typed MIR ownership adapter reports use before initialization", UseBeforeInitializationAsync),
new("typed MIR ownership adapter preserves returned parameter provenance", ParameterReferenceAsync),
new("typed MIR ownership adapter accepts finite Copy tuple aggregates", CopyTupleAsync),
new("typed MIR ownership adapter lowers non-Copy place reads as moves", NonCopyMoveAsync),
new("typed MIR ownership adapter rejects a second non-Copy source use", NonCopyUseAfterMoveAsync),
new("ownership preserves a shared-reference self-copy loan", SelfReferenceCopyAsync),
new("typed MIR ownership adapter preserves projected MIR places", ProjectedPlaceAsync),
new("typed MIR ownership adapter preserves projected borrow provenance", ProjectedBorrowAsync),
new("typed MIR ownership evidence rejects projected source drift", ProjectedEvidenceSourceDriftAsync),
new("typed MIR ownership evidence correlates non-copy source facts", ExplicitEvidenceAsync),
new("typed MIR ownership evidence rejects source drift", EvidenceMismatchAsync),
new("typed MIR ownership evidence rejects extra local facts", ExtraLocalEvidenceAsync),
new("typed MIR ownership evidence rejects missing blocks", MissingBlockEvidenceAsync),
new("typed MIR ownership evidence carries forward its operation budget", EvidenceOperationBudgetAsync),
];
private static readonly SafeCoreType Integer = SafeCoreType.Primitive(SafeCoreSemanticTypeKind.I32);
private static readonly SafeCoreType Reference = SafeCoreType.Reference(Integer, mutable: false);
private static readonly SafeCoreMirSource Source = new("ownership-adapter.rs", new TextSpan(0, 16), 0, 16);
private static Task ConstantReturnAsync()
{
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::constant",
Integer,
[],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", Source), Source),
Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
AssertEx.True(result.Program!.Functions[0].Locals.Count == 1,
"A scalar constant return must be represented by one explicit adapter local.");
AssertEx.Equal(SafeCoreOwnershipOutcome.Returned, result.Ownership!.Paths.Single().Outcome);
return Task.CompletedTask;
}
private static Task UseBeforeInitializationAsync()
{
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::uninitialized",
Integer,
[new SafeCoreMirLocal(0, "value", Integer, SafeCoreMirLocalKind.Temporary, false, Source)],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(0, Integer, Source), Source),
Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.False(result.IsSuccessful, "Typed MIR structural validity must not imply definite initialization.");
AssertEx.True(result.Ownership!.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreOwnershipDiagnosticCodes.UseAfterMove),
"The ownership phase must report a use of an uninitialized typed-MIR local.");
return Task.CompletedTask;
}
private static Task MaterializedConstantLimitAsync()
{
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::bounded_constant",
Integer,
[new SafeCoreMirLocal(0, "slot", Integer, SafeCoreMirLocalKind.User, false, Source)],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", Source), Source),
Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(
program,
new SafeCoreMirOwnershipOptions { MaximumLocalsPerFunction = 1 });
AssertEx.False(result.IsSuccessful, "Synthetic locals must obey the configured arena limit.");
AssertEx.True(result.Program is null, "A bounded adaptation failure must not publish a partial program.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreMirOwnershipDiagnosticCodes.LimitReached),
"The adapter must report the bounded local limit.");
return Task.CompletedTask;
}
private static Task PerFunctionLimitAsync()
{
SafeCoreMirProgram program = new([
ConstantFunction(0, "crate::first"),
ConstantFunction(1, "crate::second"),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(
program,
new SafeCoreMirOwnershipOptions { MaximumLocalsPerFunction = 1 });
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
AssertEx.Equal(2, result.Program!.Functions.Count);
AssertEx.True(result.Program.Functions.All(function => function.Locals.Count == 1),
"The local arena limit must apply independently to each function.");
return Task.CompletedTask;
}
private static SafeCoreMirFunction ConstantFunction(int id, string name) =>
new(
id,
name,
Integer,
[],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", Source), Source),
Source)],
0,
Source);
private static Task ParameterReferenceAsync()
{
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::reference",
Reference,
[new SafeCoreMirLocal(0, "view", Reference, SafeCoreMirLocalKind.Parameter, false, Source)],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(0, Reference, Source), Source),
Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
SafeCoreMirReferenceProvenanceResult provenance = SafeCoreMirReferenceProvenance.Analyze(program);
AssertEx.True(provenance.IsSuccessful, string.Join(Environment.NewLine, provenance.Diagnostics));
AssertEx.True(provenance.Functions.Single().ReturnOrigins.Single() is { LocalId: 0, IsParameter: true },
"A returned input reference retains its checked parameter origin.");
return Task.CompletedTask;
}
private static Task CopyTupleAsync()
{
SafeCoreType boolean = SafeCoreType.Primitive(SafeCoreSemanticTypeKind.Bool);
SafeCoreType tuple = SafeCoreType.Tuple([Integer, boolean]);
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::copy_tuple",
tuple,
[
new SafeCoreMirLocal(0, "value", tuple, SafeCoreMirLocalKind.Parameter, false, Source),
new SafeCoreMirLocal(1, "copy", tuple, SafeCoreMirLocalKind.Temporary, false, Source),
],
[new SafeCoreMirBlock(
0,
[new SafeCoreMirStatement(
1,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.Local(0, tuple, Source), Source),
Source)],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(1, tuple, Source), Source),
Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
AssertEx.Equal(SafeCoreOwnershipOutcome.Returned, result.Ownership!.Paths.Single().Outcome);
AssertEx.True(result.Program!.Functions[0].Locals.All(local => local.Kind == SafeCoreOwnershipKind.Copy),
"A tuple composed only of scalar Copy values must remain Copy in the ownership bridge.");
return Task.CompletedTask;
}
private static Task NonCopyMoveAsync()
{
SafeCoreType marker = SafeCoreType.Adt("crate::Marker");
SafeCoreMirFunction function = new(
0,
"crate::move_value",
marker,
[
new SafeCoreMirLocal(0, "source", marker, SafeCoreMirLocalKind.Parameter, false, Source)
{
IsUnitAdt = true,
DestructorFunctionId = 7,
},
new SafeCoreMirLocal(1, "destination", marker, SafeCoreMirLocalKind.Temporary, false, Source)
{
IsUnitAdt = true,
DestructorFunctionId = 7,
},
],
[new SafeCoreMirBlock(
0,
[new SafeCoreMirStatement(
1,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.Local(0, marker, Source), Source),
Source)],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(1, marker, Source), Source),
Source)],
0,
Source);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(new([function]));
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
SafeCoreOwnershipBlock block = result.Program!.Functions[0].Blocks[0];
AssertEx.Equal(SafeCoreOwnershipInstructionKind.Move, block.Instructions[0].Kind);
AssertEx.True(result.Ownership!.Paths.Single().Trace.Any(trace => trace.StartsWith("move source -> destination", StringComparison.Ordinal)),
"A non-Copy MIR Use must become an ownership move with source and destination evidence.");
AssertEx.False(result.Ownership.Paths.Single().Trace.Any(trace => trace == "assign destination"),
"A move must not be represented as a copy assignment.");
return Task.CompletedTask;
}
private static Task NonCopyUseAfterMoveAsync()
{
SafeCoreType marker = SafeCoreType.Adt("crate::Marker");
SafeCoreMirFunction function = new(
0,
"crate::move_then_use",
marker,
[
new SafeCoreMirLocal(0, "source", marker, SafeCoreMirLocalKind.Parameter, false, Source)
{
IsUnitAdt = true,
DestructorFunctionId = 7,
},
new SafeCoreMirLocal(1, "first", marker, SafeCoreMirLocalKind.Temporary, false, Source)
{
IsUnitAdt = true,
DestructorFunctionId = 7,
},
new SafeCoreMirLocal(2, "second", marker, SafeCoreMirLocalKind.Temporary, false, Source)
{
IsUnitAdt = true,
DestructorFunctionId = 7,
},
],
[new SafeCoreMirBlock(
0,
[
new SafeCoreMirStatement(1,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.Local(0, marker, Source), Source), Source),
new SafeCoreMirStatement(2,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.Local(0, marker, Source), Source), Source),
],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(1, marker, Source), Source),
Source)],
0,
Source);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(new([function]));
AssertEx.False(result.IsSuccessful, "A second non-Copy source use must remain a move/use-after-move error.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreOwnershipDiagnosticCodes.UseAfterMove),
string.Join(Environment.NewLine, result.Diagnostics));
return Task.CompletedTask;
}
private static Task SelfReferenceCopyAsync()
{
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::self-copy",
[
new SafeCoreOwnershipLocal(0, "owner", Integer, SafeCoreOwnershipKind.Move,
HasDrop: false, ScopeId: 0, IsReference: false, InitiallyInitialized: true, Source),
new SafeCoreOwnershipLocal(1, "view", Reference, SafeCoreOwnershipKind.Copy,
HasDrop: false, ScopeId: 0, IsReference: true, InitiallyInitialized: false, Source),
],
[new SafeCoreOwnershipScope(0, -1, Source)],
[new SafeCoreOwnershipBlock(
0,
0,
[
SafeCoreOwnershipInstruction.Borrow(0, 1, mutable: false, Source),
SafeCoreOwnershipInstruction.AssignReference(1, 1, Source),
],
SafeCoreOwnershipTerminator.ReturnUnit(Source),
Source)],
0,
SafeCorePanicStrategy.Unwind,
Source),
]);
SafeCoreOwnershipAnalysisResult result = SafeCoreOwnershipAnalysis.Analyze(ownership);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
AssertEx.True(result.Paths.Single().Trace.Any(trace => trace.Contains("(no-op)", StringComparison.Ordinal)),
"A shared-reference self-copy must preserve its active loan as a no-op.");
return Task.CompletedTask;
}
private static Task ProjectedPlaceAsync()
{
SafeCoreType tuple = SafeCoreType.Tuple([Integer, Integer]);
SafeCoreMirPlace first = SafeCoreMirPlace.Root(0).Append(SafeCoreMirProjection.TupleIndex(0));
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::projected-use",
Integer,
[
new SafeCoreMirLocal(0, "pair", tuple, SafeCoreMirLocalKind.Parameter, false, Source),
new SafeCoreMirLocal(1, "value", Integer, SafeCoreMirLocalKind.Temporary, false, Source),
],
[new SafeCoreMirBlock(
0,
[new SafeCoreMirStatement(1,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.PlaceValue(first, Integer, Source), Source), Source)],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(1, Integer, Source), Source), Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
SafeCoreOwnershipInstruction use = result.Program!.Functions[0].Blocks[0].Instructions[0];
AssertEx.Equal(SafeCoreOwnershipInstructionKind.Use, use.Kind);
AssertEx.True(use.Place is not null && use.Place.LocalId == 0 &&
use.Place.Projections.Single().Kind == SafeCoreOwnershipProjectionKind.TupleIndex &&
use.Place.Projections.Single().Index == 0,
"A projected MIR use must retain its root local and structural projection.");
return Task.CompletedTask;
}
private static Task ProjectedBorrowAsync()
{
SafeCoreType tuple = SafeCoreType.Tuple([Integer, Integer]);
SafeCoreType mutableReference = SafeCoreType.Reference(Integer, mutable: true);
SafeCoreMirPlace second = SafeCoreMirPlace.Root(0).Append(SafeCoreMirProjection.TupleIndex(1));
SafeCoreMirProgram program = new([
new SafeCoreMirFunction(
0,
"crate::projected-borrow",
SafeCoreType.Primitive(SafeCoreSemanticTypeKind.Unit),
[
new SafeCoreMirLocal(0, "pair", tuple, SafeCoreMirLocalKind.Parameter, true, Source),
new SafeCoreMirLocal(1, "view", mutableReference, SafeCoreMirLocalKind.Temporary, false, Source),
],
[new SafeCoreMirBlock(
0,
[new SafeCoreMirStatement(1,
SafeCoreMirRvalue.Unary("&mut",
SafeCoreMirOperand.PlaceValue(second, Integer, Source), mutableReference, Source), Source)],
SafeCoreMirTerminator.Return(null, Source), Source)],
0,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(program);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
SafeCoreOwnershipInstruction borrow = result.Program!.Functions[0].Blocks[0].Instructions.Single();
AssertEx.Equal(SafeCoreOwnershipInstructionKind.Borrow, borrow.Kind);
AssertEx.True(borrow.Place is not null && borrow.Place.Projections.Single().Index == 1 &&
borrow.RelatedPlace is not null && borrow.RelatedPlace.IsRoot && borrow.RelatedPlace.LocalId == 1,
"A projected borrow must retain the owner place and reference destination separately.");
return Task.CompletedTask;
}
private static Task ProjectedEvidenceSourceDriftAsync()
{
SafeCoreType tuple = SafeCoreType.Tuple([Integer, Integer]);
SafeCoreMirSource drifted = new("other.rs", new TextSpan(0, 16), 0, 16);
SafeCoreMirPlace first = SafeCoreMirPlace.Root(0).Append(SafeCoreMirProjection.TupleIndex(0));
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::projected-evidence",
Integer,
[
new SafeCoreMirLocal(0, "pair", tuple, SafeCoreMirLocalKind.Parameter, false, Source),
new SafeCoreMirLocal(1, "value", Integer, SafeCoreMirLocalKind.Temporary, false, Source),
],
[new SafeCoreMirBlock(
0,
[new SafeCoreMirStatement(1,
SafeCoreMirRvalue.Use(SafeCoreMirOperand.PlaceValue(first, Integer, Source), Source), Source)],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(1, Integer, Source), Source), Source)],
0,
Source),
]);
SafeCoreOwnershipPlace ownershipPlace = SafeCoreOwnershipPlace.Root(0)
.Append(SafeCoreOwnershipProjection.TupleIndex(0));
SafeCoreOwnershipFunction ownershipFunction = new(
"crate::projected-evidence",
[
new SafeCoreOwnershipLocal(0, "pair", tuple, SafeCoreOwnershipKind.Copy, false, 0, false, true, Source),
new SafeCoreOwnershipLocal(1, "value", Integer, SafeCoreOwnershipKind.Copy, false, 0, false, false, Source),
],
[new SafeCoreOwnershipScope(0, -1, Source)],
[new SafeCoreOwnershipBlock(0, 0,
[SafeCoreOwnershipInstruction.Use(ownershipPlace, drifted)],
SafeCoreOwnershipTerminator.Return(1, Source), Source)],
0,
SafeCorePanicStrategy.Unwind,
Source);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(
mir, new SafeCoreOwnershipProgram([ownershipFunction]));
AssertEx.False(result.IsSuccessful, "Projected ownership source drift must not reach analysis.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreMirOwnershipAdapter.EvidenceMismatch),
"Projected instruction source drift must retain the stable RSM3004 diagnostic.");
return Task.CompletedTask;
}
private static Task ExplicitEvidenceAsync()
{
SafeCoreMirSource source = new("ownership-evidence.rs", new TextSpan(0, 1), 0, 1);
SafeCoreType reference = SafeCoreType.Reference(Integer, mutable: false);
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::reference",
reference,
[new SafeCoreMirLocal(0, "view", reference, SafeCoreMirLocalKind.Parameter, false, source)],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(0, reference, source), source),
source)],
0,
source),
]);
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::reference",
[new SafeCoreOwnershipLocal(0, "view", reference, SafeCoreOwnershipKind.Copy,
HasDrop: false, ScopeId: 0, IsReference: true, InitiallyInitialized: true, Source: source)],
[new SafeCoreOwnershipScope(0, -1, source)],
[new SafeCoreOwnershipBlock(0, 0, [], SafeCoreOwnershipTerminator.Return(0, source), source)],
0,
SafeCorePanicStrategy.Unwind,
source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(mir, ownership);
AssertEx.True(result.IsSuccessful, string.Join(Environment.NewLine, result.Diagnostics));
AssertEx.Equal(SafeCoreOwnershipOutcome.Returned, result.Ownership!.Paths.Single().Outcome);
return Task.CompletedTask;
}
private static Task EvidenceMismatchAsync()
{
SafeCoreMirSource mirSource = new("ownership-evidence.rs", new TextSpan(0, 1), 0, 1);
SafeCoreMirSource drifted = new("other.rs", new TextSpan(0, 1), 0, 1);
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::scalar",
Integer,
[],
[new SafeCoreMirBlock(0, [],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", mirSource), mirSource),
mirSource)],
0,
mirSource),
]);
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::scalar",
[],
[new SafeCoreOwnershipScope(0, -1, drifted)],
[new SafeCoreOwnershipBlock(0, 0, [], SafeCoreOwnershipTerminator.ReturnUnit(drifted), drifted)],
0,
SafeCorePanicStrategy.Unwind,
drifted),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(mir, ownership);
AssertEx.False(result.IsSuccessful, "Source drift must not publish ownership evidence.");
AssertEx.True(result.Diagnostics.Any(diagnostic => diagnostic.Code == SafeCoreMirOwnershipAdapter.EvidenceMismatch),
"Source drift must have a stable adapter diagnostic.");
return Task.CompletedTask;
}
private static Task MissingBlockEvidenceAsync()
{
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::missing-block",
Integer,
[],
[
new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", Source), Source),
Source),
new SafeCoreMirBlock(
1,
[],
SafeCoreMirTerminator.Unreachable(Source),
Source),
],
0,
Source),
]);
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::missing-block",
[],
[new SafeCoreOwnershipScope(0, -1, Source)],
[new SafeCoreOwnershipBlock(0, 0, [],
SafeCoreOwnershipTerminator.ReturnUnit(Source), Source)],
0,
SafeCorePanicStrategy.Unwind,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(mir, ownership);
AssertEx.False(result.IsSuccessful, "Ownership evidence must cover every typed-MIR block.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreMirOwnershipAdapter.MissingEvidence),
"A missing typed-MIR block must produce MissingEvidence.");
return Task.CompletedTask;
}
private static Task ExtraLocalEvidenceAsync()
{
SafeCoreType reference = SafeCoreType.Reference(Integer, mutable: false);
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::extra-local",
reference,
[new SafeCoreMirLocal(0, "view", reference, SafeCoreMirLocalKind.Parameter, false, Source)],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Local(0, reference, Source), Source),
Source)],
0,
Source),
]);
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::extra-local",
[
new SafeCoreOwnershipLocal(0, "view", reference, SafeCoreOwnershipKind.Move,
HasDrop: false, ScopeId: 0, IsReference: true, InitiallyInitialized: true, Source: Source),
new SafeCoreOwnershipLocal(1, "extra", reference, SafeCoreOwnershipKind.Move,
HasDrop: false, ScopeId: 0, IsReference: true, InitiallyInitialized: true, Source: Source),
],
[new SafeCoreOwnershipScope(0, -1, Source)],
[new SafeCoreOwnershipBlock(0, 0, [], SafeCoreOwnershipTerminator.Return(0, Source), Source)],
0,
SafeCorePanicStrategy.Unwind,
Source),
]);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(mir, ownership);
AssertEx.False(result.IsSuccessful, "An ownership fact for a local absent from MIR must be rejected.");
AssertEx.True(result.Ownership is null, "Extra evidence must not reach the ownership analyzer.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreMirOwnershipAdapter.EvidenceMismatch &&
diagnostic.Message.Contains("extra local fact 1", StringComparison.Ordinal)),
"Extra local IDs must have a stable EvidenceMismatch diagnostic.");
return Task.CompletedTask;
}
private static Task EvidenceOperationBudgetAsync()
{
SafeCoreMirProgram mir = new([
new SafeCoreMirFunction(
0,
"crate::budget",
Integer,
[],
[new SafeCoreMirBlock(
0,
[],
SafeCoreMirTerminator.Return(SafeCoreMirOperand.Constant(Integer, "7", Source), Source),
Source)],
0,
Source),
]);
SafeCoreOwnershipProgram ownership = new([
new SafeCoreOwnershipFunction(
"crate::budget",
[],
[new SafeCoreOwnershipScope(0, -1, Source)],
[new SafeCoreOwnershipBlock(0, 0, [], SafeCoreOwnershipTerminator.ReturnUnit(Source), Source)],
0,
SafeCorePanicStrategy.Unwind,
Source),
]);
SafeCoreMirValidationResult baseline = SafeCoreMirValidation.Validate(mir,
new SafeCoreMirValidationOptions { MaximumOperations = 100_000 });
AssertEx.True(baseline.IsSuccessful, "The budget fixture must be structurally valid.");
// The fixture consumes a bounded, deterministic validation/correlation
// prefix. End exactly at the hand-off boundary so the adapter must
// reject an exhausted shared budget rather than inventing one step.
int budget = checked(baseline.OperationsUsed + 4);
SafeCoreMirOwnershipResult result = SafeCoreMirOwnershipAdapter.Analyze(mir, ownership,
new SafeCoreMirOwnershipOptions { MaximumOperations = budget });
AssertEx.True(result.IsTruncated, "The combined adapter must report exhaustion of its shared operation budget.");
AssertEx.True(result.Ownership is null || result.Ownership.IsTruncated,
"The remaining ownership budget must be bounded after validation and correlation work.");
AssertEx.True(result.Diagnostics.Any(diagnostic =>
diagnostic.Code == SafeCoreMirOwnershipAdapter.LimitReached),
"Exhausting the shared budget must retain the stable limit diagnostic.");
return Task.CompletedTask;
}
}