www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - runtime vararg can easily be broken

reply davidl <davidl nospam.org> writes:
The runtime vararg push into stack with align of 4, however programmer  
might neglect this easily and cause problem.

import std.boxer;
import std.stdio;
void func(...)
{
   Box[] arguments;
   arguments.length = _arguments.length;
   for(int i;i<_arguments.length;i++)
   {
      arguments[i] = box(_arguments[i], _argptr);
      _argptr += _arguments[i].tsize;
   }
   foreach(arg;arguments)
      writefln(arg);
}
void main()
{
   func(34,43);
   func(cast(ushort)4, cast(ushort)5); // this fails
}

I don't know if tango Stdout suffers from this.
But this really can easily cause problems.
Also the whole paradigm of coding a runtime vararg func is so troublesome  
and even much complex compared to the compile time vararg. Maybe we should  
borrow something from compiletime to aid the runtime vararg programming.


-- 
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Jun 12 2009
next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
davidl wrote:
 Also the whole paradigm of coding a runtime vararg func is so 
 troublesome and even much complex compared to the compile time vararg. 
 Maybe we should borrow something from compiletime to aid the runtime 
 vararg programming.
Got my vote!
Jun 12 2009
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
davidl wrote:
 The runtime vararg push into stack with align of 4, however programmer
 might neglect this easily and cause problem.
 
 ...
 
That's because you're not supposed to do that. std.stdarg
Jun 12 2009
next sibling parent reply grauzone <none example.net> writes:
Daniel Keep wrote:
 
 davidl wrote:
 The runtime vararg push into stack with align of 4, however programmer
 might neglect this easily and cause problem.

 ...
That's because you're not supposed to do that. std.stdarg
Contains only a template. Here's the solution (untested): void* va_arg(inout void* _argptr, TypeInfo ti) { void* result = _argptr; _argptr = _argptr + ((ti.tsize + int.sizeof - 1) & ~(int.sizeof - 1)); return arg; } (int.sizeof is always 4, but I doubt the original code runs correctly on anything but 32 bit x86 anyway.) We all know that (dynamic) varargs in D really, really suck. A lot of simple enough solutions have been proposed. But nothing happens. "The trivialer the solution, the more likely it is that the problem itself will never be solved and the programmer always has to fight with this trivial garbage."
Jun 12 2009
parent Jason House <jason.james.house gmail.com> writes:
grauzone Wrote:

 Daniel Keep wrote:
 
 davidl wrote:
 The runtime vararg push into stack with align of 4, however programmer
 might neglect this easily and cause problem.

 ...
That's because you're not supposed to do that. std.stdarg
Contains only a template. Here's the solution (untested): void* va_arg(inout void* _argptr, TypeInfo ti) { void* result = _argptr; _argptr = _argptr + ((ti.tsize + int.sizeof - 1) & ~(int.sizeof - 1)); return arg; } (int.sizeof is always 4, but I doubt the original code runs correctly on anything but 32 bit x86 anyway.) We all know that (dynamic) varargs in D really, really suck. A lot of simple enough solutions have been proposed. But nothing happens.
All non-digitalmars D compilers I've seen have done alternate implementations for this.
 "The trivialer the solution, the more likely it is that the problem 
 itself will never be solved and the programmer always has to fight with 
 this trivial garbage."
Jun 13 2009
prev sibling parent davidl <davidl nospam.org> writes:
在 Sat, 13 Jun 2009 02:17:03 +0800,Daniel Keep  
<daniel.keep.lists gmail.com> 写道:

 davidl wrote:
 The runtime vararg push into stack with align of 4, however programmer
 might neglect this easily and cause problem.

 ...
That's because you're not supposed to do that. std.stdarg
Then you probably want to accuse the spec examples of vararg functions. Also runtime vararg code shouldn't care anything about the alignment of stack for the sake of performance improvement. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Jun 12 2009