Repository navigation
Expand file tree
/
Copy pathSafeCoreCompilationTests.cs
More file actions
419 lines (393 loc) · 21.9 KB
/
Copy pathSafeCoreCompilationTests.cs
File metadata and controls
419 lines (393 loc) · 21.9 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
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Globalization;
using System.Runtime.Loader;
using System.Diagnostics.CodeAnalysis;
using RustSharp.CodeGen.IL;
using RustSharp.Compiler;
using RustSharp.Semantics;
using RustSharp.Syntax;
namespace RustSharp.Tests;
internal static class SafeCoreCompilationTests
{
private const CompilationProfile Profile = CompilationProfile.SafeCorePrimitives;
public static IReadOnlyList<TestCase> All { get; } =
[
new("safe-core runs modules calls locals and typed branches", RunsCoreAsync),
new("safe-core preserves short circuit side effects", RunsShortCircuitAsync),
new("safe-core returns from operands with no residual IL stack", RunsEarlyReturnsAsync),
new("safe-core supports integer forms and parameter mutation", RunsIntegersAsync),
new("safe-core preserves shadowing and recursion", RunsShadowingAsync),
new("safe-core diagnoses invalid types and immutable assignments", RejectsInvalidTypesAsync),
new("safe-core rejects unsupported semantics explicitly", RejectsUnsupportedAsync),
new("safe-core rejects input before writing artifacts", RejectsBeforeOutputAsync),
new("safe-core emits deterministic PE and source-mapped PDB", EmitsMetadataAsync),
new("safe-core respects cancellation and local limits", RespectsLimitsAsync),
new("safe-core checked overflow fails execution", ChecksOverflowAsync),
new("CLR LIR validates binary operands and argument indices", ValidatesNewInstructionsAsync),
new("safe-core integer display is independent of culture", DisplaysInvariantlyAsync),
new("P1-09 primitive emission retains mandatory MIR ownership and cleanup evidence", PrimitiveMirRouteAsync),
new("P1-09 primitive route preserves rejection before output", PrimitiveMirRejectionAsync),
];
private static async Task PrimitiveMirRouteAsync()
{
const string source = "pub fn add(value: i32) -> i32 { value + 1 } " +
"fn main() { let value = add(41); println!(\"{}\", value); }";
string directory = NewDirectory();
try
{
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(15));
string sourcePath = Path.Combine(directory, "primitive-route.rs");
string output = Path.Combine(directory, "program.dll");
CompilationResult checkedResult = CompilerDriver.Check(source, sourcePath, Profile, deadline.Token);
AssertEx.True(checkedResult.Success && checkedResult.Output is null,
"The primitive check must accept the source without publishing artifacts.");
CompilationResult compiled = CompilerDriver.Compile(source, sourcePath, output,
"PrimitiveMirRoute", Profile, deadline.Token);
AssertEx.True(compiled.Success, string.Join("; ", compiled.Diagnostics));
RustSharpMetadataImportResult imported = RustSharpMetadataConsumer.ReadAssembly(output,
SafeCoreTypeChecking.Profile, ["crate::add"]);
AssertEx.True(imported.IsSuccessful, string.Join("; ", imported.Diagnostics));
RustSharpMetadataDocument metadata = imported.Document!;
AssertEx.True(metadata.MirSnapshot is { Length: > 0 } && metadata.CleanupSnapshot is { Length: > 0 },
"Primitive PE metadata must carry validated MIR and cleanup evidence.");
AssertEx.Equal(2, metadata.Ownership.Length,
"Both source bodies must retain mandatory ownership evidence.");
AssertEx.True(metadata.Ownership.All(static function => function.PanicStrategy == "unwind"),
"Primitive source bodies must retain the declared unwind policy.");
RustSharpMetadataFunction exported = metadata.Functions.Single(static function =>
function.SourceQualifiedName == "crate::add");
AssertEx.True(exported.IsPublic && exported.Signature == "I32->I32",
"The primitive v1 public source identity and scalar signature must remain stable.");
RustSharpMetadataCallContract contract = metadata.CallContracts.Single(static value => value.FunctionId == "crate::add");
AssertEx.Equal(RustSharpMetadataCallContract.ScalarSchema, contract.Schema!);
AssertEx.True(contract.ParameterContracts!.SequenceEqual(["copy"]) && contract.ReturnContract == "copy",
"The scalar import ABI must retain every Copy parameter and return term.");
BoundedProcessResult run = await new BoundedProcessRunner().RunAsync(
new("dotnet", [output], directory, TimeSpan.FromSeconds(5)), deadline.Token).ConfigureAwait(false);
AssertEx.True(run.Succeeded && !run.ProcessTreeCleanupIncomplete && !run.OutputTruncated, run.StandardError);
AssertEx.Equal("42\n", run.StandardOutput.Replace("\r\n", "\n", StringComparison.Ordinal));
}
finally { DeleteOwnedDirectory(directory); }
}
private static Task PrimitiveMirRejectionAsync()
{
string directory = NewDirectory();
try
{
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(10));
(string Source, string Code)[] cases =
[
("fn main() { let value = (1, 2); }", "RST1001"),
("fn main() { let value: bool = 1; }", "RST1002"),
("fn main() { let value = 1; value = 2; }", "RST1003"),
];
AssertEx.True(cases.Length <= 3, "The primitive route rejection denominator is fixed.");
for (int index = 0; index < cases.Length; index++)
{
deadline.Token.ThrowIfCancellationRequested();
(string source, string code) = cases[index];
string sourcePath = Path.Combine(directory, "rejected.rs");
string output = Path.Combine(directory, "case-" + index + ".dll");
CompilationResult checkedResult = CompilerDriver.Check(source, sourcePath, Profile, deadline.Token);
CompilationResult compiled = CompilerDriver.Compile(source, sourcePath, output,
profile: Profile, cancellationToken: deadline.Token);
AssertEx.False(checkedResult.Success || compiled.Success,
"Invalid primitive source must reject consistently in check and compile.");
AssertEx.Equal(code, checkedResult.Diagnostics[0].Code);
AssertEx.Equal(code, compiled.Diagnostics[0].Code);
}
AssertEx.Equal(0, Directory.GetFiles(directory).Length,
"Rejected source must not leave PE, PDB, runtime or transaction output.");
}
finally { DeleteOwnedDirectory(directory); }
return Task.CompletedTask;
}
private static Task RunsCoreAsync() => RunAsync("""
mod math { pub fn adjust(value: i32, enabled: bool) -> i32 {
if enabled { value * 2 + 1 } else { value - 1 }
} }
use math::adjust;
fn main() {
let mut value = 20;
value = adjust(value, true);
let answer: i32 = if value == 41 { value + 1 } else { 0 };
println!("Safe core on .NET");
println!("{}", answer);
println!("{}", answer == 42 && !(answer < 0));
}
""", "Safe core on .NET\n42\ntrue\n");
private static Task RunsShortCircuitAsync() => RunAsync("""
fn probe() -> bool { println!("probe"); true }
fn main() {
println!("{}", false && probe());
println!("{}", true || probe());
println!("{}", true && probe());
println!("{}", false || probe());
}
""", "false\ntrue\nprobe\ntrue\nprobe\ntrue\n");
private static Task RunsEarlyReturnsAsync() => RunAsync("""
fn sum(a: i32, b: i32) -> i32 { a + b }
fn left() -> i32 { println!("left"); 3 }
fn nested(flag: bool) -> i32 {
let result = left() + if flag { return 9; } else { 4 };
result
}
fn args() -> i32 { sum(left(), { return 11; }) }
fn exits(flag: bool) -> i32 { if flag { return 1; } else { return 2; } }
fn binding() -> i32 { let unused = { return 3; true }; }
fn operand() -> i32 { let unused = 1 + { return 4; 2 }; }
fn main() {
println!("{}", nested(true));
println!("{}", nested(false));
println!("{}", args());
println!("{}", exits(false));
println!("{}", binding());
println!("{}", operand());
if true { println!("branch"); }
println!("tail");
}
""", "left\n9\nleft\n7\nleft\n11\n2\n3\n4\nbranch\ntail\n");
private static Task RunsIntegersAsync() => RunAsync("""
fn inc(mut x: i32) -> i32 { x = x + 1; x }
fn main() {
println!("{}", inc(0b10_i32) + 0o10 + 0x10 + 1_0);
println!("{}", -2147483648);
println!("{}", !0i32);
println!("{}", -1 <= 0 && 5 >= 5 && 2 != 3);
}
""", "37\n-2147483648\n-1\ntrue\n");
private static Task RunsShadowingAsync() => RunAsync("""
fn fib(n: i32) -> i32 { if n < 2 { n } else { fib(n - 1) + fib(n - 2) } }
fn main() {
let value = 7;
let value = value + 1;
{ let value = true; println!("{}", value); }
println!("{}", value);
println!("{}", fib(8));
}
""", "true\n8\n21\n");
private static Task RejectsInvalidTypesAsync()
{
(string Source, string Code)[] cases =
[
("fn main() { let value: bool = 1; }", "RST1002"),
("fn main() { let x = 1; x = 2; }", "RST1003"),
("fn main() { let mut x = 1; x = true; }", "RST1002"),
("fn main() { if 1 {} }", "RST1002"),
("fn main() { let x = if true { 1 } else { false }; }", "RST1002"),
("fn main() { if true { 1 } }", "RST1002"),
("fn f() -> i32 { true } fn main() {}", "RST1002"),
("fn f(x: i32) {} fn main() { f(true); }", "RST1002"),
("fn f(x: i32) {} fn main() { f(); }", "RST1004"),
("fn main() -> i32 { 0 }", "RST1005"),
("fn main() { let x = 2147483648; }", "RST1006"),
("fn main() { let x = -2147483649; }", "RST1006"),
("fn main() { true + false; }", "RST1002"),
("fn main() { if true { 1 } println!(\"bad\"); }", "RST1002"),
("fn value() -> i32 { return 1; true } fn main() {}", "RST1002"),
("fn main() { let x = -( -2147483648); }", "RST1006"),
("fn main() { let x = (2147483647) + 1; }", "RST1006"),
];
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(15));
foreach (var item in cases) AssertRejected(item.Source, item.Code, deadline.Token);
return Task.CompletedTask;
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "This regression loads emitted IL in the untrimmed test host to vary CurrentCulture. Native AOT is verified in a separate publish probe.")]
private static Task DisplaysInvariantlyAsync()
{
const string source = "fn main() { println!(\"{}\", -42); }";
var context = new AssemblyLoadContext("safe-core-culture-" + Guid.NewGuid().ToString("N"), isCollectible: true);
CultureInfo previousCulture = CultureInfo.CurrentCulture;
TextWriter previousOutput = Console.Out;
using var output = new StringWriter(CultureInfo.InvariantCulture);
try
{
SafeCoreClrResult lir = Lower(source);
GeneratedAssembly first = ClrLirAssemblyEmitter.EmitProgram(lir, "Invariant", source, Path.GetFullPath("invariant.rs"), "invariant.pdb");
var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NegativeSign = "minus";
CultureInfo.CurrentCulture = culture;
GeneratedAssembly second = ClrLirAssemblyEmitter.EmitProgram(lir, "Invariant", source, Path.GetFullPath("invariant.rs"), "invariant.pdb");
AssertEx.True(first.PeImage.AsSpan().SequenceEqual(second.PeImage), "Emission must be independent of culture.");
using var stream = new MemoryStream(second.PeImage);
Action entry = context.LoadFromStream(stream).EntryPoint!.CreateDelegate<Action>();
Console.SetOut(output);
entry();
AssertEx.Equal("-42" + Environment.NewLine, output.ToString());
}
finally
{
Console.SetOut(previousOutput);
CultureInfo.CurrentCulture = previousCulture;
context.Unload();
}
return Task.CompletedTask;
}
private static Task RejectsUnsupportedAsync()
{
string[] sources =
[
"fn main() { let x: i32; }",
"fn main() { let x = 1; let r = &x; }",
"fn main() { let x = (1, 2); }",
"fn main() { let x = [1, 2]; }",
"fn main() { let x: u32 = 1; }",
"fn main() { let x = 1u32; }",
"fn main() { let x = 1.5; }",
"fn main() { let x = 4 / 2; }",
"fn main() { let mut x = 1; x += 1; }",
"fn f<T>(x: T) {} fn main() {}",
"struct Data { x: i32 } fn main() {}",
"#[cfg(any())] fn main() {}",
"fn main() { println!(\"value {}\", 1); }",
"fn main() { let x = (); }",
];
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(15));
foreach (string source in sources)
{
// The parser accepts attribute syntax, while name resolution
// explicitly rejects its unevaluated semantics with RSN1007.
AssertRejected(source, source.StartsWith("#[cfg", StringComparison.Ordinal)
? SafeCoreNameResolutionDiagnosticCodes.UnsupportedSyntax
: "RST1001", deadline.Token);
}
AssertRejected("fn main() { let x = +1; }", "RSP1002", deadline.Token);
return Task.CompletedTask;
}
private static Task RejectsBeforeOutputAsync()
{
string directory = NewDirectory();
try
{
string path = Path.Combine(directory, "rejected.dll");
CompilationResult result = CompilerDriver.Compile("fn main() { let x: bool = 1; }",
Path.Combine(directory, "rejected.rs"), path, profile: Profile);
AssertEx.False(result.Success, "A type error must fail compilation.");
AssertEx.Equal(0, Directory.GetFiles(directory).Length, "Rejected input must not create output or transaction files.");
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(10));
foreach (string source in new[]
{
"mod external; fn main() {}",
"const _: i32 = 1; fn main() {}",
"const VALUE: i32 = 1; fn main() { ::VALUE; }",
"fn main() { loop {} }",
"fn main() { #[local] let value = 1; }",
})
{
deadline.Token.ThrowIfCancellationRequested();
result = CompilerDriver.Compile(source, Path.Combine(directory, "rejected.rs"), path,
profile: Profile, cancellationToken: deadline.Token);
AssertEx.False(result.Success, "Unimplemented syntax must fail compilation: " + source);
AssertEx.True(result.Diagnostics.Any(static diagnostic =>
diagnostic.Code == SafeCoreNameResolutionDiagnosticCodes.UnsupportedSyntax),
"Compilation must expose RSN1007: " + source);
AssertEx.Equal(0, Directory.GetFiles(directory).Length,
"Semantic rejection must precede output and transaction file creation: " + source);
}
}
finally { DeleteOwnedDirectory(directory); }
return Task.CompletedTask;
}
private static Task EmitsMetadataAsync()
{
const string source = "fn helper(x: i32) -> i32 { x + 1 } fn main() { println!(\"{}\", helper(4)); }";
SafeCoreClrResult lir = Lower(source);
GeneratedAssembly first = ClrLirAssemblyEmitter.EmitProgram(lir, "SafeCoreMetadata", source,
Path.GetFullPath("metadata.rs"), "metadata.pdb");
GeneratedAssembly second = ClrLirAssemblyEmitter.EmitProgram(lir, "SafeCoreMetadata", source,
Path.GetFullPath("metadata.rs"), "metadata.pdb");
AssertEx.True(first.PeImage.AsSpan().SequenceEqual(second.PeImage), "PE must be deterministic.");
AssertEx.True(first.PdbImage.AsSpan().SequenceEqual(second.PdbImage), "PDB must be deterministic.");
using var pe = new PEReader(new MemoryStream(first.PeImage));
AssertEx.Equal(2, pe.GetMetadataReader().MethodDefinitions.Count);
using var pdb = MetadataReaderProvider.FromPortablePdbStream(new MemoryStream(first.PdbImage!));
MetadataReader reader = pdb.GetMetadataReader();
AssertEx.Equal(2, reader.MethodDebugInformation.Count);
foreach (MethodDebugInformationHandle handle in reader.MethodDebugInformation)
{
MethodDebugInformation method = reader.GetMethodDebugInformation(handle);
AssertEx.True(method.GetSequencePoints().Any(point => !point.IsHidden && point.StartLine == 1),
"Each method must map to its Rust source span.");
AssertEx.False(method.LocalSignature.IsNil, "Source mappings must carry the method local signature.");
}
return Task.CompletedTask;
}
private static Task RespectsLimitsAsync()
{
using var cancelled = new CancellationTokenSource();
cancelled.Cancel();
AssertEx.Throws<OperationCanceledException>(() => CompilerDriver.Check("fn main() {}", profile: Profile,
cancellationToken: cancelled.Token));
string source = "fn main() {" + string.Concat(Enumerable.Repeat("let x = 1;", 257)) + "}";
AssertRejected(source, "RST2001");
return Task.CompletedTask;
}
private static Task ChecksOverflowAsync() => RunAsync("""
fn add(x: i32) -> i32 { x + 1 }
fn main() { println!("{}", add(2147483647)); }
""", string.Empty, expectOverflow: true);
private static Task ValidatesNewInstructionsAsync()
{
var invalidArgument = new ClrLirMethod("Main", ClrLirType.Void, [], [],
[new ClrLirBlock("entry", [new ClrLirLoadArgument(0), new ClrLirReturn()])]);
AssertEx.False(invalidArgument.Validate().IsValid, "Out-of-range arguments must fail validation.");
var invalidOperation = new ClrLirMethod("Main", ClrLirType.Bool, [], [],
[new ClrLirBlock("entry", [new ClrLirLoadBoolean(true), new ClrLirLoadBoolean(false),
new ClrLirBinary(ClrLirBinaryOperator.AddChecked, ClrLirType.Bool), new ClrLirReturn()])]);
AssertEx.False(invalidOperation.Validate().IsValid, "Boolean arithmetic must fail validation.");
return Task.CompletedTask;
}
private static SafeCoreClrResult Lower(string source)
{
SafeCoreTypeCheckResult types = SafeCoreTypeChecking.Check(SafeCoreHirLowering.Lower(SafeCoreSyntax.Parse(source)));
AssertEx.True(types.IsSuccessful, string.Join("; ", types.Diagnostics));
SafeCoreClrResult lir = SafeCoreClrLowering.Lower(types.Program!);
AssertEx.True(lir.IsSuccessful, string.Join("; ", lir.Diagnostics));
return lir;
}
private static void AssertRejected(string source, string code, CancellationToken cancellationToken = default)
{
CompilationResult result = CompilerDriver.Check(source, "rejected.rs", Profile, cancellationToken);
AssertEx.False(result.Success, "Expected rejection: " + source);
AssertEx.Equal(code, result.Diagnostics[0].Code, source + ": " + string.Join("; ", result.Diagnostics));
AssertEx.True(result.Diagnostics[0].Span.Start >= 0 && result.Diagnostics[0].Span.End <= source.Length,
"Diagnostic span must refer to the source document.");
}
private static async Task RunAsync(string source, string expected, bool expectOverflow = false)
{
string directory = NewDirectory();
try
{
string path = Path.Combine(directory, "program.dll");
CompilationResult compiled = CompilerDriver.Compile(source, Path.Combine(directory, "program.rs"),
path, profile: Profile);
AssertEx.True(compiled.Success, string.Join("; ", compiled.Diagnostics));
using var deadline = new CancellationTokenSource(TimeSpan.FromSeconds(20));
BoundedProcessResult run = await new BoundedProcessRunner().RunAsync(
new("dotnet", [path], directory, TimeSpan.FromSeconds(15)), deadline.Token).ConfigureAwait(false);
AssertEx.False(run.ProcessTreeCleanupIncomplete, "Owned process cleanup must complete.");
if (expectOverflow)
{
AssertEx.False(run.Succeeded, "Checked overflow must fail execution.");
AssertEx.True(run.StandardError.Contains("OverflowException", StringComparison.Ordinal), run.StandardError);
}
else AssertEx.True(run.Succeeded, run.StandardError);
AssertEx.Equal(expected, run.StandardOutput.Replace("\r\n", "\n", StringComparison.Ordinal));
}
finally { DeleteOwnedDirectory(directory); }
}
private static string NewDirectory()
{
string path = Path.GetFullPath(Path.Combine("artifacts", "tests", "safe-core-" + Guid.NewGuid().ToString("N")));
Directory.CreateDirectory(path);
return path;
}
private static void DeleteOwnedDirectory(string path)
{
string root = Path.GetFullPath(Path.Combine("artifacts", "tests")) + Path.DirectorySeparatorChar;
AssertEx.True(Path.GetFullPath(path).StartsWith(root, StringComparison.Ordinal) &&
Path.GetFileName(path).StartsWith("safe-core-", StringComparison.Ordinal), "Cleanup must remain in the task-owned test directory.");
Directory.Delete(path, recursive: true);
}
}