[cfe-dev] Alignment problems in Clang's internal data structures?
Reid Kleckner
rnk at google.com
Fri Feb 27 10:17:46 PST 2015
On Tue, Feb 17, 2015 at 1:42 AM, Renato Golin <renato.golin at linaro.org>
wrote:
> On 17 February 2015 at 00:38, Ahmed Bougacha <ahmed.bougacha at gmail.com>
> wrote:
> > std::size_t Size = sizeof(DeclRefExpr);
> > if (...)
> > Size += sizeof(...);
> > ...
> > void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
>
> This sounds wrong. You should have something like:
>
> std::size_t Size = sizeof(DeclRefExpr);
> std::size_t Align = llvm::alignOf<DeclRefExpr>();
> if (...) {
> Size += sizeof(...);
> Align = std::max(Align, llvm::alignOf<...>());
> }
> ...
> void *Mem = Context.Allocate(Size, Align);
This isn't enough either. You need to update the accessors to account for
alignment. They currently look like this:
NamedDecl *&getInternalFoundDecl() {
assert(hasFoundDecl());
if (hasQualifier())
return *reinterpret_cast<NamedDecl **>(&getInternalQualifierLoc() +
1);
return *reinterpret_cast<NamedDecl **>(this + 1);
}
I do *not* want to audit Clang for the 'reinterpret_cast<Foo*>(this + 1)'
pattern and add extra alignment code. We should just make sure that
anything allocated this way requires only pointer-size alignment.
I put this on http://llvm.org/pr22727.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150227/ed52de93/attachment.html>
More information about the cfe-dev
mailing list