]> git.ipfire.org Git - thirdparty/gcc.git/commit
libcpp: Fix potential unaligned access in cpp_buffer
authorLewis Hyatt <lhyatt@gmail.com>
Mon, 28 Oct 2024 21:57:41 +0000 (17:57 -0400)
committerLewis Hyatt <lhyatt@gcc.gnu.org>
Sat, 23 Nov 2024 18:44:37 +0000 (13:44 -0500)
commitc93eb81c9edbf9bec6a12a2125037ec29d1cd4d2
tree6b9ac4d1cdb56f8672f6b5d8b9f4bb1f0a580bac
parent927625d007f47377a091d541de42c7840a4a5af6
libcpp: Fix potential unaligned access in cpp_buffer

libcpp makes use of the cpp_buffer pfile->a_buff to store things while it is
handling macros. It uses it to store pointers (cpp_hashnode*, for macro
arguments) and cpp_macro objects. This works fine because a cpp_hashnode*
and a cpp_macro have the same alignment requirement on either 32-bit or
64-bit systems (namely, the same alignment as a pointer.)

When 64-bit location_t is enabled on a 32-bit sytem, the alignment
requirement may cease to be the same, because the alignment requirement of a
cpp_macro object changes to that of a uint64_t, which be larger than that of
a pointer. It's not the case for x86 32-bit, but for example, on sparc, a
pointer has 4-byte alignment while a uint64_t has 8. In that case,
intermixing the two within the same cpp_buffer leads to a misaligned
access. The code path that triggers this is the one in _cpp_commit_buff in
which a hash table with its own allocator (i.e. ggc) is not being used, so
it doesn't happen within the compiler itself, but it happens in the other
libcpp clients, such as genmatch.

Fix that up by ensuring _cpp_commit_buff commits a fully aligned chunk of the
buffer, so it's ready for anything it may be used for next.

Also modify CPP_ALIGN so that it guarantees to return an alignment at least
the size of location_t. Currently it returns the max of a pointer and a
double. I am not aware of any platform where a double may have smaller
alignment than a uint64_t, but it does not hurt to add location_t here to be
sure.

libcpp/ChangeLog:

* lex.cc (_cpp_commit_buff): Make sure that the buffer is properly
aligned for the next allocation.
* internal.h (struct dummy): Make sure alignment is large enough for
a location_t, just in case.
libcpp/internal.h
libcpp/lex.cc