Skip to content

Commit

Permalink
Allow hiding UnmanagedCallersOnly exports (dotnet#110152)
Browse files Browse the repository at this point in the history
Should fix dotnet#109341. We currently rely on the linkerscript not exporting these, now it's a per-assembly compiler switch.
  • Loading branch information
MichalStrehovsky authored Nov 26, 2024
1 parent 89adcf9 commit 354ec46
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The .NET Foundation licenses this file to you under the MIT license.
</PropertyGroup>

<ItemGroup Condition="$(IlcSystemModule) == ''">
<UnmanagedEntryPointsAssembly Include="System.Private.CoreLib" />
<UnmanagedEntryPointsAssembly Include="System.Private.CoreLib,HIDDEN" />
<AutoInitializedAssemblies Include="System.Private.CoreLib" />
<AutoInitializedAssemblies Include="System.Private.StackTraceMetadata" Condition="$(StackTraceSupport) != 'false'" />
<AutoInitializedAssemblies Include="System.Private.TypeLoader" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,16 @@ public StructMarshallingDataNode StructMarshallingData(DefType type)
/// Returns alternative symbol name that object writer should produce for given symbols
/// in addition to the regular one.
/// </summary>
public string GetSymbolAlternateName(ISymbolNode node)
public string GetSymbolAlternateName(ISymbolNode node, out bool isHidden)
{
string value;
if (!NodeAliases.TryGetValue(node, out value))
if (!NodeAliases.TryGetValue(node, out var value))
{
isHidden = false;
return null;
return value;
}

isHidden = value.Hidden;
return value.Name;
}

public ArrayOfEmbeddedPointersNode<GCStaticsNode> GCStaticsRegion = new ArrayOfEmbeddedPointersNode<GCStaticsNode>(
Expand All @@ -1451,7 +1455,7 @@ public string GetSymbolAlternateName(ISymbolNode node)

public ReadyToRunHeaderNode ReadyToRunHeader;

public Dictionary<ISymbolNode, string> NodeAliases = new Dictionary<ISymbolNode, string>();
public Dictionary<ISymbolNode, (string Name, bool Hidden)> NodeAliases = new Dictionary<ISymbolNode, (string, bool)>();

protected internal TypeManagerIndirectionNode TypeManagerIndirection = new TypeManagerIndirectionNode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ICompilationRootProvider.AddCompilationRoots(IRootingServiceProvider rootPr
{
int isaFlags = HardwareIntrinsicHelpers.GetRuntimeRequiredIsaFlags(_isaSupport);
byte[] bytes = BitConverter.GetBytes(isaFlags);
rootProvider.RootReadOnlyDataBlob(bytes, 4, "ISA support flags", "g_requiredCpuFeatures");
rootProvider.RootReadOnlyDataBlob(bytes, 4, "ISA support flags", "g_requiredCpuFeatures", exportHidden: true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ExportsFileWriter(TypeSystemContext context, string exportsFile, string[]
}

public void AddExportedMethods(IEnumerable<EcmaMethod> methods)
=> _methods.AddRange(methods.Where(m => m.Module != _context.SystemModule));
=> _methods.AddRange(methods);

public void EmitExportedMethods()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ILCompiler
/// </summary>
public interface IRootingServiceProvider
{
void AddCompilationRoot(MethodDesc method, string reason, string exportName = null);
void AddCompilationRoot(MethodDesc method, string reason, string exportName = null, bool exportHidden = false);
void AddCompilationRoot(TypeDesc type, string reason);
void AddReflectionRoot(TypeDesc type, string reason);
void AddReflectionRoot(MethodDesc method, string reason);
Expand All @@ -19,7 +19,7 @@ public interface IRootingServiceProvider
void RootGCStaticBaseForType(TypeDesc type, string reason);
void RootNonGCStaticBaseForType(TypeDesc type, string reason);
void RootModuleMetadata(ModuleDesc module, string reason);
void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName);
void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName, bool exportHidden);
void RootDelegateMarshallingData(DefType type, string reason);
void RootStructMarshallingData(DefType type, string reason);
void AddCompilationRoot(object o, string reason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void EnsureMap(NodeFactory factory)

// Bodies that are visible from outside should not be folded because we don't know
// if they're address taken.
if (factory.GetSymbolAlternateName(body) != null)
if (factory.GetSymbolAlternateName(body, out _) != null)
continue;

var key = new MethodInternKey(body, factory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,14 @@ private void EmitObject(string objectFilePath, IReadOnlyCollection<DependencyNod
n == node ? currentSymbolName : GetMangledName(n),
n.Offset + thumbBit,
n.Offset == 0 && isMethod ? nodeContents.Data.Length : 0);
if (_nodeFactory.GetSymbolAlternateName(n) is string alternateName)
if (_nodeFactory.GetSymbolAlternateName(n, out bool isHidden) is string alternateName)
{
string alternateCName = ExternCName(alternateName);
sectionWriter.EmitSymbolDefinition(
alternateCName,
n.Offset + thumbBit,
n.Offset == 0 && isMethod ? nodeContents.Data.Length : 0,
global: true);
global: !isHidden);

if (n is IMethodNode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public RootingServiceProvider(NodeFactory factory, RootAdder rootAdder)
_rootAdder = rootAdder;
}

public void AddCompilationRoot(MethodDesc method, string reason, string exportName = null)
public void AddCompilationRoot(MethodDesc method, string reason, string exportName = null, bool exportHidden = false)
{
MethodDesc canonMethod = method.GetCanonMethodTarget(CanonicalFormKind.Specific);
IMethodNode methodEntryPoint = _factory.MethodEntrypoint(canonMethod);
Expand All @@ -32,7 +32,7 @@ public void AddCompilationRoot(MethodDesc method, string reason, string exportNa
if (exportName != null)
{
exportName = _factory.NameMangler.NodeMangler.ExternMethod(exportName, method);
_factory.NodeAliases.Add(methodEntryPoint, exportName);
_factory.NodeAliases.Add(methodEntryPoint, (exportName, exportHidden));
}

if (canonMethod != method && method.HasInstantiation)
Expand Down Expand Up @@ -140,12 +140,12 @@ public void RootModuleMetadata(ModuleDesc module, string reason)
}
}

public void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName)
public void RootReadOnlyDataBlob(byte[] data, int alignment, string reason, string exportName, bool exportHidden)
{
var blob = _factory.ReadOnlyDataBlob("__readonlydata_" + exportName, data, alignment);
_rootAdder(blob, reason);
exportName = _factory.NameMangler.NodeMangler.ExternVariable(exportName);
_factory.NodeAliases.Add(blob, exportName);
_factory.NodeAliases.Add(blob, (exportName, exportHidden));
}

public void RootDelegateMarshallingData(DefType type, string reason)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public class UnmanagedEntryPointsRootProvider : ICompilationRootProvider
{
private EcmaModule _module;

public UnmanagedEntryPointsRootProvider(EcmaModule module)
public UnmanagedEntryPointsRootProvider(EcmaModule module, bool hidden = false)
{
_module = module;
Hidden = hidden;
}

public bool Hidden { get; }

public IEnumerable<EcmaMethod> ExportedMethods
{
get
Expand Down Expand Up @@ -62,12 +65,12 @@ public void AddCompilationRoots(IRootingServiceProvider rootProvider)
if (ecmaMethod.IsUnmanagedCallersOnly)
{
string unmanagedCallersOnlyExportName = ecmaMethod.GetUnmanagedCallersOnlyExportName();
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Native callable", unmanagedCallersOnlyExportName);
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Native callable", unmanagedCallersOnlyExportName, Hidden);
}
else
{
string runtimeExportName = ecmaMethod.GetRuntimeExportName();
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Runtime export", runtimeExportName);
rootProvider.AddCompilationRoot((MethodDesc)ecmaMethod, "Runtime export", runtimeExportName, Hidden);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/coreclr/tools/aot/ILCompiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,19 @@ public int Run()
compilationRoots.Add(new Win32ResourcesRootProvider(module));
}

foreach (var unmanagedEntryPointsAssembly in Get(_command.UnmanagedEntryPointsAssemblies))
foreach (var unmanagedEntryPointsAssemblyValue in Get(_command.UnmanagedEntryPointsAssemblies))
{
const string hiddenSuffix = ",HIDDEN";
bool hidden = unmanagedEntryPointsAssemblyValue.EndsWith(hiddenSuffix, StringComparison.Ordinal);
string unmanagedEntryPointsAssembly = hidden ? unmanagedEntryPointsAssemblyValue[..^hiddenSuffix.Length] : unmanagedEntryPointsAssemblyValue;

if (typeSystemContext.InputFilePaths.ContainsKey(unmanagedEntryPointsAssembly))
{
// Skip adding UnmanagedEntryPointsRootProvider for modules that have been already registered as an input module
continue;
}
EcmaModule module = typeSystemContext.GetModuleForSimpleName(unmanagedEntryPointsAssembly);
compilationRoots.Add(new UnmanagedEntryPointsRootProvider(module));
compilationRoots.Add(new UnmanagedEntryPointsRootProvider(module, hidden));
}

foreach (var rdXmlFilePath in Get(_command.RdXmlFilePaths))
Expand Down Expand Up @@ -621,7 +625,7 @@ void RunScanner()
{
foreach (var compilationRoot in compilationRoots)
{
if (compilationRoot is UnmanagedEntryPointsRootProvider provider)
if (compilationRoot is UnmanagedEntryPointsRootProvider provider && !provider.Hidden)
defFileWriter.AddExportedMethods(provider.ExportedMethods);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/aot/ILCompiler/repro/repro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ReproResponseLines Include="-g" />
<ReproResponseLines Include="-O" Condition="'$(Optimize)' == 'true'" />
<ReproResponseLines Include="--dehydrate" />
<ReproResponseLines Include="--generateunmanagedentrypoints:System.Private.CoreLib" />
<ReproResponseLines Include="--generateunmanagedentrypoints:System.Private.CoreLib,HIDDEN" />
<ReproResponseLines Include="--initassembly:System.Private.CoreLib" />
<ReproResponseLines Include="--initassembly:System.Private.StackTraceMetadata" />
<ReproResponseLines Include="--initassembly:System.Private.TypeLoader" />
Expand Down

0 comments on commit 354ec46

Please sign in to comment.