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

Seedarg/member/wip #5

Closed
wants to merge 104 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
3b9829e
Add "randomize_layout" attribute
connorkuehl Feb 1, 2019
9b0f3de
testing: Emit message when randomize_layout
connorkuehl Feb 1, 2019
f8da259
Rename Randomized to RandomizeLayout
connorkuehl Feb 1, 2019
7d221cd
fix: Go back to using D's field iterators
connorkuehl Feb 1, 2019
5749304
fix: Oops, actually use correct iterators
connorkuehl Feb 2, 2019
18bcbc0
Add reorder method for RecordDecl
connorkuehl Feb 7, 2019
8a27707
Fix infinite loop by updating tail ptr
connorkuehl Feb 7, 2019
a3fa3dd
Hack: quick and dirty randomization for testing!
connorkuehl Feb 7, 2019
4ff065b
Move Randomization to getASTRecordLayout
connorkuehl Feb 7, 2019
5d6b7c1
Added the 'no_randomize_layout' attr to clang
Nixoncole Feb 12, 2019
1ed67ef
Add performance sensitive randomization
connorkuehl Feb 13, 2019
4ba74e9
Merge pull request #17 from connorkuehl/port-randomization
Feb 15, 2019
73325c9
Delete hacky reorderFields method
connorkuehl Feb 15, 2019
53fec8d
Merge branch 'develop' of https://github.com/connorkuehl/llvm-project…
Nixoncole Feb 18, 2019
456bd5a
WIP, first attempt at emitting compiler warning
Nixoncole Feb 19, 2019
16160e3
Refactor code into tidier classes
connorkuehl Feb 19, 2019
b1f11a3
Added declaration of Diagnostic(), receiving errors.
Nixoncole Feb 19, 2019
dee16c7
builds, but cannot compile. Cant find stdio.h
Nixoncole Feb 20, 2019
bf4b55f
Sync with upstream
connorkuehl Feb 20, 2019
063f1b1
Added the 'no_randomize_layout' attr to clang
Nixoncole Feb 12, 2019
938b5cf
WIP, first attempt at emitting compiler warning
Nixoncole Feb 19, 2019
e1af06f
Added declaration of Diagnostic(), receiving errors.
Nixoncole Feb 19, 2019
a4a855d
builds, but cannot compile. Cant find stdio.h
Nixoncole Feb 20, 2019
11d18d7
putting RandstructSeed in Decl.h
Feb 21, 2019
d76e98b
RandomizeLayout identification for regression test This commit is to
Feb 21, 2019
3deff88
remove RandstructSeed declaration from Decl.h
Feb 21, 2019
753b226
Revert commit so it may be introduced by PR
connorkuehl Feb 21, 2019
69c348e
RandomizeLayout identification for regression test
Feb 21, 2019
5e9c123
RandomizeLayout identification for regression test
Feb 21, 2019
12c5b41
Merge branch 'RandomizeLayout_Regression_Test_Addition' of https://gi…
Feb 22, 2019
162bda5
value not being saved in global
Feb 22, 2019
97bb5ce
removing unused coede
Feb 22, 2019
6ef63eb
removing randstruct constructor call
Feb 22, 2019
e0cfd8a
Added basic implementation of auto selection for strucutres of only f…
Jafosterja Feb 23, 2019
9d3845e
Clean up PR
Feb 23, 2019
287458e
Merge branch 'no_ranomize_layout_attr' of https://github.com/clang-ra…
Feb 23, 2019
f94de06
Add DiagGroup to NoRandomizeLayout attribute
connorkuehl Feb 23, 2019
fadd247
Add NoRandomizeLayout to supported attributes list
connorkuehl Feb 23, 2019
ae1bf92
Add space after semicolon in warning message
connorkuehl Feb 23, 2019
b3a2306
test commit
Feb 23, 2019
a9200c2
printf debugging
Feb 23, 2019
5ceee39
manually adding arg before executing
Feb 24, 2019
5dd2a45
adding include to job.cpp
Feb 24, 2019
7272914
First draft for attribute documentation
Nixoncole Feb 24, 2019
76f2bd1
throwaway commit
Feb 24, 2019
c4863cc
more printf debugging
Feb 24, 2019
1dbbe31
even more printf debugging
Feb 24, 2019
09a54e9
Edit ClangRandstruct entry in AttrDocs.td
Feb 24, 2019
d1d871c
added frandstruct-seed= to CC1Options.td
Feb 24, 2019
efa2157
added frandstruct-seed= to CC1Options.td
Feb 24, 2019
7a329b5
removing =
Feb 24, 2019
495b157
removing =
Feb 24, 2019
651fdc4
error: linker -f may not be used without -shared
Feb 24, 2019
013a77d
error: linker -f may not be used without -shared
Feb 24, 2019
184695d
error: linker -f may not be used without -shared
Feb 24, 2019
0594d7e
gen. commit
Feb 24, 2019
3656e01
wip fixing linker errors
Feb 24, 2019
f07aff2
wip fixing linker errors
Feb 24, 2019
213041f
wip fixing linker errors
Feb 25, 2019
ab0393f
wip fixing linker errors
Feb 25, 2019
d923c6f
wip fixing linker errors
Feb 25, 2019
25246d2
wip fixing linker errors
Feb 25, 2019
1455704
wip fixing linker errors
Feb 25, 2019
3dcf748
wip fixing linker errors
Feb 25, 2019
b47a59e
(#20) Merge RandomizeLayout regression test
connorkuehl Feb 25, 2019
46b491e
generic commit
Feb 25, 2019
d149823
generic commit
Feb 25, 2019
58cfc9b
generic commit
Feb 25, 2019
e0dcc7a
generic commit
Feb 25, 2019
8590e8e
generic commit
Feb 25, 2019
690b679
generic commit
Feb 25, 2019
82bd310
generic commit
Feb 26, 2019
9949798
Docs for the single attribute, 'randomize_layout'.
Nixoncole Feb 26, 2019
f06d4cc
touched up other docs ad ran 'clang-format'
Nixoncole Feb 26, 2019
6fedc9d
Refactored the auto selection code
Jafosterja Feb 26, 2019
6900a62
generic commit
Feb 26, 2019
9cab894
Changed frandstruct to randstruct
Feb 26, 2019
ca2372f
Removed 'header' that is only for when you include docs for multiple …
Nixoncole Feb 26, 2019
63cf353
Added comments
Jafosterja Feb 26, 2019
a9655bd
Merge pull request #27 from clang-randstruct/randomize_layout_docs
jeffreytakahashi Feb 26, 2019
f5e549d
Merge pull request #26 from jcantrell/seedarg/global/wip
Feb 26, 2019
312a0e6
Revert "(#20) Merge RandomizeLayout regression test"
Feb 26, 2019
32fae72
Re-adding regression test fix
Feb 26, 2019
d95e624
Revert "generic commit"
Feb 26, 2019
fe25c0e
Skip Unions when selecting from randomization
connorkuehl Feb 26, 2019
1731e3a
Add warning when union has randomize_layout attr
connorkuehl Feb 26, 2019
e39c922
Removed const qualifier from reorganize() and randomize(), since rng …
Feb 27, 2019
e33d38e
merging changes from develop
Feb 27, 2019
ca6c6d8
Merge pull request #35 from clang-randstruct/seedarg/global/wip
Feb 27, 2019
be0971b
Merge pull request #33 from clang-randstruct/skip-unions
Feb 27, 2019
b4446bd
Removed unnecessary functions
Jafosterja Feb 27, 2019
0a9f695
adding flag
Feb 27, 2019
dd7a572
Merge branch 'develop' into randstruct-auto
connorkuehl Feb 27, 2019
3e53f43
Add -randstruct-auto command line argument
connorkuehl Feb 27, 2019
97166c8
Refactored code for better readability
Jafosterja Feb 27, 2019
a8106ca
Merge branch 'randstruct-auto' into Autoselect
connorkuehl Feb 27, 2019
2a220f2
Delete NoRandomizeLayout check
connorkuehl Feb 27, 2019
46a4a57
merging in develop
Feb 28, 2019
2c5f3eb
Added changes requested by Nixoncole
Jafosterja Feb 28, 2019
3a0479d
Merge pull request #29 from clang-randstruct/Autoselect
Feb 28, 2019
20696c1
merging in changes from develop
Feb 28, 2019
e3be585
generic commit
Feb 28, 2019
dccaff2
generic commit
Feb 28, 2019
4a6549a
ran clang-format on RecordField/Layout files
Feb 28, 2019
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
1 change: 1 addition & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Module;
class NamespaceDecl;
class ParmVarDecl;
class RecordDecl;
class RecordFieldReorganizer;
class Stmt;
class StringLiteral;
class TagDecl;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,8 @@ class DeclContext {
friend class ExternalASTSource;
/// For CreateStoredDeclsMap
friend class DependentDiagnostic;
/// For fine-grained control of field order
friend class RecordFieldReorganizer;
/// For hasNeedToReconcileExternalVisibleStorage,
/// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups
friend class ASTWriter;
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/AST/RandstructSeed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef RANDSTRUCTSEED_H
#define RANDSTRUCTSEED_H
#include <string>
namespace clang {
extern std::string RandstructSeed;
extern bool RandstructAutoSelect;
}
#endif
61 changes: 61 additions & 0 deletions clang/include/clang/AST/RecordFieldReorganizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===-- RecordFieldReorganizer.h - Interface for manipulating field order --*-
// C++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This header file contains the base class that defines an interface for
// manipulating a RecordDecl's field layouts.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_AST_RECORDFIELDREORGANIZER_H
#define LLVM_CLANG_LIB_AST_RECORDFIELDREORGANIZER_H

#include "Decl.h"
#include <random>

namespace clang {

// FIXME: Find a better alternative to SmallVector with hardcoded size!

class RecordFieldReorganizer {
public:
virtual ~RecordFieldReorganizer() = default;
void reorganizeFields(const ASTContext &C, const RecordDecl *D);

protected:
virtual void reorganize(const ASTContext &C, const RecordDecl *D,
SmallVector<Decl *, 64> &NewOrder) = 0;

private:
void commit(const RecordDecl *D,
SmallVectorImpl<Decl *> &NewFieldOrder) const;
};

class Randstruct : public RecordFieldReorganizer {
private:
std::seed_seq Seq;
std::default_random_engine rng;

public:
/// Determines if the Record can be safely and easily randomized based on
/// certain criteria (see implementation).
Randstruct(std::string seed) : Seq(seed.begin(), seed.end()), rng(Seq) {}
static bool isTriviallyRandomizable(const RecordDecl *D);

protected:
SmallVector<Decl *, 64> randomize(SmallVector<Decl *, 64> fields);
SmallVector<Decl *, 64> perfrandomize(const ASTContext &ctx,
SmallVector<Decl *, 64> fields);
virtual void reorganize(const ASTContext &C, const RecordDecl *D,
SmallVector<Decl *, 64> &NewOrder) override;
};

} // namespace clang

#endif
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -3201,3 +3201,10 @@ def ObjCExternallyRetained : InheritableAttr {
let Subjects = SubjectList<[NonParmVar, Function, Block, ObjCMethod]>;
let Documentation = [ObjCExternallyRetainedDocs];
}

def RandomizeLayout : InheritableAttr {
let Spellings = [GCC<"randomize_layout">, Declspec<"randomize_layout">,
Keyword<"randomize_layout">];
let Subjects = SubjectList<[Record]>;
let Documentation = [ClangRandstructDocs];
}
20 changes: 19 additions & 1 deletion clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -4094,4 +4094,22 @@ Likewise, when applied to a strong local variable, that variable becomes
``const`` and is considered externally-retained.

When compiled without ``-fobjc-arc``, this attribute is ignored.
}]; }
}];
}

