[PATCH] D143300: [randstruct] Don't allow implicit forward decl to stop struct randomization
Bill Wendling via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 6 09:58:10 PST 2023
void updated this revision to Diff 495194.
void added a comment.
Fix test and add check for EnumDecl.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D143300/new/
https://reviews.llvm.org/D143300
Files:
clang/lib/Sema/SemaDecl.cpp
clang/test/CodeGen/init-randomized-struct-fwd-decl.c
Index: clang/test/CodeGen/init-randomized-struct-fwd-decl.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/init-randomized-struct-fwd-decl.c
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -triple=x86_64-unknown-linux -emit-llvm -frandomize-layout-seed=1234567890abcdef < %s | FileCheck %s
+// RUN: %clang_cc1 -triple=x86_64-unknown-linux -emit-llvm -frandomize-layout-seed=1234567890abcdef -DFORWARD_DECL < %s | FileCheck -check-prefix=FWD-DECL %s
+// PR60349
+
+// Clang will add a forward declaration of "struct bar" and "enum qux" to the
+// structures. This shouldn't prevent these structures from being randomized.
+// So the 'f' element shouldn't be at the start of the structure anymore.
+
+#ifdef FORWARD_DECL
+struct bar;
+enum qux;
+#endif
+
+struct foo {
+ struct bar *(*f)(void);
+ struct bar *(*g)(void);
+ struct bar *(*h)(void);
+ struct bar *(*i)(void);
+ struct bar *(*j)(void);
+ struct bar *(*k)(void);
+};
+
+// CHECK-LABEL: define {{.*}}@t1
+// CHECK-NOT: getelementptr inbounds %struct.foo, ptr %3, i32 0, i32 0
+
+// FWD-DECL-LABEL: define {{.*}}@t1
+// FWD-DECL-NOT: getelementptr inbounds %struct.foo, ptr %3, i32 0, i32 0
+struct bar *t1(struct foo *z) {
+ return z->f();
+}
+
+struct baz {
+ enum qux *(*f)(void);
+ enum qux *(*g)(void);
+ enum qux *(*h)(void);
+ enum qux *(*i)(void);
+ enum qux *(*j)(void);
+ enum qux *(*k)(void);
+};
+
+// CHECK-LABEL: define {{.*}}@t2
+// CHECK-NOT: getelementptr inbounds %struct.baz, ptr %3, i32 0, i32 0
+
+// FWD-DECL-LABEL: define {{.*}}@t2
+// FWD-DECL-NOT: getelementptr inbounds %struct.baz, ptr %3, i32 0, i32 0
+enum qux *t2(struct baz *z) {
+ return z->f();
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -18874,10 +18874,26 @@
ProcessDeclAttributeList(S, Record, Attrs);
// Check to see if a FieldDecl is a pointer to a function.
- auto IsFunctionPointer = [&](const Decl *D) {
+ auto IsFunctionPointerOrForwardDecl = [&](const Decl *D) {
const FieldDecl *FD = dyn_cast<FieldDecl>(D);
- if (!FD)
+ if (!FD) {
+ // Check whether this is a forward declaration that was inserted by
+ // Clang. This happens when a non-forward declared / defined type is
+ // used, e.g.:
+ //
+ // struct foo {
+ // struct bar *(*f)();
+ // struct bar *(*g)();
+ // };
+ //
+ // "struct bar" shows up in the decl AST as a "RecordDecl" with an
+ // incomplete definition.
+ if (const auto *RD = dyn_cast<RecordDecl>(D))
+ return !RD->isCompleteDefinition();
+ if (const auto *ED = dyn_cast<EnumDecl>(D))
+ return !ED->isCompleteDefinition();
return false;
+ }
QualType FieldType = FD->getType().getDesugaredType(Context);
if (isa<PointerType>(FieldType)) {
QualType PointeeType = cast<PointerType>(FieldType)->getPointeeType();
@@ -18891,7 +18907,7 @@
if (!getLangOpts().CPlusPlus &&
(Record->hasAttr<RandomizeLayoutAttr>() ||
(!Record->hasAttr<NoRandomizeLayoutAttr>() &&
- llvm::all_of(Record->decls(), IsFunctionPointer))) &&
+ llvm::all_of(Record->decls(), IsFunctionPointerOrForwardDecl))) &&
!Record->isUnion() && !getLangOpts().RandstructSeed.empty() &&
!Record->isRandomized()) {
SmallVector<Decl *, 32> NewDeclOrdering;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143300.495194.patch
Type: text/x-patch
Size: 3533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230206/2621e311/attachment-0001.bin>
More information about the cfe-commits
mailing list