#Does clang erase your UB code if you access a badly-aligned uintN_t?

68 messages · Page 1 of 1 (latest)

celest condor
#

Title.
A.k.a., can we just say "well, our system requirements are to use only modern CPUs that allow missaligned access, this is fine"?

tacit trenchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

turbid ember
#

!tias

tacit trenchBOT
celest condor
#

nah, trial and error is a fool's game

#

(like how for 20 years people though "yeah, vloatile bools are thread safe, don't worry guys" when they are in fact, not)

turbid ember
#

Your question is about a specific implementation, the way to answer it is to look at how the specific implementation behaves.

#

(If only there was some website that let you easily do this)

celest condor
#

we do know that when UB is involved, the compiler is allowed to burn your house down

#

so dismissing misalligned access sounds very suspicious

#

unless, some C++26 standard updated this behavior

tropic moth
#

"is the compiler allowed to delete your code?" is a very different question from "will clang 17.0.1 delete this particular code in this particular example under those particular circumstances with these particular flags?"

#

the compiler is absolutely allowed to delete your code if your code invokes UB.

#

and just because the current version of a particular compiler doesn't doesn't mean the next version won't.

#

¯_(ツ)_/¯

celest condor
#

!solved

tacit trenchBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

tropic moth
#

the answer is: maybe, it depends

turbid ember
zealous dome
#

memcpy

#

If it can be optimized out to an unaligned read, it probably will be?

celest condor
# zealous dome `memcpy`

I know the safe way to force correct alignment with a memcpy, or the wrapper around memcpy called std::bit_cast (since C++20, allowed only for std::is_trivial<T>::value types)

celest condor
zealous dome
#

I don't see how you could use bit_cast for this

zealous dome
celest condor
celest condor
zealous dome
#

What are you gonna pass as a parameter

celest condor
tropic moth
#

that'd be UB

zealous dome
celest condor
#

kay, then the memcpy is the only legit way

tropic moth
#

bit_cast takes a pointer to the object whose bits you want to cast

celest condor
tropic moth
#

sry, reference, yes

#

you could copy the bytes into a byte array and then bit_cast that

#

but memcpy is potentially simpler in that case

celest condor
tropic moth
#

😛

celest condor
#

so, std::is_trivial<T>::value is used

turbid ember
#

std::is_trivial is deprecated

turbid ember
turbid ember
#

I don't think so

zealous dome
turbid ember
#

I would have to double check. Chars are special, they alias with everything

#

In practice this is always fine

tropic moth
#

pretty sure that's UB

zealous dome
zealous dome
turbid ember
tropic moth
#

lol

zealous dome
#

A conspiracy by big char to push for more SA violations? 😛

celest condor
# turbid ember `uint32_t num = bit_cast<uint32_t>(*((char(*)[4])someRawAddress));` should be ok

yes

reinterpret_cast docs:

Type aliasing
Type accessibility

If a type T_ref is similar to any of the following types,
Similar types

Informally, two types are similar if, ignoring top-level cv-qualification:

    they are the same type; or
    they are both pointers, and the pointed-to types are similar; or
    they are both pointers to member of the same class, and the types of the pointed-to members are similar; or
    they are both arrays and the array element types are similar. 
#

so, fixed length N-dimensional arrays can be used for well defined reinterpret_cast

zealous dome
turbid ember
#

There's no question about whether a reinterpret_cast here is ok

#

Reinterpret casts in general are never UB. It's how you use the result that can be.

celest condor
turbid ember
#

(well, function pointers might be the one case)

zealous dome
#

I mean you posted this quote and said that it makes the cast (and the subsequent dereference I assume) well-defined

#

I don't see how the quote proves that

celest condor