From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 05A0E3858D33; Mon, 4 Mar 2024 05:26:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 05A0E3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709530013; bh=vFDYFNLL3iFyijdzbD9ZqQeM6dZLGj4w19ZsG0B+6y8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=QmX1s6CdtQFKAqOUuIKGIlMdUtPUr8vR2otY8xHCoIYAtgs2Fg+vkBt8WVXBsMk5/ 3xhIGyLzUMh5GgTgTa03E/E6+K/UHWnhLJpyJokJh+LzTEhISutKwnZB9FjIcpijbn kMEKSNHpTpAoQhiZ+DWMoWXrkP/FPfOTmj+4XSLI= From: "akihiko.odaki at daynix dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug sanitizer/114217] -fsanitize=alignment false positive with intended unaligned struct member access Date: Mon, 04 Mar 2024 05:26:52 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: sanitizer X-Bugzilla-Version: 13.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: akihiko.odaki at daynix dot com X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114217 --- Comment #8 from Akihiko Odaki --- (In reply to Jakub Jelinek from comment #7) > GCC actually doesn't diagnose on mere pointer assignment, but what trigge= rs > the alignment check is > &entry->offset > even when the code later on just takes its address, entry must be > sufficiently aligned, otherwise entry->offset is invalid. > Under standard C rules, already forming the pointer would be UB, so > somewhere in the caller when you prepare what to pass to the f function. >=20 > If you want something that will still be invalid C, > but will not trigger UBSAN errors, then e.g. > unsigned long long h(struct dir_entry *entry) > { > return get_unaligned((unsigned long long *) (((char *) entry) + offse= tof > (struct dir_entry, offset))); > } > will do. It would certainly workaround the issue, but it's only dirtier and brings no benefit except suppressed UBSan errors. Why not allow get_unaligned(&entry->offset) when UBSan does not complain about that hack? > If you want something that will be valid even in C, don't pass struct > dir_entry *entry > argument, but void *entry instead, and use e.g. > __get_unaligned_t(__typeof(((struct dir_entry *)0)->offset), ((char > *)entry)+offsetof(struct dir_entry, offset))) > You can surely hide that all under some macro. The definition still involves UB for ((struct dir_entry *)0)->offset. Perha= ps __typeof() may be considered as an exception, but what if offsetof() is def= ined as follows? #define offsetof(T, x) ((uintptr_t)&(((T *)0)->x)) GCC does provide __builtin_offsetof(), but I think definitions of offsetof() like this are still prevalent, and expected to work although it's UB. If GCC tolerates this kind of trick, why not tolerate get_unaligned(&entry->offset= )?=