public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "akihiko.odaki at daynix dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug sanitizer/114217] New: -fsanitize=alignment false positive with intended unaligned struct member access
Date: Sun, 03 Mar 2024 07:03:06 +0000	[thread overview]
Message-ID: <bug-114217-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114217

            Bug ID: 114217
           Summary: -fsanitize=alignment false positive with intended
                    unaligned struct member access
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: akihiko.odaki at daynix dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org
  Target Milestone: ---

-fsanitize=alignment generates a false positive error for an intended unaligned
struct member access. The intention of unaligned struct member access is
expressed with __builtin_memcpy() as done by QEMU or packed struct access as
done by Linux. GCC translates such a construct to code to access memory
unaligned for architectures like rv64gc as intended but also emits code to
enforce the alignment.

The relevant code of QEMU is at:
https://gitlab.com/qemu-project/qemu/-/blob/v8.2.1/include/qemu/bswap.h?ref_type=tags
The relevant code of Linux is at:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/asm-generic/unaligned.h?h=v6.7

FYI, this issue is reproducible also with clang 17.0.1, and I'm going to open
an issue for it, too.

To reproduce the issue, compile the code shown below with -O2
-fsanitize=alignment for rv64gc:

#include <stdint.h>

typedef uint64_t u64;

/*
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/compiler_attributes.h?h=v6.7
*/

/*
 *   gcc:
https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
 * clang:
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
 */
#define __packed                        __attribute__((__packed__))

/*
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/asm-generic/unaligned.h?h=v6.7
*/

#define __get_unaligned_t(type, ptr) ({                                        
\
        const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);     
\
        __pptr->x;                                                             
\
})

#define __put_unaligned_t(type, val, ptr) do {                                 
\
        struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);           
\
        __pptr->x = (val);                                                     
\
} while (0)

#define get_unaligned(ptr)      __get_unaligned_t(typeof(*(ptr)), (ptr))
#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))

/*
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/btrfs/inode.c?h=v6.7
*/

struct dir_entry {
        u64 ino;
        u64 offset;
        unsigned type;
        int name_len;
};

/*
 * This function is intended to perform an unaligned access.
 * GCC emits code for an unaligned operation as intended,
 * but also emits code to assert alignment.
 */
u64 f(struct dir_entry *entry)
{
    return get_unaligned(&entry->offset);
}

/*
 * This function is intended to perform an aligned access.
 * GCC emits code for an aligned operation,
 * and emits code to assert alignment.
 */
u64 g(struct dir_entry *entry)
{
    return entry->offset;
}

             reply	other threads:[~2024-03-03  7:03 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-03  7:03 akihiko.odaki at daynix dot com [this message]
2024-03-03  7:10 ` [Bug sanitizer/114217] " pinskia at gcc dot gnu.org
2024-03-03  7:15 ` pinskia at gcc dot gnu.org
2024-03-03  7:19 ` akihiko.odaki at daynix dot com
2024-03-03  7:22 ` pinskia at gcc dot gnu.org
2024-03-03  7:29 ` akihiko.odaki at daynix dot com
2024-03-03  7:46 ` akihiko.odaki at daynix dot com
2024-03-03 19:01 ` jakub at gcc dot gnu.org
2024-03-04  5:26 ` akihiko.odaki at daynix dot com
2024-03-04  7:46 ` jakub at gcc dot gnu.org
2024-03-04  7:54 ` jakub at gcc dot gnu.org
2024-03-04  8:11 ` akihiko.odaki at daynix dot com
2024-03-04  8:35 ` jakub at gcc dot gnu.org
2024-03-04  8:45 ` akihiko.odaki at daynix dot com
2024-03-04 21:48 ` i at maskray dot me

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-114217-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).