Skip to content

[BUG] Delegate proxies crash with NRE when user delegate is null (only if rewrite to function pointers) #2916

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

Closed
1 task done
maxkatz6 opened this issue Jul 2, 2024 · 1 comment · Fixed by #2917
Closed
1 task done
Labels

Comments

@maxkatz6
Copy link
Contributor

maxkatz6 commented Jul 2, 2024

Description

This is a weird issue, as I wasn't able to reproduce it with a WASM Mono runtime or normal desktop application. But only with NativeAOT-LLVM.
It seems this issue is only reproducible after I replaced managed delegates with function pointers as part of #2615

But I decided to open this issue here instead, as it actually looks like a bug in SkiaSharp code, and exception is somehow swallowed outside of it.

Code

SkiaSharp has many methods accepting callbacks. Like:

public bool InstallPixels (SKImageInfo info, IntPtr pixels, int rowBytes, SKBitmapReleaseDelegate releaseProc, object context)
{
	var cinfo = SKImageInfoNative.FromManaged (ref info);
	var del = releaseProc != null && context != null
		? new SKBitmapReleaseDelegate ((addr, _) => releaseProc (addr, context))
		: releaseProc;
	DelegateProxies.Create (del, out _, out var ctx);
	return SkiaApi.sk_bitmap_install_pixels (Handle, &cinfo, (void*)pixels, (IntPtr)rowBytes, DelegateProxies.SKBitmapReleaseDelegateProxy, (void*)ctx);
}

The problem with this callback, that it passes proxy function to the DllImport even when releaseProc is null.

And from proxy implementation, it will always crash with NRE, as context is an empty pointer.

private static void SKSurfaceReleaseDelegateProxyImplementation (void* address, void* context)
{
	var del = Get<SKSurfaceReleaseDelegate> ((IntPtr)context, out var gch);
	try {
		del.Invoke ((IntPtr)address, null);
	} finally {
		gch.Free ();
	}
}

Expected Behavior

No exception.

Actual Behavior

Exception.

Version of SkiaSharp

3.x (Alpha)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Other (Please indicate in the description)

Platform / Operating System Version

Reproduced on WASM (both Mono and NativeAOT)
_

Relevant Log Output


dotnet.native.js:1553
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
  put_char @ dotnet.native.js:1553
  $SystemNative_LogError @ dotnet.native.wasm:0x14219fc
  $S_P_CoreLib_Internal_Console_Error__Write @ dotnet.native.wasm:0x6a6783
  $S_P_CoreLib_System_RuntimeExceptionHelpers__FailFast @ dotnet.native.wasm:0xd0c2d8
  $S_P_CoreLib_System_RuntimeExceptionHelpers__RuntimeFailFast @ dotnet.native.wasm:0x10b8b5b
  $S_P_CoreLib_System_Runtime_EH__HandleUnhandledException @ dotnet.native.wasm:0x10b6dfa
  $SkiaSharp_SkiaSharp_DelegateProxies__SKBitmapReleaseDelegateProxyImplementation$F1_Filter @ dotnet.native.wasm:0xed436a
  $S_P_CoreLib_System_Runtime_EH__CallFilterFunclet @ dotnet.native.wasm:0x114210f
  $S_P_CoreLib_System_Runtime_EH__DispatchException @ dotnet.native.wasm:0xd903c
  $S_P_CoreLib_System_Runtime_EH__RhpThrowEx @ dotnet.native.wasm:0x108ba01
  $S_P_CoreLib_Internal_Runtime_CompilerHelpers_ThrowHelpers__ThrowNullReferenceException @ dotnet.native.wasm:0x108b96d
  $SkiaSharp_SkiaSharp_DelegateProxies__SKBitmapReleaseDelegateProxyImplementation @ dotnet.native.wasm:0xed4317
  $SkMakePixelRefWithProc(int, int, unsigned long, void*, void ()(void, void*), void*)::PixelRef::~PixelRef() @ dotnet.native.wasm:0x185b3e4
  $SkRefCntBase::internal_dispose() const @ dotnet.native.wasm:0x18765ed
  $SkBitmap::~SkBitmap() @ dotnet.native.wasm:0x185b791
  $sk_bitmap_destructor @ dotnet.native.wasm:0x1943b5e
  $SkiaSharp_SkiaSharp_SKBitmap__DisposeNative @ dotnet.native.wasm:0xdbe66c
  $SkiaSharp_SkiaSharp_SKNativeObject__Dispose @ dotnet.native.wasm:0xa68216
  $SkiaSharp_SkiaSharp_SKBitmap__Dispose @ dotnet.native.wasm:0xdbe69d
  $SkiaSharp_SkiaSharp_SKNativeObject__Dispose_0 @ dotnet.native.wasm:0xc74915
  $Avalonia_Skia_Avalonia_Skia_ImmutableBitmap___ctor_3

Code of Conduct

  • I agree to follow this project's Code of Conduct
@maxkatz6 maxkatz6 changed the title [BUG] Null callback crash the app when used from delegate proxies (NativeAOT-LLVM only?) [BUG] Delegate proxies crash with NRE when user delegate is null (only if rewrite to function pointers) Jul 3, 2024
@maxkatz6
Copy link
Contributor Author

maxkatz6 commented Jul 3, 2024

Updated description, as I was able to find more specific reason.
This crash doesn't happen on master or any nuget build, but code itself is faulty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant