Bug 1805 - P/Invoke returning native ptr to struct with one IntPtr field
Summary: P/Invoke returning native ptr to struct with one IntPtr field
Status: RESOLVED INVALID
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-01 06:58 UTC by Dmitry Azaraev
Modified: 2011-11-02 09:48 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Dmitry Azaraev 2011-11-01 06:58:34 UTC
I'm extending mono/tests with next:

libtest.c:

// simple return some pointer (from some allocator)
LIBTEST_API void* STDCALL
mono_return_custom_ptr ()
{
    return (void*)0x12345678;
}

pinvoke11.cs:

// define returning struct
[StructLayout(LayoutKind.Sequential)]
public struct custom_ptr
{
    public IntPtr value;
}

and in Main method adding follow:
    custom_ptr cp = mono_return_custom_ptr();
    Console.WriteLine("{0:X8}", (int)cp.value);

make it...

Running pinvoke11.exe with any of this Mono Runtimes on Ubuntu 11.10:
Mono 2.10.5 
Mono 2.10.6 (built from sources)
Mono latest (built from sources)

got invalid result:

$ ./pinvoke11.exe
BFE2E5A8
$ ./pinvoke11.exe
BF9E0F58
$ ./pinvoke11.exe
BF9DC4C8
 
Note, that this technique works fine with Mono 2.10.6 on Windows.
Comment 1 Dmitry Azaraev 2011-11-02 04:55:23 UTC
Sorry, i got this behaviour on X86 platform.
Same test works fine with .NET 4.
Comment 2 Paolo Molaro 2011-11-02 09:34:31 UTC
Your p/invoke binding is incorrect: if the native code returns a pointer, the managed method signature must return a pointer as well. In a similar case, if the native code returned a structure containing a pointer, the managed signature would need to return a struct with a pointer.

The incorrect code happens to work on windows because the ABI there says that small structures get returned in a register (which happens to be the same register where a pointer would be returned), while on Linux they are returned with a different mechanism.
Comment 3 Dmitry Azaraev 2011-11-02 09:48:18 UTC
Thanks for clear explanation!