Bug 43009 - segmentation fault with -O3 when accessing byte-aligned array as dwords
Summary: segmentation fault with -O3 when accessing byte-aligned array as dwords
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.4.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-09 15:46 UTC by Aleksei Kazantsev
Modified: 2010-03-07 17:36 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
the preprocessed file (*.i*) that triggers the bug (12.95 KB, text/plain)
2010-02-09 15:48 UTC, Aleksei Kazantsev
Details
output for: gcc -v -save-temps -Wall -Werror -O3 2.c -o 2 (1.09 KB, text/plain)
2010-02-09 15:52 UTC, Aleksei Kazantsev
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Aleksei Kazantsev 2010-02-09 15:46:52 UTC
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)
Target: x86_64-linux-gnu

Resulting binary crushes with segmentation fault if gcc compile options include -O3.

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

void process_buf(uint32_t *buf)
{
 int i;
 uint32_t t = 0;
 for(i = 0; i < 16; i++)
  buf[i] = ++t; // segmentation fault
}

int main(int argc, char **argv)
{
 char *buf = malloc(65);
 int n;
 process_buf((uint32_t*)(buf + 1));
 n = write(1, buf + 1, 64);
 return 0;
}
Comment 1 Aleksei Kazantsev 2010-02-09 15:48:31 UTC
Created attachment 19828 [details]
the preprocessed file (*.i*) that triggers the bug
Comment 2 Aleksei Kazantsev 2010-02-09 15:52:06 UTC
Created attachment 19829 [details]
output for: gcc -v -save-temps -Wall -Werror -O3 2.c -o 2
Comment 3 Richard Biener 2010-02-09 16:15:32 UTC
Your pointer isn't properly aligned to be accessed via uint32_t*.
Comment 4 Aleksei Kazantsev 2010-02-09 16:47:22 UTC
(In reply to comment #3)
> Your pointer isn't properly aligned to be accessed via uint32_t*.
> 

And should it? If 'yes', then why GCC generates working code for that source without optimization or with -O2 ? Or even when some debug printf(...) inserted in the for-loop body. Strange behaviour, anyway.
Comment 5 Andrew Pinski 2010-02-09 17:07:07 UTC
The alignment requirements of uint32_t are not being satisfied. That causes undefined behavior which means it could work in one case but not the other.
Comment 6 Eric Botcazou 2010-02-09 18:20:03 UTC
> Your pointer isn't properly aligned to be accessed via uint32_t*.

That's hardly satisfactory an answer.  GCC has always generated working code on non-strict alignment platforms in this case and one can expect it to continue.

Moreover, the psABI is explicit:

"Like the Intel386 architecture, the AMD64 architecture in general does not
require all data accesses to be properly aligned. Misaligned data accesses are
slower than aligned accesses but otherwise behave identically. The only exception is that __m128 must always be aligned properly."

and there is no __m128 in the source code.
Comment 7 Richard Biener 2010-02-09 23:18:28 UTC
(In reply to comment #6)
> > Your pointer isn't properly aligned to be accessed via uint32_t*.
> 
> That's hardly satisfactory an answer.  GCC has always generated working code on
> non-strict alignment platforms in this case and one can expect it to continue.
> 
> Moreover, the psABI is explicit:
> 
> "Like the Intel386 architecture, the AMD64 architecture in general does not
> require all data accesses to be properly aligned. Misaligned data accesses are
> slower than aligned accesses but otherwise behave identically. The only
> exception is that __m128 must always be aligned properly."
> 
> and there is no __m128 in the source code.

The C language standard says such pointer invokes undefined behavior.  The
non-working code is vectorized, and vectorized code can have strict
alignment requirements.

Comment 8 Eric Botcazou 2010-02-10 10:02:47 UTC
> The non-working code is vectorized, and vectorized code can have strict
> alignment requirements.

Yes, the alignment requirements are surreptitiously changed by -ftree-vectorize on x86 and x86-64.  This ought to be documented in the manual.
Comment 9 Christoph Rupp 2010-02-27 20:12:47 UTC
I experienced the same issue. I'm not able to reproduce this in a short snippet, but if someone needs my whole source package then i can provide it.
Comment 10 Manuel López-Ibáñez 2010-03-07 16:17:27 UTC
Cannot we warn for this?
Comment 11 Richard Biener 2010-03-07 17:36:52 UTC
(In reply to comment #10)
> Cannot we warn for this?

As usual, if we knew the data is not aligned we'd not "miscompile" it.