public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] varasm: Fix type confusion bug
@ 2022-12-01 15:33 Alex Coplan
  2022-12-01 16:12 ` Richard Sandiford
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Coplan @ 2022-12-01 15:33 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1186 bytes --]

Hi,

This patch fixes a type confusion bug in varasm.cc:assemble_variable.
The problem is that the current code calls:

  sect = get_variable_section (decl, false);

and then accesses sect->named.name without checking whether the section
is in fact a named section. In the surrounding else clause, we only know
that SECTION_STYLE (sect) != SECTION_NOSWITCH, so it is possible that
the section is an unnamed section.

In practice, this means that we end up doing a wild string compare
between a function pointer and the string literal ".vtable_map_vars".
This is because sect->named.name aliases sect->unnamed.callback in the
section union.

This can be seen in GDB with a simple testcase such as "int x;".

This patch fixes the issue by checking the SECTION_STYLE of the section
is in fact SECTION_NAMED before trying to do the string comparison.

We drop the existing check of whether sect->named.name is non-NULL
because this should presumably always be the case for a named section.

Bootstrapped/regtested on aarch64-none-linux-gnu, OK for trunk?

Thanks,
Alex

gcc/ChangeLog:

	* varasm.cc (assemble_variable): Fix type confusion bug when
	checking for ".vtable_map_vars" section.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 472 bytes --]

diff --git a/gcc/varasm.cc b/gcc/varasm.cc
index 9dfbebbb915..6851201b6a2 100644
--- a/gcc/varasm.cc
+++ b/gcc/varasm.cc
@@ -2400,7 +2400,7 @@ assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
   else
     {
       /* Special-case handling of vtv comdat sections.  */
-      if (sect->named.name
+      if (SECTION_STYLE (sect) == SECTION_NAMED
 	  && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
 	handle_vtv_comdat_section (sect, decl);
       else

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

* Re: [PATCH] varasm: Fix type confusion bug
  2022-12-01 15:33 [PATCH] varasm: Fix type confusion bug Alex Coplan
@ 2022-12-01 16:12 ` Richard Sandiford
  2022-12-01 17:59   ` Alex Coplan
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Sandiford @ 2022-12-01 16:12 UTC (permalink / raw)
  To: Alex Coplan via Gcc-patches; +Cc: Alex Coplan

Alex Coplan via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> Hi,
>
> This patch fixes a type confusion bug in varasm.cc:assemble_variable.
> The problem is that the current code calls:
>
>   sect = get_variable_section (decl, false);
>
> and then accesses sect->named.name without checking whether the section
> is in fact a named section. In the surrounding else clause, we only know
> that SECTION_STYLE (sect) != SECTION_NOSWITCH, so it is possible that
> the section is an unnamed section.
>
> In practice, this means that we end up doing a wild string compare
> between a function pointer and the string literal ".vtable_map_vars".
> This is because sect->named.name aliases sect->unnamed.callback in the
> section union.
>
> This can be seen in GDB with a simple testcase such as "int x;".
>
> This patch fixes the issue by checking the SECTION_STYLE of the section
> is in fact SECTION_NAMED before trying to do the string comparison.
>
> We drop the existing check of whether sect->named.name is non-NULL
> because this should presumably always be the case for a named section.
>
> Bootstrapped/regtested on aarch64-none-linux-gnu, OK for trunk?

OK, thanks.  I think it's OK for backports too if you like,
since it's a regression from around 2013.

Richard

>
> Thanks,
> Alex
>
> gcc/ChangeLog:
>
> 	* varasm.cc (assemble_variable): Fix type confusion bug when
> 	checking for ".vtable_map_vars" section.
>
> diff --git a/gcc/varasm.cc b/gcc/varasm.cc
> index 9dfbebbb915..6851201b6a2 100644
> --- a/gcc/varasm.cc
> +++ b/gcc/varasm.cc
> @@ -2400,7 +2400,7 @@ assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
>    else
>      {
>        /* Special-case handling of vtv comdat sections.  */
> -      if (sect->named.name
> +      if (SECTION_STYLE (sect) == SECTION_NAMED
>  	  && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
>  	handle_vtv_comdat_section (sect, decl);
>        else

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

* Re: [PATCH] varasm: Fix type confusion bug
  2022-12-01 16:12 ` Richard Sandiford
@ 2022-12-01 17:59   ` Alex Coplan
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Coplan @ 2022-12-01 17:59 UTC (permalink / raw)
  To: gcc-patches, richard.sandiford

On 01/12/2022 16:12, Richard Sandiford wrote:
> Alex Coplan via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> > Hi,
> >
> > This patch fixes a type confusion bug in varasm.cc:assemble_variable.
> > The problem is that the current code calls:
> >
> >   sect = get_variable_section (decl, false);
> >
> > and then accesses sect->named.name without checking whether the section
> > is in fact a named section. In the surrounding else clause, we only know
> > that SECTION_STYLE (sect) != SECTION_NOSWITCH, so it is possible that
> > the section is an unnamed section.
> >
> > In practice, this means that we end up doing a wild string compare
> > between a function pointer and the string literal ".vtable_map_vars".
> > This is because sect->named.name aliases sect->unnamed.callback in the
> > section union.
> >
> > This can be seen in GDB with a simple testcase such as "int x;".
> >
> > This patch fixes the issue by checking the SECTION_STYLE of the section
> > is in fact SECTION_NAMED before trying to do the string comparison.
> >
> > We drop the existing check of whether sect->named.name is non-NULL
> > because this should presumably always be the case for a named section.
> >
> > Bootstrapped/regtested on aarch64-none-linux-gnu, OK for trunk?
> 
> OK, thanks.  I think it's OK for backports too if you like,
> since it's a regression from around 2013.

Thanks, I've pushed the patch to trunk, and will backport if there are
no complaints after a week or so.

Alex

> 
> Richard
> 
> >
> > Thanks,
> > Alex
> >
> > gcc/ChangeLog:
> >
> > 	* varasm.cc (assemble_variable): Fix type confusion bug when
> > 	checking for ".vtable_map_vars" section.
> >
> > diff --git a/gcc/varasm.cc b/gcc/varasm.cc
> > index 9dfbebbb915..6851201b6a2 100644
> > --- a/gcc/varasm.cc
> > +++ b/gcc/varasm.cc
> > @@ -2400,7 +2400,7 @@ assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
> >    else
> >      {
> >        /* Special-case handling of vtv comdat sections.  */
> > -      if (sect->named.name
> > +      if (SECTION_STYLE (sect) == SECTION_NAMED
> >  	  && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
> >  	handle_vtv_comdat_section (sect, decl);
> >        else

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

end of thread, other threads:[~2022-12-01 17:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 15:33 [PATCH] varasm: Fix type confusion bug Alex Coplan
2022-12-01 16:12 ` Richard Sandiford
2022-12-01 17:59   ` Alex Coplan

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