ITADN

Jsonifier will break address sanitizers and relies on UB for string writing

#30Closedstephenberry 创建于 2024-05-06
S
stephenberrycommented
Jsonifier often reads beyond the initialized length of a string using SWAR/SIMD when writing strings, which will break address sanitizers on GCC and MSVC. This is also undefined behavior and could cause segfaults. Glaze used to reserve memory and then read this reserved memory when writing strings, which is much safer, but this also triggers address sanitizers and is UB. The reserve approach makes sure the memory is there (with caveats), but it doesn't address the corner case of how small string optimization (SBO) is handled across machines, and compilers could try to optimize this memory in ways that would cause segfaults by reading it. It's sad, because reading uninitialized memory is faster and I had to sacrifice string writing performance in Glaze, but you'll have to do so as well in Jsonifier, there's just no way around it. If you did find a way around it that would be amazing, but I looked at this hard and per the standard it isn't safe. With the reserve approach Clang's address sanitizer will not complain, so don't just rely on address sanitizers to tell you that what you are doing is safer, and other address sanitizers will error on this approach.
关闭于 2024-05-06 2 条评论