D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7026 - 64 bit optimizer bug
Summary: 64 bit optimizer bug
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 major
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-11-28 11:13 UTC by David Nadlinger
Modified: 2011-11-28 21:12 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Nadlinger 2011-11-28 11:13:45 UTC
The following code (reduced varint encoding) works as expected on Linux x86_64 if built without flags or just one of -O and -release, but fails if both -O -release are specified:
---
import core.stdc.stdio;

ubyte foo(uint n) {
  ubyte[5] buf = void;
  ubyte wsize;

  while (true) {
    if ((n & ~0x7F) == 0) {
      buf[wsize++] = cast(ubyte)n;
      break;
    } else {
      buf[wsize++] = cast(ubyte)((n & 0x7F) | 0x80);
      n >>= 7;
    }
  }

  printf("%hhu\n", wsize);
  return buf[0];
}

void main() {
  printf("%hhx\n", foo(3));
}
---

More specifically, the output (printf()s for shorter assembly) is »1 e0« for the optimized build instead of »1 3«, and the program crashes most of the time (different errors: segfaults, illegal instruction, glibc free() assert triggers, …) – stack corruption?