Bug 9449 – Static arrays of 128bit types segfault on initialization. Incorrect calling of memset128ii.
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2013-02-04T03:51:00Z
Last change time
2015-03-10T15:58:46Z
Keywords
pull, SIMD, wrong-code
Assigned to
bugzilla
Creator
tbanelwebmin
Comments
Comment #0 by tbanelwebmin — 2013-02-04T03:51:35Z
This small code crashs.
----------------------------------
import core.simd;
void main()
{
ubyte16 table[1];
}
----------------------------------
It crashes in:
void[] *_memset128ii(void[] *p, void[] value, size_t count);
It seems that a wrong "count" is passed in by the _Dmain() function.
Details:
DMD64 D Compiler v2.061
Linux Ubuntu
x86_64
AMD Phenom II
Comment #1 by hsteoh — 2013-02-08T20:36:49Z
Something is very screwy with the executable that DMD produces for this code. For example:
$ cat test.d
import core.simd;
void main() {
ubyte16 table[1];
}
$ dmd -L--export-dynamic -g -m64 test.d
$ gdb test
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /tmp/test...done.
(gdb) break Dmain
Segmentation fault
$
Commenting out the ubyte16 line makes the problem go away (it will correctly set the breakpoint). Looks like the codegen is screwed up somewhere?
Comment #2 by hsteoh — 2013-02-08T21:39:27Z
Actually, this looks like a compiler bug. The ubyte16 alias translates to __vector(ubyte[16]), which is a compiler built-in magic type.
Here's the disassembly of Dmain:
0000000000418620 <_Dmain>:
418620: 55 push %rbp
418621: 48 8b ec mov %rsp,%rbp
418624: 48 83 ec 10 sub $0x10,%rsp
418628: 48 be 01 00 00 00 00 movabs $0x1,%rsi
41862f: 00 00 00
418632: 66 0f 6f 05 e6 77 01 movdqa 0x177e6(%rip),%xmm0 # 42fe20 <_IO_stdin_used+0x10>
418639: 00
41863a: 48 8d 7d f0 lea -0x10(%rbp),%rdi
41863e: e8 a9 07 00 00 callq 418dec <_memset128ii>
418643: 31 c0 xor %eax,%eax
418645: c9 leaveq
418646: c3 retq
Here's the disassembly of _memset128ii:
0000000000418dec <_memset128ii>:
418dec: 55 push %rbp
418ded: 48 8b ec mov %rsp,%rbp
418df0: 48 83 ec 20 sub $0x20,%rsp
418df4: 48 89 75 e8 mov %rsi,-0x18(%rbp)
418df8: 48 89 55 f0 mov %rdx,-0x10(%rbp)
418dfc: 49 89 f8 mov %rdi,%r8
418dff: 49 89 fb mov %rdi,%r11
418e02: 49 89 c9 mov %rcx,%r9
418e05: 49 c1 e1 04 shl $0x4,%r9
418e09: 4c 03 cf add %rdi,%r9
418e0c: 4d 3b c1 cmp %r9,%r8
418e0f: 73 18 jae 418e29 <_memset128ii+0x3d>
418e11: 48 8b 55 f0 mov -0x10(%rbp),%rdx
418e15: 48 8b 45 e8 mov -0x18(%rbp),%rax
418e19: 49 89 00 mov %rax,(%r8)
418e1c: 49 89 50 08 mov %rdx,0x8(%r8)
418e20: 49 83 c0 10 add $0x10,%r8
418e24: 4d 39 c8 cmp %r9,%r8
418e27: 72 e8 jb 418e11 <_memset128ii+0x25>
418e29: 49 8b c3 mov %r11,%rax
418e2c: 48 8b e5 mov %rbp,%rsp
418e2f: 5d pop %rbp
418e30: c3 retq
Note that the expected parameters to memset128ii appear to not be passed by Dmain; I traced the execution into memset128ii and found that it was trying to memset an unreasonably large range of memory (2e+15 bytes), probably because the wrong arguments were passed to it.
Since the only druntime code involved is template wrapper around the compiler magic type __vector, the fault must lie with the compiler SIMD intrinsics.
Comment #3 by maxim — 2013-02-09T02:48:23Z
_memset128ii expects:
%rcx => size_t count
%rdx => .ptr of value array
$rsi => .length of value array
%rdi => pointer to first argument array
what _Dmain passes:
%rcx => nothing (garbage)
%rdx => nothing (garbage)
%rsi => size_t count
%rdi => pointer to 16 byte object
%xmm0 => ubyte16[1] array
Comment #4 by hsteoh — 2013-02-09T22:26:56Z
Seems to be related to bug 8518.
Comment #5 by maxim — 2013-02-09T22:40:37Z
(In reply to comment #4)
> Seems to be related to bug 8518.
Thanks for founding. The root of the problem (at least this one) is when dmd frontend parses and generates list of expressions, it does not create "hidden" expression which calls _memset128ii. Instead it does this when it executes AssignExpression::toElem() and later calls setArray() which issues call to _memset128ii. However it does not convert ubyte16[1] from static array to dynamic array and passes it as a static array. Since _memset128ii expects dynamic array, the program goes off the rails.
Comment #7 by john.loughran.colvin — 2013-04-21T09:25:52Z
(In reply to comment #5)
> (In reply to comment #4)
> > Seems to be related to bug 8518.
>
> Thanks for founding. The root of the problem (at least this one) is when dmd
> frontend parses and generates list of expressions, it does not create "hidden"
> expression which calls _memset128ii. Instead it does this when it executes
> AssignExpression::toElem() and later calls setArray() which issues call to
> _memset128ii. However it does not convert ubyte16[1] from static array to
> dynamic array and passes it as a static array. Since _memset128ii expects
> dynamic array, the program goes off the rails.
I'm pretty sure the use of void[] in _memset128ii is simply so as to have a 128bit data type. It's never used as, or expected to be, an array.
_memset128ii doesn't care whether it's being passed a static or dynamic array, it just blindly increments a pointer and writes to it "count" times.
(In reply to comment #3)
> _memset128ii expects:
>
> %rcx => size_t count
> %rdx => .ptr of value array
> $rsi => .length of value array
> %rdi => pointer to first argument array
This is incorrect. _memset128 expects:
RCX: size_t count
RDX: higher 64 bits of value
RSI: lower 64 bits of value
RDI: pointer to the 1st element of the destination array.
Comment #8 by john.loughran.colvin — 2013-04-21T09:27:06Z
(In reply to comment #7)
> (In reply to comment #5)
> > (In reply to comment #4)
> > > Seems to be related to bug 8518.
> >
> > Thanks for founding. The root of the problem (at least this one) is when dmd
> > frontend parses and generates list of expressions, it does not create "hidden"
> > expression which calls _memset128ii. Instead it does this when it executes
> > AssignExpression::toElem() and later calls setArray() which issues call to
> > _memset128ii. However it does not convert ubyte16[1] from static array to
> > dynamic array and passes it as a static array. Since _memset128ii expects
> > dynamic array, the program goes off the rails.
>
> I'm pretty sure the use of void[] in _memset128ii is simply so as to have a
> 128bit data type. It's never used as, or expected to be, an array.
>
> _memset128ii doesn't care whether it's being passed a static or dynamic array,
> it just blindly increments a pointer and writes to it "count" times.
>
> (In reply to comment #3)
> > _memset128ii expects:
> >
> > %rcx => size_t count
> > %rdx => .ptr of value array
> > $rsi => .length of value array
> > %rdi => pointer to first argument array
>
> This is incorrect. _memset128 expects:
>
> RCX: size_t count
> RDX: higher 64 bits of value
> RSI: lower 64 bits of value
> RDI: pointer to the 1st element of the destination array.
/s/_memset128/_memset128ii
Comment #9 by maxim — 2013-04-21T10:52:44Z
(In reply to comment #7)
> I'm pretty sure the use of void[] in _memset128ii is simply so as to have a
> 128bit data type. It's never used as, or expected to be, an array.
>
> _memset128ii doesn't care whether it's being passed a static or dynamic array,
> it just blindly increments a pointer and writes to it "count" times.
I think it does matter whether dynamic array was passed or a static one due to how arguments are passed.
> (In reply to comment #3)
> > _memset128ii expects:
> >
> > %rcx => size_t count
> > %rdx => .ptr of value array
> > $rsi => .length of value array
> > %rdi => pointer to first argument array
>
> This is incorrect. _memset128 expects:
>
> RCX: size_t count
> RDX: higher 64 bits of value
> RSI: lower 64 bits of value
> RDI: pointer to the 1st element of the destination array.
I see no difference between length dynamic array property and your "lower 64 bits of value" (also between ptr and "higher 64 bits of value"). And passing a pointer to dynamic array is not the same thing as passing pointer to the first element: http://dpaste.dzfl.pl/8f91aed8
Comment #10 by john.loughran.colvin — 2013-04-21T11:56:15Z
(In reply to comment #9)
> (In reply to comment #7)
> > I'm pretty sure the use of void[] in _memset128ii is simply so as to have a
> > 128bit data type. It's never used as, or expected to be, an array.
> >
> > _memset128ii doesn't care whether it's being passed a static or dynamic array,
> > it just blindly increments a pointer and writes to it "count" times.
>
> I think it does matter whether dynamic array was passed or a static one due to
> how arguments are passed.
It doesn't matter in this case because it is a pointer being passed, not an array at all.
> > (In reply to comment #3)
> > > _memset128ii expects:
> > >
> > > %rcx => size_t count
> > > %rdx => .ptr of value array
> > > $rsi => .length of value array
> > > %rdi => pointer to first argument array
> >
> > This is incorrect. _memset128 expects:
> >
> > RCX: size_t count
> > RDX: higher 64 bits of value
> > RSI: lower 64 bits of value
> > RDI: pointer to the 1st element of the destination array.
>
> I see no difference between length dynamic array property and your "lower 64
> bits of value" (also between ptr and "higher 64 bits of value").
because value is not an array. As I said before, void[] is just used because conveniently void[].sizeof == 16 (128 bits) on x64 (the check for x64 is done inside e2ir.c)
> And passing a
> pointer to dynamic array is not the same thing as passing pointer to the first
> element: http://dpaste.dzfl.pl/8f91aed8
See my comment above. Each *element* of the array is being represented by a void[]. There is no D style array passing happening here, static or otherwise, it's just pointers.
Imagine replacing void[] with a hypothetical _128BitType and you'll see what I mean.
I'm currently re-implementing memset.d and updating the compiler to interact with the new functions. This should hopefully fix this bug and maybe 9969 also, if there isn't a nasty backend bug hiding behind it all.
Comment #11 by john.loughran.colvin — 2013-04-21T12:11:09Z
(In reply to comment #10)
> There is no D style array passing happening here, static or otherwise,
> it's just pointers.
Sorry, mistake.
There is an array being passed as "value", but from the callers point of view (generated in dmd) it's not an array at all, it's just a 128 bit type.
Comment #12 by maxim — 2013-04-22T09:44:52Z
(In reply to comment #10)
> > I see no difference between length dynamic array property and your "lower 64
> > bits of value" (also between ptr and "higher 64 bits of value").
>
> because value is not an array. As I said before, void[] is just used because
> conveniently void[].sizeof == 16 (128 bits) on x64 (the check for x64 is done
> inside e2ir.c)
Value is actually accepted as an array due to passing conversions and unusual usage inside memset function is no excuse for changing ABI interpretation. Clearly, anyone can pass many different things through some inappropriate parameter but it does not mean that each time callee would adjust passing convention for different types. I don't see point in this dispute further . I argue that value is technically accepted as dynamic array and you argue that it is treated as 128 bit element. These points don't contradict and however arguments are called (lower value or length property) does change the picture - there is no correspondence between what is passed and what is received.
> I'm currently re-implementing memset.d and updating the compiler to interact
> with the new functions. This should hopefully fix this bug and maybe 9969 also,
> if there isn't a nasty backend bug hiding behind it all.
I doubt that it is possible without dmd hacking but good luck.
Comment #13 by acehreli — 2013-10-26T11:30:17Z
I hit the same bug without any obvious SIMD operations:
struct Point
{
double f;
double g;
}
void main()
{
Point[1] arr;
}
Ali
Comment #14 by john.loughran.colvin — 2014-07-17T12:27:26Z
Comment #15 by john.loughran.colvin — 2014-07-17T12:29:05Z
*** Issue 8518 has been marked as a duplicate of this issue. ***
Comment #16 by bugzilla — 2014-07-18T07:14:39Z
(In reply to John Colvin from comment #14)
> I'm marking this a regression as, whatever the reason, it worked with 2.065.0
>
> Walter I've assigning you because it seems likely that
> https://github.com/D-Programming-Language/dmd/commit/
> 6c2a2878200e0df1c73db976a747abf61b6a5e1a) and
> (https://github.com/D-Programming-Language/druntime/commit/
> a405a02394e2c26c6a66c3fc5ef3777bb86cd973 caused it, but I'm not certain.
That seems very unlikely, as those changes were dated June 2012, long before 2.065.
Comment #17 by john.loughran.colvin — 2014-07-18T09:19:32Z
(In reply to Walter Bright from comment #16)
> That seems very unlikely, as those changes were dated June 2012, long before
> 2.065.
The issue existed before 2.065, but was somehow masked for a while.
Comment #18 by bugzilla — 2014-07-18T09:25:56Z
This is not a regression, it never worked. Nevertheless, I'm working on a fix.
Can't reproduce on the auto-tester (Git master). Please open a new issue with more details about your system configuration if the problem still persists.
Comment #33 by github-bugzilla — 2015-02-18T03:38:09Z
Hi !
I've hit this bug with GIT HEAD dmd and vibe.d built with win32driver.
I've disassembled .exe and it crashes with movdqa, the address is misaligned.
I've changed movdqa opcode to movdqu in dmd's code and all works OK now.
Comment #35 by code — 2015-03-10T15:58:46Z
(In reply to Temtaime from comment #34)
> Hi !
> I've hit this bug with GIT HEAD dmd and vibe.d built with win32driver.
>
> I've disassembled .exe and it crashes with movdqa, the address is misaligned.
> I've changed movdqa opcode to movdqu in dmd's code and all works OK now.
Works with 2.067.0-b4, closing this.
Please open a new ticket if you have this problem on a win32 machine.