Fix UBSan error reports in ValueMapCallbackVH and AssertingVH<T> empty/tombstone keys generation.

Summary:
One more attempt to fix UBSan reports: make sure DenseMapInfo::getEmptyKey()
and DenseMapInfo::getTombstoneKey() doesn't do any upcasts/downcasts to/from Value*.

Test Plan: check-llvm test suite with/without UBSan bootstrap

Reviewers: chandlerc, dexonsmith

Subscribers: llvm-commits, majnemer

Differential Revision: http://reviews.llvm.org/D6903

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225558 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov
2015-01-09 23:17:25 +00:00
parent 43cc8a5fd1
commit f79e2d40b2
2 changed files with 27 additions and 32 deletions

View File

@ -190,23 +190,20 @@ class AssertingVH
friend struct DenseMapInfo<AssertingVH<ValueTy> >; friend struct DenseMapInfo<AssertingVH<ValueTy> >;
#ifndef NDEBUG #ifndef NDEBUG
ValueTy *getValPtr() const { Value *getRawValPtr() const { return ValueHandleBase::getValPtr(); }
return static_cast<ValueTy*>(ValueHandleBase::getValPtr()); void setRawValPtr(Value *P) { ValueHandleBase::operator=(P); }
}
void setValPtr(ValueTy *P) {
ValueHandleBase::operator=(GetAsValue(P));
}
#else #else
ValueTy *ThePtr; Value *ThePtr;
ValueTy *getValPtr() const { return ThePtr; } Value *getRawValPtr() const { return ThePtr; }
void setValPtr(ValueTy *P) { ThePtr = P; } void setRawValPtr(Value *P) { ThePtr = P; }
#endif #endif
// Convert a ValueTy*, which may be const, to the raw Value*.
// Convert a ValueTy*, which may be const, to the type the base
// class expects.
static Value *GetAsValue(Value *V) { return V; } static Value *GetAsValue(Value *V) { return V; }
static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); } static Value *GetAsValue(const Value *V) { return const_cast<Value*>(V); }
ValueTy *getValPtr() const { return static_cast<ValueTy *>(getRawValPtr()); }
void setValPtr(ValueTy *P) { setRawValPtr(GetAsValue(P)); }
public: public:
#ifndef NDEBUG #ifndef NDEBUG
AssertingVH() : ValueHandleBase(Assert) {} AssertingVH() : ValueHandleBase(Assert) {}
@ -214,7 +211,7 @@ public:
AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {} AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
#else #else
AssertingVH() : ThePtr(nullptr) {} AssertingVH() : ThePtr(nullptr) {}
AssertingVH(ValueTy *P) : ThePtr(P) {} AssertingVH(ValueTy *P) : ThePtr(GetAsValue(P)) {}
#endif #endif
operator ValueTy*() const { operator ValueTy*() const {
@ -237,27 +234,23 @@ public:
// Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap. // Specialize DenseMapInfo to allow AssertingVH to participate in DenseMap.
template<typename T> template<typename T>
struct DenseMapInfo<AssertingVH<T> > { struct DenseMapInfo<AssertingVH<T> > {
typedef DenseMapInfo<T*> PointerInfo;
static inline AssertingVH<T> getEmptyKey() { static inline AssertingVH<T> getEmptyKey() {
return AssertingVH<T>(PointerInfo::getEmptyKey()); AssertingVH<T> Res;
Res.setRawValPtr(DenseMapInfo<Value *>::getEmptyKey());
return Res;
} }
static inline T* getTombstoneKey() { static inline AssertingVH<T> getTombstoneKey() {
return AssertingVH<T>(PointerInfo::getTombstoneKey()); AssertingVH<T> Res;
Res.setRawValPtr(DenseMapInfo<Value *>::getTombstoneKey());
return Res;
} }
static unsigned getHashValue(const AssertingVH<T> &Val) { static unsigned getHashValue(const AssertingVH<T> &Val) {
return PointerInfo::getHashValue(Val); return DenseMapInfo<Value *>::getHashValue(Val.getRawValPtr());
} }
#ifndef NDEBUG
static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) { static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
// Avoid downcasting AssertingVH<T> to T*, as empty/tombstone keys may not return DenseMapInfo<Value *>::isEqual(LHS.getRawValPtr(),
// be properly aligned pointers to T*. RHS.getRawValPtr());
return LHS.ValueHandleBase::getValPtr() == RHS.ValueHandleBase::getValPtr();
} }
#else
static bool isEqual(const AssertingVH<T> &LHS, const AssertingVH<T> &RHS) {
return LHS == RHS;
}
#endif
}; };
template <typename T> template <typename T>

View File

@ -224,6 +224,9 @@ class ValueMapCallbackVH : public CallbackVH {
: CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))), : CallbackVH(const_cast<Value*>(static_cast<const Value*>(Key))),
Map(Map) {} Map(Map) {}
// Private constructor used to create empty/tombstone DenseMap keys.
ValueMapCallbackVH(Value *V) : CallbackVH(V), Map(nullptr) {}
public: public:
KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); } KeyT Unwrap() const { return cast_or_null<KeySansPointerT>(getValPtr()); }
@ -266,19 +269,18 @@ public:
template<typename KeyT, typename ValueT, typename Config> template<typename KeyT, typename ValueT, typename Config>
struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > { struct DenseMapInfo<ValueMapCallbackVH<KeyT, ValueT, Config> > {
typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH; typedef ValueMapCallbackVH<KeyT, ValueT, Config> VH;
typedef DenseMapInfo<KeyT> PointerInfo;
static inline VH getEmptyKey() { static inline VH getEmptyKey() {
return VH(PointerInfo::getEmptyKey(), nullptr); return VH(DenseMapInfo<Value *>::getEmptyKey());
} }
static inline VH getTombstoneKey() { static inline VH getTombstoneKey() {
return VH(PointerInfo::getTombstoneKey(), nullptr); return VH(DenseMapInfo<Value *>::getTombstoneKey());
} }
static unsigned getHashValue(const VH &Val) { static unsigned getHashValue(const VH &Val) {
return PointerInfo::getHashValue(Val.Unwrap()); return DenseMapInfo<KeyT>::getHashValue(Val.Unwrap());
} }
static unsigned getHashValue(const KeyT &Val) { static unsigned getHashValue(const KeyT &Val) {
return PointerInfo::getHashValue(Val); return DenseMapInfo<KeyT>::getHashValue(Val);
} }
static bool isEqual(const VH &LHS, const VH &RHS) { static bool isEqual(const VH &LHS, const VH &RHS) {
return LHS == RHS; return LHS == RHS;