From patchwork Mon Oct 31 19:01:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 59697 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B3813853550 for ; Mon, 31 Oct 2022 19:02:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1B3813853550 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667242929; bh=NvTwrXMyDtrbSmKx+iSJo3ERPUA7nF8lOdJYhbUzp7k=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jXmJUfkFXIgpaxR8FFRJKeJUrWp4MIDH1Y9S7wE5u74UsX4bDKiBrAlspDtmss1nY vUDe/bivUZpwATAt9kkkXolkrlPjAhMuw27ms3dxi4Kv5epDuVAQbhRUcMNj0HlUWU 5aMsvR6EtYyl7/D8NkRXgbcO1giCgJUSenE4FgBM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by sourceware.org (Postfix) with ESMTPS id BA2113856091 for ; Mon, 31 Oct 2022 19:01:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BA2113856091 Received: by mail-oi1-x232.google.com with SMTP id p127so13698284oih.9 for ; Mon, 31 Oct 2022 12:01:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NvTwrXMyDtrbSmKx+iSJo3ERPUA7nF8lOdJYhbUzp7k=; b=6jKVJtUdF1/hTRweVM9n1AN4RGoMAHVRDvwdjR1qb+ezrZtzR7vDCMrOJ7DdHrzUjJ FnJN4Kj9/QlaaZqmbwW8+5hX2+CTnWC2dyDTF/3m/zgZGGRBd6duN9aHcLa/meeIuhrO zWxhu0hDhRj0YF6ANlkkUT3nQe8yxbbRHOaIvL6Fnt888PiIPlhLkhsK2V/pUFQ/uF4U kOUbCVvpayjbVhCDJ/ycxH+Y3dg3AeZGZv6q3lwxBn43tMw0gmnt+gZ9tPKVQcZuVltd /7X4eTVXBbH4Nt6YQRs0RHZhpkvSkLQUz7jh3UlSGedMMZnhvCpRWz0yggTwI/9G7cro upzQ== X-Gm-Message-State: ACrzQf3IYipQa649jtDgPtD/BVIC/Vnn0Q2b+6eA86XMC2Q/FwHxSmLt PzrMZE/y/chZ41/cZAiB/VQE07bg5Oe0JKCS X-Google-Smtp-Source: AMsMyM4EN61O2viP8xeFE0oDjGe08IFzH64vBB/2MUJtEPzUNfAkoQdkHz7Zt3g2xbH1/QZNbfinNA== X-Received: by 2002:a05:6808:4cf:b0:359:ff1c:2fab with SMTP id a15-20020a05680804cf00b00359ff1c2fabmr4397665oie.221.1667242895873; Mon, 31 Oct 2022 12:01:35 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:17c8:f12d:93ab:bc0f:dd56]) by smtp.gmail.com with ESMTPSA id l4-20020a0568301d6400b0066c495a651dsm2008302oti.38.2022.10.31.12.01.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 12:01:35 -0700 (PDT) To: libc-alpha@sourceware.org, Szabolcs Nagy Subject: [PATCH] elf: Remove allocate use on _dl_debug_printf Date: Mon, 31 Oct 2022 16:01:31 -0300 Message-Id: <20221031190131.2183199-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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. Reviewed-by: Szabolcs Nagy --- 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 #include #include +#include /* 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. */