Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6580363ybl; Wed, 15 Jan 2020 07:00:13 -0800 (PST) X-Google-Smtp-Source: APXvYqy1I72t19pMBT11v9KIpeFMfD1q/tVBwI3qLT2HuQn9SRC8MOsfdl9rWIBNYleP2DR4fmAs X-Received: by 2002:a9d:5542:: with SMTP id h2mr2863068oti.146.1579100413516; Wed, 15 Jan 2020 07:00:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579100413; cv=none; d=google.com; s=arc-20160816; b=H/x2SHIoB6B4A7iT/ZQxqfDFyx94J8c+YTOUx5nR2AL6Y4uYyEIrRYD7GOlAXtnFcH xN165olXzkreWSwoihajOrZSFpB0SpjupTW/ypt/B/A/hIEVFBn3yGfWlWb5MFZoTtEi mTqLWQG4NMmxhnIo6fO6Dba+i9mSiMMcSWJ2pStYrJlbHR3uG6dpOQKY1dJb04VEfb0L ANUrV7r5XKTIi9TFUujQQ2CtfqEewusWiYfMehuHlhWwnbfqhktWrN+6Xo2W7Yiqs5Uh Th6zho7Es0fwwGwhBwTNYjebU2Q7nP0Ns8n62j+IMuFxNavi3sw9+JwvINmVtQMR1sku lYKw== 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=NmZV6ZNor/aMnsybdf9Y2H14ROOnyA+MZIhvgXeb7nw=; b=SutwecMhyT7N3d/vRQoUyzDcwt/Z3RtajkRtN8ES/TSsD5ULDoLbDaBoCdGhXbycZI KlV60XvgbpVzHEKB9zSsvIlogvMYiqFehd0b26toSo95rn+DfHVyYEO2Dt1NPdZnMEpy +Dwxxe6fYzQWnK2N+EwHLC3Q5mstBLFO5LoMbxLj+8Ueq4qn0GhedKILwLWTIci6ohbB 9lyEXJFogJZKKcj6ZQcOO4nCBeHwGy2d2lWqiWnX0cpXq80QdZIknIhig06d7yoWnBUM diPbrwtFEWf3h2tpesluCVDxlNh/89I48WKE9kuFOdzaNqwPeDuwBqZbbBsHZwYtPXCZ fwlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qNEZxBFu; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d9si8328480otf.81.2020.01.15.07.00.00; Wed, 15 Jan 2020 07:00:13 -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=@google.com header.s=20161025 header.b=qNEZxBFu; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729112AbgAOO5t (ORCPT + 99 others); Wed, 15 Jan 2020 09:57:49 -0500 Received: from mail-qv1-f65.google.com ([209.85.219.65]:37320 "EHLO mail-qv1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729011AbgAOO5t (ORCPT ); Wed, 15 Jan 2020 09:57:49 -0500 Received: by mail-qv1-f65.google.com with SMTP id f16so7464871qvi.4 for ; Wed, 15 Jan 2020 06:57:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=NmZV6ZNor/aMnsybdf9Y2H14ROOnyA+MZIhvgXeb7nw=; b=qNEZxBFuHgepbe5MkJcZ7SkeuRPDFbFExzWM7bIuACkIF5m5dMo/797c91mMptly0B H1NngPZ41XCxD8QWCrw30X/lbuDzQBMZORa9RsrpkNUxmhxF8mVOJM3Iu48ojLFwy2oj rgj/YU8/6+O/sa1Rin0saBAlu/uDpYTFqOUMijbptMGw8Chuj8Y4qqqvqogM4Y/Nzqxw kP9GsVEvGI49TJu2rxXKSwnO247ImhbDgM2Ock70tZcMK9SK0cYHNUyOphF5uw3GGTGm Q9xaW5zpK3ns4vUceei3zbtYEdwPxPrMOzbi715D13qacQeEfHfpauCiP6+nDd9CGVVi pIvQ== 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=NmZV6ZNor/aMnsybdf9Y2H14ROOnyA+MZIhvgXeb7nw=; b=ftdAk1CgWGpg6f7inLLvC30x25A9Dwbmtduhvfm73NNqiMD642XYFBbMv/xTr5+gy5 3YG8EhiCuZuvK1WHfcsv0xBPBiSDpO94Dh96Uo0lX0UX3JvagPqVvXnX4MmdRUjEh0NJ Lq2xfuPraCfLr9ivCWIVPIffx92nxpIwY/pZgh1gs5t/1g2Cw0ee8UfZFRCLc8nqpEQ3 I9oOOINulvSENJDBDOYzhoV8OjSvffPNhYHnRuHPLhyiOje2y24hhvOsyy0AL32ODYHR 8auY3iQUe4Bj8ddz1CgcODdv+JNv4LfuqobniLMBm5i4r/Dy0rYPVs7DvyzqXSxbdlhZ RPsw== X-Gm-Message-State: APjAAAUI+GkYxQ8wXYM2ssWyiurI7JjjMhFH7BGXddsjuRxTz0yh6pYL i7YmJdU5yigu/8Y4djxAvHQVnAOUO6jhyU9iIlA3Fg== X-Received: by 2002:ad4:4810:: with SMTP id g16mr25308035qvy.22.1579100267932; Wed, 15 Jan 2020 06:57:47 -0800 (PST) MIME-Version: 1.0 References: <20200115063710.15796-1-dja@axtens.net> <20200115063710.15796-2-dja@axtens.net> <917cc571-a25c-3d3e-547c-c537149834d6@c-s.fr> In-Reply-To: <917cc571-a25c-3d3e-547c-c537149834d6@c-s.fr> From: Dmitry Vyukov Date: Wed, 15 Jan 2020 15:57:36 +0100 Message-ID: Subject: Re: [PATCH 1/2] kasan: stop tests being eliminated as dead code with FORTIFY_SOURCE To: Christophe Leroy Cc: Daniel Axtens , linux-s390 , linux-xtensa@linux-xtensa.org, "the arch/x86 maintainers" , LKML , kasan-dev , Linux-MM , Daniel Micay , Alexander Potapenko , Andrey Ryabinin , linuxppc-dev , Linux ARM 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 On Wed, Jan 15, 2020 at 3:47 PM Christophe Leroy wrote: > > Le 15/01/2020 =C3=A0 15:43, Dmitry Vyukov a =C3=A9crit : > > On Wed, Jan 15, 2020 at 7:37 AM Daniel Axtens wrote: > >> > >> 3 KASAN self-tests fail on a kernel with both KASAN and FORTIFY_SOURCE= : > >> memchr, memcmp and strlen. > >> > >> When FORTIFY_SOURCE is on, a number of functions are replaced with > >> fortified versions, which attempt to check the sizes of the operands. > >> However, these functions often directly invoke __builtin_foo() once th= ey > >> have performed the fortify check. The compiler can detect that the res= ults > >> of these functions are not used, and knows that they have no other sid= e > >> effects, and so can eliminate them as dead code. > >> > >> Why are only memchr, memcmp and strlen affected? > >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > >> > >> Of string and string-like functions, kasan_test tests: > >> > >> * strchr -> not affected, no fortified version > >> * strrchr -> likewise > >> * strcmp -> likewise > >> * strncmp -> likewise > >> > >> * strnlen -> not affected, the fortify source implementation calls = the > >> underlying strnlen implementation which is instrumente= d, not > >> a builtin > >> > >> * strlen -> affected, the fortify souce implementation calls a __b= uiltin > >> version which the compiler can determine is dead. > >> > >> * memchr -> likewise > >> * memcmp -> likewise > >> > >> * memset -> not affected, the compiler knows that memset writes to= its > >> first argument and therefore is not dead. > >> > >> Why does this not affect the functions normally? > >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > >> > >> In string.h, these functions are not marked as __pure, so the compiler > >> cannot know that they do not have side effects. If relevant functions = are > >> marked as __pure in string.h, we see the following warnings and the > >> functions are elided: > >> > >> lib/test_kasan.c: In function =E2=80=98kasan_memchr=E2=80=99: > >> lib/test_kasan.c:606:2: warning: statement with no effect [-Wunused-va= lue] > >> memchr(ptr, '1', size + 1); > >> ^~~~~~~~~~~~~~~~~~~~~~~~~~ > >> lib/test_kasan.c: In function =E2=80=98kasan_memcmp=E2=80=99: > >> lib/test_kasan.c:622:2: warning: statement with no effect [-Wunused-va= lue] > >> memcmp(ptr, arr, size+1); > >> ^~~~~~~~~~~~~~~~~~~~~~~~ > >> lib/test_kasan.c: In function =E2=80=98kasan_strings=E2=80=99: > >> lib/test_kasan.c:645:2: warning: statement with no effect [-Wunused-va= lue] > >> strchr(ptr, '1'); > >> ^~~~~~~~~~~~~~~~ > >> ... > >> > >> This annotation would make sense to add and could be added at any poin= t, so > >> the behaviour of test_kasan.c should change. > >> > >> The fix > >> =3D=3D=3D=3D=3D=3D=3D > >> > >> Make all the functions that are pure write their results to a global, > >> which makes them live. The strlen and memchr tests now pass. > >> > >> The memcmp test still fails to trigger, which is addressed in the next > >> patch. > >> > >> Cc: Daniel Micay > >> Cc: Andrey Ryabinin > >> Cc: Alexander Potapenko > >> Cc: Dmitry Vyukov > >> Fixes: 0c96350a2d2f ("lib/test_kasan.c: add tests for several string/m= emory API functions") > >> Signed-off-by: Daniel Axtens > >> --- > >> lib/test_kasan.c | 30 +++++++++++++++++++----------- > >> 1 file changed, 19 insertions(+), 11 deletions(-) > >> > >> diff --git a/lib/test_kasan.c b/lib/test_kasan.c > >> index 328d33beae36..58a8cef0d7a2 100644 > >> --- a/lib/test_kasan.c > >> +++ b/lib/test_kasan.c > >> @@ -23,6 +23,14 @@ > >> > >> #include > >> > >> +/* > >> + * We assign some test results to these globals to make sure the test= s > >> + * are not eliminated as dead code. > >> + */ > >> + > >> +int int_result; > >> +void *ptr_result; > > > > These are globals, but are not static and don't have kasan_ prefix. > > But I guess this does not matter for modules? > > Otherwise: > > > > Reviewed-by: Dmitry Vyukov > > > > I think if you make them static, GCC will see they aren't used and will > eliminate everything still ? static volatile? :)