]> git.ipfire.org Git - thirdparty/gcc.git/commit
Use the counted_by attribute of pointers in array bound checker.
authorQing Zhao <qing.zhao@oracle.com>
Mon, 16 Jun 2025 21:08:32 +0000 (21:08 +0000)
committerQing Zhao <qing.zhao@oracle.com>
Tue, 1 Jul 2025 20:22:06 +0000 (20:22 +0000)
commit9d579c522d551eaa807e438206e19a91a3def67f
tree446ae52e8ae1114b4940128684af912a0c1d79a9
parent7165ca43caf47007f5ceaa46c034618d397d42ec
Use the counted_by attribute of pointers in array bound checker.

Current array bound checker only instruments ARRAY_REF, and the INDEX
information is the 2nd operand of the ARRAY_REF.

When extending the array bound checker to pointer references with
counted_by attributes, the hardest part is to get the INDEX of the
corresponding array ref from the offset computation expression of
the pointer ref.  I.e.

Given an OFFSET expression, and the ELEMENT_SIZE,
get the index expression from the OFFSET.
For example:
  OFFSET:
   ((long unsigned int) m * (long unsigned int) SAVE_EXPR <n>) * 4
  ELEMENT_SIZE:
   (sizetype) SAVE_EXPR <n> * 4
get the index as (long unsigned int) m.

gcc/c-family/ChangeLog:

* c-gimplify.cc (is_address_with_access_with_size): New function.
(ubsan_walk_array_refs_r): Instrument an INDIRECT_REF whose base
address is .ACCESS_WITH_SIZE or an address computation whose base
address is .ACCESS_WITH_SIZE.
* c-ubsan.cc (ubsan_instrument_bounds_pointer_address): New function.
(struct factor_t): New structure.
(get_factors_from_mul_expr): New function.
(get_index_from_offset): New function.
(get_index_from_pointer_addr_expr): New function.
(is_instrumentable_pointer_array_address): New function.
(ubsan_array_ref_instrumented_p): Change prototype.
Handle MEM_REF in addtional to ARRAY_REF.
(ubsan_maybe_instrument_array_ref): Handle MEM_REF in addtional
to ARRAY_REF.

gcc/testsuite/ChangeLog:

* gcc.dg/ubsan/pointer-counted-by-bounds-2.c: New test.
* gcc.dg/ubsan/pointer-counted-by-bounds-3.c: New test.
* gcc.dg/ubsan/pointer-counted-by-bounds-4.c: New test.
* gcc.dg/ubsan/pointer-counted-by-bounds-5.c: New test.
* gcc.dg/ubsan/pointer-counted-by-bounds.c: New test.
gcc/c-family/c-gimplify.cc
gcc/c-family/c-ubsan.cc
gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/ubsan/pointer-counted-by-bounds.c [new file with mode: 0644]