elf: Get rid of alloca usage in _dl_start_profile

Message ID 20230929135222.1195747-1-josimmon@redhat.com
State New
Headers
Series elf: Get rid of alloca usage in _dl_start_profile |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed

Commit Message

Joe Simmons-Talbott Sept. 29, 2023, 1:52 p.m. UTC
  Replace alloca usage with a scratch_buffer.
---
 elf/dl-profile.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
  

Comments

Joe Simmons-Talbott Oct. 3, 2023, 7:33 p.m. UTC | #1
On Fri, Sep 29, 2023 at 09:52:19AM -0400, Joe Simmons-Talbott wrote:
> Replace alloca usage with a scratch_buffer.

This was tested by enabling profiling in the build and running a test
application with LD_PROFILE defined.  I also generated a directory with
over 1024 characters and defined LD_PROFILE_OUTPUT pointing to that
directory to test the malloc path.  In both cases the .profile file was
created.  I wasn't able to process the .profile files with gprof though
as it complained about the version.

Thanks,
Joe
> ---
>  elf/dl-profile.c | 17 +++++++++++++++--
>  1 file changed, 15 insertions(+), 2 deletions(-)
> 
> diff --git a/elf/dl-profile.c b/elf/dl-profile.c
> index 8be0065fbd..8ae50020bb 100644
> --- a/elf/dl-profile.c
> +++ b/elf/dl-profile.c
> @@ -22,6 +22,7 @@
>  #include <fcntl.h>
>  #include <inttypes.h>
>  #include <limits.h>
> +#include <scratch_buffer.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> @@ -201,6 +202,8 @@ _dl_start_profile (void)
>    int s_scale;
>  #define SCALE_1_TO_1	0x10000L
>    const char *errstr = NULL;
> +  struct scratch_buffer sbuf;
> +  scratch_buffer_init (&sbuf);
>  
>    /* Compute the size of the sections which contain program code.  */
>    for (ph = GL(dl_profile_map)->l_phdr;
> @@ -318,8 +321,14 @@ _dl_start_profile (void)
>    /* First determine the output name.  We write in the directory
>       OUTPUT_DIR and the name is composed from the shared objects
>       soname (or the file name) and the ending ".profile".  */
> -  filename = (char *) alloca (strlen (GLRO(dl_profile_output)) + 1
> -			      + strlen (GLRO(dl_profile)) + sizeof ".profile");
> +  size_t filename_len = (strlen (GLRO(dl_profile_output)) + 1
> +	+ strlen (GLRO(dl_profile)) + sizeof ".profile");
> +  if (!scratch_buffer_set_array_size (&sbuf, 1, filename_len))
> +    {
> +      _dl_error_printf ("failed to allocate memory");
> +      return;
> +    }
> +  filename = sbuf.data;
>    cp = __stpcpy (filename, GLRO(dl_profile_output));
>    *cp++ = '/';
>    __stpcpy (__stpcpy (cp, GLRO(dl_profile)), ".profile");
> @@ -339,6 +348,7 @@ _dl_start_profile (void)
>  	__close_nocancel (fd);
>        _dl_error_printf (errstr, filename,
>  			__strerror_r (errnum, buf, sizeof buf));
> +      scratch_buffer_free (&sbuf);
>        return;
>      }
>  
> @@ -380,6 +390,7 @@ _dl_start_profile (void)
>  
>        _dl_error_printf ("%s: file is no correct profile data file for `%s'\n",
>  			filename, GLRO(dl_profile));
> +      scratch_buffer_free (&sbuf);
>        return;
>      }
>  
> @@ -483,6 +494,8 @@ _dl_start_profile (void)
>  
>    /* Turn on profiling.  */
>    running = 1;
> +
> +  scratch_buffer_free (&sbuf);
>  }
>  
>  
> -- 
> 2.39.2
>
  
Joe Simmons-Talbott Oct. 10, 2023, 7:05 p.m. UTC | #2
Ping.

