Skip to content

Fix setting of DllImportResolver in crossgen2. - #167

Merged
erozenfeld merged 1 commit into
dotnet:masterfrom
erozenfeld:FixDllImportResolverSetting
Nov 26, 2019
Merged

erozenfeld merged 1 commit into
dotnet:masterfrom
erozenfeld:FixDllImportResolverSetting

Conversation

@erozenfeld

Copy link
Copy Markdown
Contributor

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.

@davidwrighton davidwrighton left a comment

Copy link
Copy Markdown
Member

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

erozenfeld commented Nov 22, 2019 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

@jkotas PTAL

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM otherwise

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed.

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@jkotas

jkotas commented Nov 25, 2019

Copy link
Copy Markdown
Member

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

erozenfeld commented Nov 25, 2019 •

Copy link
Copy Markdown
Contributor Author

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.

@AntonLapounov AntonLapounov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:shipit:

@erozenfeld

Copy link
Copy Markdown
Contributor 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