www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11310] New: Alignment failure in test42.d

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310

           Summary: Alignment failure in test42.d
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bugzilla digitalmars.com



22:43:57 PDT ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 20 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310


safety0ff.bugz <safety0ff.bugz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |safety0ff.bugz gmail.com



00:01:04 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




00:03:26 PDT ---
That is the assembly without printf's, btw.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




12:13:18 PDT ---
Created an attachment (id=1273)
Edb screenshot for second printf

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




12:13:49 PDT ---
Created an attachment (id=1274)
Edb screenshot for assert test

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




12:20:33 PDT ---
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.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




12:59:27 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




22:25:39 PDT ---
Created an attachment (id=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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




05:52:16 PDT ---
Created an attachment (id=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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310


safety0ff.bugz <safety0ff.bugz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



11:52:46 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




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


fix Issue 11310 - Fix RIP relative offset calculation for TEST instruction

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 24 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310




Commit pushed to 2.064 at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/6edfd4cdd96889dd539f4b888fcf7e1bd70e09a1


fix Issue 11310 - Fix RIP relative offset calculation for TEST instruction

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 24 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11310


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 24 2013