This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Help] Add bounds checking to TREE_VEC_ELT
- From: Zack Weinberg <zack at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org, gcc-bugs at gcc dot gnu dot org
- Date: Wed, 13 Nov 2002 01:23:38 -0800
- Subject: [Help] Add bounds checking to TREE_VEC_ELT
I noticed that we make no attempt to bounds-check TREE_VEC_ELT
accesses. The appended patch adds this checking. Unfortunately, it
breaks the compiler quite thoroughly:
crtstuff.c: In function `__do_global_dtors_aux':
crtstuff.c:280: internal compiler error: tree check:
accessed elt 0 of tree_vec with 0 elts in optimize_inline_calls,
at tree-inline.c:1370
[note that TREE_VEC_ELT is 0-based - a TREE_VEC with 0 elts can't be
used at all]
I'm sure there are more bugs than just this one. I need to go to bed
now; anyone interested in squishing all said bugs before I get up
tomorrow morning? -_^
zw
* tree.h (TREE_VEC_ELT_CHECK): New checking macro - applies
array bounds check for TREE_VECs.
(TREE_VEC_ELT): Use it.
* tree.c (tree_vec_elt_check_failed): New function.
===================================================================
Index: tree.c
--- tree.c 23 Oct 2002 17:13:15 -0000 1.278
+++ tree.c 13 Nov 2002 09:22:13 -0000
@@ -4665,6 +4665,22 @@ tree_class_check_failed (node, cl, file,
tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
}
+/* Similar to above, except that the check is for the bounds of a TREE_VEC's
+ (dynamically sized) vector. */
+
+void
+tree_vec_elt_check_failed (idx, len, file, line, function)
+ int idx;
+ int len;
+ const char *file;
+ int line;
+ const char *function;
+{
+ internal_error
+ ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
+ idx, len, function, trim_filename (file), line);
+}
+
#endif /* ENABLE_TREE_CHECKING */
/* For a new vector type node T, build the information necessary for
===================================================================
Index: tree.h
--- tree.h 23 Oct 2002 17:13:15 -0000 1.360
+++ tree.h 13 Nov 2002 09:22:14 -0000
@@ -317,12 +317,26 @@ struct tree_common GTY(())
__FUNCTION__); \
__t; })
+#define TREE_VEC_ELT_CHECK(t, i) __extension__ \
+(*({const tree __t = t; \
+ const int __i = (i); \
+ if (TREE_CODE (__t) != TREE_VEC) \
+ tree_check_failed (__t, TREE_VEC, \
+ __FILE__, __LINE__, __FUNCTION__); \
+ if (i < 0 || i >= __t->vec.length) \
+ tree_vec_elt_check_failed (i, __t->vec.length, \
+ __FILE__, __LINE__, __FUNCTION__); \
+ &__t->vec.a[__i]; }))
+
extern void tree_check_failed PARAMS ((const tree, enum tree_code,
const char *, int, const char *))
ATTRIBUTE_NORETURN;
extern void tree_class_check_failed PARAMS ((const tree, int,
const char *, int, const char *))
ATTRIBUTE_NORETURN;
+extern void tree_vec_elt_check_failed PARAMS ((int, int, const char *,
+ int, const char *))
+ ATTRIBUTE_NORETURN;
#else /* not ENABLE_TREE_CHECKING, or not gcc */
@@ -330,6 +344,7 @@ extern void tree_class_check_failed PARA
#define TREE_CLASS_CHECK(t, code) (t)
#define CST_OR_CONSTRUCTOR_CHECK(t) (t)
#define EXPR_CHECK(t) (t)
+#define TREE_VEC_ELT_CHECK(t, i) ((t)->vec.a[i])
#endif
@@ -810,9 +825,10 @@ struct tree_list GTY(())
/* In a TREE_VEC node. */
#define TREE_VEC_LENGTH(NODE) (TREE_VEC_CHECK (NODE)->vec.length)
-#define TREE_VEC_ELT(NODE,I) (TREE_VEC_CHECK (NODE)->vec.a[I])
#define TREE_VEC_END(NODE) \
((void) TREE_VEC_CHECK (NODE), &((NODE)->vec.a[(NODE)->vec.length]))
+
+#define TREE_VEC_ELT(NODE,I) TREE_VEC_ELT_CHECK (NODE, I)
struct tree_vec GTY(())
{