Skip to content

Commit 3a0479d

Browse files
author
Tim Pugh
authored
Merge pull request #29 from clang-randstruct/Autoselect
Autoselect
2 parents be0971b + 2c5f3eb commit 3a0479d

File tree

7 files changed

+25
-5
lines changed

7 files changed

+25
-5
lines changed

clang/include/clang/AST/RandstructSeed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#include <string>
44
namespace clang {
55
extern std::string RandstructSeed;
6+
extern bool RandstructAutoSelect;
67
}
78
#endif

clang/include/clang/AST/RecordFieldReorganizer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class RecordFieldReorganizer {
3737
};
3838

3939
class Randstruct : public RecordFieldReorganizer {
40+
public:
41+
/// Determines if the Record can be safely and easily randomized based on certain criteria (see implementation).
42+
static bool isTriviallyRandomizable(const RecordDecl *D);
4043
protected:
4144
virtual void reorganize(const ASTContext &C, const RecordDecl *D,
4245
SmallVector<Decl *, 64> &NewOrder) const override;

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,9 @@ def freroll_loops : Flag<["-"], "freroll-loops">, Group<f_Group>,
17511751
HelpText<"Turn on loop reroller">, Flags<[CC1Option]>;
17521752
def fno_reroll_loops : Flag<["-"], "fno-reroll-loops">, Group<f_Group>,
17531753
HelpText<"Turn off loop reroller">;
1754+
def randstruct_auto : Flag<["-"], "randstruct-auto">,
1755+
HelpText<"Enable automatic structure selection for field randomization; "
1756+
"Disable for specific structures with attribute no_randomize_layout">, Flags<[CC1Option]>;
17541757
def ftrigraphs : Flag<["-"], "ftrigraphs">, Group<f_Group>,
17551758
HelpText<"Process trigraph sequences">, Flags<[CC1Option]>;
17561759
def fno_trigraphs : Flag<["-"], "fno-trigraphs">, Group<f_Group>,

clang/lib/AST/RecordFieldReorganizer.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace clang {
2727
std::string RandstructSeed = "";
28+
bool RandstructAutoSelect = false;
2829

2930
void RecordFieldReorganizer::reorganizeFields(const ASTContext &C,
3031
const RecordDecl *D) const {
@@ -37,7 +38,6 @@ void RecordFieldReorganizer::reorganizeFields(const ASTContext &C,
3738
mutateGuard.insert(f);
3839
fields.push_back(f);
3940
}
40-
4141
// Now allow subclass implementations to reorder the fields
4242
reorganize(C, D, fields);
4343

@@ -52,7 +52,6 @@ void RecordFieldReorganizer::reorganizeFields(const ASTContext &C,
5252

5353
commit(D, fields);
5454
}
55-
5655
void RecordFieldReorganizer::commit(
5756
const RecordDecl *D, SmallVectorImpl<Decl *> &NewFieldOrder) const {
5857
Decl *First, *Last;
@@ -254,5 +253,12 @@ void Randstruct::reorganize(const ASTContext &C, const RecordDecl *D,
254253
SmallVector<Decl *, 64> randomized = perfrandomize(C, NewOrder);
255254
NewOrder = randomized;
256255
}
257-
256+
bool Randstruct::isTriviallyRandomizable(const RecordDecl *D) {
257+
for (auto f : D->fields()){
258+
//If an element of the structure does not have a
259+
//function type is not a function pointer
260+
if(f->getFunctionType() == nullptr){ return false; }
261+
}
262+
return true;
263+
}
258264
} // namespace clang

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,8 +2987,8 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
29872987
if (Entry) return *Entry;
29882988

29892989
const ASTRecordLayout *NewEntry = nullptr;
2990-
2991-
bool ShouldBeRandomized = D->getAttr<RandomizeLayoutAttr>() != nullptr;
2990+
2991+
bool ShouldBeRandomized = (RandstructAutoSelect && Randstruct::isTriviallyRandomizable(D)) || D->getAttr<RandomizeLayoutAttr>() != nullptr;
29922992
if (ShouldBeRandomized) {
29932993
// There is no technical benefit to randomizing the fields of a union
29942994
// since they all share the same offset of zero.

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,6 +4411,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
44114411
CmdArgs.push_back(A->getValue(0));
44124412
}
44134413

4414+
if (Arg *A = Args.getLastArg(options::OPT_randstruct_auto)) {
4415+
CmdArgs.push_back( "-randstruct-auto" );
4416+
}
4417+
44144418
// -fvisibility= and -fvisibility-ms-compat are of a piece.
44154419
if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ,
44164420
options::OPT_fvisibility_ms_compat)) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,9 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
16741674
if (const Arg* A = Args.getLastArg(OPT_randstruct_seed)) {
16751675
RandstructSeed = A->getValue(0);
16761676
}
1677+
if (const Arg* A = Args.getLastArg(OPT_randstruct_auto)) {
1678+
RandstructAutoSelect = true;
1679+
}
16771680
Opts.AddPluginActions = Args.getAllArgValues(OPT_add_plugin);
16781681
for (const auto *AA : Args.filtered(OPT_plugin_arg))
16791682
Opts.PluginArgs[AA->getValue(0)].emplace_back(AA->getValue(1));

0 commit comments

Comments
 (0)