[Mono-dev] Patch for IntPtr bug

russell.kay at realtimeworlds.com russell.kay at realtimeworlds.com
Wed Feb 11 11:02:00 EST 2009


Replying to myself here

Using the output from this small program

using System;

namespace ConsoleApplication4
{
    enum Ptr : long
    {
    }

    class Program
    {
        static unsafe void Main(string[] args)
        {
            IntPtr test = new IntPtr(0xffffffff);
            Ptr testLong = (Ptr)test.ToInt64();
            Console.WriteLine("test = {0}, testLong={1}", test,
testLong);
            IntPtr testAnother = (IntPtr)testLong;
            Console.WriteLine("testAnother = {0}", testAnother);
            void* p = test.ToPointer();
            Console.WriteLine("p = {0}", (long)p);
        }
    }
}

You will see that under MS.NET the output is 

test = 4294967295, testLong=4294967295
testAnother = 4294967295
p = 4294967295

note the complete absence of sign extended values.

The output from Mono on OpenSuse 11.0 Linux

test = -1, testLong=-1
testAnother = -1
p = 4294967295

So the pointer conversion from void* to long is not sign extending under
Mono, and it is different output to MS.NET.

Russell

-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of
russell.kay at realtimeworlds.com
Sent: 11 February 2009 15:45
To: robertj at gmx.net; mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Patch for IntPtr bug

We get addresses on Linux that are >2Gb from malloc in the native code,
if we go through the conversion from int (i.e.)IntPtr.ToInt64() is not
giving a sign extended value back when this happens and then a cast to
IntPtr will fail. The cast from a pointer i.e. void* or byte* will also
not sign extend the long.

The conversion is not sign extending the long (perhaps because it does
actually fit)...

My patch is correct.

Russell

-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert
Jordan
Sent: 11 February 2009 15:24
To: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Patch for IntPtr bug

russell.kay at realtimeworlds.com wrote:

> I encountered a problem when casting a long to an IntPtr, which is
> something we have ended up doing a lot (we want to keep binary
assembly
> compatibility between x86 and x64, only changing the native code on
the
> different architectures). We encountered a problem with Mono as there
> has been a check introduced into the IntPtr constructor from a long,
> this is checking the range of the long to ensure it is in range,
> unfortunately the check is incorrect. A long can easily hold from
> Int32.MinValue to UInt32.MaxValue (and not Int32.MaxValue as it
> currently in there).
> 

The patch is wrong, since casting a long > Int32.MaxValue to a
4 byte signed intptr is simply invalid.

The current check in IntPtr.cs also matches MS.NET.

Robert

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

____________________________________________________________________
This email has been scanned by the MessageLabs Email Security System

____________________________________________________________________
DISCLAIMER

This message and any attachments contain privileged and confidential
information intended for the use of the addressee named above. If you
are not the intended recipient of this message, you are hereby notified
that any use, dissemination, distribution or reproduction of this
message is prohibited. Please note that we cannot guarantee that this
message or any attachment is virus free or that it has not been
intercepted and amended. The views of the author may not necessarily
reflect those of Realtime Worlds Ltd.

 

Realtime Worlds Ltd is registered in Scotland, number 225628. Registered
Office: 152 West Marketgait, Dundee, DD1 1NJ.
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

____________________________________________________________________
This email has been scanned by the MessageLabs Email Security System

____________________________________________________________________
DISCLAIMER

This message and any attachments contain privileged and confidential information intended for the use of the addressee named above. If you are not the intended recipient of this message, you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. Please note that we cannot guarantee that this message or any attachment is virus free or that it has not been intercepted and amended. The views of the author may not necessarily reflect those of Realtime Worlds Ltd.

 

Realtime Worlds Ltd is registered in Scotland, number 225628. Registered Office: 152 West Marketgait, Dundee, DD1 1NJ.


More information about the Mono-devel-list mailing list