Patchwork void dangling line table after loading pch

login
register
mail settings
Submitter Dodji Seketeli
Date Aug. 22, 2011, 11:10 a.m.
Message ID <m3y5ylhfbz.fsf@redhat.com>
Download mbox | patch
Permalink /patch/110906/
State New
Headers show

Comments

Dodji Seketeli - Aug. 22, 2011, 11:10 a.m.
Hello,

In c_common_read_pch when gt_pch_restore loads a new pch, the previous
line table (referenced from the global 'line_table') is
garbage-collected and a new one is built.

As the global instance of cpp_reader referenced by the local variable
'pfile' has a pfile->line_table member that references the
'line_table' global, pfile->line_table needs to be set to the new
value of line_table after gt_pch_restore is called, otherwise
pfile->line_table points to garbage-collected memory.  This is what
the call to cpp_set_line_map in c_common_read_pch is for.

The problem is that cpp_set_line_map is called too late as
cpp_read_state (called before cpp_set_line_map) indirectly touches
some pfile->line_table dangling garbage-collected memory[1].

This doesn't cause any visible havoc in trunk yet but I am seeing it
crashing as I am fiddling with line map stuff on the side.

The two-liner patch below just calls cpp_set_line_map right after
gt_pch_restore to restore pfile->line_table before anyone touches it.

[1]: This happens via cpp_read_state -> _cpp_create_definition ->
_cpp_create_iso_definition -> _cpp_lex_token -> _cpp_lex_direct ->
linemap_position_for_column.

Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.

OK for trunk?

gcc/

	* c-family/c-pch.c (c_common_read_pch): Re-set line table right
	after reading in the pch.
---
 gcc/c-family/c-pch.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Diego Novillo - Aug. 22, 2011, 2:32 p.m.
On 11-08-22 07:10 , Dodji Seketeli wrote:
> Hello,
>
> In c_common_read_pch when gt_pch_restore loads a new pch, the previous
> line table (referenced from the global 'line_table') is
> garbage-collected and a new one is built.
>
> As the global instance of cpp_reader referenced by the local variable
> 'pfile' has a pfile->line_table member that references the
> 'line_table' global, pfile->line_table needs to be set to the new
> value of line_table after gt_pch_restore is called, otherwise
> pfile->line_table points to garbage-collected memory.  This is what
> the call to cpp_set_line_map in c_common_read_pch is for.
>
> The problem is that cpp_set_line_map is called too late as
> cpp_read_state (called before cpp_set_line_map) indirectly touches
> some pfile->line_table dangling garbage-collected memory[1].
>
> This doesn't cause any visible havoc in trunk yet but I am seeing it
> crashing as I am fiddling with line map stuff on the side.
>
> The two-liner patch below just calls cpp_set_line_map right after
> gt_pch_restore to restore pfile->line_table before anyone touches it.
>
> [1]: This happens via cpp_read_state ->  _cpp_create_definition ->
> _cpp_create_iso_definition ->  _cpp_lex_token ->  _cpp_lex_direct ->
> linemap_position_for_column.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.
>
> OK for trunk?
>
> gcc/
>
> 	* c-family/c-pch.c (c_common_read_pch): Re-set line table right
> 	after reading in the pch.

OK.


Diego.
Dodji Seketeli - Aug. 22, 2011, 4:25 p.m.
Diego Novillo <dnovillo@google.com> writes:

> On 11-08-22 07:10 , Dodji Seketeli wrote:

[...]

>> gcc/
>>
>> 	* c-family/c-pch.c (c_common_read_pch): Re-set line table right
>> 	after reading in the pch.
>
> OK.

Thanks, committed to revision r177964.

Patch

diff --git a/gcc/c-family/c-pch.c b/gcc/c-family/c-pch.c
index b429d9d..3c2fd18 100644
--- a/gcc/c-family/c-pch.c
+++ b/gcc/c-family/c-pch.c
@@ -431,6 +431,7 @@  c_common_read_pch (cpp_reader *pfile, const char *name,
   timevar_pop (TV_PCH_CPP_RESTORE);
 
   gt_pch_restore (f);
+  cpp_set_line_map (pfile, line_table);
 
   timevar_push (TV_PCH_CPP_RESTORE);
   if (cpp_read_state (pfile, name, f, smd) != 0)
@@ -445,7 +446,6 @@  c_common_read_pch (cpp_reader *pfile, const char *name,
   fclose (f);
 
   line_table->trace_includes = saved_trace_includes;
-  cpp_set_line_map (pfile, line_table);
   linemap_add (line_table, LC_RENAME, 0, saved_loc.file, saved_loc.line);
 
   /* Give the front end a chance to take action after a PCH file has