You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling IWICImagingFactory.CreateStream simply never succeeds: it crashes with either an access violation or a null reference exception. This might be a regression, since I don't remember having issues like that with an earlier version of Vortice (it was probably 1.8.x).
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.GCHandle.get_Target()
at SharpGen.Runtime.CppObjectShadow.ToShadow[[System.__Canon, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](IntPtr)
at SharpGen.Runtime.CppObjectVtbl.ToShadow[[System.__Canon, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](IntPtr)
at SharpGen.Runtime.ComObjectVtbl.AddRefImpl(IntPtr)
at Vortice.WIC.IWICStream.InitializeFromIStream(SharpGen.Runtime.Win32.IStream)
at Vortice.WIC.IWICStream.Initialize(System.IO.Stream)
at Vortice.WIC.IWICImagingFactory.CreateStream(System.IO.Stream)
windowscodecs crashes when trying to dereference the location of AddRef (vtbl[1]) method in ComStreamProxy Vtbl. The memory where the vtbl is supposed to be located is not mapped into memory (possibly already released to Windows NT kernel or never valid in the first place). One of the last SharpGen.Runtime methods to execute is the following stack:
It takes place during execution of native InitializeFromIStream, before call to CExternalStream::HrInit. Before the call to Release, SharpGen.Runtime.Win32.IStreamShadow for GUID 0000000c-0000-0000-C000-000000000046 is valid (_nativePointer != null, Callback == iStreamRef), IsDisposed is false. After that Release call finishes, it is invalid (_nativePointer is null, IsDisposed is true).
I think I've found the point when everything goes wrong, but I'm yet to figure out why exactly is that wrong and how to fix that.
Extra AddRef to _streamProxy in IWICStream is a workaround (and doesn't cause leaks). I don't think it's a correct solution. I continue figuring out how to fix it.
The same codegen with the last Vortice.Runtime.COM works fine without AddRef. Unfortunately, I've run out of time today. I will continue working on this issue tomorrow.
@amerkoleci are you sure this is the right way forward? IStream is pretty generic and should support fetching data on-the-fly. I'd postpone such changes until I can assess if the current behavior is intended.
MS Docs defines Seek parameter plibNewPosition to be optional. SharpGen is aware of that (at least in C++ model). However, it has generated the following code for Shadow Vtbl:
As you can see, when passed NULL as plibNewPosition it will create nullref (yay unsafe), and will attempt to assign the value there regardless of the fact that is is null. Access violation.
SharpDX does check for null. The code was removed in SharpGen.Runtime.COM 1.1.0 and the codegen for that became automatic (and wrong). I'll see if I can fix it fast enough.
Unfortunately, this will take a full day to fix. I want to do it right (in marshallers), instead of doing a hack in a single place (in reverse callback codegen).
Unfortunately, this will take a full day to fix. I want to do it right (in marshallers), instead of doing a hack in a single place (in reverse callback codegen).
Ok don't worry, take your time :) and thanks for your effort
This turned out to depend a lot on implementation details of different marshallers. The variable allocation is chaotic and not specified. The upcoming week I'm very busy and I will be unable to finish the work I've started in my branch. Sorry. For now this is a known regression from SharpDX, but the previous regression from SharpGen 1.2 was fixed, so this isn't really high priority.
Activity
amerkoleci commentedon Apr 13, 2021
Not sure why.
In the example here works:
https://github.com/amerkoleci/Vortice.Windows/blob/main/src/samples/HelloDirect3D11/Program.cs#L167
But I'm writing there, not reading
amerkoleci commentedon Apr 13, 2021
@andrew-boyarshin can there be any issue with ComStreamProxy?
andrew-boyarshin commentedon Apr 13, 2021
@amerkoleci I'll take a look tomorrow.
amerkoleci commentedon Apr 14, 2021
Issue seams here:
https://github.com/SharpGenTools/SharpGen.Runtime.COM/blob/558c63e1f90c3464d7a5947cae5d54e111940d05/ComObjectVtbl.cs#L59
andrew-boyarshin commentedon Apr 14, 2021
This will likely require new SharpGen release. I'll have time to debug and fix this issue ~6 hours from now.
andrew-boyarshin commentedon Apr 14, 2021
Yeah. I've got the whole pack (using custom Checked runtime):
What is worse, my initial theory has been disproved. I'll need to rethink my approach to debug this issue.
andrew-boyarshin commentedon Apr 14, 2021
windowscodecs crashes when trying to dereference the location of
AddRef(vtbl[1]) method inComStreamProxyVtbl. The memory where the vtbl is supposed to be located is not mapped into memory (possibly already released to Windows NT kernel or never valid in the first place). One of the last SharpGen.Runtime methods to execute is the following stack:It takes place during execution of native
InitializeFromIStream, before call toCExternalStream::HrInit. Before the call toRelease,SharpGen.Runtime.Win32.IStreamShadowfor GUID0000000c-0000-0000-C000-000000000046is valid (_nativePointer != null,Callback == iStreamRef),IsDisposedisfalse. After that Release call finishes, it is invalid (_nativePointerisnull,IsDisposedistrue).I think I've found the point when everything goes wrong, but I'm yet to figure out why exactly is that wrong and how to fix that.
amerkoleci commentedon Apr 14, 2021
What changed? Why it worked before?
andrew-boyarshin commentedon Apr 14, 2021
That's a question I'm now trying to figure out an answer to. :)
andrew-boyarshin commentedon Apr 14, 2021
Extra
AddRefto_streamProxyinIWICStreamis a workaround (and doesn't cause leaks). I don't think it's a correct solution. I continue figuring out how to fix it.andrew-boyarshin commentedon Apr 14, 2021
Using
SharpGen.Runtime.COMversion 1.2.0 with current 2.0 codegen results in the crash even with extraAddRef.andrew-boyarshin commentedon Apr 14, 2021
The same codegen with the last
Vortice.Runtime.COMworks fine withoutAddRef. Unfortunately, I've run out of time today. I will continue working on this issue tomorrow.andrew-boyarshin commentedon Apr 15, 2021
Last
Vortice.Runtime.COMworks fine withoutAddRefwith 2.0 beta 6 codegen using 1.2.1 SharpGen.Runtime, crashes with 2.0 SharpGen.Runtime.amerkoleci commentedon Apr 15, 2021
Why that? Maybe you changed IStream shadow generation code?
andrew-boyarshin commentedon Apr 15, 2021
I don't think the problem is in Runtime.COM. It's likely in SharpGen.Runtime (or the code generated for it).
12 remaining items
amerkoleci commentedon Apr 16, 2021
Ok, for now I saw you cannot create decoder from IStream initialized from file, thats why you have:
https://docs.microsoft.com/en-us/windows/win32/api/wincodec/nf-wincodec-iwicimagingfactory-createdecoderfromfilehandle
https://docs.microsoft.com/en-us/windows/win32/api/wincodec/nf-wincodec-iwicimagingfactory-createdecoderfromfilename
sa-exe commentedon Apr 16, 2021
Just FYI, this is how you would do this using SharpDX. Needless to say, it doesn't crash.
amerkoleci commentedon Apr 16, 2021
Well, not sure then, @andrew-boyarshin probably another marshal issue in SharpGen.Runtime
andrew-boyarshin commentedon Apr 16, 2021
Nullptr dereference on write happens in
CExternalStream::HrSeekwhen callingm_pIStream->Seek(nMove, seekOrigin, pnPosition).andrew-boyarshin commentedon Apr 16, 2021
MS Docs defines Seek parameter
plibNewPositionto be optional. SharpGen is aware of that (at least in C++ model). However, it has generated the following code for Shadow Vtbl:As you can see, when passed NULL as plibNewPosition it will create nullref (yay unsafe), and will attempt to assign the value there regardless of the fact that is is null. Access violation.
amerkoleci commentedon Apr 16, 2021
Can we expect quick fix for this as well?
andrew-boyarshin commentedon Apr 16, 2021
SharpDX does check for null. The code was removed in SharpGen.Runtime.COM 1.1.0 and the codegen for that became automatic (and wrong). I'll see if I can fix it fast enough.
amerkoleci commentedon Apr 16, 2021
This has to be fixed on automatic shadow codegen as there can be different automatic callback shadows doing so
andrew-boyarshin commentedon Apr 16, 2021
Unfortunately, this will take a full day to fix. I want to do it right (in marshallers), instead of doing a hack in a single place (in reverse callback codegen).
amerkoleci commentedon Apr 16, 2021
Ok don't worry, take your time :) and thanks for your effort
andrew-boyarshin commentedon Apr 18, 2021
This turned out to depend a lot on implementation details of different marshallers. The variable allocation is chaotic and not specified. The upcoming week I'm very busy and I will be unable to finish the work I've started in my branch. Sorry. For now this is a known regression from SharpDX, but the previous regression from SharpGen 1.2 was fixed, so this isn't really high priority.
amerkoleci commentedon Apr 18, 2021
Okay, no problem, take your time and keep me updated when new SharpGen beta is out! Thanks again.
amerkoleci commentedon Apr 28, 2021
Any news here @andrew-boyarshin?
andrew-boyarshin commentedon Apr 28, 2021
@amerkoleci I work on this almost every day. The changes are huge, I'm trying to simplify logic without breaking anything.