-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ubsan] Use internal_memcpy to copy ubsan bits size #121586
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
Conversation
@llvm/pr-subscribers-compiler-rt-sanitizer Author: None (earnol) ChangesWhile fetching amounts of bits used to correctly display ubsan value reinterpret_cast was used, however as noted by Jakub Jelínek in #96240 discussion it might cause issues due to potentially unaligned memory access. The patch addresses this problem. Full diff: https://github.com/llvm/llvm-project/pull/121586.diff 1 Files Affected:
diff --git a/compiler-rt/lib/ubsan/ubsan_value.h b/compiler-rt/lib/ubsan/ubsan_value.h
index 430c9ea0dc8d15..058de943ba7fd8 100644
--- a/compiler-rt/lib/ubsan/ubsan_value.h
+++ b/compiler-rt/lib/ubsan/ubsan_value.h
@@ -150,8 +150,11 @@ class TypeDescriptor {
unsigned getIntegerBitCount() const {
DCHECK(isIntegerTy());
- if (isSignedBitIntTy())
- return *reinterpret_cast<const u32 *>(getBitIntBitCountPointer());
+ if (isSignedBitIntTy()) {
+ u32 BitCountValue;
+ internal_memcpy(&BitCountValue, getBitIntBitCountPointer(), sizeof(BitCountValue));
+ return BitCountValue;
+ }
else
return getIntegerBitWidth();
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. out of interest, did you run into this in practice anywhere?
✅ With the latest revision this PR passed the C/C++ code formatter. |
Nope. Just noticed that nobody still addressed this comment and thought it might be reasonable to handle it in appropriate way. The reason is some platforms (Sony Emotion Engine for example) works in those situations, but actually return incorrect value. |
While fetching amounts of bits used to correctly display ubsan value reinterpret_cast was used, however as noted by Jakub Jelínek in llvm#96240 discussion it might cause issues due to potentially unaligned memory access. The patch addresses this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
shouldn't the compiler be able to optimize the memcpy with target specific knowledge? |
It should. However i do prefer to do stuff explicitly: lower failure chances. |
While fetching amounts of bits used to correctly display ubsan value reinterpret_cast was used, however as noted by Jakub Jelínek in #96240 discussion it might cause issues due to potentially unaligned memory access. The patch addresses this problem.