Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting of DllImportResolver in crossgen2. #167

Merged
merged 1 commit into from
Nov 26, 2019
Merged

Fix setting of DllImportResolver in crossgen2. #167

merged 1 commit into from
Nov 26, 2019

Conversation

erozenfeld
Copy link
Member

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.

Copy link
Member

@davidwrighton davidwrighton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally dislike statics in crossgen2, but as the DllImport operation itself is logically static, I feel this is ok.

@erozenfeld
Copy link
Member Author

erozenfeld commented Nov 22, 2019

I pushed a change that ensures that the resolver is registered before we attempt to load the jit. I verified that with this change --jitpath option works correctly both on Windows and Linux.

@erozenfeld
Copy link
Member Author

@jkotas PTAL

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise

{
return true;
if (PAL_RegisterModule("libclrjitilc.so") == (IntPtr)1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (PAL_RegisterModule("libclrjitilc.so") == (IntPtr)1)
if (PAL_RegisterModule("libclrjitilc.so") == IntPtr.Zero)

As far as I can tell, PAL_RegisterModule returns 0 on failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed.

{
return true;
if (PAL_RegisterModule("libclrjitilc.so") == IntPtr.Zero)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PAL_RegisterModule does not use the DllImport resolver, so I think you have to pass the full path to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard/impossible to make it work 100% of time. It may be best to ignore the errors from PAL_RegisterModule.

The whole PAL_RegisterModule scheme is pretty broken. I would be best to not have it. The proper way to fix this would be to delete PAL_RegisterModule export from the JIT, and do the work that it does inside jitStartup instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the check for errors from PAL_RegisterModule. I think moving the call to PAL_InitializeDll to jitStartup should be a separate change as it affects all the callers of jitStartup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment and link to cleanup issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (_jitConfig.JitPath != null)
{
NativeLibrary.SetDllImportResolver(typeof(CorInfoImpl).Assembly, JitLibraryResolver);
}

jitStartup(GetJitHost(_jitConfig.UnmanagedInstance));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be insider the register method as well. It is not a good idea to call it multiple time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@jkotas
Copy link
Member

jkotas commented Nov 25, 2019

It would be nice if you can at least move the call to jitStartup into the Register function. jitStartup is not thread-safe and calling it on multiple threads in parallel can result into unpredictable behavior.

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.
@erozenfeld
Copy link
Member Author

erozenfeld commented Nov 25, 2019

It would be nice if you can at least move the call to jitStartup into the Register function. jitStartup is not thread-safe and calling it on multiple threads in parallel can result into unpredictable behavior.

Done.

Copy link
Member

@AntonLapounov AntonLapounov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@erozenfeld
Copy link
Member Author

@jkotas Are you ok with the current version?

@erozenfeld erozenfeld merged commit 4f9ae42 into dotnet:master Nov 26, 2019
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants