Received: by 2002:a05:7412:8d09:b0:fa:4c10:6cad with SMTP id bj9csp615855rdb; Tue, 16 Jan 2024 10:13:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGCEeHbf7YC9arA/9ApSdUjZI8XzsNY5TwZ0ddFStDJZnIxJ5oc2zQt/6kVm/l4FKN3GXd2 X-Received: by 2002:a05:6402:17c8:b0:559:b6bb:e001 with SMTP id s8-20020a05640217c800b00559b6bbe001mr566807edy.54.1705428800952; Tue, 16 Jan 2024 10:13:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705428800; cv=pass; d=google.com; s=arc-20160816; b=z34w3WLFbDfHkMTxYvPjuxWYwQyfrKGElehxWwB0eG4pK4UW2fEMUxSUYn3YgZYqO9 /5gFi0ULyF5J/k+D2Rx6YWpwFPksThxz8JJ9T4u5QeDfFwUAuHnLQoFrcTOZ4w3OsjeR V4OMm0FP1SBf6L+DQJzcqYE6ad8xHJ4iEpYmlpPI9qOfyrF6u6z2HzV3Q9583fXj6MSS VhMatB9zbLYo98vSJ6eizQQ9xLI0TVo/yAl3CAEWFZ0cwG4PtSORd9VO+Yr7SN7Wo6Fj RmVymWkQ6Cm+xpiQPVV+KvBVIORgmPDbehvrGP7pn+//PzCk50JsdOiFDhU9cK/TdSw4 GyiQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=8c8I13Su6HTMW66B6BpT4d4txlCoy+2n6n8rpUT0geg=; fh=UYJTiEavTsnkQkKRiOTFrWKQc3D/EV5eJAPyLJQj3H0=; b=jfO37q6DX7oe+0Lxqh9bKNzzdedJD1FuAyY6QByKybkMs72puDkjzOF7qfTXqrCB8B +wIXl6F9ky11PkDpEFSin/7loyE9rXAyMwjd5uIUBMsjdX9JqvjexeITCGVofF1L41x5 361qVBVj9gkoITnLLE6i2mtXmNbL/h3+gaVjXgH2+9vUAyx+2etGin5rNfK7LUXr9yTZ C670kLF1SgV+H4DxCj65etDccimU9omTOqYFP5chGSU/wgWGcFQ9oTs0E5Uvd53jdXaa u6UXVPdN6D0M4I3IBNKj2CwkScoTVY5g7pqgjTnnPVJvz2G6wsqpBuGmM++uiet/B+6i 2kuA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GLXwYIFP; arc=pass (i=1 dkim=pass dkdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-27661-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27661-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ay20-20020a056402203400b00554c82eabe5si5154708edb.481.2024.01.16.10.13.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jan 2024 10:13:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-27661-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GLXwYIFP; arc=pass (i=1 dkim=pass dkdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-27661-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-27661-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B0DF21F2220F for ; Tue, 16 Jan 2024 18:13:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 388541CF82; Tue, 16 Jan 2024 18:12:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GLXwYIFP" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD68C1CD2A for ; Tue, 16 Jan 2024 18:12:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705428744; cv=none; b=Uv7AYD2meDj73+SHiP3KAcBMaSuHGJs6lKtQDaHenwR9HGk/e6DhlTtzrRl844M93QLZ1wKDjPnr3W3+oNZol9KsbdvCAe/rF0tlIi0fDjz1gTWpMllrcN39WtkW/pX47F39apReFLSFhI3hsbs/wXH/9fn3IAq7HnpvOrZjHeU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705428744; c=relaxed/simple; bh=Y6BcPN6C0vM20TSEHpKXRm+HFeCEV83BLrODxyzHY28=; h=DKIM-Signature:Received:X-MC-Unique:Received:Received:From:To:Cc: Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding: X-Scanned-By; b=IfjegZ6c0m2Fu1qVH7bBxseG/n9zkKr358VxzkNR9w5P1eDpL+Kr6UE+XpaFYrHjTjO+4qhPWXfzlZkEfI/+lXe4PJywbFWl5DUCi6utM62r5fLKDs4CT5tItVsQSK6QFNcv4T1EKLpVya/VISGovGWvwagQnwXY7GBNF9nacow= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GLXwYIFP; arc=none smtp.client-ip=170.10.133.124 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705428739; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=8c8I13Su6HTMW66B6BpT4d4txlCoy+2n6n8rpUT0geg=; b=GLXwYIFPFRaCDFdoftMbQgv++LsLBPtkr6n2qPUhKW5U+YSFnOvq/zHdeD3Fx5kZkDIEcR L4+5Q2maRZgPDLAXN+qhxZrtqnDvSyTCpUWgs56QQ2TScbI9y3d6vAZk2GM+M/snEAsJvX wktLuKNwn4LV3gLbrVDa1CcTsAtbrTk= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-102-pEZk0p04PqOzbVZIyPPROA-1; Tue, 16 Jan 2024 13:12:14 -0500 X-MC-Unique: pEZk0p04PqOzbVZIyPPROA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 46A32299E742; Tue, 16 Jan 2024 18:12:14 +0000 (UTC) Received: from cmirabil.redhat.com (unknown [10.22.32.104]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99A84492BC6; Tue, 16 Jan 2024 18:12:13 +0000 (UTC) From: Charles Mirabile To: linux-kernel@vger.kernel.org Cc: Charles Mirabile , Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Ammar Faizi Subject: [PATCH] nolibc/stdlib: Improve `getauxval(3)` implementation Date: Tue, 16 Jan 2024 13:11:47 -0500 Message-ID: <20240116181147.2230944-1-cmirabil@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Previously the getauxval function checked for a doubly null entry (i.e. one whose type and value both were 0) in the auxv array before exiting the loop. At least on x86-64, the ABI only specifies that one more long will be present with value 0 (type AT_NULL) after the pairs of auxv entries. Whether or not it has a corresponding value is unspecified. This value is present on linux, but there is no reason to check it as simply seeing an auxv entry whose type value is AT_NULL should be enough. This is a matter of taste, but I think processing the data in a structured way by coercing it into an array of type value pairs, using multiple return style, and a for loop with a clear exit condition is more readable than the existing infinite loop with multiple exit points and a return value variable. I also added a call to set errno to ENOENT when the entry is not found as glibc does which allows programs to disambiguate between the case of an auxv that is not present, and one that is, but with value zero. Fixes: c61a078015f3 ("nolibc/stdlib: Implement `getauxval(3)` function") Signed-off-by: Charles Mirabile --- tools/include/nolibc/stdlib.h | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index bacfd35c5156..47be99c5a539 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -104,27 +104,19 @@ char *getenv(const char *name) static __attribute__((unused)) unsigned long getauxval(unsigned long type) { - const unsigned long *auxv = _auxv; - unsigned long ret; - - if (!auxv) - return 0; - - while (1) { - if (!auxv[0] && !auxv[1]) { - ret = 0; - break; - } - - if (auxv[0] == type) { - ret = auxv[1]; - break; - } - - auxv += 2; - } - - return ret; + const struct { + unsigned long type, val; + } *auxv = (void *)_auxv; + + if (!auxv || !type) + goto out; + + for (; auxv->type; ++auxv) + if (auxv->type == type) + return auxv->val; +out: + SET_ERRNO(ENOENT); + return 0; } static __attribute__((unused)) -- 2.41.0