def ClangRandstructDocs : Documentation {
let Category = DocCatVariable;
let Content = [{
The attribute ``randomize_layout`` can be applied to the declaration of
a record. ``randomize_layout`` instructs the compiler to randomize the memory layout
of the member variables of the record.
.. code-block:: c

// Indicates that this struct should be randomized by Randstruct implementation.
struct s {
char *a;
char *b;
char *c;
}__attribute__((randomize_layout));
}];
}
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticASTKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,6 @@ def warn_padded_struct_size : Warning<
InGroup<Padded>, DefaultIgnore;
def warn_unnecessary_packed : Warning<
"packed attribute is unnecessary for %0">, InGroup<Packed>, DefaultIgnore;
def warn_randomize_attr_union : Warning<
"union declared with 'randomize_layout' attribute">, InGroup<DiagGroup<"randomize-layout">>;
}
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/CC1Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ def fcaret_diagnostics_max_lines :
HelpText<"Set the maximum number of source lines to show in a caret diagnostic">;
def fmessage_length : Separate<["-"], "fmessage-length">, MetaVarName<"<N>">,
HelpText<"Format message diagnostics so that they fit within N columns or fewer, when possible.">;
def randstruct_seed : Separate<["-"], "randstruct-seed">, MetaVarName<"<N>">,
HelpText<"Randomization seed for random struct layouts">;
def verify_EQ : CommaJoined<["-"], "verify=">,
MetaVarName<"<prefixes>">,
HelpText<"Verify diagnostic output using comment directives that start with"
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">,
def fmerge_all_constants : Flag<["-"], "fmerge-all-constants">, Group<f_Group>,
Flags<[CC1Option, CoreOption]>, HelpText<"Allow merging of constants">;
def fmessage_length_EQ : Joined<["-"], "fmessage-length=">, Group<f_Group>;
def randstruct_seed_EQ : Joined<["-"], "randstruct-seed=">, Group<f_Group>;
def fms_extensions : Flag<["-"], "fms-extensions">, Group<f_Group>, Flags<[CC1Option, CoreOption]>,
HelpText<"Accept some non-standard constructs supported by the Microsoft compiler">;
def fms_compatibility : Flag<["-"], "fms-compatibility">, Group<f_Group>, Flags<[CC1Option, CoreOption]>,
Expand Down Expand Up @@ -1750,6 +1751,9 @@ def freroll_loops : Flag<["-"], "freroll-loops">, Group<f_Group>,
HelpText<"Turn on loop reroller">, Flags<[CC1Option]>;
def fno_reroll_loops : Flag<["-"], "fno-reroll-loops">, Group<f_Group>,
HelpText<"Turn off loop reroller">;
def randstruct_auto : Flag<["-"], "randstruct-auto">,
HelpText<"Enable automatic structure selection for field randomization; "
"Disable for specific structures with attribute no_randomize_layout">, Flags<[CC1Option]>;
def ftrigraphs : Flag<["-"], "ftrigraphs">, Group<f_Group>,
HelpText<"Process trigraph sequences">, Flags<[CC1Option]>;
def fno_trigraphs : Flag<["-"], "fno-trigraphs">, Group<f_Group>,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_clang_library(clangAST
InheritViz.cpp
ItaniumCXXABI.cpp
ItaniumMangle.cpp
RecordFieldReorganizer.cpp
Mangle.cpp
MicrosoftCXXABI.cpp
MicrosoftMangle.cpp
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ DeclContext::BuildDeclChain(ArrayRef<Decl *> Decls,
PrevDecl = D;
}

// The last one in the chain should have a null next!
PrevDecl->NextInContextAndBits.setPointer(nullptr);

return std::make_pair(FirstNewDecl, PrevDecl);
}

Expand Down
Loading