question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

calli with [out] struct containing byvalarray fields leave fields uninitialized

See original GitHub issue

Having 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jbevaincommented, May 7, 2020

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.

0reactions
jpbruyerecommented, May 7, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found