public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] void dangling line table after loading pch
@ 2011-08-22 12:06 Dodji Seketeli
  2011-08-22 15:22 ` Diego Novillo
  0 siblings, 1 reply; 3+ messages in thread
From: Dodji Seketeli @ 2011-08-22 12:06 UTC (permalink / raw)
  To: Diego Novillo; +Cc: Tom Tromey, GCC Patches

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(-)

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
-- 
		Dodji

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] void dangling line table after loading pch
  2011-08-22 12:06 [PATCH] void dangling line table after loading pch Dodji Seketeli
@ 2011-08-22 15:22 ` Diego Novillo
  2011-08-22 17:37   ` Dodji Seketeli
  0 siblings, 1 reply; 3+ messages in thread
From: Diego Novillo @ 2011-08-22 15:22 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Tom Tromey, GCC Patches

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] void dangling line table after loading pch
  2011-08-22 15:22 ` Diego Novillo
@ 2011-08-22 17:37   ` Dodji Seketeli
  0 siblings, 0 replies; 3+ messages in thread
From: Dodji Seketeli @ 2011-08-22 17:37 UTC (permalink / raw)
  To: Diego Novillo; +Cc: Tom Tromey, GCC Patches

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.

-- 
		Dodji

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-08-22 16:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-22 12:06 [PATCH] void dangling line table after loading pch Dodji Seketeli
2011-08-22 15:22 ` Diego Novillo
2011-08-22 17:37   ` Dodji Seketeli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).