λshμηarie
λshμηarie
CC#
Created by λshμηarie on 9/4/2023 in #help
C#/C Interop M1 Padding/Alignment Issues
I have a small application I've written to help showcase this issue that I have uploaded to GitHub (https://github.com/Flash619/InteropTest). When calling C methods from C# with a series of complex parameters (size_t, uint32_t, and void *) it seems memory alignment / marshalling is somehow thrown off. When both the DLL and C# are built and executed on intel x64 linux, the interop works flawlessly. However when built and executed on an M1 MacBook, the variables do not match those expected. C# Side [DllImport("clib", EntryPoint = "test_interop", CallingConvention = CallingConvention.Cdecl)] private static extern int test_interop(int number1, int number2, int number3, byte[] data1, int number4, byte[] data2, int number5, int number6, int number7, int number8); C Side int test_interop( const uint32_t number1, const uint32_t number2, const uint32_t number3, const void *data1, const size_t number4, const void *data3, const size_t number5, const size_t number6, const size_t number7, const uint32_t number8 ) Output
SENT: number1: 24 number2: 48 number3: 1223 number4: 86 number5: 108 number6: 132 number7: 1935 number8: 213 RECEIVED: number1: 24 number2: 48 number3: 1223 number4: 86 number5: 108 number6: 132 number7: 914828035983 number8: 1843242248
As you can see above, number7 and number8 are both incorrect, and if I remove arguments it will still be inaccurate in various unpredictable ways... The same code built/executed on linux works flawlessly so I'm sorta confused... Possibly an M1 specific issue due to the varying sizes of size_t and uint32_t?
26 replies