CsWin32: Implement SetLastError ourselves when allowMarshaling is false
It’s worth noting that CsWin32’s support for disabling marshalling won’t actually be usable with DisableRuntimeMarshallingAttribute yet because of SetLastError: https://docs.microsoft.com/en-us/dotnet/standard/native-interop/disabled-marshalling#disabled-features
_Originally posted by @alexrp in https://github.com/microsoft/CsWin32/issues/593#issuecomment-1179162557_
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 15 (10 by maintainers)
Commits related to this issue
- Implement `SetLastError` ourselves on .NET when marshaling is not allowed Closes #600 — committed to microsoft/CsWin32 by AArnott 3 years ago
Good news: Updating CsWin32 to
0.3.31-beta(daily) and disabling runtime marshalling in all projects Just Works. 🙂I’d love for you to try it out. Would it work for you to consume the daily builds, per these instructions? https://github.com/microsoft/CsWin32#consuming-daily-builds
Would it be possible to at least not generate the SetLastError when allowMarshalling is false? I can write the surrounding code myself but SetLastError is making CsWIn32 unusable in project with marshalling disabled.
Cool! Thanks for the write up, that is super helpful. Don’t assign it to me just yet. I might focus on a couple of other issues first. But It seems mostly straight forward. I will see if I get time this week.
Thanks!
SetLastError=truedoes not lead to an exception being thrown, and in this code change we do not propose to add that behavior.DllImportAttribute.SetLastErrorindicates that the native method may callSetLastError. Some of these methods only call this in error conditions, and calling this method overwrites any previous value. But this value is in TLS (thread local storage) so each thread has its own copy. If you want to read the value set by a prior Win32 call, you must read it immediately after the call to ensure it wasn’t overwritten by later code.When the CLR emits the interop code for a
DllImportmethod, if the SetLastError property is set to true, the CLR callsSetLastError(0)first, then invokes the method, and then callsGetLastErrorand stores the result in a CLR-specific TLS memory location so that even if the CLR internally makes other native calls, the user call toMarshal.GetLastWin32Errorwill always return the value from the last user-p/invoke on that thread.I can’t find it in the documentation now, but I believe when the CLR does do marshaling, it isn’t exactly guaranteed that after an interop call completes that the CLR hasn’t injected other interop calls for its own purposes, in which case the ‘last error’ may be overwritten by the time the first interop call returns. This means that if CsWin32 generates a method that tries to imitate the CLR’s marshaling behavior, there isn’t a guarantee that our own call to GetLastError will actually return the value from our own native call – it might be from a CLR engine’s call into Win32. So we have to be careful.
Now, by targeting net7.0 and using the
[LibraryImport(SetLastError=true)]attribute, we can see what kind of code it generates:Notice how the
[DllImport]attribute in its generated code is on a local function, and the attribute lacks theSetLastError=trueargument. So theexternAPI is no longer a method that others can call. The method is now a wrapper of theexternmethod. And that wrapper is equivalent to what the CLR would have produced if it were marshaling the call. This is what CsWin32 must do.But here’s the rub: while net7.0 evidently guarantees the correctness of this code, I don’t think the CLR (e.g. net472) makes the same guarantee. Remember how I said the call into the extern method may inadvertently trigger the CLR to make other native calls before it returns? But I guess with Core CLR it’s safe. Well, that and perhaps because LibraryImport always generates enough code to ensure that marshaling never occurs.
So… I think CsWin32 should emit this same kind of wrapper, but only when these conditions are true:
SetLastError = truemethod.allowMarshalinghas been set tofalseStill want to tackle it, @elachlan?