3 ms·
It's surprising to see so much debate about when or where UB "happens". Unfortunately this article doesn't refer to the best tool to root out UB - UBSan. If y
by wyldfire 4y ago
It's surprising to see so much debate about when or where UB "happens".
Unfortunately this article doesn't refer to the best tool to root out UB - UBSan. If you write C or C++, you should have a UBSan+ASan build that is configured to terminate with an error when it encounters violations. You'll save yourself a lot of hair-pulling later. Then you don't have to wonder about the nebulous topics like what "line" the UB "happened" on.
For some compilers, when they add an optimization that capitalizes on an aspect of UB, they will also add a corresponding warning. So make sure you have warnings enabled -- and in most cases you'll want -Werror because generally no one scrutinizes the build output unless there's a failure.
- AstralStorm 4y agoThis is all fine and dandy, but UBSan and ASan are memory hogs that just won't fit in my single digit MB microcontroller. So the only way to run them is too use an emulator, and that means I cannot sanitize drivers.
- wyldfire 4y agoYeah they are and that's too bad. HWASan is another option that reduces the overhead but would only work on a core that had MTE (which your microcontroller is not as likely to have).
- AstralStorm 4y agoActually it is an enhanced armv7 which might work. Though officially HWASan requires armv8. Besides that we have Zephyr stack and heap overflow detection, but it is not quite as accurate as ASan, and does not handle cases UBSan does.
- wyldfire 4y agogwp-asan (like electric fence) is another option for invalid access to heap-allocated buffers. Requires an MMU and creates additional TLB pressure w/some allocation overhead, but no instrumentation overhead and no access overhead.
- cyber_kinetist 4y agoI really hope that Windows Clang would soon support UBSan. It already supports ASan and is really helpful, but sometimes you need to go the extra mile to catch the most hard-to-find errors.
- obi1kenobi 4y agoThe UBSan+ASan combo is great! Rust's miri is another good one. This post is already fairly long (~10min read) and I didn't want to make it any longer. It's also already getting flamed in predictable ways ("nonsense! UB has to be reachable to be a problem!") so I didn't want to add more ways to get flamed for it (: (I'm the post's author.)