[PATCH] Fix -fsanitize=alignment reports on DenseMap empty/tombstone keys.

Alexey Samsonov vonosmas at gmail.com
Mon Dec 22 19:53:35 PST 2014


Hi chandlerc, rsmith,

Use large alignment when calculating empty/tombstone keys
for T*. We can't use alignof(T), as T may be forward-declared at
this point, and PointerLikeTypeTraits<T*>::NumLowBitsAvailable may
be too small.

http://reviews.llvm.org/D6768

Files:
  include/llvm/ADT/DenseMapInfo.h

Index: include/llvm/ADT/DenseMapInfo.h
===================================================================
--- include/llvm/ADT/DenseMapInfo.h
+++ include/llvm/ADT/DenseMapInfo.h
@@ -30,14 +30,21 @@
 // Provide DenseMapInfo for all pointers.
 template<typename T>
 struct DenseMapInfo<T*> {
+  // Objects of types T may require large alignment, which we might not know (if
+  // T is forward-declared). Generate empty/tombstone keys to account for that.
+  enum { MaxAlignmentLog = 5 };
   static inline T* getEmptyKey() {
     uintptr_t Val = static_cast<uintptr_t>(-1);
-    Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
+    Val <<= PointerLikeTypeTraits<T *>::NumLowBitsAvailable < MaxAlignmentLog
+                ? MaxAlignmentLog
+                : PointerLikeTypeTraits<T *>::NumLowBitsAvailable;
     return reinterpret_cast<T*>(Val);
   }
   static inline T* getTombstoneKey() {
     uintptr_t Val = static_cast<uintptr_t>(-2);
-    Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
+    Val <<= PointerLikeTypeTraits<T *>::NumLowBitsAvailable < MaxAlignmentLog
+                ? MaxAlignmentLog
+                : PointerLikeTypeTraits<T *>::NumLowBitsAvailable;
     return reinterpret_cast<T*>(Val);
   }
   static unsigned getHashValue(const T *PtrVal) {

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6768.17583.patch
Type: text/x-patch
Size: 1286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141223/67df175d/attachment.bin>


More information about the llvm-commits mailing list