Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoselect #29

Merged
merged 12 commits into from
Feb 28, 2019
4 changes: 4 additions & 0 deletions clang/include/clang/AST/RecordFieldReorganizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ class RecordFieldReorganizer {
private:
void commit(const RecordDecl *D,
SmallVectorImpl<Decl *> &NewFieldOrder) const;
bool autostructselect(const SmallVector<Decl *,64> fields) const;
};

class Randstruct : public RecordFieldReorganizer {
public:
//Automatic Structure selection

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method is public, we should use Doxygen syntax and give a better description here.

/// Determines if the Record can be safely and easily randomized based on certain criteria (see implementation).

bool isTriviallyRandomizable(const RecordDecl *D) const;
protected:
virtual void reorganize(const ASTContext &C, const RecordDecl *D,
SmallVector<Decl *, 64> &NewOrder) const override;
Expand Down
12 changes: 9 additions & 3 deletions clang/lib/AST/RecordFieldReorganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void RecordFieldReorganizer::reorganizeFields(const ASTContext &C,
mutateGuard.insert(f);
fields.push_back(f);
}

// Now allow subclass implementations to reorder the fields
reorganize(C, D, fields);

Expand All @@ -50,7 +49,6 @@ void RecordFieldReorganizer::reorganizeFields(const ASTContext &C,

commit(D, fields);
}

void RecordFieldReorganizer::commit(
const RecordDecl *D, SmallVectorImpl<Decl *> &NewFieldOrder) const {
Decl *First, *Last;
Expand Down Expand Up @@ -249,5 +247,13 @@ void Randstruct::reorganize(const ASTContext &C, const RecordDecl *D,
SmallVector<Decl *, 64> randomized = perfrandomize(C, NewOrder);
NewOrder = randomized;
}

bool Randstruct::isTriviallyRandomizable(const RecordDecl *D) const {

for (auto f : D->fields()){
//If an element of the structure does not have a
//function type is not a function pointer
if(f->getFunctionType() == nullptr){ return false; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double spaces here intentional?

}
return true;
}
} // namespace clang
9 changes: 4 additions & 5 deletions clang/lib/AST/RecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2986,11 +2986,10 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
if (Entry) return *Entry;

const ASTRecordLayout *NewEntry = nullptr;

bool ShouldBeRandomized = D->getAttr<RandomizeLayoutAttr>() != nullptr;
if (ShouldBeRandomized) {
Randstruct randstruct;
randstruct.reorganizeFields(*this, D);
Randstruct randstruct;
bool ShouldBeRandomized = randstruct.isTriviallyRandomizable(D) || D->getAttr<RandomizeLayoutAttr>() != nullptr;
if(ShouldBeRandomized){
randstruct.reorganizeFields(*this, D);
}

if (isMsLayout(*this)) {
Expand Down