Skip to content

Commit 4f9ae42

Browse files
authored
Fix setting of DllImportResolver in crossgen2. (#167)
After change dotnet/coreclr#27068 we are creating multiple instances of CorInfoImpl. That broke the scenario when jitpath is set: we are calling NativeLibrary.SetDllImportResolver more than once, which results in `System.InvalidOperationException: A resolver is already set for the assembly.` This fix ensures that we call NativeLibrary.SetDllImportResolver at most once. This change also ensures that we set the resolver before attempting to load the jit. That fixes the --jitpath scenario on Linux.
1 parent 6fe9120 commit 4f9ae42

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ private enum ImageFileMachine
5858

5959
private ExceptionDispatchInfo _lastException;
6060

61-
private static bool s_jitRegistered = RegisterJITModule();
62-
6361
[DllImport(JitLibrary)]
6462
private extern static IntPtr PAL_RegisterModule([MarshalAs(UnmanagedType.LPUTF8Str)] string moduleName);
6563

@@ -113,50 +111,41 @@ private IntPtr AllocException(Exception ex)
113111
[DllImport(JitSupportLibrary)]
114112
private extern static char* GetExceptionMessage(IntPtr obj);
115113

116-
private JitConfigProvider _jitConfig;
114+
private static JitConfigProvider s_jitConfig;
117115

118116
private readonly UnboxingMethodDescFactory _unboxingThunkFactory;
119117

120-
private static bool RegisterJITModule()
118+
public static void RegisterJITModule(JitConfigProvider jitConfig)
121119
{
122-
if ((Environment.OSVersion.Platform == PlatformID.Unix) || (Environment.OSVersion.Platform == PlatformID.MacOSX))
120+
s_jitConfig = jitConfig;
121+
if (jitConfig.JitPath != null)
123122
{
124-
return PAL_RegisterModule("libclrjitilc.so") != (IntPtr)1;
123+
NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, JitLibraryResolver);
125124
}
126-
else
125+
126+
if (Environment.OSVersion.Platform == PlatformID.Unix)
127127
{
128-
return true;
128+
// TODO: The PAL_RegisterModule export should be removed from the JIT
129+
// and the call to PAL_InitializeDLL should be moved to jitStartup.
130+
// https://github.com/dotnet/coreclr/issues/27941
131+
PAL_RegisterModule("libclrjitilc.so");
129132
}
133+
134+
jitStartup(GetJitHost(jitConfig.UnmanagedInstance));
130135
}
131136

132-
private IntPtr JitLibraryResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
137+
private static IntPtr JitLibraryResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
133138
{
134139
IntPtr libHandle = IntPtr.Zero;
135140
if (libraryName == JitLibrary)
136141
{
137-
libHandle = NativeLibrary.Load(_jitConfig.JitPath, assembly, searchPath);
142+
libHandle = NativeLibrary.Load(s_jitConfig.JitPath, assembly, searchPath);
138143
}
139144
return libHandle;
140145
}
141146

142-
public CorInfoImpl(JitConfigProvider jitConfig)
147+
public CorInfoImpl()
143148
{
144-
//
145-
// Global initialization
146-
//
147-
_jitConfig = jitConfig;
148-
if (!s_jitRegistered)
149-
{
150-
throw new IOException("Failed to register JIT");
151-
}
152-
153-
if (_jitConfig.JitPath != null)
154-
{
155-
NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, JitLibraryResolver);
156-
}
157-
158-
jitStartup(GetJitHost(_jitConfig.UnmanagedInstance));
159-
160149
_jit = getJit();
161150
if (_jit == IntPtr.Zero)
162151
{
@@ -264,7 +253,7 @@ private void PublishCode()
264253
var relocs = _relocs.ToArray();
265254
Array.Sort(relocs, (x, y) => (x.Offset - y.Offset));
266255

267-
int alignment = _jitConfig.HasFlag(CorJitFlag.CORJIT_FLAG_SIZE_OPT) ?
256+
int alignment = s_jitConfig.HasFlag(CorJitFlag.CORJIT_FLAG_SIZE_OPT) ?
268257
_compilation.NodeFactory.Target.MinimumFunctionAlignment :
269258
_compilation.NodeFactory.Target.OptimumFunctionAlignment;
270259

@@ -3032,7 +3021,7 @@ private bool isMethodDefinedInCoreLib()
30323021
private uint getJitFlags(ref CORJIT_FLAGS flags, uint sizeInBytes)
30333022
{
30343023
// Read the user-defined configuration options.
3035-
foreach (var flag in _jitConfig.Flags)
3024+
foreach (var flag in s_jitConfig.Flags)
30363025
flags.Set(flag);
30373026

30383027
// Set the rest of the flags that don't make sense to expose publically.

src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ internal ReadyToRunCodegenCompilation(
223223
_jitConfigProvider = configProvider;
224224

225225
_inputFilePath = inputFilePath;
226+
227+
CorInfoImpl.RegisterJITModule(configProvider);
226228
}
227229

228230
public override void Compile(string outputFile)
@@ -282,7 +284,7 @@ protected override void ComputeDependencyNodeDependencies(List<DependencyNodeCor
282284
{
283285
using (PerfEventSource.StartStopEvents.JitMethodEvents())
284286
{
285-
CorInfoImpl corInfoImpl = cwt.GetValue(Thread.CurrentThread, thread => new CorInfoImpl(this, _jitConfigProvider));
287+
CorInfoImpl corInfoImpl = cwt.GetValue(Thread.CurrentThread, thread => new CorInfoImpl(this));
286288
corInfoImpl.CompileMethod(methodCodeNodeNeedingCode);
287289
}
288290
}

src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ unsafe partial class CorInfoImpl
140140
private OffsetMapping[] _debugLocInfos;
141141
private NativeVarInfo[] _debugVarInfos;
142142

143-
public CorInfoImpl(ReadyToRunCodegenCompilation compilation, JitConfigProvider jitConfig)
144-
: this(jitConfig)
143+
public CorInfoImpl(ReadyToRunCodegenCompilation compilation)
144+
: this()
145145
{
146146
_compilation = compilation;
147147
}

0 commit comments

Comments
 (0)