public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access
@ 2014-01-27 13:32 martin at netbsd dot org
  2014-01-27 18:40 ` [Bug c/59958] " joseph at codesourcery dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: martin at netbsd dot org @ 2014-01-27 13:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958

            Bug ID: 59958
           Summary: alpha does not deal with non-aligned returns from
                    malloc() when doing byte wise access
           Product: gcc
           Version: 4.8.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: martin at netbsd dot org

Created attachment 31962
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31962&action=edit
generated assembler code (cc -O2 -S test.c)

On NetBSD/alpha a call to malloc requesting a two byte allocation may return a
pointer that is only 2-byte algined. We believe this to be within the C std
spec (as alignemnt is good enough for all complete objects fitting in the
allocated space).

However, gcc seems to assume return values from malloc to have higher
alignment.

This test program:

--8<--
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        char *p;
        size_t i;

        for (i = 0; i < 24; i++) {
                p = malloc(2);
                printf("got %p\n", p);
                p[0] = 'A'; p[1] = 0;
                printf("stored string: %s\n", p);
        }

        return 0;
}
-->8--

shows the problem:

[..]
got 0x3fffdc02238
stored string: A
got 0x3fffdc0223a
pid 600 (a.out): unaligned access: va=0x3fffdc0223a pc=0x120000bc4
ra=0x120000bbc sp=0x1ffffdb08 op=ldl
pid 600 (a.out): unaligned access: va=0x3fffdc0223a pc=0x120000be4
ra=0x120000bbc sp=0x1ffffdb08 op=stl
stored string: A
got 0x3fffdc0223c
stored string: A
got 0x3fffdc0223e
pid 600 (a.out): unaligned access: va=0x3fffdc0223e pc=0x120000bc4
ra=0x120000bbc sp=0x1ffffdb08 op=ldl
pid 600 (a.out): unaligned access: va=0x3fffdc0223e pc=0x120000be4
ra=0x120000bbc sp=0x1ffffdb08 op=stl
stored string: A
got 0x3fffdc02240
[..]


This might be more of a problem with target alpha and the byte access code,
compiling the test program with -mbwx does help (but not all alpha CPUs support
that).

gcc version used:

> cc -v
Using built-in specs.
COLLECT_GCC=cc
Target: alpha--netbsd
Configured with: /usr/src7/tools/gcc/../../external/gpl3/gcc/dist/configure
--target=alpha--netbsd --enable-long-long --enable-threads
--with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD
nb1 20120916' --with-system-zlib --enable-__cxa_atexit
--with-mpc-lib=/var/obj/mknative/alpha/usr/src7/external/lgpl3/mpc/lib/libmpc
--with-mpfr-lib=/var/obj/mknative/alpha/usr/src7/external/lgpl3/mpfr/lib/libmpfr
--with-gmp-lib=/var/obj/mknative/alpha/usr/src7/external/lgpl3/gmp/lib/libgmp
--with-mpc-include=/usr/src7/external/lgpl3/mpc/dist/src
--with-mpfr-include=/usr/src7/external/lgpl3/mpfr/dist/src
--with-gmp-include=/usr/src7/external/lgpl3/gmp/lib/libgmp/arch/alpha
--enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch
--build=x86_64-unknown-netbsd6.0. --host=alpha--netbsd
--with-sysroot=/var/obj/mknative/alpha/usr/src7/destdir.alpha
Thread model: posix
gcc version 4.8.3 (NetBSD nb1 20131213)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/59958] alpha does not deal with non-aligned returns from malloc() when doing byte wise access
  2014-01-27 13:32 [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access martin at netbsd dot org
@ 2014-01-27 18:40 ` joseph at codesourcery dot com
  2014-01-28 10:28 ` martin at netbsd dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: joseph at codesourcery dot com @ 2014-01-27 18:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
The response to C90 DR#075 said memory from malloc must be suitably 
aligned for all types, not just those fitting in space of the given size, 
and nothing relevant has changed in the malloc specification in C99/C11 so 
it's reasonable to assume this still applies (for standard types, that is, 
not types using C11 _Alignof).

http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_075.html


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/59958] alpha does not deal with non-aligned returns from malloc() when doing byte wise access
  2014-01-27 13:32 [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access martin at netbsd dot org
  2014-01-27 18:40 ` [Bug c/59958] " joseph at codesourcery dot com
@ 2014-01-28 10:28 ` martin at netbsd dot org
  2014-01-29  0:45 ` [Bug target/59958] " pinskia at gcc dot gnu.org
  2014-01-29 13:51 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: martin at netbsd dot org @ 2014-01-28 10:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958

--- Comment #2 from Martin Husemann <martin at netbsd dot org> ---
Is the alignment expected from malloc() configurable in gcc and/or different
from the standard stack alignment?

A small test program along the lines of

  if ((uintptr_t)malloc(1) & mask) printf("yes\n") else printf("no\n");

and checking what gcc -O2 optimizes away for different masks seems to show that
on alpha & 7 is optimized, while & 15 is not. This sounds good for alpha.

However, I get the same results for amd64 - where I would have expected the
required alignement to be 16 byte. Is this a bug in our amd64 target
configuration, or am I misundertanding something?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/59958] alpha does not deal with non-aligned returns from malloc() when doing byte wise access
  2014-01-27 13:32 [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access martin at netbsd dot org
  2014-01-27 18:40 ` [Bug c/59958] " joseph at codesourcery dot com
  2014-01-28 10:28 ` martin at netbsd dot org
@ 2014-01-29  0:45 ` pinskia at gcc dot gnu.org
  2014-01-29 13:51 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-29  0:45 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Martin Husemann from comment #2)
> Is the alignment expected from malloc() configurable in gcc and/or different
> from the standard stack alignment?

Yes MALLOC_ABI_ALIGNMENT:
@defmac MALLOC_ABI_ALIGNMENT
Alignment, in bits, a C conformant malloc implementation has to
provide.  If not defined, the default value is @code{BITS_PER_WORD}.
@end defmac




> 
> A small test program along the lines of
> 
>   if ((uintptr_t)malloc(1) & mask) printf("yes\n") else printf("no\n");
> 
> and checking what gcc -O2 optimizes away for different masks seems to show
> that on alpha & 7 is optimized, while & 15 is not. This sounds good for
> alpha.
> 
> However, I get the same results for amd64 - where I would have expected the
> required alignement to be 16 byte. Is this a bug in our amd64 target
> configuration, or am I misundertanding something?

misunderstanding the default and most likely the history of this macro.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/59958] alpha does not deal with non-aligned returns from malloc() when doing byte wise access
  2014-01-27 13:32 [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access martin at netbsd dot org
                   ` (2 preceding siblings ...)
  2014-01-29  0:45 ` [Bug target/59958] " pinskia at gcc dot gnu.org
@ 2014-01-29 13:51 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-01-29 13:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59958

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
The original issue is invalid (bug in netbsd).


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-01-29 13:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-27 13:32 [Bug c/59958] New: alpha does not deal with non-aligned returns from malloc() when doing byte wise access martin at netbsd dot org
2014-01-27 18:40 ` [Bug c/59958] " joseph at codesourcery dot com
2014-01-28 10:28 ` martin at netbsd dot org
2014-01-29  0:45 ` [Bug target/59958] " pinskia at gcc dot gnu.org
2014-01-29 13:51 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).