public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] elf: Get rid of alloca usage in _dl_start_profile
@ 2023-09-29 13:52 Joe Simmons-Talbott
  2023-10-03 19:33 ` Joe Simmons-Talbott
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-09-29 13:52 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Replace alloca usage with a scratch_buffer.
---
 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


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

* Re: [PATCH] elf: Get rid of alloca usage in _dl_start_profile
  2023-09-29 13:52 [PATCH] elf: Get rid of alloca usage in _dl_start_profile Joe Simmons-Talbott
@ 2023-10-03 19:33 ` Joe Simmons-Talbott
  2023-10-10 19:05   ` Joe Simmons-Talbott
  2023-10-30 12:44   ` Andreas Schwab
  0 siblings, 2 replies; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-10-03 19:33 UTC (permalink / raw)
  To: libc-alpha

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
> 


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

* Re: [PATCH] elf: Get rid of alloca usage in _dl_start_profile
  2023-10-03 19:33 ` Joe Simmons-Talbott
@ 2023-10-10 19:05   ` Joe Simmons-Talbott
  2023-10-30 12:24     ` Joe Simmons-Talbott
  2023-10-30 12:44   ` Andreas Schwab
  1 sibling, 1 reply; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-10-10 19:05 UTC (permalink / raw)
  To: libc-alpha

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


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

* Re: [PATCH] elf: Get rid of alloca usage in _dl_start_profile
  2023-10-10 19:05   ` Joe Simmons-Talbott
@ 2023-10-30 12:24     ` Joe Simmons-Talbott
  0 siblings, 0 replies; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-10-30 12:24 UTC (permalink / raw)
  To: libc-alpha

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


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

* Re: [PATCH] elf: Get rid of alloca usage in _dl_start_profile
  2023-10-03 19:33 ` Joe Simmons-Talbott
  2023-10-10 19:05   ` Joe Simmons-Talbott
@ 2023-10-30 12:44   ` Andreas Schwab
  2023-10-31 20:17     ` Joe Simmons-Talbott
  1 sibling, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2023-10-30 12:44 UTC (permalink / raw)
  To: Joe Simmons-Talbott; +Cc: libc-alpha

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.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

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

* Re: [PATCH] elf: Get rid of alloca usage in _dl_start_profile
  2023-10-30 12:44   ` Andreas Schwab
@ 2023-10-31 20:17     ` Joe Simmons-Talbott
  0 siblings, 0 replies; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-10-31 20:17 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: libc-alpha

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


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

end of thread, other threads:[~2023-10-31 20:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-29 13:52 [PATCH] elf: Get rid of alloca usage in _dl_start_profile Joe Simmons-Talbott
2023-10-03 19:33 ` Joe Simmons-Talbott
2023-10-10 19:05   ` Joe Simmons-Talbott
2023-10-30 12:24     ` Joe Simmons-Talbott
2023-10-30 12:44   ` Andreas Schwab
2023-10-31 20:17     ` Joe Simmons-Talbott

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