Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flaky test: AxHost_GetIPictureFromPicture_InvokeEnhancedMetafile_Roundtrips #2004

Closed
RussKie opened this issue Oct 2, 2019 · 8 comments · Fixed by #2736
Closed

Flaky test: AxHost_GetIPictureFromPicture_InvokeEnhancedMetafile_Roundtrips #2004

RussKie opened this issue Oct 2, 2019 · 8 comments · Fixed by #2736
Labels
area-Interop 🪲 bug Product bug (most likely) test-bug Problem in test source code (most likely)

Comments

@RussKie
Copy link
Member

RussKie commented Oct 2, 2019

  • .NET Core Version: master @ 3750c43

Problem description:

AxHost_GetIPictureFromPicture_InvokeEnhancedMetafile_Roundtrips test fails on CI but is passing locally.
E.g.

Error message
System.Runtime.InteropServices.ExternalException : A generic error occurred in GDI+.

Stack trace
   at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
   at System.Drawing.Imaging.Metafile..ctor(IntPtr henhmetafile, Boolean deleteEmf)
   at System.Windows.Forms.AxHost.GetPictureFromParams(UInt32 handle, PICTYPE type, UInt32 paletteHandle, Int32 width, Int32 height) in D:\a\1\s\src\System.Windows.Forms\src\System\Windows\Forms\AxHost.cs:line 5085
   at System.Windows.Forms.AxHost.GetPictureFromIPicture(Object picture) in D:\a\1\s\src\System.Windows.Forms\src\System\Windows\Forms\AxHost.cs:line 5029
   at System.Windows.Forms.Tests.AxHostTests.SubAxHost.GetPictureFromIPicture(Object picture) in D:\a\1\s\src\System.Windows.Forms\tests\UnitTests\System\Windows\Forms\AxHostTests.cs:line 1370
   at System.Windows.Forms.Tests.AxHostTests.AxHost_GetIPictureFromPicture_InvokeEnhancedMetafile_Roundtrips() in D:\a\1\s\src\System.Windows.Forms\tests\UnitTests\System\Windows\Forms\AxHostTests.cs:line 1265

Expected behavior:

The test pass

/cc: @hughbe

@RussKie RussKie added the test-bug Problem in test source code (most likely) label Oct 2, 2019
RussKie added a commit to RussKie/winforms that referenced this issue Oct 2, 2019
@weltkante
Copy link
Contributor

As noted on the other similar issues this test also seems to be not running on STA, being only annotated with a [Fact] attribute. Considering that OleInitialize native API initializes the thread to STA I'd expect all OLE functions to require STA ...

@merriemcgaw merriemcgaw added this to the Future milestone Oct 3, 2019
@hughbe
Copy link
Contributor

hughbe commented Oct 4, 2019

So I don’t think I actually changed the IPicture interface. I just added tests. This suggests that either this is a test bug or a bug that was there before caused by either previous refactoring or that has always been there

@hughbe
Copy link
Contributor

hughbe commented Oct 4, 2019

Scratch that: metafile support WAS recently added in .Net Core. Previously in .NET framework it didnt work

@weltkante
Copy link
Contributor

Test failure is independent from OleInitialize.

@weltkante
Copy link
Contributor

weltkante commented Oct 4, 2019

This and #2005 are interop mistakes, making OLE_HANDLE unsigned leads to the wrong sign extension, if the high bit in the handle is set this will fail the test. Had to reprogram the scenario from the test in C++ to figure out why it fails ...

USER and GDI handles are apparently sign extended (documented here and here for example), so regardless of whether OLE_HANDLE is signed or unsigned, when it contains such a handle it must be sign extended when passing it to a method taking an IntPtr.

The bug is in AxHost.GetPictureFromParams which takes an uint handle and casts it to IntPtr in various code paths. Other code paths than metafiles will fail randomly as well, for example icons and bitmaps also are GDI handles and thus must be sign extended.

Now the main question is whether OLE_HANDLE can contain something else than a USER or GDI handle, because if not then we could just turn it into an int instead of uint. Otherwise it needs careful auditing of every place which uses OLE_HANDLE to make sure they each are aware of the handle type before casting.

List of handle types requiring sign extension

Copied from the 2nd doc link above for reference.

USER handles:

  • HWND
  • HMENU
  • HICON
  • HCURSOR
  • HDWP*
  • HHOOK
  • HACCEL
  • HWINSTA
  • HDESK
  • HKL
  • HMONITOR
  • HWINEVENTHOOK*.

GDI handles:

  • HBITMAP
  • HPALETTE
  • HMETAFILE
  • HENHMETAFILE
  • HMETAFILEPICT
  • HBRUSH
  • HFONT
  • HDC
  • HRGN

Also here it was mentioned the data type may be relevant for IPictureDisp interop, I don't know if you can silently exchange uint with int or if this would cause errors when doing IDispatch invocations.

/cc: @hughbe @JeremyKuhne @RussKie for interop awareness. Also might want to reclassify this issue since the tests are detecting a real bug.

@RussKie RussKie added 🪲 bug Product bug (most likely) area-Interop labels Oct 8, 2019
@RussKie RussKie modified the milestones: Future, 5.0 Oct 8, 2019
@RussKie
Copy link
Member Author

RussKie commented Oct 8, 2019

Thank you @weltkante for taking the time and the analysis.

@JeremyKuhne @hughbe can you please lead on this?

@hughbe
Copy link
Contributor

hughbe commented Jan 18, 2020

Now the main question is whether OLE_HANDLE can contain something else than a USER or GDI handle, because if not then we could just turn it into an int instead of uint. Otherwise it needs careful auditing of every place which uses OLE_HANDLE to make sure they each are aware of the handle type before casting.

In our uses we only do HDC/HPALETTE/``HBITMAP/HENHMETAFILE`/`HMETAFILE`/`HFONT` which are all sign extended

@ghost ghost added the 🚧 work in progress Work that is current in progress label Jan 19, 2020
@ghost ghost removed the 🚧 work in progress Work that is current in progress label Jan 19, 2020
@RussKie RussKie removed this from the 5.0 milestone Jan 19, 2020
@RussKie
Copy link
Member Author

RussKie commented Jan 19, 2020

Thank you

@ghost ghost locked as resolved and limited conversation to collaborators Feb 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Interop 🪲 bug Product bug (most likely) test-bug Problem in test source code (most likely)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants