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

Stephan Bergmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 30 02:01:45 PDT 2022

sberg updated this revision to Diff 441313.
sberg added a comment.

Updated the prospective git commit message as follow:

  [test] Check for more -fsanitize=array-bounds behavior
  ...that had temporarily regressed with (since reverted)
  "[clang] Introduce -fstrict-flex-arrays=<n> for stricter handling of flexible
  arrays", and had then been seen to cause issues in the wild:
  For one, the HarfBuzz project has various "fake" flexible array members of the
  > 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).
  (The second case is specific to C++.  Extend the test setup so that all the
  other tests are now run for both C and C++, just in case the behavior could ever
  start to diverge for those two languages.)
  Differential Revision: https://reviews.llvm.org/D128783




Index: clang/test/CodeGen/bounds-checking-fam.c
--- clang/test/CodeGen/bounds-checking-fam.c
+++ clang/test/CodeGen/bounds-checking-fam.c
@@ -1,5 +1,6 @@
 // REQUIRES: x86-registered-target
 // RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-0
+// RUN: %clang_cc1 -emit-llvm -triple x86_64 -fsanitize=array-bounds -x c++ %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-0,CXX,CXX-STRICT-0
 /// Before flexible array member was added to C99, many projects use a
 /// one-element array as the last emember of a structure as an alternative.
@@ -14,21 +15,48 @@
 struct Three {
   int a[3];
+#define FLEXIBLE 1
+struct Macro {
+  int a[FLEXIBLE];
-// CHECK-LABEL: define {{.*}} @test_one(
+// CHECK-LABEL: define {{.*}} @{{.*}}test_one{{.*}}(
 int test_one(struct One *p, int i) {
   // CHECK-STRICT-0-NOT: @__ubsan
   return p->a[i] + (p->a)[i];
-// CHECK-LABEL: define {{.*}} @test_two(
+// CHECK-LABEL: define {{.*}} @{{.*}}test_two{{.*}}(
 int test_two(struct 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(
+// CHECK-LABEL: define {{.*}} @{{.*}}test_three{{.*}}(
 int test_three(struct 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(struct Macro *p, int i) {
+  // CHECK-STRICT-0-NOT: @__ubsan
+  return p->a[i] + (p->a)[i];
+#if defined __cplusplus
+struct Base {
+  int b;
+struct NoStandardLayout : Base {
+  int a[1];
+// CXX-LABEL: define {{.*}} @{{.*}}test_nostandardlayout{{.*}}(
+int test_nostandardlayout(NoStandardLayout *p, int i) {
+  // CXX-STRICT-0-NOT: @__ubsan
+  return p->a[i] + (p->a)[i];

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

More information about the cfe-commits mailing list