[PATCH] D128783: Check for more -fsanitize=array-bounds regressions

Stephan Bergmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 28 23:33:37 PDT 2022


sberg created this revision.
sberg added a reviewer: MaskRay.
sberg added a project: clang.
Herald added a subscriber: StephenFan.
Herald added a project: All.
sberg requested review of this revision.

...that had been introduced with (since reverted) https://github.com/llvm/llvm-project/commit/886715af962de2c92fac4bd37104450345711e4a "[clang] Introduce -fstrict-flex-arrays=<n> for stricter handling of flexible arrays", and caused issues in the wild:

For one, the HarfBuzz project has various "fake" flexible array members of the form

  Type                arrayZ[HB_VAR_ARRAY];

in https://github.com/harfbuzz/harfbuzz/blob/main/src/hb-open-type.hh, where `HB_VAR_ARRAY` is a macro defined as

  #ifndef HB_VAR_ARRAY
  #define HB_VAR_ARRAY 1
  #endif

in https://github.com/harfbuzz/harfbuzz/blob/main/src/hb-machinery.hh.

For another, the Firebird project in https://github.com/FirebirdSQL/firebird/blob/master/src/lock/lock_proto.h uses a trailing member

  srq lhb_hash[1];                        // Hash table

as a "fake" flexible array, but declared in a

  struct lhb : public Firebird::MemoryHeader

that is not a standard-layout class (because the `Firebird::MemoryHeader` base class also declares non-static data members).

(Checking for the second case required changing the test file from C to C++.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128783

Files:
  clang/test/CodeGen/bounds-checking-fam.c
  clang/test/CodeGen/bounds-checking-fam.cpp


Index: clang/test/CodeGen/bounds-checking-fam.cpp
===================================================================
--- clang/test/CodeGen/bounds-checking-fam.cpp
+++ clang/test/CodeGen/bounds-checking-fam.cpp
@@ -14,21 +14,43 @@
 struct Three {
   int a[3];
 };
+#define FLEXIBLE 1
+struct Macro {
+  int a[FLEXIBLE];
+};
+struct Base {
+  int b;
+};
+struct NoStandardLayout : Base {
+  int a[1];
+};
 
-// CHECK-LABEL: define {{.*}} @test_one(
-int test_one(struct One *p, int i) {
+// CHECK-LABEL: define {{.*}} @{{.*}}test_one{{.*}}(
+int test_one(One *p, int i) {
   // CHECK-STRICT-0-NOT: @__ubsan
   return p->a[i] + (p->a)[i];
 }
 
-// CHECK-LABEL: define {{.*}} @test_two(
-int test_two(struct Two *p, int i) {
+// CHECK-LABEL: define {{.*}} @{{.*}}test_two{{.*}}(
+int test_two(Two *p, int i) {
   // CHECK-STRICT-0:     call void @__ubsan_handle_out_of_bounds_abort(
   return p->a[i] + (p->a)[i];
 }
 
-// CHECK-LABEL: define {{.*}} @test_three(
-int test_three(struct Three *p, int i) {
+// CHECK-LABEL: define {{.*}} @{{.*}}test_three{{.*}}(
+int test_three(Three *p, int i) {
   // CHECK-STRICT-0:     call void @__ubsan_handle_out_of_bounds_abort(
   return p->a[i] + (p->a)[i];
 }
+
+// CHECK-LABEL: define {{.*}} @{{.*}}test_macro{{.*}}(
+int test_macro(Macro *p, int i) {
+  // CHECK-STRICT-0-NOT: @__ubsan
+  return p->a[i] + (p->a)[i];
+}
+
+// CHECK-LABEL: define {{.*}} @{{.*}}test_nostandardlayout{{.*}}(
+int test_nostandardlayout(NoStandardLayout *p, int i) {
+  // CHECK-STRICT-0-NOT: @__ubsan
+  return p->a[i] + (p->a)[i];
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128783.440863.patch
Type: text/x-patch
Size: 1558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220629/a11a5c68/attachment-0001.bin>


More information about the cfe-commits mailing list