From efa25b253d5c5456cc6fa1fec95271a8c6c7ee02 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Tue, 12 Jul 2022 01:04:49 +0300 Subject: [PATCH 1/2] Mark `Assembly.GetCallingAssembly` with `[RequiresDynamicCode]`. --- .../src/System/Reflection/Assembly.CoreCLR.cs | 2 ++ .../src/System/Reflection/Assembly.NativeAot.cs | 4 +++- src/libraries/System.Runtime/ref/System.Runtime.cs | 1 + .../src/System/Reflection/Assembly.Mono.cs | 2 ++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs index 970684d6330db..5cc72842e017a 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -71,6 +72,7 @@ public static Assembly GetExecutingAssembly() } [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod + [RequiresDynamicCode("Assembly.GetCallingAssembly is not supported in AOT environments.")] public static Assembly GetCallingAssembly() { // LookForMyCallersCaller is not guaranteed to return the correct stack frame diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs index fd1e3e899fd7a..29d97390e27f7 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Configuration.Assemblies; +using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; using System.IO; @@ -16,7 +17,8 @@ public abstract partial class Assembly : ICustomAttributeProvider, ISerializable [System.Runtime.CompilerServices.Intrinsic] public static Assembly GetExecutingAssembly() { throw NotImplemented.ByDesign; } //Implemented by toolchain. - + + [RequiresDynamicCode("Assembly.GetCallingAssembly is not supported in AOT environments.")] public static Assembly GetCallingAssembly() { if (AppContext.TryGetSwitch("Switch.System.Reflection.Assembly.SimulatedCallingAssembly", out bool isSimulated) && isSimulated) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 07abb8a23199f..7dbfef6240064 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -10731,6 +10731,7 @@ public virtual event System.Reflection.ModuleResolveEventHandler? ModuleResolve public static string CreateQualifiedName(string? assemblyName, string? typeName) { throw null; } public override bool Equals(object? o) { throw null; } public static System.Reflection.Assembly? GetAssembly(System.Type type) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Assembly.GetCallingAssembly is not supported in AOT environments.")] public static System.Reflection.Assembly GetCallingAssembly() { throw null; } public virtual object[] GetCustomAttributes(bool inherit) { throw null; } public virtual object[] GetCustomAttributes(System.Type attributeType, bool inherit) { throw null; } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Assembly.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Assembly.Mono.cs index ba531f8083f1e..826d4f35d4c85 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Assembly.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Assembly.Mono.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -42,6 +43,7 @@ public static Assembly GetExecutingAssembly() [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMark); + [RequiresDynamicCode("Assembly.GetCallingAssembly is not supported in AOT environments.")] [MethodImplAttribute(MethodImplOptions.InternalCall)] public static extern Assembly GetCallingAssembly(); From 31b5aac1fc727fa7223d26ea9eeb92100addc674 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 11 Jul 2022 23:33:21 +0300 Subject: [PATCH 2/2] Mark all APIs that call `Assembly.GetCallingAssembly` with `[RequiresDynamicCode]`. --- .../src/System/Reflection/Assembly.NativeAot.cs | 4 ++-- .../ref/Microsoft.VisualBasic.Core.cs | 3 +++ .../src/Microsoft/VisualBasic/CompilerServices/ProjectData.vb | 2 ++ .../src/Microsoft/VisualBasic/ErrObject.vb | 2 ++ .../src/Microsoft/VisualBasic/FileSystem.vb | 1 + 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs index 29d97390e27f7..3ee40bdd2a5c0 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs @@ -17,7 +17,7 @@ public abstract partial class Assembly : ICustomAttributeProvider, ISerializable [System.Runtime.CompilerServices.Intrinsic] public static Assembly GetExecutingAssembly() { throw NotImplemented.ByDesign; } //Implemented by toolchain. - + [RequiresDynamicCode("Assembly.GetCallingAssembly is not supported in AOT environments.")] public static Assembly GetCallingAssembly() { @@ -56,5 +56,5 @@ public static Assembly LoadWithPartialName(string partialName) return null; } } - } + } } diff --git a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs index c1c8a764666de..224023fa8bf86 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs +++ b/src/libraries/Microsoft.VisualBasic.Core/ref/Microsoft.VisualBasic.Core.cs @@ -316,6 +316,7 @@ internal ErrObject() { } public string Source { get { throw null; } set { } } public void Clear() { } public System.Exception? GetException() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("ErrObject.Raise is not supported in AOT environments. Throw exceptions with the Throw keyword instead.")] public void Raise(int Number, object? Source = null, object? Description = null, object? HelpFile = null, object? HelpContext = null) { } } [System.FlagsAttribute] @@ -329,6 +330,7 @@ public enum FileAttribute Directory = 16, Archive = 32, } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The FileSystem module is not supported in AOT environments. Use members of the System.IO namespace instead.")] [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] public sealed partial class FileSystem { @@ -1146,6 +1148,7 @@ public sealed partial class ProjectData internal ProjectData() { } public static void ClearProjectError() { } public static System.Exception CreateProjectError(int hr) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("ProjectData.EndApp is not supported in AOT environments. Use System.Environment.Exit(0) instead.")] public static void EndApp() { } public static void SetProjectError(System.Exception? ex) { } public static void SetProjectError(System.Exception? ex, int lErl) { } diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ProjectData.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ProjectData.vb index afed7d21fb0c7..5620bf25a2028 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ProjectData.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/CompilerServices/ProjectData.vb @@ -2,6 +2,7 @@ ' The .NET Foundation licenses this file to you under the MIT license. Imports System +Imports System.Diagnostics.CodeAnalysis Namespace Global.Microsoft.VisualBasic.CompilerServices @@ -177,6 +178,7 @@ Namespace Global.Microsoft.VisualBasic.CompilerServices Err.Clear() End Sub + Public Shared Sub EndApp() FileSystem.CloseAllFiles(System.Reflection.Assembly.GetCallingAssembly()) System.Environment.Exit(0) 'System.Environment.Exit will cause finalizers to be run at shutdown diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/ErrObject.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/ErrObject.vb index 2c6b57998348f..5f8cec45ac497 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/ErrObject.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/ErrObject.vb @@ -5,6 +5,7 @@ Imports Microsoft.VisualBasic.CompilerServices Imports Microsoft.VisualBasic.CompilerServices.Utils Imports System +Imports System.Diagnostics.CodeAnalysis Imports System.Runtime.InteropServices Namespace Microsoft.VisualBasic @@ -269,6 +270,7 @@ Namespace Microsoft.VisualBasic ''' If not supplied, we try to look one up based on the error code being raised ''' ''' + Public Sub Raise(ByVal Number As Integer, Optional ByVal Source As Object = Nothing, Optional ByVal Description As Object = Nothing, diff --git a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/FileSystem.vb b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/FileSystem.vb index 6cfdf1cb09944..b96a170f223a9 100644 --- a/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/FileSystem.vb +++ b/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/FileSystem.vb @@ -16,6 +16,7 @@ Imports Microsoft.VisualBasic.CompilerServices.Utils Namespace Microsoft.VisualBasic + Public Module FileSystem Private Const ERROR_FILE_NOT_FOUND As Integer = 2