From patchwork Wed Dec 14 21:30:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 61953 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 81265383EB3F for ; Wed, 14 Dec 2022 21:31:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81265383EB3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671053479; bh=JFG6C+M2ZGycdtKILsxGouOuADif+hOXWJd3JB5acw8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=SmSdu1FnqK7hflFkfP3/IxFUVKUj6AoyVt/4CeS01rBlXbGUkobIxxIHmnFLL4EIZ gK7WAbuSWW7bhoztk7CFbnJmmSJrm6+Kvu03gXx1Vg894RNQrOcUVIRf4hnE7rMK7C Dmd9Zf8zt28UuVYqjxw2ELZM3bPpPW8dzN2icaPs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id D0CF53872543 for ; Wed, 14 Dec 2022 21:30:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D0CF53872543 Received: by mail-pj1-x102a.google.com with SMTP id fy4so8406842pjb.0 for ; Wed, 14 Dec 2022 13:30:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JFG6C+M2ZGycdtKILsxGouOuADif+hOXWJd3JB5acw8=; b=iCI7pQnLedelsqma87QZCO9+t2W4HIAERfyleKXuu+7+6oPjbOgOxPli/+R1C54jhK LVRW635jR49PXYU52ZOx4nylH6x0ebKeqhksStXPj94wsCYlSEfKNPz70NltRmYch/34 YQuAVlqeYvFT9Fex2BE0Kh01iSuG1+0jPqjIYlrDeM5PGixWGOo6d9UR/L0WOBZJEFa6 Y51G8SX5nkZQqMZXYF1De5I4myh/phUP2El5ZdNoGUx5W6AEa4OJmFYvm2hRFAzhIxAP 5aCIbmqtF4QLAXasVjalKLH2puH8LLDu8lLKDiSA7Vy5ugsfGpk7ArLBUN0z5EITTg5G UQdg== X-Gm-Message-State: ANoB5pmPw/6b4xPqJ4jhAf7VDsA17oU2T/tNI0nperD40uzYLzA1sjv6 LK++LXtORFUH4ptguyDDLNidtKWwQmg= X-Google-Smtp-Source: AA0mqf7NJd1cApUssKOYEiGJPoj5tHdTlDjstpAukYZyX8VGVoESzX+qb/zDRT7S5EfkIItUaSQ46Q== X-Received: by 2002:a05:6a20:93a3:b0:ad:a277:e57f with SMTP id x35-20020a056a2093a300b000ada277e57fmr16385427pzh.34.1671053456768; Wed, 14 Dec 2022 13:30:56 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.207]) by smtp.gmail.com with ESMTPSA id c2-20020a63ef42000000b00478c48cf73csm269932pgk.82.2022.12.14.13.30.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 13:30:55 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 6E693740079; Wed, 14 Dec 2022 13:30:53 -0800 (PST) To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH v5] x86: Add a testcase for BZ #29863 Date: Wed, 14 Dec 2022 13:30:53 -0800 Message-Id: <20221214213053.4174989-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When a thread is updating the memory area of memcmp and another thread is doing memcmp, the memcmp return value is undefined. Add a testcase to verify that memcmp won't segfault in this case. --- sysdeps/x86/Makefile | 8 +++ sysdeps/x86/tst-memcmp-race.c | 106 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 sysdeps/x86/tst-memcmp-race.c diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index 56fd5fc805..c3f05a06ab 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -257,3 +257,11 @@ tests += \ tests-static += \ tst-sysconf-cache-linesize-static endif + +ifeq ($(subdir),nptl) +tests += \ + tst-memcmp-race \ +# tests + +CFLAGS-tst-memcmp-race.c += -O0 +endif diff --git a/sysdeps/x86/tst-memcmp-race.c b/sysdeps/x86/tst-memcmp-race.c new file mode 100644 index 0000000000..eefa9e43c4 --- /dev/null +++ b/sysdeps/x86/tst-memcmp-race.c @@ -0,0 +1,106 @@ +/* Test case for memcmp with race condition. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Verify that there is no segfault when one thread is updating the + memory block of memcmp and the other thread is doing memcmp. */ + +#define TEST_MAIN +#define TEST_NAME "memcmp" + +#include +#include +#include +#include +#include + +#define NUM_THREADS 2 +#define LOOP1 10000 +#define LOOP2 1000000 + +typedef int (*proto_t) (const void *, const void *, size_t); + +IMPL (memcmp, 1) + +struct arg +{ + proto_t func; + size_t len; + int i; +}; + +static void * +childThread (void *tArgs) +{ + struct arg *a = (struct arg *) tArgs; + int i; + + if (0 == a->i % 2) + { + for (i = 0; i < LOOP1; i++) + { + int result = a->func (buf1, buf2, a->len); + printf ("i = %d : result = %d\n", i, result); + } + } + else + { + size_t offset = a->len > 16 ? a->len - 16 : 1; + for (i = 0; i < LOOP2; i++) + buf1[offset] = i & 1; + } + + return NULL; +} + +static void +do_one_test (proto_t func, size_t len) +{ + int i; + pthread_t threads[NUM_THREADS]; + struct arg a[NUM_THREADS]; + + for (i = 0; i < NUM_THREADS; ++i) + { + a[i].func = func; + a[i].len = len; + a[i].i = i; + threads[i] = xpthread_create (NULL, childThread, (void *)&a[i]); + } + + for (i = 0; i < NUM_THREADS; ++i) + xpthread_join (threads[i]); +} + +int +test_main (void) +{ + test_init (); + + memset (buf1, 1, page_size); + memset (buf2, 1, page_size); + + for (size_t i = 0; i <= 11; i++) + { + FOR_EACH_IMPL (impl, 0) + do_one_test ((proto_t) impl->fn, (1 << i) + 1); + } + + return 0; +} + +#include