Bug 29854 - P/Invoke corrupting arguments for certain functions in arm64 mode
Summary: P/Invoke corrupting arguments for certain functions in arm64 mode
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.6.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 8.13 (C6 alpha)
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2015-05-07 14:56 UTC by Andrew Piper
Modified: 2015-07-15 10:23 UTC (History)
5 users (show)

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


Attachments
Demo project (4.84 MB, application/x-zip-compressed)
2015-05-07 14:56 UTC, Andrew Piper
Details


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 Developer Community or GitHub 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 FIXED

Description Andrew Piper 2015-05-07 14:56:15 UTC
Created attachment 11124 [details]
Demo project

Summary:
P/Invoke is corrupting arguments passed from managed code to unmanaged code for some functions.

Overview:
I have a p/invoke signature for a function I wrote in a native library. The p/invoke signature is:
[DllImport("__Internal", CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr btFixedConstraint_Create(IntPtr rbA, IntPtr rbB, Vector3 jointPos, Quaternion jointRot);

and the native function is:
extern "C" _AnomalousExport btFixedConstraint* btFixedConstraint_Create(void* rbA, void* rbB, Vector3 jointPos, Quaternion jointRot)

Quaternion is a struct with x, y, z and w components and Vector3 is a struct with x, y, and z components.

When this function is called in arm64 mode on ios the quaternion will get 1.4013e-45 (basically 0) in its first component (x) and each additional component y, z and w will have the values for x, y and z. All other arguments pass correctly.

The expected output of this function call is:

2015-05-07 13:55:26.553 PInvokeBug[2179:827741] Running 64 bit
2015-05-07 13:55:26.555 PInvokeBug[2179:827741] Passed void rbA is 0x0
Passed rbB is 0x0
Passed vector3 is 1, 2, 3
Passed quaternion is 4, 5, 6, 7

and the actual output is:

2015-05-07 13:56:46.481 PInvokeBug[2186:828150] Running 64 bit
2015-05-07 13:56:46.482 PInvokeBug[2186:828150] Passed void rbA is 0x0
Passed rbB is 0x0
Passed vector3 is 1, 2, 3
Passed quaternion is 1.4013e-45, 4, 5, 6

For reference the 32 bit output is:

2015-05-07 13:55:26.553 PInvokeBug[2179:827741] Running 32 bit
2015-05-07 13:55:26.555 PInvokeBug[2179:827741] Passed void rbA is 0x0
Passed rbB is 0x0
Passed vector3 is 1, 2, 3
Passed quaternion is 4, 5, 6, 7

This started happening when I updated to 8.10 from 8.6, however, I backtracked and the error is introduced between 8.6.1 and 8.6.2. Building the example with 8.6.1 will show correct output in 32 and 64 bit mode. The other details of my build environment are as follows:
Xamarin Studio 5.9 (431)
XCode 6.3.1
Xamarin.iOS 8.6.2

The behavior was the same using xcode 6.1.1 also. In addition the entire project was built on OS X Visual Studio was not involved at any time.

Steps to reproduce:
I have attached a project that should reproduce this bug. The native code is included as static libraries and the c# code contains the relevant details about the native code being called.
1. Open PInvokeBug.sln.
2. Make sure you are in Debug mode (only setup for debug mode).
3. Attach a 64 bit ipad and click run to run on the device.
4. You should see output that matches the incorrect output above.
Comment 1 Andrew Piper 2015-05-07 14:59:17 UTC
I was originally discussing this on the forum http://forums.xamarin.com/discussion/39932/cannot-upgrade-past-8-6-1-runtime-errors-in-arm64-mode

The first post is not as relevant since on further investigation I found that the error was in a p/invoke call and not the bullet physics library.
Comment 2 Andrew Piper 2015-05-07 15:09:13 UTC
I forgot to mention that the version of mono installed on my mac at build time was 4.0.0, but I saw the same behavior on mono 3.12.1.
Comment 3 Sebastien Pouliot 2015-05-07 16:16:14 UTC
maccore/master suffers from the same (just before last bump)

2015-05-07 16:06:10.056 PInvokeBug[1991:725487] Running 64 bit
2015-05-07 16:06:13.517 PInvokeBug[1991:725487] Passed void rbA is 0x0
Passed rbB is 0x0
Passed vector3 is 1, 2, 3
Passed quaternion is 0, 4, 5, 6

@Zoltan can you have a look ?
Comment 4 Zoltan Varga 2015-05-07 19:57:12 UTC
Fixed in mono-extensions 66c8cdcf3a83ce7afb7eba2ea2c0008f383fe02d.

As a workaround, pass the individual fields instead of a struct, i.e.: use

        public static extern IntPtr btFixedConstraint_Create(IntPtr rbA, IntPtr rbB, float jointPosX, float jointPosY, float jointPosZ, ...
Comment 5 Sebastien Pouliot 2015-05-07 20:46:49 UTC
Thanks Zoltan!

maccore/master bumped in 3469f06b5afec9881ecc6c7968563ad98f95bb45 to include it
Comment 7 Zoltan Varga 2015-05-08 04:49:17 UTC
I'd wait so it gets more testing.