From patchwork Wed Dec 14 19:51:34 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: 1715919 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=C9ghMGXC; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NXQx80gMGz2403 for ; Thu, 15 Dec 2022 06:51:55 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD59B384F94F for ; Wed, 14 Dec 2022 19:51:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD59B384F94F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671047513; bh=KPYlH0/jkxtp+doFifSlg9se2SLK6qWz1zwHYbvhqnM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=C9ghMGXCeW2vX6p00s9Uw2J0Vr6nRLCmyNr0ja+xhX8I16bMnpYWeDUzUTV/m1Q+o CT7kso/a4xKeSxMs6QKgkL10Sr5XPXizWVTUZEsMLInlblOwkI4hQSZhF1jXUqL57o sSJ3hnHabC1HasZh/OVJ5Hor+G5W2Le9O3EGJfiA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 2C81E3852A62 for ; Wed, 14 Dec 2022 19:51:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2C81E3852A62 Received: by mail-pj1-x102e.google.com with SMTP id u5so8075770pjy.5 for ; Wed, 14 Dec 2022 11:51:37 -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=KPYlH0/jkxtp+doFifSlg9se2SLK6qWz1zwHYbvhqnM=; b=IE60gyrJ7jT2aM1xrA2mrKXh3vrEfST/xFhzHjpCSC0mkUW8pDsnUMzk/bMtmzf4n1 e2fwULDZatB4H7zw3eBIgj3obkt0lQVYWEZ/DjxgaRPG0ox8zOa+2EjFFWzKs/d/SqBP sx9UrQ5a9o/asDhU4tVCMGsVi1B2qdjFGD1FLLpaoVkaK1MtXhiJ7a+zadUyBBg1HeYO rJUGL8+VuR1xzqyAChmULL1ayRd3WcIG3sSw858zff6/5oxRuDJBetk5CLu1D6kU/eno m7MGmULsRBkmgwv5UMNrFg8m33v+Da6FQOtLjeAUWbZOHRawpahBOGbkkxXWxqgLkjPj OSSg== X-Gm-Message-State: ANoB5pmymmwzwxELkABepuz5x3uonn1JZ6h9mOk91nllJVIzlyRfpjFi eky4SRzXlm2jc+1L5BM8OHHBnNc78zY= X-Google-Smtp-Source: AA0mqf4R36p7tNqa8ZzXjc7ObOyUg4oV4ULo9w8DnaUa9bCVcF8wIVIlj4RODAGqWGqf7EHICa1DlQ== X-Received: by 2002:a17:902:ab54:b0:189:6bda:e993 with SMTP id ij20-20020a170902ab5400b001896bdae993mr25515548plb.54.1671047495955; Wed, 14 Dec 2022 11:51:35 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.207]) by smtp.gmail.com with ESMTPSA id w15-20020a1709026f0f00b00188fcc4fc00sm2255493plk.79.2022.12.14.11.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 11:51:35 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 35CAA740079; Wed, 14 Dec 2022 11:51:34 -0800 (PST) To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH v2] x86: Add a testcase for BZ #29863 Date: Wed, 14 Dec 2022 11:51:34 -0800 Message-Id: <20221214195134.4168417-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.7 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+incoming=patchwork.ozlabs.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 | 117 ++++++++++++++++++++++++++++++++++ 2 files changed, 125 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..5630ae9472 --- /dev/null +++ b/sysdeps/x86/tst-memcmp-race.c @@ -0,0 +1,117 @@ +/* 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 STR "abcdefghijklmnopq" +#define LOOP1 100 +#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 + { + for (i = 0; i < LOOP2; i++) + buf1[1] = (0 == (i % 2)) ? 'b' : 'c'; + } + + 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 (); + + size_t remain = page_size; + size_t offset = 0; + do + { + size_t len = sizeof STR - 1; + if (len > remain) + len = remain; + memcpy (buf1 + offset, STR, len); + memcpy (buf2 + offset, STR, len); + offset += len; + remain -= len; + } + while (remain > 0); + + 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