On Tue, Oct 03, 2023 at 03:33:16PM -0400, Joe Simmons-Talbott wrote:
> On Fri, Sep 29, 2023 at 09:52:19AM -0400, Joe Simmons-Talbott wrote:
> > Replace alloca usage with a scratch_buffer.
> 
> This was tested by enabling profiling in the build and running a test
> application with LD_PROFILE defined.  I also generated a directory with
> over 1024 characters and defined LD_PROFILE_OUTPUT pointing to that
> directory to test the malloc path.  In both cases the .profile file was
> created.  I wasn't able to process the .profile files with gprof though
> as it complained about the version.
> 
> Thanks,
> Joe
> > ---
> >  elf/dl-profile.c | 17 +++++++++++++++--
> >  1 file changed, 15 insertions(+), 2 deletions(-)
> > 
> > diff --git a/elf/dl-profile.c b/elf/dl-profile.c
> > index 8be0065fbd..8ae50020bb 100644
> > --- a/elf/dl-profile.c
> > +++ b/elf/dl-profile.c
> > @@ -22,6 +22,7 @@
> >  #include <fcntl.h>
> >  #include <inttypes.h>
> >  #include <limits.h>
> > +#include <scratch_buffer.h>
> >  #include <stdio.h>
> >  #include <stdlib.h>
> >  #include <string.h>
> > @@ -201,6 +202,8 @@ _dl_start_profile (void)
> >    int s_scale;
> >  #define SCALE_1_TO_1	0x10000L
> >    const char *errstr = NULL;
> > +  struct scratch_buffer sbuf;
> > +  scratch_buffer_init (&sbuf);
> >  
> >    /* Compute the size of the sections which contain program code.  */
> >    for (ph = GL(dl_profile_map)->l_phdr;
> > @@ -318,8 +321,14 @@ _dl_start_profile (void)
> >    /* First determine the output name.  We write in the directory
> >       OUTPUT_DIR and the name is composed from the shared objects
> >       soname (or the file name) and the ending ".profile".  */
> > -  filename = (char *) alloca (strlen (GLRO(dl_profile_output)) + 1
> > -			      + strlen (GLRO(dl_profile)) + sizeof ".profile");
> > +  size_t filename_len = (strlen (GLRO(dl_profile_output)) + 1
> > +	+ strlen (GLRO(dl_profile)) + sizeof ".profile");
> > +  if (!scratch_buffer_set_array_size (&sbuf, 1, filename_len))
> > +    {
> > +      _dl_error_printf ("failed to allocate memory");
> > +      return;
> > +    }
> > +  filename = sbuf.data;
> >    cp = __stpcpy (filename, GLRO(dl_profile_output));
> >    *cp++ = '/';
> >    __stpcpy (__stpcpy (cp, GLRO(dl_profile)), ".profile");
> > @@ -339,6 +348,7 @@ _dl_start_profile (void)
> >  	__close_nocancel (fd);
> >        _dl_error_printf (errstr, filename,
> >  			__strerror_r (errnum, buf, sizeof buf));
> > +      scratch_buffer_free (&sbuf);
> >        return;
> >      }
> >  
> > @@ -380,6 +390,7 @@ _dl_start_profile (void)
> >  
> >        _dl_error_printf ("%s: file is no correct profile data file for `%s'\n",
> >  			filename, GLRO(dl_profile));
> > +      scratch_buffer_free (&sbuf);
> >        return;
> >      }
> >  
> > @@ -483,6 +494,8 @@ _dl_start_profile (void)
> >  
> >    /* Turn on profiling.  */
> >    running = 1;
> > +
> > +  scratch_buffer_free (&sbuf);
> >  }
> >  
> >  
> > -- 
> > 2.39.2
> > 
>
  
Joe Simmons-Talbott Oct. 30, 2023, 12:24 p.m. UTC | #3
Ping.

On Tue, Oct 10, 2023 at 03:05:29PM -0400, Joe Simmons-Talbott wrote:
> Ping.
> 
> On Tue, Oct 03, 2023 at 03:33:16PM -0400, Joe Simmons-Talbott wrote:
> > On Fri, Sep 29, 2023 at 09:52:19AM -0400, Joe Simmons-Talbott wrote:
> > > Replace alloca usage with a scratch_buffer.
> > 
> > This was tested by enabling profiling in the build and running a test
> > application with LD_PROFILE defined.  I also generated a directory with
> > over 1024 characters and defined LD_PROFILE_OUTPUT pointing to that
> > directory to test the malloc path.  In both cases the .profile file was
> > created.  I wasn't able to process the .profile files with gprof though
> > as it complained about the version.
> > 
> > Thanks,
> > Joe
> > > ---
> > >  elf/dl-profile.c | 17 +++++++++++++++--
> > >  1 file changed, 15 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/elf/dl-profile.c b/elf/dl-profile.c
> > > index 8be0065fbd..8ae50020bb 100644
> > > --- a/elf/dl-profile.c
> > > +++ b/elf/dl-profile.c
> > > @@ -22,6 +22,7 @@
> > >  #include <fcntl.h>
> > >  #include <inttypes.h>
> > >  #include <limits.h>
> > > +#include <scratch_buffer.h>
> > >  #include <stdio.h>
> > >  #include <stdlib.h>
> > >  #include <string.h>
> > > @@ -201,6 +202,8 @@ _dl_start_profile (void)
> > >    int s_scale;
> > >  #define SCALE_1_TO_1	0x10000L
> > >    const char *errstr = NULL;
> > > +  struct scratch_buffer sbuf;
> > > +  scratch_buffer_init (&sbuf);
> > >  
> > >    /* Compute the size of the sections which contain program code.  */
> > >    for (ph = GL(dl_profile_map)->l_phdr;
> > > @@ -318,8 +321,14 @@ _dl_start_profile (void)
> > >    /* First determine the output name.  We write in the directory
> > >       OUTPUT_DIR and the name is composed from the shared objects
> > >       soname (or the file name) and the ending ".profile".  */
> > > -  filename = (char *) alloca (strlen (GLRO(dl_profile_output)) + 1
> > > -			      + strlen (GLRO(dl_profile)) + sizeof ".profile");
> > > +  size_t filename_len = (strlen (GLRO(dl_profile_output)) + 1
> > > +	+ strlen (GLRO(dl_profile)) + sizeof ".profile");
> > > +  if (!scratch_buffer_set_array_size (&sbuf, 1, filename_len))
> > > +    {
> > > +      _dl_error_printf ("failed to allocate memory");
> > > +      return;
> > > +    }
> > > +  filename = sbuf.data;
> > >    cp = __stpcpy (filename, GLRO(dl_profile_output));
> > >    *cp++ = '/';
> > >    __stpcpy (__stpcpy (cp, GLRO(dl_profile)), ".profile");
> > > @@ -339,6 +348,7 @@ _dl_start_profile (void)
> > >  	__close_nocancel (fd);
> > >        _dl_error_printf (errstr, filename,
> > >  			__strerror_r (errnum, buf, sizeof buf));
> > > +      scratch_buffer_free (&sbuf);
> > >        return;
> > >      }
> > >  
> > > @@ -380,6 +390,7 @@ _dl_start_profile (void)
> > >  
> > >        _dl_error_printf ("%s: file is no correct profile data file for `%s'\n",
> > >  			filename, GLRO(dl_profile));
> > > +      scratch_buffer_free (&sbuf);
> > >        return;
> > >      }
> > >  
> > > @@ -483,6 +494,8 @@ _dl_start_profile (void)
> > >  
> > >    /* Turn on profiling.  */
> > >    running = 1;
> > > +
> > > +  scratch_buffer_free (&sbuf);
> > >  }
> > >  
> > >  
> > > -- 
> > > 2.39.2
> > > 
> > 
>
  
