calli with [out] struct containing byvalarray fields leave fields uninitialized
See original GitHub issueHaving a struct with byvalarray:
public struct testData {
public int x;
public int y;
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] data;
public int z;
public int w;
}
Creating a DynamicMethod with indirect call to c library automatically initialize the array fields.
The c func signature is: void getData (struct testData* pData){}
.
DynamicMethod method = new DynamicMethod ("test", null, new Type[] { typeof (testData).MakeByRefType () });
ILGenerator gen = method.GetILGenerator ();
gen.Emit (OpCodes.Ldarg_0);
gen.Emit (OpCodes.Ldc_I8, funcPtr.ToInt64 ());
gen.Emit (OpCodes.Conv_I);
gen.EmitCalli (OpCodes.Calli, CallingConvention.StdCall, null, new Type[] { typeof (testData).MakeByRefType () });
gen.Emit (OpCodes.Ret);
I call the dynamic method delegate with (out testData data)
inline declaration, and arrays are initialized.
Trying to do the same with Cecil using the CallSite
class to make the call, arrays are Null.
Am I missing something to have arrays initialized?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Uninitialized C structure field [duplicate]
1 Answer 1 ... structure c & s have a static storage duration and they are always initialized. If programmer does not initialize...
Read more >Instantiate struct without setting values - help
In C I would have an instance of my struct and fill in the fields as I parse the data. However, in Rust...
Read more >Working with partially initialized structs : r/rust
My current code uses mem::uninitialized() and I'm trying to rewrite it using the latest up-to-date UB rules. I'm dealing with an FFI API ......
Read more >Initializing struct fields with `MaybeUninit` - rust-lang/rust
The MaybeUninit docs have one weird, concerning section: There is currently no supported way to create a raw pointer or reference to a...
Read more >Struct and union initialization
The members of the current object are initialized in their natural order, unless designators are used (since C99): array elements in subscript ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No clue sorry. I’m going to close this, feel free to re-open with a self-contained if you end up finding out that Cecil generates something wrong.
I’m on linux, but with localinit on/off, nothing change and also I don’t use local with this [out] parameter. byvalArrays must be initialized by some piece of code in Marshaling, default ctor of such structs leave arrays to null, and trying to init them manually (with new byte[256]) destabilize the runtime. but PtrToStruct (hGlobHnd), classic PInvoke, and calli through a dynamic method initialize the arrays. My intuition is that there is a flag somewhere (assembly, module, method) that enable or not such fixed array init, but I may be completely wrong. I should handle more tests on my side, I was asking here hoping someone already know something about this problem.