Skip to content

Commit d6be6a2

Browse files
Harden NativeAOT safety (#5089)
1 parent 124cb9b commit d6be6a2

8 files changed

Lines changed: 58 additions & 251 deletions

File tree

‎.github/workflows/dotnet-test.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ jobs:
101101
shell: pwsh
102102
run: |
103103
dotnet build-server shutdown
104+
dotnet restore UniGetUI.Avalonia/UniGetUI.Avalonia.csproj `
105+
--runtime win-x64 `
106+
/p:Configuration=Release `
107+
/p:PublishAot=true `
108+
/p:SelfContained=true
104109
dotnet publish UniGetUI.Avalonia/UniGetUI.Avalonia.csproj `
105110
--no-restore `
106111
--configuration Release `
@@ -113,3 +118,6 @@ jobs:
113118
/p:TrimmerSingleWarn=false `
114119
/p:UseSharedCompilation=false `
115120
--verbosity minimal
121+
if (-not (Test-Path "${{ runner.temp }}\unigetui-nativeaot-publish\UniGetUI.exe")) {
122+
throw "NativeAOT publish did not produce UniGetUI.exe."
123+
}

‎AGENTS.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ dotnet publish src/UniGetUI.Avalonia/UniGetUI.Avalonia.csproj /p:Configuration=R
5454
- Self-contained, publish-trimmed (partial), Windows App SDK self-contained
5555
- Tests use **xUnit** (`[Fact]`, `Assert.*`)
5656

57+
## NativeAOT and Trim Safety
58+
59+
Release packages are self-contained, fully trimmed NativeAOT binaries on every supported RID. Treat NativeAOT safety as a non-negotiable production requirement: changes must work without runtime-generated code or metadata that the trimmer cannot prove is required.
60+
61+
- Consider every `IL2xxx` trim warning and `IL3xxx` AOT warning a defect. Do not blanket-suppress warnings or use a broad `RequiresUnreferencedCode`, `RequiresDynamicCode`, `DynamicDependency`, `DynamicallyAccessedMembers`, linker descriptor, or root assembly as a workaround. An exception must be narrowly scoped, explain the concrete runtime requirement, and have a publish-path test that proves it is safe.
62+
- Use `JsonSerializerContext`/`JsonTypeInfo<T>` source-generated metadata for every production JSON serialization or deserialization path, including generic helpers, HTTP payloads, settings, IPC, telemetry, and persisted state. Do not call reflection-based serializer overloads or retain a reflection fallback for unknown application types. Add the closed type to a context and test the persisted or transported path.
63+
- Do not introduce production reflection-driven activation or discovery (`Assembly.Load`, `Type.GetType`, `Activator`, member lookup/invocation), runtime generic construction, expression compilation, runtime code generation, or dynamically loaded plugins. Prefer explicit registrations, direct constructors, generated metadata, and compiled XAML bindings. If a framework-owned dynamic boundary cannot be removed, isolate it and add published-NativeAOT runtime coverage.
64+
- New shipping COM interop must use source-generated interfaces and wrappers (`GeneratedComInterface`, `StrategyBasedComWrappers`) or raw ABI calls; do not add `ComImport`, `CoClass`, RCW activation, or legacy marshal-to-object APIs to a NativeAOT path. Define the ABI precisely, balance COM reference ownership, and test both x64 and arm64 where applicable.
65+
- Prefer `LibraryImport` for new P/Invokes. Existing `DllImport` declarations are acceptable only when their marshalling is supported by NativeAOT; keep platform guards, calling conventions, ownership, callback lifetimes, and x64/arm64 struct layouts explicit and covered.
66+
- Audit all new or changed startup, XAML/view-loading, dependency-injection, serialization, COM/WinRT, P/Invoke, and package-manager code against the publish output, not only a Debug build. Run the relevant NativeAOT publish profile, for example:
67+
68+
```shell
69+
dotnet publish src/UniGetUI.Avalonia/UniGetUI.Avalonia.csproj --configuration Release --runtime win-x64 --self-contained true /p:Platform=x64 /p:PublishProfile=Win-x64-NativeAot
70+
```
71+
72+
- When modifying a shared or portable path, retain the same safety guarantees for all checked-in NativeAOT publish profiles: `win-x64`, `win-arm64`, `linux-x64`, `linux-arm64`, `osx-x64`, and `osx-arm64`. Record unsupported runtime-matrix coverage as a gap, never as a pass.
73+
5774
## Avalonia DevTools (Developer-Only)
5875

5976
Use these rules when changing Avalonia diagnostics/devtools behavior:

‎src/UniGetUI.Core.Settings.Tests/SettingsTest.cs‎

Lines changed: 2 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,7 @@ string[] ls3Array
297297
Assert.False(Settings.RemoveFromList(SettingName, ls2[0]));
298298
Assert.False(Settings.ListContains(SettingName, ls2[0]));
299299

300-
Settings.SetList(SettingName, ls3);
301-
Assert.Equal(ls3.Count, Settings.GetList<SerializableTest>(SettingName)?.Count);
302-
Assert.Equal(
303-
ls3[1].sub.sub,
304-
Settings.GetListItem<SerializableTest>(SettingName, 1)?.sub.sub
305-
);
306-
Assert.True(Settings.RemoveFromList(SettingName, ls3[0]));
307-
Assert.False(Settings.RemoveFromList(SettingName, ls3[0]));
308-
Assert.Equal(
309-
ls3[1].sub.sub,
310-
Settings.GetListItem<SerializableTest>(SettingName, 0)?.sub.sub
311-
);
300+
Assert.Throws<InvalidOperationException>(() => Settings.SetList(SettingName, ls3));
312301
Settings.ClearList(SettingName); // Cleanup
313302
Assert.Empty(
314303
Settings.GetList<object>(SettingName)
@@ -383,157 +372,7 @@ string[] strArray
383372

384373
Settings.SetDictionaryItem(SettingName, "key", 12);
385374
Assert.Equal(12, Settings.GetDictionaryItem<string, int>(SettingName, "key"));
386-
Settings.SetDictionary(SettingName, test);
387-
Assert.Equal(
388-
JsonSerializer.Serialize(test, Settings.SerializationOptions),
389-
File.ReadAllText(
390-
Path.Join(
391-
CoreData.UniGetUIUserConfigurationDirectory,
392-
$"{Settings.ResolveKey(SettingName)}.json"
393-
)
394-
)
395-
);
396-
Assert.Equal(
397-
test[keyArray[0]]?.sub.count,
398-
Settings
399-
.GetDictionary<string, SerializableTest?>(SettingName)
400-
?[keyArray[0]]?.sub.count
401-
);
402-
Assert.Equal(
403-
test[keyArray[1]]?.sub.count,
404-
Settings
405-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[1])
406-
?.sub.count
407-
);
408-
Settings.SetDictionaryItem(SettingName, keyArray[0], test[keyArray[1]]);
409-
Assert.Equal(
410-
test[keyArray[1]]?.sub.count,
411-
Settings
412-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[0])
413-
?.sub.count
414-
);
415-
Assert.NotEqual(
416-
test[keyArray[0]]?.sub.count,
417-
Settings
418-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[0])
419-
?.sub.count
420-
);
421-
Assert.Equal(
422-
test[keyArray[1]]?.sub.count,
423-
Settings
424-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[1])
425-
?.sub.count
426-
);
427-
Assert.Equal(
428-
test[keyArray[1]]?.count,
429-
Settings
430-
.SetDictionaryItem(
431-
SettingName,
432-
keyArray[0],
433-
new SerializableTest(
434-
"this is not test data",
435-
-12000,
436-
new SerializableTestSub("this sub is not test data", -13000)
437-
)
438-
)
439-
?.count
440-
);
441-
Assert.Equal(
442-
-12000,
443-
Settings
444-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[0])
445-
?.count
446-
);
447-
Assert.Equal(
448-
"this is not test data",
449-
Settings
450-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[0])
451-
?.title
452-
);
453-
Assert.Equal(
454-
-13000,
455-
Settings
456-
.GetDictionaryItem<string, SerializableTest?>(SettingName, keyArray[0])
457-
?.sub.count
458-
);
459-
Settings.SetDictionaryItem(
460-
SettingName,
461-
"this is not a test data key",
462-
test[keyArray[0]]
463-
);
464-
Assert.Equal(
465-
test[keyArray[0]]?.title,
466-
Settings
467-
.GetDictionaryItem<string, SerializableTest?>(
468-
SettingName,
469-
"this is not a test data key"
470-
)
471-
?.title
472-
);
473-
Assert.Equal(
474-
test[keyArray[0]]?.sub.count,
475-
Settings
476-
.GetDictionaryItem<string, SerializableTest?>(
477-
SettingName,
478-
"this is not a test data key"
479-
)
480-
?.sub.count
481-
);
482-
Assert.True(
483-
Settings.DictionaryContainsKey<string, SerializableTest?>(
484-
SettingName,
485-
"this is not a test data key"
486-
)
487-
);
488-
Assert.True(
489-
Settings.DictionaryContainsValue<string, SerializableTest?>(
490-
SettingName,
491-
test[keyArray[0]]
492-
)
493-
);
494-
Assert.NotNull(
495-
Settings.RemoveDictionaryKey<string, SerializableTest?>(
496-
SettingName,
497-
"this is not a test data key"
498-
)
499-
);
500-
Assert.Null(
501-
Settings.RemoveDictionaryKey<string, SerializableTest?>(
502-
SettingName,
503-
"this is not a test data key"
504-
)
505-
);
506-
Assert.False(
507-
Settings.DictionaryContainsKey<string, SerializableTest?>(
508-
SettingName,
509-
"this is not a test data key"
510-
)
511-
);
512-
Assert.False(
513-
Settings.DictionaryContainsValue<string, SerializableTest?>(
514-
SettingName,
515-
test[keyArray[0]]
516-
)
517-
);
518-
Assert.True(
519-
Settings.DictionaryContainsValue<string, SerializableTest?>(
520-
SettingName,
521-
test[keyArray[2]]
522-
)
523-
);
524-
525-
Assert.Equal(
526-
JsonSerializer.Serialize(
527-
Settings.GetDictionary<string, SerializableTest>(SettingName),
528-
Settings.SerializationOptions
529-
),
530-
File.ReadAllText(
531-
Path.Join(
532-
CoreData.UniGetUIUserConfigurationDirectory,
533-
$"{Settings.ResolveKey(SettingName)}.json"
534-
)
535-
)
536-
);
375+
Assert.Throws<InvalidOperationException>(() => Settings.SetDictionary(SettingName, test));
537376

538377
Settings.ClearDictionary(SettingName); // Cleanup
539378
Assert.Empty(

‎src/UniGetUI.Core.Settings/SettingsEngine_Dictionaries.cs‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ private static readonly ConcurrentDictionary<
7777
);
7878
return value;
7979
}
80+
catch (InvalidOperationException)
81+
{
82+
throw;
83+
}
8084
catch (Exception ex)
8185
{
8286
Logger.Error($"Could not load dictionary name {setting}");
@@ -99,18 +103,22 @@ Dictionary<KeyT, ValueT> value
99103
where KeyT : notnull
100104
{
101105
string setting = ResolveKey(settingsKey);
102-
_dictionarySettings[settingsKey] = value.ToDictionary(
103-
kvp => (object)kvp.Key,
104-
kvp => (object?)kvp.Value
105-
);
106-
107106
var file = Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{setting}.json");
108107
try
109108
{
110109
if (value.Count != 0)
111110
File.WriteAllText(file, SettingsJson.SerializeDictionary(value));
112111
else if (File.Exists(file))
113112
File.Delete(file);
113+
114+
_dictionarySettings[settingsKey] = value.ToDictionary(
115+
kvp => (object)kvp.Key,
116+
kvp => (object?)kvp.Value
117+
);
118+
}
119+
catch (InvalidOperationException)
120+
{
121+
throw;
114122
}
115123
catch (Exception e)
116124
{

‎src/UniGetUI.Core.Settings/SettingsEngine_Lists.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public static partial class Settings
6262
listSettings[setting] = value.Cast<object>().ToList();
6363
return value;
6464
}
65+
catch (InvalidOperationException)
66+
{
67+
throw;
68+
}
6569
catch (Exception ex)
6670
{
6771
Logger.Error($"Could not load list {setting} from settings");
@@ -78,14 +82,19 @@ public static partial class Settings
7882

7983
public static void SetList<T>(string setting, List<T> value)
8084
{
81-
listSettings[setting] = value.Cast<object>().ToList();
8285
var file = Path.Join(CoreData.UniGetUIUserConfigurationDirectory, $"{setting}.json");
8386
try
8487
{
8588
if (value.Count != 0)
8689
File.WriteAllText(file, SettingsJson.SerializeList(value));
8790
else if (File.Exists(file))
8891
File.Delete(file);
92+
93+
listSettings[setting] = value.Cast<object>().ToList();
94+
}
95+
catch (InvalidOperationException)
96+
{
97+
throw;
8998
}
9099
catch (Exception e)
91100
{

0 commit comments

Comments
 (0)