This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Warn about possible stack exhaution
Needs ReviewPublic

Authored by Mordante on Oct 27 2019, 9:17 AM.

Details

Summary

When a long set of expressions is chained it may overflow the stack. This warns about the issue.

Note I'm not sure whether AnalyzeImplicitConversions is the best place to add this test, but it was the best I could find. Also not sure what the naming convention for these helpers is. Another option is to put the original code in a lambda in AnalyzeImplicitConversions and thus remove the extra function.

Fixes https://bugs.llvm.org/show_bug.cgi?id=14030

Depends on D69478 (if this patch is unwanted it is possible to remove the dependency.)

Diff Detail

Event Timeline

Mordante created this revision.Oct 27 2019, 9:17 AM
xbolva00 added inline comments.
clang/lib/Sema/SemaChecking.cpp
12339

Please do not change function names.

xbolva00 added inline comments.Oct 27 2019, 10:05 AM
clang/lib/Sema/SemaChecking.cpp
12339

Ah, you had to change it.

Can this be solved maybe via new bool argument for AnalyzeImplicitConversions(.., arg)? If arg is false, we could just call S.runWithSufficientStackSpace(AnalyzeImplicitConversions(...., true)), otherwise continue in normal flow.

Mordante marked 3 inline comments as done.Oct 27 2019, 2:01 PM
Mordante added inline comments.
clang/lib/Sema/SemaChecking.cpp
12339

I like this idea better than my original approach. Thanks for the suggestion.

Mordante updated this revision to Diff 226590.Oct 27 2019, 2:03 PM
Mordante marked an inline comment as done.

Add an extra argument to AnalyzeImplicitConversions as suggested by @xbolva00 .

We should resist using runWithSufficientStackSpace where possible. Have you tried making this process data-recursive instead? It looks fairly straightforward to form a worklist of expressions to which we want to apply AnalyseImplicitConversions, and run through that list until no new entries are added.

clang/include/clang/Basic/DiagnosticSemaKinds.td
3481

The note should explain what the problem is. If you want to suggest a solution, that could be OK, but should be secondary and non-obvious from the description of the problem. In this case, a note saying the expression is too complex would cover both explaining what the problem is, and implying the solution (make the expression less complex).

Also, Clang diagnostics start with lowercase letters.

clang/lib/Sema/SemaChecking.cpp
12339

How about renaming the original function to AnalyzeImplicitConversionsImpl and using the original name for the runWithSufficientStackSpace version?

clang/test/Sema/stack-overflow-expr-int.c
3

You can use not --crash if you want to allow crashes. But testing that we do crash seems inappropriate here; if we keep using runWithSufficientStackSpace, it would seem preferable to test that we produce the diagnostic, and make sure the test passes regardless of whether we crash.

I'll look at a worklist first, before fixing the other issues. However it seems it's not as trivial as I hoped. The recursion occurs in the SequenceChecker which has a WorkList but that's not used for this part. I'll try to find a solution.