Skip to content
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

ASan/MSan should call __sanitizer_unaligned_{load,store} functions when performing unaligned reads/writes (TSan already calls equivalents) #81722

Closed
thomcc opened this issue Feb 3, 2021 · 5 comments
Labels
A-sanitizers Area: Sanitizers for correctness and code quality C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@thomcc
Copy link
Member

thomcc commented Feb 3, 2021

There are special functions exposed by several of the sanitizers for performing unaligned loads and stores. The functions have names like __sanitizer_unaligned_{load,store}{16,32,64}¹, and come with the runtimes of at least MSan/ASan/TSan, but not LSan.

The documentation for them notes:

Some of the sanitizers (for example ASan/TSan) could miss bugs that happen in unaligned loads/stores. To find such bugs reliably, you need to replace plain unaligned loads/stores with these calls.

Under -Zsanitize=thread we end up calling functions like __tsan_unaligned_read4 which turns out to be what tsan's implementations for these do anyway: https://github.com/llvm/llvm-project/blob/350fafabe9d3bda75e80bf077303eb5a09130b53/compiler-rt/lib/tsan/rtl/tsan_interface.cpp

However, we don't do this for any other sanitizer. ASan and MSan both provide implementations of these. I don't know how beneficial this is for MSan, but for ASan at least is mentioned in the header.

Unless I'm mistaken, we should be doing this for MSan and ASan. The fact that we do it for TSan gives me hope that it can be added without too much effort. Alternatively, maybe the current behavior is intentional, and these are unneeded for our case for a reason that I'm not aware of.

For clarity, I haven't seen ASan miss issues because of this (and in truth have never gotten MSan working well), I just stumbled across it.

Anyway, Godbolt for convenience: https://rust.godbolt.org/z/Ed4arK. Change -Zsanitize=thread to use -Zsanitize=address or -Zsanitize=memory to see what I mean.


¹ This is included by the (more commonly used) headers <sanitizer/[atml]san_interface.h>. These are definitely public even though they begin with underscores.

@thomcc thomcc changed the title ASan/MSan should call __sanitizer_unaligned_{load,store} functions for (TSan already calls equivalents) ASan/MSan should call __sanitizer_unaligned_{load,store} functions when performing unaligned reads/writes (TSan already calls equivalents) Feb 3, 2021
@jonas-schievink jonas-schievink added A-sanitizers Area: Sanitizers for correctness and code quality C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Feb 3, 2021
@tmiasko
Copy link
Contributor

tmiasko commented Feb 4, 2021

This is expected, ASAN and MSAN instrumentation usually interacts with shadow memory directly rather than calling into the runtime.

@thomcc
Copy link
Member Author

thomcc commented Feb 4, 2021

Is the documentation of those functions incorrect then?

@tmiasko
Copy link
Contributor

tmiasko commented Feb 7, 2021

The instrumentation is performed under assumption that operations will have required alignment. It would be best combined with separate detection of misaligned loads & stores, because otherwise errors caused by such operations might not be detected.

@thomcc
Copy link
Member Author

thomcc commented Feb 8, 2021

I'm not sure I follow. I'm just saying to do it using the times the compiler statically knows the loads are misaligned. Most loads wouldn't go through this, only ones in the cases of:

  • Explicit calls to ptr::read_unaligned and equivalent.
  • Access to unaligned #[repr(packed)] structs.

These are legal misaligned accesses (when done properly), and the compiler knows about them statically. E.g, I'm not sure there needs to be detection of unaligned ops, the compiler already handles this correctly for tsan AFAICT.

Or do you mean something like: that we insert checks if the address is in-face unaligned at runtime, and only call the unaligned function if actually unaligned? That would be pretty odd, if I'm being honest.

@tmiasko
Copy link
Contributor

tmiasko commented Feb 8, 2021

I'm just saying to do it using the times the compiler statically knows the loads are misaligned.

This is exactly how it works (btw, I wouldn't call loads in read_unaligned misaligned). If you compare ASAN instrumentation you will see that read_unaligned consults the shadow memory twice to take into account that load is unaligned, while read consults shadow memory only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sanitizers Area: Sanitizers for correctness and code quality C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants