From 5834e96a08fd8b86a42428f38a95903d2f1de202 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Mon, 13 Jul 2020 13:47:39 +0100 Subject: ipa-devirt: Fix crash in obj_type_ref_class [PR95114] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The testcase has failed since r9-5035, because obj_type_ref_class tries to look up an ODR type when no ODR type information is available. (The information was available earlier in the compilation, but was freed during pass_ipa_free_lang_data.) We then crash dereferencing the null get_odr_type result. The test passes with -O2. However, it fails again if -fdump-tree-all is used, since obj_type_ref_class is called indirectly from the dump routines. Other code creates ODR type entries on the fly by passing “true” as the insert parameter. But obj_type_ref_class can't do that unconditionally, since it should have no side-effects when used from the dumping code. Following a suggestion from Honza, this patch adds parameters to say whether the routines are being called from dump routines and uses those to derive the insert parameter. gcc/ PR middle-end/95114 * tree.h (virtual_method_call_p): Add a default-false parameter that indicates whether the function is being called from dump routines. (obj_type_ref_class): Likewise. * tree.c (virtual_method_call_p): Likewise. * ipa-devirt.c (obj_type_ref_class): Likewise. Lazily add ODR type information for the type when the parameter is false. * tree-pretty-print.c (dump_generic_node): Update calls to virtual_method_call_p and obj_type_ref_class accordingly. gcc/testsuite/ PR middle-end/95114 * g++.target/aarch64/pr95114.C: New test. --- gcc/ipa-devirt.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'gcc/ipa-devirt.c') diff --git a/gcc/ipa-devirt.c b/gcc/ipa-devirt.c index 0340dec..3ab7049 100644 --- a/gcc/ipa-devirt.c +++ b/gcc/ipa-devirt.c @@ -1883,10 +1883,11 @@ add_type_duplicate (odr_type val, tree type) return build_bases; } -/* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */ +/* REF is OBJ_TYPE_REF, return the class the ref corresponds to. + FOR_DUMP_P is true when being called from the dump routines. */ tree -obj_type_ref_class (const_tree ref) +obj_type_ref_class (const_tree ref, bool for_dump_p) { gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF); ref = TREE_TYPE (ref); @@ -1902,8 +1903,10 @@ obj_type_ref_class (const_tree ref) tree ret = TREE_TYPE (ref); if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret)) ret = TYPE_CANONICAL (ret); + else if (odr_type ot = get_odr_type (ret, !for_dump_p)) + ret = ot->type; else - ret = get_odr_type (ret)->type; + gcc_assert (for_dump_p); return ret; } -- cgit v1.1