Fix undefined behavior when compiling in C++14 due to sized operator delete

being called with the wrong size: convert CGFunctionInfo to use TrailingObjects
and ask TrailingObjects to provide a working 'operator delete' for us.

llvm-svn: 260181
This commit is contained in:
Richard Smith 2016-02-09 01:05:04 +00:00
parent 1b65c3279d
commit c99f11b373
2 changed files with 20 additions and 9 deletions

View File

@ -20,6 +20,7 @@
#include "clang/AST/CharUnits.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/TrailingObjects.h"
#include <cassert>
namespace llvm {
@ -331,13 +332,19 @@ public:
}
};
// Implementation detail of CGFunctionInfo, factored out so it can be named
// in the TrailingObjects base class of CGFunctionInfo.
struct CGFunctionInfoArgInfo {
CanQualType type;
ABIArgInfo info;
};
/// CGFunctionInfo - Class to encapsulate the information about a
/// function definition.
class CGFunctionInfo : public llvm::FoldingSetNode {
struct ArgInfo {
CanQualType type;
ABIArgInfo info;
};
class CGFunctionInfo final
: public llvm::FoldingSetNode,
private llvm::TrailingObjects<CGFunctionInfo, CGFunctionInfoArgInfo> {
typedef CGFunctionInfoArgInfo ArgInfo;
/// The LLVM::CallingConv to use for this function (as specified by the
/// user).
@ -374,13 +381,17 @@ class CGFunctionInfo : public llvm::FoldingSetNode {
unsigned ArgStructAlign;
unsigned NumArgs;
ArgInfo *getArgsBuffer() {
return reinterpret_cast<ArgInfo*>(this+1);
return getTrailingObjects<ArgInfo>();
}
const ArgInfo *getArgsBuffer() const {
return reinterpret_cast<const ArgInfo*>(this + 1);
return getTrailingObjects<ArgInfo>();
}
size_t numTrailingObjects(OverloadToken<ArgInfo>) { return NumArgs + 1; }
friend class TrailingObjects;
CGFunctionInfo() : Required(RequiredArgs::All) {}
public:
@ -391,6 +402,7 @@ public:
CanQualType resultType,
ArrayRef<CanQualType> argTypes,
RequiredArgs required);
void operator delete(void *p) { TrailingObjects::operator delete(p); }
typedef const ArgInfo *const_arg_iterator;
typedef ArgInfo *arg_iterator;

View File

@ -569,8 +569,7 @@ CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
CanQualType resultType,
ArrayRef<CanQualType> argTypes,
RequiredArgs required) {
void *buffer = operator new(sizeof(CGFunctionInfo) +
sizeof(ArgInfo) * (argTypes.size() + 1));
void *buffer = operator new(totalSizeToAlloc<ArgInfo>(argTypes.size() + 1));
CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
FI->CallingConvention = llvmCC;
FI->EffectiveCallingConvention = llvmCC;