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] -fsanitize=alignment false positive with intended unaligned struct member access
Date: Sun, 03 Mar 2024 07:29:05 +0000	[thread overview]
Message-ID: <bug-114217-4-x5lfBVAv1G@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114217-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #5 from Akihiko Odaki <akihiko.odaki at daynix dot com> ---
(In reply to Andrew Pinski from comment #4)
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
> include/asm-generic/unaligned.h?h=v6.7
> 
> is correct except it should not expose get_unaligned/put_unaligned since the
> undefined code happens way before.
> 
> The problem is with the btrfs code in btrfs_filldir:
> ```
> static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
> {
> 	while (entries--) {
> 		struct dir_entry *entry = addr; /// THIS IS BROKEN and causes the
> -fsanitize=alignment error
> 		char *name = (char *)(entry + 1);
> 
> 		ctx->pos = get_unaligned(&entry->offset);
> 		if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
> 					 get_unaligned(&entry->ino),
> 					 get_unaligned(&entry->type)))
> 			return 1;
> 		addr += sizeof(struct dir_entry) +
> 			get_unaligned(&entry->name_len);
> 		ctx->pos++;
> 	}
> 	return 0;
> }
> ```
> 
> Added comment on where the error comes from. The get_unaligned macro really
> should not be used here. What should be used here is an unaligned version of
> `struct dir_entry` instead.

I understand the idea. What I don't get is that GCC still emits code for
unaligned memory access in such a case. It is just a waste of performance if
GCC doesn't provide a guarantee that the unaligned access is performed in such
a case and is not optimal.

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

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-03  7:03 [Bug sanitizer/114217] New: " akihiko.odaki at daynix dot com
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 [this message]
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-x5lfBVAv1G@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).