> This looks not a generic enough fix to me - consider
>
> void foo(void) { int a[100000]; a[0] = 1; a[99999] = 1; }
> int main() { try { foo (); } catch (...) {} }
>
> with -fnon-call-exceptions. If we'd like to catch the SEGV from stack
> overflows then your fix doesn't handle the non-recursive case nor the case
> where -fstack-check is not supplied.
But -fstack-check is required to detect stack overflows... Moreover the above
testcase will be entirely optimized at -O so there is no stack overflow at -O.
> So to me your attempt in fixing this isn't complete but a complete fix would
> be quite pessimizing :/ (for -fnon-call-exceptions)
Then let's at least fix the recursive case since it's simple and cheap. But I
can indeed key this on -fnon-call-exceptions explicitly.
> At least all this should be documented somewhere, that is, what to expect
> when trying to catch stack faults in general with -fnon-call-exceptions
> [-fstack-check].
It's already documented that you need -fstack-check to detect stack overflows.
But I can add a blurb to the -fnon-call-exceptions entry about it.
Revised patch attached.
--
Eric Botcazou
Index: ipa-pure-const.c
===================================================================
--- ipa-pure-const.c (revision 259642)
+++ ipa-pure-const.c (working copy)
@@ -674,12 +674,21 @@ 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_non_call_exceptions
+ && 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 +804,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 +816,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 +1829,18 @@ 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
+ && opt_for_fn (y->decl,
+ flag_non_call_exceptions)
+ && flag_stack_check))))
can_throw = true;
}
for (ie = w->indirect_calls; ie && !can_throw;
@@ -2288,7 +2300,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 +2364,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_non_call_exceptions && flag_stack_check))
continue;
}
Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi (revision 259642)
+++ doc/invoke.texi (working copy)
@@ -12812,6 +12812,9 @@ not exist everywhere. Moreover, it only
instructions to throw exceptions, i.e.@: memory references or floating-point
instructions. It does not allow exceptions to be thrown from
arbitrary signal handlers such as @code{SIGALRM}.
+This option must be specified if you enable @option{-fstack-check} and
+want stack overflows to throw exceptions. Note that this again requires
+platform-specific runtime support.
@item -fdelete-dead-exceptions
@opindex fdelete-dead-exceptions