Bug 11310 – Alignment failure in test42.d

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2013-10-20T22:43:00Z
Last change time
2013-10-24T23:52:09Z
Keywords
pull
Assigned to
nobody
Creator
bugzilla

Attachments

IDFilenameSummaryContent-TypeSize
127311310a.pngEdb screenshot for second printfimage/png107167
127411310b.pngEdb screenshot for assert testimage/png109147
127911310c.pngEdb screenshot for unoptimized versionimage/png21075
1280hackfix.diffThis hack makes the program run properly for metext/plain703

Comments

Comment #0 by bugzilla — 2013-10-20T22:43:57Z
Here's the code that fails in test/runnable/test42.d: ----------------------------- align(16) struct S41 { int[4] a; } shared int x41; shared S41 s41; void test41() { printf("&x = %p\n", &x41); printf("&s = %p\n", &s41); assert((cast(int)&s41 & 0xF) == 0); } ---------------------------- when compiled on Linux with: -O -fPIC Curiously, it prints: &x = 0x7fef00 &s = 0x7fef10 which shows it is aligned correctly - so why does the assert fail?
Comment #1 by safety0ff.bugz — 2013-10-21T00:01:04Z
I noticed importing random stuff (e.g. std.string, std.stdio, std.algorithm) made it run without error, then I looked at the assembly code: Disassembly of section .text._D6_113106test41FZv: 0000000000000000 <_D6_113106test41FZv>: 0: 55 push %rbp 1: 48 8b ec mov %rsp,%rbp 4: f6 05 00 00 00 00 0f testb $0xf,0x0(%rip) b: 74 0a je 17 <_D6_113106test41FZv+0x17> d: bf 11 00 00 00 mov $0x11,%edi 12: e8 00 00 00 00 callq 17 <_D6_113106test41FZv+0x17> 17: 5d pop %rbp 18: c3 retq 19: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) Looks like it is and'ing the instruction pointer with 0xf.
Comment #2 by safety0ff.bugz — 2013-10-21T00:03:26Z
That is the assembly without printf's, btw.
Comment #3 by safety0ff.bugz — 2013-10-21T12:13:18Z
Created attachment 1273 Edb screenshot for second printf
Comment #4 by safety0ff.bugz — 2013-10-21T12:13:49Z
Created attachment 1274 Edb screenshot for assert test
Comment #5 by safety0ff.bugz — 2013-10-21T12:20:33Z
As you can see in the first screen shot, the address for s41 is calculated properly, whereas in the second screenshot the is a mistake. rip for the printf calculation: 0x419c84 rip for the assert calculation: 0x419c99 difference: 0x15 offset for the printf calculation: 0x21e35c offset for the assert calculation: 0x21e344 difference: 0x18 Looking at the relocation section of the executable, printf seems to be giving the correct location (0x63cdd0.)
Comment #6 by safety0ff.bugz — 2013-10-21T12:59:27Z
Looking at the non-optimized version of the code, the test instruction operates on dwords, which makes the constant 3 bytes longer (i.e. the difference noted in comment 5,) the constant off of rip is the same as before.
Comment #7 by safety0ff.bugz — 2013-10-22T22:25:39Z
Created attachment 1279 Edb screenshot for unoptimized version Just to make it more clear what the issue is (I was quite sick when I wrote the other comment): The code responsible for calculating the offsets from the IP to the offset table is calculating an offset that is off by 3 bytes. This offset is appropriate for the instruction present in the unoptimized version which operates on dword.
Comment #8 by safety0ff.bugz — 2013-10-23T05:52:16Z
Created attachment 1280 This hack makes the program run properly for me I don't know if there are cases where this will produce invalid code, I'll pull out the 64 bit architecture manual tonight and figure it out.
Comment #9 by safety0ff.bugz — 2013-10-23T11:52:46Z
After checking manuals, I've concluded that this is the correct fix and I've submitted a pull request: https://github.com/D-Programming-Language/dmd/pull/2691
Comment #10 by github-bugzilla — 2013-10-24T23:50:29Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/6ec2800443a088b42722b1580a3388111ed9c2ab fix Issue 11310 - Fix RIP relative offset calculation for TEST instruction. https://github.com/D-Programming-Language/dmd/commit/5138bc4a1aa09e935875c7343adeee3def5ca21c Merge pull request #2692 from Safety0ff/fix11310m fix Issue 11310 - Fix RIP relative offset calculation for TEST instruction
Comment #11 by github-bugzilla — 2013-10-24T23:51:29Z
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/6edfd4cdd96889dd539f4b888fcf7e1bd70e09a1 Merge pull request #2692 from Safety0ff/fix11310m fix Issue 11310 - Fix RIP relative offset calculation for TEST instruction