[PATCH] D144136: Add a "remark" to report on array accesses

Kees Cook via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 18 11:55:35 PST 2023


kees added a comment.

Here's a test-case. I'd expect 6 remarks from building this:

  /* Build with -Wall -O2 -fstrict-flex-arrays=3 -fsanitize=bounds -Rarray-bounds */
  #include <stdint.h>
  #include <stdio.h>
  #include <string.h>
  #include <malloc.h>
  
  #define report_size(p, index)      do {    \
      const size_t bdos = __builtin_dynamic_object_size(p, 1); \
      \
      if (__builtin_constant_p(bdos)) { \
          if (bdos == SIZE_MAX) { \
              printf(#p " has unknowable size\n"); \
          } else { \
              printf(#p " has a fixed size: %zu\n", bdos); \
          } \
      } else { \
          printf(#p " has a dynamic size: %zu\n", bdos); \
      } \
      printf(#p "[" #index "] assignment: %d\n", (p)[index] = 15); \
  } while (0)
  
  struct fixed {
      unsigned long flags;
      size_t foo;
      int array[16];
  };
  
  /* should emit "fixed" */
  void do_fixed(struct fixed *p, int index)
  {
      report_size(p->array, 0);
      report_size(p->array, index);
  }
  
  struct flex {
      unsigned long flags;
      size_t foo;
      int array[];
  };
  
  /* should emit "dynamic" */
  void do_dynamic(unsigned char count, int index)
  {
      /* malloc() is marked with __attribute__((alloc_size(1))) */
      struct flex *p = malloc(sizeof(*p) + count * sizeof(*p->array));
      report_size(p->array, 0);
      report_size(p->array, index);
      free(p);
  }
  
  /* should emit "unknowable" */
  void do_unknown(struct flex *p, int index)
  {
      report_size(p->array, 0);
      report_size(p->array, index);
  }

Currently, it only emits once for the compile-time known index with a compile-time known array size:

  array.c:31:17: remark: accessing fixed sized array 'int[16]' by 0 [-Rarray-bounds]                  
      report_size(p->array, 0);                                                                                       ^                                 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144136/new/

https://reviews.llvm.org/D144136



More information about the cfe-commits mailing list