D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13769 - Wrong argument passing for variadic functions in 64 bits
Summary: Wrong argument passing for variadic functions in 64 bits
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 All
: P1 major
Assignee: No Owner
URL:
Keywords: backend, wrong-code
Depends on:
Blocks:
 
Reported: 2014-11-23 14:36 UTC by Mathias LANG
Modified: 2020-08-31 10:28 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mathias LANG 2014-11-23 14:36:00 UTC
The following code:
````
private unittest {
  import core.vararg, std.stdio;
  void fix() {}

  void test_bug(size_t bug, ...) { assert(bug == size_t.max); }
  void test_fixed(size_t bug, ...) { fix(); assert(bug == size_t.max); }
  void print_bug(size_t bug, ...) { writeln(bug); assert(bug == size_t.max); }
  void print_fix(size_t bug, ...) { fix(); writeln(bug); assert(bug == size_t.max); }

  print_bug(size_t.max);
  print_fix(size_t.max);
  test_fixed(size_t.max);
  test_bug(size_t.max);
}
````

Will trigger an unittest when put any Phobos module (for simplicity, I tested std.algorithm).

Example:
````
make[1]: Leaving directory '/mnt/shared/projects/dlang/druntime'
140734760252720
18446744073709551615
****** FAIL std.algorithm
core.exception.AssertError@std/algorithm.d(417): Assertion failure
----------------
generated/linux/release/64/unittest/libphobos2-ut.so(+0x28fb8e1) [0x7f2bf2fcd8e1]
````

As you see, the parameters passed during the first function call inside that function are wrong. I only appears with 64 bits parameters (including string).

I couldn't find any stand-alone snippet that reproduce the error. My guess is that it might originate from the way it's tested (shared library), but I currently lack the time to investigate further.

The issue was originally found in https://github.com/D-Programming-Language/phobos/pull/2677

This also outline the fact that variadic functions are poorly tested in Phobos.
Comment 1 Walter Bright 2020-08-31 09:28:40 UTC
The following:

void main()
{
  import core.vararg, std.stdio;
  void fix() {}

  void test_bug(size_t bug, ...) { assert(bug == size_t.max); }
  void test_fixed(size_t bug, ...) { fix(); assert(bug == size_t.max); }
  void print_bug(size_t bug, ...) { writeln(bug); assert(bug == size_t.max); }
  void print_fix(size_t bug, ...) { fix(); writeln(bug); assert(bug == size_t.max); }

  print_bug(size_t.max);
  print_fix(size_t.max);
  test_fixed(size_t.max);
  test_bug(size_t.max);
}

works without error.
Comment 2 Mathias LANG 2020-08-31 10:28:53 UTC
Indeed, I cannot reproduce anymore, be it on OSX or Linux. Closing as WORKSFORME.