@@ -323,6 +323,10 @@ Wchar-subscripts
C ObjC C++ ObjC++ Var(warn_char_subscripts) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
Warn about subscripts whose type is \"char\"
+Wchkp
+C ObjC C++ ObjC++ Var(warn_chkp) Warning EnabledBy(Wall)
+Warn about memory access errors found by Pointer Bounds Checker
+
Wclobbered
C ObjC C++ ObjC++ Var(warn_clobbered) Warning EnabledBy(Wextra)
Warn about variables that might be changed by \"longjmp\" or \"vfork\"
new file mode 100644
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fcheck-pointer-bounds -mmpx -fdump-tree-chkp -O2 -fdump-tree-chkpopt" } */
+/* { dg-final { scan-tree-dump-not "bndcl" "chkpopt" } } */
+/* { dg-final { scan-tree-dump-not "bndcu" "chkpopt" } } */
+
+
+int test (int *p)
+{
+ p = (int *)__builtin___bnd_set_ptr_bounds (p, sizeof (int));
+ return *p;
+}
new file mode 100644
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-fcheck-pointer-bounds -mmpx -fdump-tree-chkp -O2 -Wchkp" } */
+
+int test (int *p)
+{
+ p = (int *)__builtin___bnd_set_ptr_bounds (p, sizeof (int));
+ return *(p + 1); /* { dg-warning "memory access check always fail" "" } */
+}
@@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see
#include "gimplify-me.h"
#include "expr.h"
#include "tree-chkp.h"
+#include "diagnostic.h"
enum check_type
{
@@ -693,6 +694,48 @@ chkp_get_check_result (struct check_info *ci, tree bounds)
return res;
}
+/* Try to compare bounds value and address value
+ used in the check CI. If we can prove that check
+ always pass then remove it. */
+static void
+chkp_remove_check_if_pass (struct check_info *ci)
+{
+ int result = 0;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ fprintf (dump_file, "Trying to remove check: ");
+ print_gimple_stmt (dump_file, ci->stmt, 0, 0);
+ }
+
+ result = chkp_get_check_result (ci, ci->bounds);
+
+ if (result == 1)
+ {
+ gimple_stmt_iterator i = gsi_for_stmt (ci->stmt);
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, " action: delete check (always pass)\n");
+
+ gsi_remove (&i, true);
+ unlink_stmt_vdef (ci->stmt);
+ release_defs (ci->stmt);
+ ci->stmt = NULL;
+ }
+ else if (result == -1)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, " action: keep check (always fail)\n");
+ warning_at (gimple_location (ci->stmt), OPT_Wchkp,
+ "memory access check always fail");
+ }
+ else if (result == 0)
+ {
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, " action: keep check (cannot compute result)\n");
+ }
+}
+
/* For bounds used in CI check if bounds are produced by
intersection and we may use outer bounds instead. If
transformation is possible then fix check statement and
@@ -776,6 +819,27 @@ chkp_remove_excess_intersections (void)
}
}
+/* Try to remove all checks which are known to alwyas pass. */
+static void
+chkp_remove_constant_checks (void)
+{
+ basic_block bb;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Searching for redundant checks...\n");
+
+ FOR_EACH_BB_FN (bb, cfun)
+ {
+ struct bb_checks *bbc = &check_infos[bb->index];
+ unsigned int no;
+
+ /* Iterate through all found checks in BB. */
+ for (no = 0; no < bbc->checks.length (); no++)
+ if (bbc->checks[no].stmt)
+ chkp_remove_check_if_pass (&bbc->checks[no]);
+ }
+}
+
/* Return fast version of string function FNCODE. */
static tree
chkp_get_nobnd_fndecl (enum built_in_function fncode)
@@ -1043,6 +1107,8 @@ chkp_opt_execute (void)
chkp_remove_excess_intersections ();
+ chkp_remove_constant_checks ();
+
chkp_release_check_info ();
chkp_opt_fini ();