Skip to content

Commit d47081a

Browse files
author
Fadi Hanna
authoredNov 27, 2019
Fix EH handling in PInvoke stubs and remove workaround (#327)
* Fix EH handling in PInvoke stubs and remove workaround The fix in the JIT is to inline the raw PInvoke call if: 1) We are compiling a PInvoke IL Stub 2) We are compiling using the PInvoke helpers
1 parent a868078 commit d47081a

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed
 

‎src/coreclr/src/jit/importer.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -6393,6 +6393,13 @@ bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)
63936393
// jit\jit64\ebvts\mcpp\sources2\ijw\__clrcall\vector_ctor_dtor.02\deldtor_clr.exe
63946394
if (block->hasTryIndex())
63956395
{
6396+
// This does not apply to the raw pinvoke call that is inside the pinvoke
6397+
// ILStub. In this case, we have to inline the raw pinvoke call into the stub,
6398+
// otherwise we would end up with a stub that recursively calls itself, and end
6399+
// up with a stack overflow.
6400+
if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_IL_STUB) && opts.ShouldUsePInvokeHelpers())
6401+
return true;
6402+
63966403
return false;
63976404
}
63986405
#endif // _TARGET_64BIT_

‎src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/IL/Stubs/PInvokeILEmitter.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ private void EmitPInvokeCall(PInvokeILCodeStreams ilCodeStreams)
7676

7777
private MethodIL EmitIL()
7878
{
79-
// Temp workaround to disable PInvoke stubs that require marshalling.
80-
// https://github.com/dotnet/runtime/issues/248
81-
{
82-
if (Marshaller.IsMarshallingRequired(_targetMethod))
83-
throw new NotSupportedException();
84-
}
85-
8679
if (!_importMetadata.Flags.PreserveSig)
8780
throw new NotSupportedException();
8881

@@ -95,13 +88,11 @@ private MethodIL EmitIL()
9588
ILCodeStream unmarshallingCodestream = pInvokeILCodeStreams.UnmarshallingCodestream;
9689
ILCodeStream cleanupCodestream = pInvokeILCodeStreams.CleanupCodeStream;
9790

98-
/* Temp workaround: disable EH blocks because of https://github.com/dotnet/runtime/issues/248
99-
10091
// Marshalling is wrapped in a finally block to guarantee cleanup
10192
ILExceptionRegionBuilder tryFinally = emitter.NewFinallyRegion();
10293

10394
marshallingCodestream.BeginTry(tryFinally);
104-
cleanupCodestream.BeginHandler(tryFinally);*/
95+
cleanupCodestream.BeginHandler(tryFinally);
10596

10697
// Marshal the arguments
10798
for (int i = 0; i < _marshallers.Length; i++)
@@ -112,12 +103,11 @@ private MethodIL EmitIL()
112103
EmitPInvokeCall(pInvokeILCodeStreams);
113104

114105
ILCodeLabel lReturn = emitter.NewCodeLabel();
115-
/* Temp workaround: disable EH blocks because of https://github.com/dotnet/runtime/issues/248
116106
unmarshallingCodestream.Emit(ILOpcode.leave, lReturn);
117107
unmarshallingCodestream.EndTry(tryFinally);
118108

119109
cleanupCodestream.Emit(ILOpcode.endfinally);
120-
cleanupCodestream.EndHandler(tryFinally);*/
110+
cleanupCodestream.EndHandler(tryFinally);
121111

122112
cleanupCodestream.EmitLabel(lReturn);
123113

0 commit comments

Comments
 (0)
Please sign in to comment.