clang-randstruct / llvm-project

Randomize the order of fields in a structure layout as a compile-time hardening feature
3 stars 1 forks source link

Refactor Randstruct code into tidier classes #19

Closed connorkuehl closed 5 years ago

connorkuehl commented 5 years ago

This PR compartmentalizes our code into tidier classes and removes the "hacky" RecordDecl::reorderFields() method that our code was the only client of.

Clang seems to make extensive use of friend classes, so, that's what I've done here.

The way it works is RecordFieldReorganizer is a base class that provides an interface for safely reordering a RecordDecl's fields. Sub-classes are required to override the protected reorganize method. This is what our new Randstruct class is doing.

Callers will then call reorganizeFields() which drives the safe reordering of a RecordDecl's fields. I say "safe" because this method will ensure that no fields are added or dropped as a result of the reordering, meaning that future subclasses cannot sabotage the Record (why would they though?)