On Thu, Nov 3, 2016 at 12:51 AM Uros Bizjak <ubiz...@gmail.com> wrote: > > Hello! > > > This patch adds a test to the cmpstrnsi pattern in i386.md so that it > > will bail out (FAIL) if neither of the strings is a constant string. It > > can only work as a proper strncmp if the length is not longer than both > > of the strings. This change is required if expand_builtin_strncmp is > > going to try expansion of strncmp when neither string argument is > > constant. > > The patch needs a ChangeLog entry, but otherwise looks good.
I am checking in this patch to add a testcase for this condition. -- H.J.
From 78170fb45ffc86a8c7fc18a662e3264045aced1b Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.to...@gmail.com> Date: Sun, 17 May 2020 06:44:59 -0700 Subject: [PATCH] x86: Add gcc.target/i386/strncmp-1.c Add a strncmp test for the cmpstrn pattern with neither of the strings is a constant string. We can expand the cmpstrn pattern to "repz cmpsb" only if one of the strings is a constant so that expand_builtin_strncmp() can write the length argument to be the minimum of the const string length and the actual length argument. Otherwise, "repz cmpsb" may pass the 0 byte. * gcc.target/i386/strncmp-1.c: New test. --- gcc/testsuite/gcc.target/i386/strncmp-1.c | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 gcc/testsuite/gcc.target/i386/strncmp-1.c diff --git a/gcc/testsuite/gcc.target/i386/strncmp-1.c b/gcc/testsuite/gcc.target/i386/strncmp-1.c new file mode 100644 index 00000000000..044fc5cc5fa --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/strncmp-1.c @@ -0,0 +1,47 @@ +/* { dg-do run { target mmap } } */ +/* { dg-options "-O2" } */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> + +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + +int +__attribute__ ((noclone, noinline)) +compare (char *d, char *s, unsigned int l) +{ + return __builtin_strncmp (d, s, l); +} + +int +main () +{ + size_t page_size = sysconf(_SC_PAGESIZE); + char *buf = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (buf == MAP_FAILED) + { + perror ("mmap"); + abort (); + } + + if (mprotect (buf + page_size, page_size, PROT_NONE)) + { + perror ("mprotect"); + abort (); + } + + char *src1 = buf + page_size - sizeof ("foo"); + char *src2 = buf; + memcpy (src1, "foo", sizeof ("foo")); + memcpy (src2, "foo", sizeof ("foo")); + int result = compare (src1, src2, sizeof ("foo") + 16); + if (result != 0) + abort (); + return 0; +} -- 2.26.2