[Mono-bugs] [Bug 688008] Mono fails to run F# code involving native pointers that works on .NET

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun Apr 17 06:05:51 EDT 2011


https://bugzilla.novell.com/show_bug.cgi?id=688008

https://bugzilla.novell.com/show_bug.cgi?id=688008#c2


--- Comment #2 from Stephan Tolksdorf <st at quanttec.com> 2011-04-17 10:05:50 UTC ---
It's clearly unverifiable and "unsafe" code, but I'm not sure it's also invalid
code.

I.12.3.2.1 of ECMA-335 says:

"A managed pointer (type &) can be explicitly converted to an unmanaged pointer
(type  native unsigned int), although this is not verifiable and might produce
a runtime exception"

So this should certainly work if the F# compiler inserted a conv.i instruction
before stloc (see also I.14.4.2 and III.1.5 Table 8)

I.12.3.2.1 also says 

"Instructions that create pointers which are guaranteed not to point into the
memory manager‘s heaps (e.g., ldloca, ldarga, and ldsflda) produce transient
pointers (type *) that can be used wherever a managed pointer (type &) or
unmanaged pointer (type native unsigned int) is expected."

In the test case above the pointer is not a transient pointer in the strict
sense of the definition, but one might argue that there is no harm in also
allowing this assignment, as the code is unverifiable anyway and the same
assignment should work if the managed pointer is known to be a transient one.

However, it seems that Mono doesn't cover the transient pointer case either, as
the following test case demonstrates:

F# code:

let test2() =
    let mutable c = '!'
    let ni = NativePtr.toNativeInt &&c
    System.Console.WriteLine(new string(NativePtr.ofNativeInt<char> ni, 0, 1))

test2()

IL code for test2:

method public static void test2() cil managed
{
    .maxstack 5
    .locals init (
        [0] char c,
        [1] native int ni)
    L_0000: nop 
    L_0001: ldc.i4.s 0x21
    L_0003: stloc.0 
    L_0004: ldloca.s c
    L_0006: stloc.1 
    L_0007: ldloc.1 
    L_0008: ldc.i4.0 
    L_0009: ldc.i4.1 
    L_000a: newobj instance void [mscorlib]System.String::.ctor(char*, int32,
int32)
    L_000f: call void [mscorlib]System.Console::WriteLine(string)
    L_0014: ret 
}

Mono exception when running test:

Unhandled Exception: System.InvalidProgramException: Invalid IL code in
Test.Test:test2 (): IL_0006: stloc.1

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.


More information about the mono-bugs mailing list