Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6117059ybl; Tue, 14 Jan 2020 22:15:55 -0800 (PST) X-Google-Smtp-Source: APXvYqxU06mFYjupNwQjz0WojdqkVCXrfXq8oiAsKWOIPAsjOts6dtEoytlgXs5mPLg22kjEIgB5 X-Received: by 2002:a05:6830:1e16:: with SMTP id s22mr1652145otr.340.1579068955201; Tue, 14 Jan 2020 22:15:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579068955; cv=none; d=google.com; s=arc-20160816; b=aW05+kLBWotCEGmHC4a8fKU8px+s/tElbnIX4NzwplKlXiKSjyPhrordmpdvYzsJmF 8uQ1EsW7gKetr/RQ9rkyigUZZ1NGCPZfQW3QHdMycRMSlRFc2cU0bckBSy2crNYrfasC WSv8VRYsK3GO4w8C8HZFXSA2gg8iO4C2Tk16HE8BlQK6kDo2HydRG9dat4C0jelqUF3u 6y18Lu6DvDVVDgesdG6zIraEdiLW/5zvFTDQTKk+DOs7X7Bz7XwyfTNnvQMGwSbZaYJv 3ASvxNYyXeHEt4bhnbh3g6pllu+Q+X58WBe5onA3YbQdg4S0m0Z3oFMpPXZKfCG1DUoV yosw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=VEBiEWWczDCOsezp9xBUmXOvLcrB5S1wxD0g5MzN8aA=; b=hN6V7dAZ59/pXGwnz1X2u+bQpAyzAwagZ50NKnSBOQdFodSh6TVFu9KcSJJfDqYLHh IBSdxwDYRX2OJmAnHz76q5HEsKnoJNXrMmhyOA2dxk4+o+sZYlhRUJX2rtXYSqUF0oD0 n8fQlEFzYd/Ch8DndqU8931fjaC7UVRnJgrjhq84lBc9Jc+gybFiRBcEskJT3RWPAQ88 0ueZiFDQmXYLRFX63rZDthy+5cTLT/bUiHQK6URN2Ue7m9MfC2nRumk6Ydu+mu2izcgR r9eGqdFZ4GcjW3nPxwDgHs9L5+IlZiqBqr7YO4kQfJH866C+qgwK4yuBFMNrGeoVPec5 1S7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YECmlLyE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z1si10540604otm.242.2020.01.14.22.15.39; Tue, 14 Jan 2020 22:15:55 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YECmlLyE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726566AbgAOGOQ (ORCPT + 99 others); Wed, 15 Jan 2020 01:14:16 -0500 Received: from mail-yw1-f65.google.com ([209.85.161.65]:45634 "EHLO mail-yw1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726440AbgAOGOP (ORCPT ); Wed, 15 Jan 2020 01:14:15 -0500 Received: by mail-yw1-f65.google.com with SMTP id d7so10572094ywl.12 for ; Tue, 14 Jan 2020 22:14:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=VEBiEWWczDCOsezp9xBUmXOvLcrB5S1wxD0g5MzN8aA=; b=YECmlLyEJydlZuQRbc4ygVEZ22HwLEJpx9ImEnLFgfMbG12BYqnWQll5imuGXxorWz O83fsaCcuui1Mm27iQQNAFiAO3s/g25ffSvXoiUcbkF6eEdOo9wlvn59YGJ9wcbBiEJR lZcIjNCU6E0EaQeX9RKRYXW3WKu6/qoaejM8dqk2N+xtr4wydJGMRcYqGvyr6+asqXsf YaSblQBtelLSJwf/rBW5GHAShS4zueLC6ygBAE4NxbuV2F56z2QesIjjtRdwI0+9bvqv +Mt0sSHhC5C1e3wXp1+SHxhaaIL/W+HRymGNyNI10x1z/4b9W6djbnqxXgOAdqMP7MVm Jk+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=VEBiEWWczDCOsezp9xBUmXOvLcrB5S1wxD0g5MzN8aA=; b=OOY0heYB0IO06s9MR+9LuhG6oWiniopsc2btdJlkY/oQYe/fQlURmg5matLqMi7vgh W36MczC426PitaE/XOFs81gtzm0XbhDZKA7J/7TANYiQJv7z/1Lnmi5ep+r70jJIrOYL hzkL16NZmzia0JHekYejdwSEZcbjICn5xnHiTAwgzyzjSMZmBCx5oOF/IeS228uQi2Qn k/gJPogPUPPBJON7ZrwUX886+Os3prRt/dZkUitfOGqk6OXywXXXd+3Bm7SxcITBkdoh DM1etkmpE3QGXUdoRLKJmuQEt1EwyGv2KUuy2kpqHJZ3kQkoUuH/xwYq4BlP0O1q82/4 pXKA== X-Gm-Message-State: APjAAAUdVxSTdAUWfEb+3+ldlwQoQ0KT1BqWFrsH5/WOi6XapazQhek7 BSW4R7lap+up/Zta5C+MMCYQWSgq/RnnkkxI1GQ8wA== X-Received: by 2002:a81:2782:: with SMTP id n124mr20570754ywn.444.1579068853832; Tue, 14 Jan 2020 22:14:13 -0800 (PST) MIME-Version: 1.0 References: <20200113164158.15803-1-siddhesh@gotplt.org> In-Reply-To: <20200113164158.15803-1-siddhesh@gotplt.org> From: Masami Hiramatsu Date: Wed, 15 Jan 2020 15:14:02 +0900 Message-ID: Subject: Re: [PATCH] kselftest: Minimise dependency of get_size on C library interfaces To: Siddhesh Poyarekar Cc: linux-kselftest@vger.kernel.org, Shuah Khan , Linux kernel mailing list , Tim Bird Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, [Cc: Tim Bird] 2020=E5=B9=B41=E6=9C=8814=E6=97=A5(=E7=81=AB) 1:43 Siddhesh Poyarekar : > > It was observed[1] on arm64 that __builtin_strlen led to an infinite > loop in the get_size selftest. This is because __builtin_strlen (and > other builtins) may sometimes result in a call to the C library > function. The C library implementation of strlen uses an IFUNC > resolver to load the most efficient strlen implementation for the > underlying machine and hence has a PLT indirection even for static > binaries. Because this binary avoids the C library startup routines, > the PLT initialization never happens and hence the program gets stuck > in an infinite loop. > > On x86_64 the __builtin_strlen just happens to expand inline and avoid > the call but that is not always guaranteed. > > Further, while testing on x86_64 (Fedora 31), it was observed that the > test also failed with a segfault inside write() because the generated > code for the write function in glibc seems to access TLS before the > syscall (probably due to the cancellation point check) and fails > because TLS is not initialised. > > To mitigate these problems, this patch reduces the interface with the > C library to just the syscall function. The syscall function still > sets errno on failure, which is undesirable but for now it only > affects cases where syscalls fail. > > [1] https://bugs.linaro.org/show_bug.cgi?id=3D5479 > Thank you for the fix! I confirmed this fixes the issue. ---- root@devnote2:/opt/kselftest/size# ./get_size TAP version 13 # Testing system size. ok 1 get runtime memory use # System runtime memory report (units in Kilobytes): --- Total: 16085116 Free: 2042880 Buffer: 814052 In use: 13228184 ... 1..1 ---- Tested-by: Masami Hiramatsu > Signed-off-by: Siddhesh Poyarekar > Reported-by: Masami Hiramatsu > --- > tools/testing/selftests/size/get_size.c | 24 ++++++++++++++++++------ > 1 file changed, 18 insertions(+), 6 deletions(-) > > diff --git a/tools/testing/selftests/size/get_size.c b/tools/testing/self= tests/size/get_size.c > index d4b59ab979a0..f55943b6d1e2 100644 > --- a/tools/testing/selftests/size/get_size.c > +++ b/tools/testing/selftests/size/get_size.c > @@ -12,23 +12,35 @@ > * own execution. It also attempts to have as few dependencies > * on kernel features as possible. > * > - * It should be statically linked, with startup libs avoided. > - * It uses no library calls, and only the following 3 syscalls: > + * It should be statically linked, with startup libs avoided. It uses > + * no library calls except the syscall() function for the following 3 > + * syscalls: > * sysinfo(), write(), and _exit() > * > * For output, it avoids printf (which in some C libraries > * has large external dependencies) by implementing it's own > * number output and print routines, and using __builtin_strlen() > + * > + * The test may crash if any of the above syscalls fails because in some > + * libc implementations (e.g. the GNU C Library) errno is saved in > + * thread-local storage, which does not get initialized due to avoiding > + * startup libs. > */ > > #include > #include > +#include > > #define STDOUT_FILENO 1 > > static int print(const char *s) > { > - return write(STDOUT_FILENO, s, __builtin_strlen(s)); > + size_t len =3D 0; > + > + while (s[len] !=3D '\0') > + len++; > + > + return syscall(SYS_write, STDOUT_FILENO, s, len); > } > > static inline char *num_to_str(unsigned long num, char *buf, int len) > @@ -80,12 +92,12 @@ void _start(void) > print("TAP version 13\n"); > print("# Testing system size.\n"); > > - ccode =3D sysinfo(&info); > + ccode =3D syscall(SYS_sysinfo, &info); > if (ccode < 0) { > print("not ok 1"); > print(test_name); > print(" ---\n reason: \"could not get sysinfo\"\n ...\n")= ; > - _exit(ccode); > + syscall(SYS_exit, ccode); > } > print("ok 1"); > print(test_name); > @@ -101,5 +113,5 @@ void _start(void) > print(" ...\n"); > print("1..1\n"); > > - _exit(0); > + syscall(SYS_exit, 0); > } > -- > 2.24.1 > -- Masami Hiramatsu