Hi, when stack checking is enabled in Ada, it is supposed to be able to handle the case of a recursive function that does essentially nothing. But in this case the IPA machinery will compute that the function is nothrow, which means that the Storage_Error exception cannot be caught by the caller.
Tested on x86-64/Linux, OK for the mainline? 2018-04-26 Eric Botcazou <ebotca...@adacore.com> * ipa-pure-const.c (check_call): In non-IPA mode, set can_throw flag for a recursive call that can throw if stack checking is enabled. (check_stmt): Fix formatting. (propagate_nothrow): Set can_throw for a recursive call if exceptions and stack checking are enabled. (pass_nothrow::execute): Do not set the nothrow flag if there is a recursive call that can throw and stack checking is enabled. 2018-04-26 Eric Botcazou <ebotca...@adacore.com> * gnat.dg/stack_check4.adb: New test. -- Eric Botcazou
Index: ipa-pure-const.c =================================================================== --- ipa-pure-const.c (revision 259642) +++ ipa-pure-const.c (working copy) @@ -674,12 +674,19 @@ check_call (funct_state local, gcall *ca local->can_free = true; /* When not in IPA mode, we can still handle self recursion. */ - if (!ipa && callee_t + if (!ipa + && callee_t && recursive_call_p (current_function_decl, callee_t)) { if (dump_file) - fprintf (dump_file, " Recursive call can loop.\n"); + fprintf (dump_file, " recursive call can loop\n"); local->looping = true; + if (possibly_throws_externally && flag_stack_check) + { + if (dump_file) + fprintf (dump_file, " recursive call can throw\n"); + local->can_throw = true; + } } /* Either callee is unknown or we are doing local analysis. Look to see if there are any bits available for the callee (such as by @@ -795,8 +802,7 @@ check_stmt (gimple_stmt_iterator *gsip, print_gimple_stmt (dump_file, stmt, 0); } - if (gimple_has_volatile_ops (stmt) - && !gimple_clobber_p (stmt)) + if (gimple_has_volatile_ops (stmt) && !gimple_clobber_p (stmt)) { local->pure_const_state = IPA_NEITHER; if (dump_file) @@ -808,8 +814,7 @@ check_stmt (gimple_stmt_iterator *gsip, ipa ? check_ipa_load : check_load, ipa ? check_ipa_store : check_store); - if (gimple_code (stmt) != GIMPLE_CALL - && stmt_could_throw_p (stmt)) + if (gimple_code (stmt) != GIMPLE_CALL && stmt_could_throw_p (stmt)) { if (cfun->can_throw_non_call_exceptions) { @@ -1822,13 +1827,17 @@ propagate_nothrow (void) not be interposed. When callee is compiled with non-call exceptions we also must check that the declaration is bound to current - body as other semantically equivalent body may still - throw. */ + body as other semantically equivalent body may throw. + Moreover, if the call is recursive, we must consider that + the function may throw a stack overflow exception. */ if (avail <= AVAIL_INTERPOSABLE || (!TREE_NOTHROW (y->decl) && (get_function_state (y)->can_throw || (opt_for_fn (y->decl, flag_non_call_exceptions) - && !e->callee->binds_to_current_def_p (w))))) + && !e->callee->binds_to_current_def_p (w)) + || (y == w + && flag_exceptions + && flag_stack_check)))) can_throw = true; } for (ie = w->indirect_calls; ie && !can_throw; @@ -2288,7 +2297,7 @@ make_pass_warn_function_noreturn (gcc::c return new pass_warn_function_noreturn (ctxt); } -/* Simple local pass for pure const discovery reusing the analysis from +/* Simple local pass for nothrow discovery reusing the analysis from ipa_pure_const. This pass is effective when executed together with other optimization passes in early optimization pass queue. */ @@ -2352,8 +2361,9 @@ pass_nothrow::execute (function *) if (is_gimple_call (gsi_stmt (gsi))) { tree callee_t = gimple_call_fndecl (gsi_stmt (gsi)); - if (callee_t && recursive_call_p (current_function_decl, - callee_t)) + if (callee_t + && recursive_call_p (current_function_decl, callee_t) + && !flag_stack_check) continue; }
-- { dg-do compile } -- { dg-options "-Wstack-usage=512" } with Stack_Usage4_Pkg; use Stack_Usage4_Pkg; procedure Stack_Usage4 is BS : Bounded_String := Get; S : String := BS.Data (BS.Data'First .. BS.Len); begin null; end;