Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions clang/lib/StaticAnalyzer/Checkers/PointerAlignmentChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,36 @@ int getTrailingZerosCount(const MemRegion *R, ProgramStateRef State,
return -1;
unsigned NaturalAlign = ASTCtx.getTypeAlignInChars(PT).getQuantity();

if (const FieldRegion *FR = R->getAs<FieldRegion>()) {
unsigned FullOffsetInChars = 0;
for (auto *SubR = R->getAs<SubRegion>(); SubR;
SubR = SubR->getSuperRegion()->getAs<SubRegion>()) {
// If this is the a field of a larger struct, we can use the alignment
// of the containing struct combined with the offset to try to increase
// the assumed alignment.
const RegionOffset &Offset = FR->getAsOffset();
if (!Offset.hasSymbolicOffset()) {
if (const auto *Base =
FR->getSuperRegion()->getAs<TypedValueRegion>()) {
auto BaseTy = Base->getValueType();
unsigned BaseAlign = ASTCtx.getTypeAlignInChars(BaseTy).getQuantity();
uint64_t FieldOffsetBits = Offset.getOffset();
unsigned Offset =
ASTCtx.toCharUnitsFromBits(FieldOffsetBits).getQuantity();
unsigned OffsetAlign =
(Offset == 0) ? BaseAlign : (1ULL << llvm::countr_zero(Offset));
unsigned InferredAlign = std::min(BaseAlign, OffsetAlign);
NaturalAlign = std::max(NaturalAlign, InferredAlign);
}
}
const auto *FR = SubR->getAs<FieldRegion>();
if (!FR)
break;

const RegionOffset &LocalOffset = FR->getAsOffset();
if (LocalOffset.hasSymbolicOffset())
break;

const auto *Base = FR->getSuperRegion()->getAs<TypedValueRegion>();
if (!Base)
break;

auto BaseTy = Base->getValueType();
unsigned BaseAlign = ASTCtx.getTypeAlignInChars(BaseTy).getQuantity();
uint64_t LocalOffsetBits = LocalOffset.getOffset();
FullOffsetInChars +=
ASTCtx.toCharUnitsFromBits(LocalOffsetBits).getQuantity();
unsigned FullOffsetAlign =
(FullOffsetInChars == 0)
? BaseAlign
: (1ULL << llvm::countr_zero(FullOffsetInChars));

unsigned InferredAlign = std::min(BaseAlign, FullOffsetAlign);
NaturalAlign = std::max(NaturalAlign, InferredAlign);
}

if (const ElementRegion *ER = R->getAs<ElementRegion>()) {
Expand Down
20 changes: 20 additions & 0 deletions clang/test/Analysis/pointer-alignment.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,23 @@ long long * __sealed_capability sealed_cast(int * __sealed_capability in) {
// verified when unsealed.
return (long long * __sealed_capability)in;
}

// ---
struct __attribute__((aligned(8))) AlignedOuterStruct {
struct UnalignedInnerStruct {
int a;
int b;
int c;
int d;
} inner;
};

void write_to_first_member2(struct AlignedOuterStruct *chunk) {
// No warning because alignment of is inferred from the parent struct.
*(long long*)(&chunk->inner.a) = 0;
}

void write_to_second_member2(struct AlignedOuterStruct *chunk) {
// No warning because alignment of is inferred from the parent struct.
*(long long*)(&chunk->inner.c) = 0;
}