[PATCH] Use properly aligned pointers for empty/tombstone DenseMap keys.
Alexey Samsonov
vonosmas at gmail.com
Tue Aug 19 13:30:15 PDT 2014
Hi rsmith, chandlerc,
We should respect alignOf<T*>() when creating empty/tombstone keys
for DenseMap<T*, foo>. Otherwise this can lead to casts on misaligned
addresses, which is undefined behavior, as reported by UBSan.
http://reviews.llvm.org/D4976
Files:
include/llvm/ADT/DenseMapInfo.h
Index: include/llvm/ADT/DenseMapInfo.h
===================================================================
--- include/llvm/ADT/DenseMapInfo.h
+++ include/llvm/ADT/DenseMapInfo.h
@@ -14,6 +14,7 @@
#ifndef LLVM_ADT_DENSEMAPINFO_H
#define LLVM_ADT_DENSEMAPINFO_H
+#include "llvm/Support/AlignOf.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/type_traits.h"
@@ -32,12 +33,20 @@
struct DenseMapInfo<T*> {
static inline T* getEmptyKey() {
uintptr_t Val = static_cast<uintptr_t>(-1);
- Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
+ const unsigned Alignment =
+ PointerLikeTypeTraits<T*>::NumLowBitsAvailable > alignOf<T*>()
+ ? (unsigned)PointerLikeTypeTraits<T*>::NumLowBitsAvailable
+ : alignOf<T*>();
+ Val <<= Alignment;
return reinterpret_cast<T*>(Val);
}
static inline T* getTombstoneKey() {
uintptr_t Val = static_cast<uintptr_t>(-2);
- Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
+ const unsigned Alignment =
+ PointerLikeTypeTraits<T*>::NumLowBitsAvailable > alignOf<T*>()
+ ? (unsigned)PointerLikeTypeTraits<T*>::NumLowBitsAvailable
+ : alignOf<T*>();
+ Val <<= Alignment;
return reinterpret_cast<T*>(Val);
}
static unsigned getHashValue(const T *PtrVal) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4976.12680.patch
Type: text/x-patch
Size: 1332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140819/e25d68e6/attachment.bin>
More information about the llvm-commits
mailing list