Bug 20441 – Wrong code with -O -fPIC and pointer subtraction
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-12-10T17:02:37Z
Last change time
2019-12-13T12:46:37Z
Keywords
industry, pull, wrong-code
Assigned to
No Owner
Creator
Don
Comments
Comment #0 by clugdbug — 2019-12-10T17:02:37Z
This reduced test case tested with 2.086, but original bug was found with 2.089 and 2.078.
Assert fails if compiled with -O -fPIC. Passes if compiled without -O.
---
const(char)* moo(const (char) *s)
{
return s;
}
void main ()
{
const(char) *x = "abc".ptr;
assert( moo(x) - x == 0 );
}
---
Comment #1 by pro.mathias.lang — 2019-12-10T18:02:27Z
Can reproduce on OSX with `dmd -O -run ...`
Comment #2 by clugdbug — 2019-12-10T19:30:55Z
On any non-ancient DMD version, -fPIC is enabled by default in the dmd.conf file.
I have run the testcase on 2.078 with -fPIC removed from the dmd.conf file. It requires both -fPIC and -O to trigger the bug.
Comment #3 by clugdbug — 2019-12-12T09:28:13Z
And here is what the optimizer produces. The problematic instruction is highlighted.
0x000055555558dbe8 <+0>: push %rbp
0x000055555558dbe9 <+1>: mov %rsp,%rbp
0x000055555558dbec <+4>: lea 0x31d6d(%rip),%rdi # 0x5555555bf960
0x000055555558dbf3 <+11>: callq 0x55555558dbdc <_moo>
=> 0x000055555558dbf8 <+16>: sub $0x31d66,%rax
0x000055555558dbfe <+22>: jne 0x55555558dc04 <_Dmain+28>
0x000055555558dc00 <+24>: xor %eax,%eax
0x000055555558dc02 <+26>: pop %rbp
0x000055555558dc03 <+27>: retq
The mistake is that the addressing mode flag to make it RIP-relative, is missing.
Instead of
sub RAX, [RIP + 0x31d66]
it is generating
sub RAX, [0x31d66]
Comment #4 by clugdbug — 2019-12-12T11:02:20Z
Wrong code is generated for any of the 'orthogonal' operators. This is another example.
size_t roo(size_t r) { return r; }
void main ()
{
size_t y = cast(size_t)("abc".ptr);
assert ( (roo(y)^y) == 0);
}
Bug probably exists in cdorth() in cod2.d. As far as I can tell, that function never generates RIP-relative addressing under any circumstances.
Comment #5 by dlang-bot — 2019-12-13T08:57:08Z
@WalterBright created dlang/dmd pull request #10661 "fix Issue 20441 - Wrong code with -O -fPIC and pointer subtraction" fixing this issue:
- fix Issue 20441 - Wrong code with -O -fPIC and pointer subtraction
https://github.com/dlang/dmd/pull/10661
Comment #6 by dlang-bot — 2019-12-13T12:46:37Z
dlang/dmd pull request #10661 "fix Issue 20441 - Wrong code with -O -fPIC and pointer subtraction" was merged into stable:
- a4bc86ce3a0c4122fe27c84ae801c42f1c8edeef by Walter Bright:
fix Issue 20441 - Wrong code with -O -fPIC and pointer subtraction
https://github.com/dlang/dmd/pull/10661