Bug 18274 – va_arg (TypeInfo) broken for static arrays
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-01-21T15:27:53Z
Last change time
2018-01-22T07:18:23Z
Assigned to
No Owner
Creator
Johannes Pfau
Comments
Comment #0 by johannespfau — 2018-01-21T15:27:53Z
This code is currently broken:
https://run.dlang.io/is/68hf1w
---------------------------------------------
void foo(...)
{
uint[4] data;
va_arg(_argptr, _arguments[0], &data);
//data = va_arg!(uint[4])(_argptr);
writeln(data);
}
void main()
{
uint[4] value = [1, 2, 3, 4];
foo(value);
}
---------------------------------------------
[4, 0, 3567405808, 32767]
The commented, static variant works. The problem is that DMD passes static arrays as {.ptr, .length} slice with TypeInfo_Tuple for D variadic functions.
va_arg would have to detect this and instead of just copying the data it has to dereference the pointer. I'm not sure if this is easily possible though: DMD passes TypeInfo_Tuple instead of TypeInfo_StaticArray. Can TypeInfo_Tuple only occur for static arrays? If so, fixing this is easy. If not, we have to pass proper TypeInfo for static array parameters.
I don't know why we even do this, at least on AArch64 we can have much better performance by just passing static arrays by value in the same way a struct would be passed. Passing as ptr+length requires another special case in the va_arg function for static arrays.
Comment #1 by johannespfau — 2018-01-22T07:18:23Z
OK, I see that my assumption to get out exactly the same type as passed in was flawed. If you strictly interpret the passed arguments according to _arguments, the type of the data changed from static to dynamic array, but the data is still correct. So this is not really a bug.