Fix segfault in -fsave-optimization-record (PR tree-optimization/86636)
commite4aaf333ac8fc79edeaac2fd4812c8d1d4f548b4
authordmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 24 Jul 2018 16:06:58 +0000 (24 16:06 +0000)
committerdmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 24 Jul 2018 16:06:58 +0000 (24 16:06 +0000)
treefcbc70ee67c79c6671935010a9ff4e91b5119c4a
parent18891c646dfbe6026f97569d2c37337e36595435
Fix segfault in -fsave-optimization-record (PR tree-optimization/86636)

There are various ways that it's possible for a gimple statement to
have an UNKNOWN_LOCATION, and for that UNKNOWN_LOCATION to be wrapped
in an ad-hoc location to capture inlining information.

For such a location, LOCATION_FILE (loc) is NULL.

Various places in -fsave-optimization-record were checking for
  loc != UNKNOWN_LOCATION
and were passing LOCATION_FILE (loc) to code that assumed a non-NULL
filename, thus leading to segfaults for the above cases.

This patch updates the tests to use
  LOCATION_LOCUS (loc) != UNKNOWN_LOCATION
instead, to look through ad-hoc location wrappers, fixing the segfaults.

It also adds various assertions to the affected code.

gcc/ChangeLog:
PR tree-optimization/86636
* json.cc (json::object::set): Fix comment.  Add assertions.
(json::array::append): Move here from json.h.  Add comment and an
assertion.
(json::string::string): Likewise.
* json.h (json::array::append): Move to json.cc.
(json::string::string): Likewise.
* optinfo-emit-json.cc
(optrecord_json_writer::impl_location_to_json): Assert that we
aren't attempting to write out UNKNOWN_LOCATION, or an ad-hoc
wrapper around it.  Expand the location once, rather than three
times.
(optrecord_json_writer::inlining_chain_to_json): Fix the check for
UNKNOWN_LOCATION, to use LOCATION_LOCUS to look through ad-hoc
wrappers.
(optrecord_json_writer::optinfo_to_json): Likewise, in four
places.  Fix some overlong lines.

gcc/testsuite/ChangeLog:
PR tree-optimization/86636
* gcc.c-torture/compile/pr86636.c: New test.

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