[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHp75Vf9S8jKQGAYRrmSET7YJQNoHMzUC6VVTAOT7DbwcCcc4Q@mail.gmail.com>
Date: Sun, 30 Jan 2022 20:56:40 +0200
From: Andy Shevchenko <andy.shevchenko@...il.com>
To: Kees Cook <keescook@...omium.org>
Cc: Geert Uytterhoeven <geert@...ux-m68k.org>,
Peter Rosin <peda@...ntia.se>,
Andy Shevchenko <andy@...nel.org>,
Matteo Croce <mcroce@...rosoft.com>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
llvm@...ts.linux.dev, linux-hardening@...r.kernel.org
Subject: Re: [PATCH] lib/test_string.c: Add test for strlen()
On Sun, Jan 30, 2022 at 8:36 PM Kees Cook <keescook@...omium.org> wrote:
>
> Add a simple test for strlen() functionality, including using it as a
> constant expression.
...
> +/*
> + * Unlike many other string functions, strlen() can be used in
> + * static initializers when string lengths are known at compile
> + * time. (i.e. Under these conditions, strlen() is a constant
> + * expression.) Make sure it can be used this way.
> + */
> +static const int strlen_ce = strlen("tada, a constant expression");
So, the compiler will replace this by a constant and then eliminate
the condition completely from the code. Did I understand this
correctly?
> +static __init int strlen_selftest(void)
> +{
> + /* String length ruler: 123456789012345 */
> + static const char normal[] = "I am normal";
> + static const char *ptr = "where do I go?";
> + static const char trailing[] = "hidden NULLs\0\0\0";
> + static const char leading[] = "\0\0hidden text";
> +
> + if (strlen(normal) != 11)
> + return 0x100001;
> + if (strlen(ptr++) != 14)
> + return 0x100002;
> + if (strlen(ptr++) != 13)
> + return 0x100003;
> + if (strlen(trailing) != 12)
> + return 0x100004;
> + if (strlen(leading) != 0)
> + return 0x100005;
> + if (strlen_ce != 27)
> + return 0x100006;
...so this part won't ever appear in the assembly (assuming -O2).
Same to the rest? If so, why is this not a part of the compiler tests?
> + return 0;
> +}
--
With Best Regards,
Andy Shevchenko
Powered by blists - more mailing lists