tree-object-size: Use trees and support negative offsets
commit422f9eb7011b76c12ff00ffaee2bcc9cdddf16d5
authorSiddhesh Poyarekar <siddhesh@gotplt.org>
Fri, 17 Dec 2021 01:37:18 +0000 (17 07:07 +0530)
committerSiddhesh Poyarekar <siddhesh@gotplt.org>
Fri, 17 Dec 2021 03:45:30 +0000 (17 09:15 +0530)
tree7caf8a641a42f07397a39931545e113efba5ee36
parent871504b0dd5cd023d3a28cf9e5ccbda75928b102
tree-object-size: Use trees and support negative offsets

Transform tree-object-size to operate on tree objects instead of host
wide integers.  This makes it easier to extend to dynamic expressions
for object sizes.

The compute_builtin_object_size interface also now returns a tree
expression instead of HOST_WIDE_INT, so callers have been adjusted to
account for that.

The trees in object_sizes are each an object_size object with members
size (the bytes from the pointer to the end of the object) and wholesize
(the size of the whole object).  This allows analysis of negative
offsets, which can now be allowed to the extent of the object bounds.
Tests have been added to verify that it actually works.

gcc/ChangeLog:

* tree-object-size.h (compute_builtin_object_size): Return tree
instead of HOST_WIDE_INT.
* builtins.c (fold_builtin_object_size): Adjust.
* gimple-fold.c (gimple_fold_builtin_strncat): Likewise.
* ubsan.c (instrument_object_size): Likewise.
* tree-object-size.c (object_size): New structure.
(object_sizes): Change type to vec<object_size>.
(initval): New function.
(unknown): Use it.
(size_unknown_p, size_initval, size_unknown): New functions.
(object_sizes_unknown_p): Use it.
(object_sizes_get): Return tree.
(object_sizes_initialize): Rename from object_sizes_set_force
and set VAL parameter type as tree.  Add new parameter WHOLEVAL.
(object_sizes_set): Set VAL parameter type as tree and adjust
implementation.  Add new parameter WHOLEVAL.
(size_for_offset): New function.
(decl_init_size): Adjust comment.
(addr_object_size): Change PSIZE parameter to tree and adjust
implementation.  Add new parameter PWHOLESIZE.
(alloc_object_size): Return tree.
(compute_builtin_object_size): Return tree in PSIZE.
(expr_object_size, call_object_size, unknown_object_size):
Adjust for object_sizes_set change.
(merge_object_sizes): Drop OFFSET parameter and adjust
implementation for tree change.
(plus_stmt_object_size): Call collect_object_sizes_for directly
instead of merge_object_size and call size_for_offset to get net
size.
(cond_expr_object_size, collect_object_sizes_for,
object_sizes_execute): Adjust for change of type from
HOST_WIDE_INT to tree.
(check_for_plus_in_loops_1): Likewise and skip non-positive
offsets.

gcc/testsuite/ChangeLog:

* gcc.dg/builtin-object-size-1.c (test9): New test.
(main): Call it.
* gcc.dg/builtin-object-size-2.c (test8): New test.
(main): Call it.
* gcc.dg/builtin-object-size-3.c (test9): New test.
(main): Call it.
* gcc.dg/builtin-object-size-4.c (test8): New test.
(main): Call it.
* gcc.dg/builtin-object-size-5.c (test5, test6, test7): New
tests.

Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
gcc/builtins.c
gcc/gimple-fold.c
gcc/testsuite/gcc.dg/builtin-object-size-1.c
gcc/testsuite/gcc.dg/builtin-object-size-2.c
gcc/testsuite/gcc.dg/builtin-object-size-3.c
gcc/testsuite/gcc.dg/builtin-object-size-4.c
gcc/testsuite/gcc.dg/builtin-object-size-5.c
gcc/tree-object-size.c
gcc/tree-object-size.h
gcc/ubsan.c