Andreas Schwab Oct. 30, 2023, 12:44 p.m. UTC | #4
On Okt 03 2023, Joe Simmons-Talbott wrote:

> created.  I wasn't able to process the .profile files with gprof though
> as it complained about the version.

The profile data is used by sprof, not gprof.
  
Joe Simmons-Talbott Oct. 31, 2023, 8:17 p.m. UTC | #5
On Mon, Oct 30, 2023 at 01:44:56PM +0100, Andreas Schwab wrote:
> On Okt 03 2023, Joe Simmons-Talbott wrote:
> 
> > created.  I wasn't able to process the .profile files with gprof though
> > as it complained about the version.
> 
> The profile data is used by sprof, not gprof.

Thanks.  This sent me down a two-day rabbit hole but I was finally able
to ensure that profiling works with this patch.

Thanks,
Joe
  

Patch

diff --git a/elf/dl-profile.c b/elf/dl-profile.c
index 8be0065fbd..8ae50020bb 100644
--- a/elf/dl-profile.c
+++ b/elf/dl-profile.c
@@ -22,6 +22,7 @@ 
 #include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <scratch_buffer.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -201,6 +202,8 @@  _dl_start_profile (void)
   int s_scale;
 #define SCALE_1_TO_1	0x10000L
   const char *errstr = NULL;
+  struct scratch_buffer sbuf;
+  scratch_buffer_init (&sbuf);
 
   /* Compute the size of the sections which contain program code.  */
   for (ph = GL(dl_profile_map)->l_phdr;
@@ -318,8 +321,14 @@  _dl_start_profile (void)
   /* First determine the output name.  We write in the directory
      OUTPUT_DIR and the name is composed from the shared objects
      soname (or the file name) and the ending ".profile".  */
-  filename = (char *) alloca (strlen (GLRO(dl_profile_output)) + 1
-			      + strlen (GLRO(dl_profile)) + sizeof ".profile");
+  size_t filename_len = (strlen (GLRO(dl_profile_output)) + 1
+	+ strlen (GLRO(dl_profile)) + sizeof ".profile");
+  if (!scratch_buffer_set_array_size (&sbuf, 1, filename_len))
+    {
+      _dl_error_printf ("failed to allocate memory");
+      return;
+    }
+  filename = sbuf.data;
   cp = __stpcpy (filename, GLRO(dl_profile_output));
   *cp++ = '/';
   __stpcpy (__stpcpy (cp, GLRO(dl_profile)), ".profile");
@@ -339,6 +348,7 @@  _dl_start_profile (void)
 	__close_nocancel (fd);
       _dl_error_printf (errstr, filename,
 			__strerror_r (errnum, buf, sizeof buf));
+      scratch_buffer_free (&sbuf);
       return;
     }
 
@@ -380,6 +390,7 @@  _dl_start_profile (void)
 
       _dl_error_printf ("%s: file is no correct profile data file for `%s'\n",
 			filename, GLRO(dl_profile));
+      scratch_buffer_free (&sbuf);
       return;
     }
 
@@ -483,6 +494,8 @@  _dl_start_profile (void)
 
   /* Turn on profiling.  */
   running = 1;
+
+  scratch_buffer_free (&sbuf);
 }