public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] elf: Remove allocate use on _dl_debug_printf
@ 2022-10-31 19:01 Adhemerval Zanella
  2022-11-01 15:06 ` Szabolcs Nagy
  0 siblings, 1 reply; 2+ messages in thread
From: Adhemerval Zanella @ 2022-10-31 19:01 UTC (permalink / raw)
  To: libc-alpha, Szabolcs Nagy

The maximum number of directives is already limited by the maximum
value of iovec, and current padding usage on _dl_map_object_from_fd
specifies a value of 16 (2 times sizeof (void *)) in hexa, which is
lesser than the INT_STRLEN_BOUND(void *) (20 for LP64).

This works if pointer are larger than 8 bytes, for instance 16.
In this case the maximum padding would be 32 and the IFMTSIZE would
be 40.

The resulting code does use a slight larger static stack, below it
the output of -fstack-usage (for x86_64):

 * master:
   dl-printf.c:35:1:_dl_debug_vdprintf     1344    dynamic

 * patch:
   dl-printf.c:36:1:_dl_debug_vdprintf     2416    static

However, there is an improvement in code generation:

 * master
   text    data     bss     dec     hex filename
   3309       0       0    3309     ced elf/dl-printf.os

 * patch
   text    data     bss     dec     hex filename
   3151       0       0    3151     c4f elf/dl-printf.os

Checked on x86_64-linux-gnu.
---
 elf/dl-printf.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/elf/dl-printf.c b/elf/dl-printf.c
index 00c114002c..1b55c87cfd 100644
--- a/elf/dl-printf.c
+++ b/elf/dl-printf.c
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <sys/uio.h>
 #include <unistd.h>
+#include <intprops.h>
 
 /* Bare-bones printf implementation.  This function only knows about
    the formats and flags needed and can handle only up to 64 stripes in
@@ -36,6 +37,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
 {
 # define NIOVMAX 64
   struct iovec iov[NIOVMAX];
+  /* Maximum size for 'd', 'u', and 'x' including padding.  */
+  enum { IFMTSIZE = INT_STRLEN_BOUND(void *) };
+  char ifmtbuf[NIOVMAX][IFMTSIZE];
   int niov = 0;
   pid_t pid = 0;
   char pidbuf[12];
@@ -100,6 +104,8 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
 	  if (*fmt == '*')
 	    {
 	      width = va_arg (arg, int);
+	      /* The maximum padding accepted is up to pointer size.  */
+	      assert (width < IFMTSIZE);
 	      ++fmt;
 	    }
 
@@ -160,14 +166,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
 #endif
 		  }
 
-		/* We use alloca() to allocate the buffer with the most
-		   pessimistic guess for the size.  Using alloca() allows
-		   having more than one integer formatting in a call.  */
-		int size = 1 + 3 * sizeof (unsigned long int);
-		if (width + 1 > size)
-		  size = width + 1;
-		char *buf = (char *) alloca (size);
-		char *endp = &buf[size];
+		char *endp = &ifmtbuf[niov][IFMTSIZE];
 		char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
 
 		/* Pad to the width the user specified.  */
-- 
2.34.1


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

* Re: [PATCH] elf: Remove allocate use on _dl_debug_printf
  2022-10-31 19:01 [PATCH] elf: Remove allocate use on _dl_debug_printf Adhemerval Zanella
@ 2022-11-01 15:06 ` Szabolcs Nagy
  0 siblings, 0 replies; 2+ messages in thread
From: Szabolcs Nagy @ 2022-11-01 15:06 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

The 10/31/2022 16:01, Adhemerval Zanella wrote:
> The maximum number of directives is already limited by the maximum
> value of iovec, and current padding usage on _dl_map_object_from_fd
> specifies a value of 16 (2 times sizeof (void *)) in hexa, which is
> lesser than the INT_STRLEN_BOUND(void *) (20 for LP64).

less than

> 
> This works if pointer are larger than 8 bytes, for instance 16.

pointers are

> In this case the maximum padding would be 32 and the IFMTSIZE would
> be 40.
> 
> The resulting code does use a slight larger static stack, below it

slightly
s/below it//

> the output of -fstack-usage (for x86_64):
> 
>  * master:
>    dl-printf.c:35:1:_dl_debug_vdprintf     1344    dynamic
> 
>  * patch:
>    dl-printf.c:36:1:_dl_debug_vdprintf     2416    static
> 
> However, there is an improvement in code generation:
> 
>  * master
>    text    data     bss     dec     hex filename
>    3309       0       0    3309     ced elf/dl-printf.os
> 
>  * patch
>    text    data     bss     dec     hex filename
>    3151       0       0    3151     c4f elf/dl-printf.os
> 
> Checked on x86_64-linux-gnu.

looks good with typos fixed in the commit message.

Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>

> ---
>  elf/dl-printf.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/elf/dl-printf.c b/elf/dl-printf.c
> index 00c114002c..1b55c87cfd 100644
> --- a/elf/dl-printf.c
> +++ b/elf/dl-printf.c
> @@ -27,6 +27,7 @@
>  #include <string.h>
>  #include <sys/uio.h>
>  #include <unistd.h>
> +#include <intprops.h>
>  
>  /* Bare-bones printf implementation.  This function only knows about
>     the formats and flags needed and can handle only up to 64 stripes in
> @@ -36,6 +37,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
>  {
>  # define NIOVMAX 64
>    struct iovec iov[NIOVMAX];
> +  /* Maximum size for 'd', 'u', and 'x' including padding.  */
> +  enum { IFMTSIZE = INT_STRLEN_BOUND(void *) };
> +  char ifmtbuf[NIOVMAX][IFMTSIZE];
>    int niov = 0;
>    pid_t pid = 0;
>    char pidbuf[12];
> @@ -100,6 +104,8 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
>  	  if (*fmt == '*')
>  	    {
>  	      width = va_arg (arg, int);
> +	      /* The maximum padding accepted is up to pointer size.  */
> +	      assert (width < IFMTSIZE);

OK

>  	      ++fmt;
>  	    }
>  
> @@ -160,14 +166,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
>  #endif
>  		  }
>  
> -		/* We use alloca() to allocate the buffer with the most
> -		   pessimistic guess for the size.  Using alloca() allows
> -		   having more than one integer formatting in a call.  */
> -		int size = 1 + 3 * sizeof (unsigned long int);
> -		if (width + 1 > size)
> -		  size = width + 1;
> -		char *buf = (char *) alloca (size);
> -		char *endp = &buf[size];
> +		char *endp = &ifmtbuf[niov][IFMTSIZE];
>  		char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);

OK.

>  
>  		/* Pad to the width the user specified.  */
> -- 
> 2.34.1
> 

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

end of thread, other threads:[~2022-11-01 15:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-31 19:01 [PATCH] elf: Remove allocate use on _dl_debug_printf Adhemerval Zanella
2022-11-01 15:06 ` Szabolcs Nagy

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