Fix -fsave-optimization-record ICE (PR tree-optimization/87025)
commitf2e20cc0ae6d8f3c02cc1b65c71cf64c0f6806e5
authordmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 19 Nov 2018 16:42:03 +0000 (19 16:42 +0000)
committerdmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 19 Nov 2018 16:42:03 +0000 (19 16:42 +0000)
tree42bd6ec09491b80d40c3f59d6bfbf4458b5abe5b
parent80d492fc1524f3299cea9aca12cb7ac0f24b144c
Fix -fsave-optimization-record ICE (PR tree-optimization/87025)

PR tree-optimization/87025 reports an ICE within
-fsave-optimization-record's optrecord_json_writer.

The issue is that dump_context::begin_scope creates an optinfo
of kind OPTINFO_KIND_SCOPE, but fails to call
dump_context::end_any_optinfo, so the optinfo for the scope remains
pending.

The JSON writer would normally push a JSON array for the "scope" optinfo
when the latter is emitted.  However, if a dump_* call happens that
doesn't flush the "scope" optinfo e.g. dump_printf (as opposed to
dump_printf_loc), that dump_ call is added to the pending optinfo, and
optinfo::handle_dump_file_kind changes the pending optinfo's m_kind
(e.g. to OPTINFO_KIND_NOTE).  Hence when the pending optinfo is
eventually emitted, it isn't OPTINFO_KIND_SCOPE anymore, and hence
the JSON writer doesn't create and push a JSON array for it, leading
to dump_context's view of scopes getting out-of-sync with that of
the JSON writer's.

Later, dump_context::end_scope unconditionally tries to pop the JSON scope
array, but no JSON scope array was added, leading to an assertion
failure (or crash).

The fix is to call dump_context::end_any_optinfo immediately after
creating the scope optinfo, so that it is emitted immediately, ensuring
that the JSON writer stays in-sync with the dump_context.

gcc/ChangeLog:
PR tree-optimization/87025
* dumpfile.c (dump_context::begin_scope): Call end_any_optinfo
immediately after creating the scope optinfo.
(selftest::test_pr87025): New function.
(selftest::dumpfile_c_tests): Call it.
* optinfo-emit-json.cc (optrecord_json_writer::pop_scope): Assert
that we're not popping the top-level records array.
* optinfo.cc (optinfo::handle_dump_file_kind): Assert that we're
not changing the kind of a "scope" optinfo.

gcc/testsuite/ChangeLog:
PR tree-optimization/87025
* gcc.dg/pr87025.c: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@266280 138bc75d-0d04-0410-961f-82ee72b054a4
gcc/ChangeLog
gcc/dumpfile.c
gcc/optinfo-emit-json.cc
gcc/optinfo.cc
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr87025.c [new file with mode: 0644]