-
Notifications
You must be signed in to change notification settings - Fork 683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On abusive use of reinterpret_cast
UB
#3215
Comments
Taxonomy of
|
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
Very interesting, thanks for the report.
Do you have resources on this point? It is not obvious to me that it is true. |
It is the common consensus I have found on the internet:
The article give this example This other article give a more advanced use case: http://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html
Edit: In this PR, I have replaced a few reinterpret_cast by memcpy. I have disassembled reported the hottest spot in the function. This is a memcpy that have been translated to x86 movups instructions (move unpacked 512 bits). I would not be surprised if the reinterpret_cast i changed was initially compiled with a movaps (move aligned). I don't think we are in need of performance to the point we have to care about such platform specific details. Program correctness is way more important than micro performance improvement. |
Thanks!
Absolutely, I wasn't trying to argue the rational for the change. |
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
see autowarefoundation#3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
* fix(perception): remove UB reinterpret_cast see #3215 Signed-off-by: Vincent Richard <richard-v@macnica.co.jp> * fix(pointcloud_preprocessor): remove UB reinterpret_cast Signed-off-by: Vincent Richard <richard-v@macnica.co.jp> * refactor Signed-off-by: Vincent Richard <richard-v@macnica.co.jp> --------- Signed-off-by: Vincent Richard <richard-v@macnica.co.jp>
Checklist
Description
As pointed here, there are places in Autoware where
reinterpret_cast
is abusively used to "convert" data types.There are endless talks over the internet about how misleading
reinterpret_cast
is. Basically, dereferencing a reinterpreted pointerp
with*reinterpret_cast<T*>(p)
is only valid ifp
originally points to data of typeT
(e.g.p = new T()
), or aconst
/signed
/unsigned
variant, or ifT==char
(orunsigned char
orstd::byte
). Ifp
originally points to any other type, even if "it looks" the same, is straight UB.For example, copying
PointXYIZ
fromuint8_t[]
like here is UB:autoware.universe/sensing/pointcloud_preprocessor/src/outlier_filter/ring_outlier_filter_nodelet.cpp
Line 123 in cb103ec
To create a valid
PointXYZI
from the buffer, it would be necessary at least to cast each field separately (e.g. likep.x = *reinterpret_cast<float*>(&data[p_offset + x_field_offset])
).Edit: according to this excellent writeup, reinterpret casting from a buffer violates strict aliasing rules (but I guess it is ok in the scope of an immediate copy?), and could run into misaligned memory issues (but all the data we manipulate is always aligned, right?...). The author suggests the only safe way to manipulate data is to
memcpy
(yew!). Hopefully, C++ makes it possible to wrap things and avoid the memcpy boilerplate (for example theconst_unaligned_pointer
described here: http://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html)Similarly, even though
PointXYZIRADRT
andPointXYZI
"start the same", it is UB to usereinterpret_cast
to copy between the 2 structs. For example:It may "work" now... until the compiler decides otherwise.
Purpose
Remove all UB
reinterpret_cast
in Autoware code base.Possible approaches
Definition of done
No more yolo
reinterpret_cast
UB.The text was updated successfully, but these errors were encountered: