From patchwork Wed Dec 14 20:03:03 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: 1715922 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=vSFC5BiM; 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 4NXRBN1Pnjz23yy for ; Thu, 15 Dec 2022 07:03:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D5C338446BA for ; Wed, 14 Dec 2022 20:03:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D5C338446BA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671048202; bh=BPtr8ABqTshlIw7ye33ILgmeGrXD/4305q15aLH20EI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vSFC5BiMTqKPFHyVeoY9x91Iz294tVpsH0AsXB8rGKMBOuiRJcub3Sxxg41hqrSmo CnNNS8qoG4/2vjgrNNyKXmTUwS3aDbo2JsKjSmapVJOaTtsqR3ybBaqyZm2C3w6aaa ivOwihbxJhTJtxnUDg4g2YCWzhrp98/Ujo73gbds= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id 696AA3864A24 for ; Wed, 14 Dec 2022 20:03:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 696AA3864A24 Received: by mail-pj1-x1032.google.com with SMTP id v13-20020a17090a6b0d00b00219c3be9830so358304pjj.4 for ; Wed, 14 Dec 2022 12:03:06 -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=BPtr8ABqTshlIw7ye33ILgmeGrXD/4305q15aLH20EI=; b=BAMO+o03uvDsKwmjtuXsuIdfsQ9cktQZXssEAeFOOwDuSqzp0nHU+TF0LBTNQ+vlkU zlX1i0wpvhg9+JbEnJlvgpC51sfH+oBPfoX9YkNOqwuHg5QIxKSBDjzUuU+8A2cRZM7E 8hYtVbitfnQb87GW6MuPu/fO5j6jT+aUfctH5sgWta/DFSdU6/7uYJHhKBaUi8WGBxOW z9Zlxl/4sdQ0voRQp9k/QLwB21k6OSchFB4dAvQ5nmowpi48y/kF3O2jqZypr8LIH/b+ wqVeFtUuXiyjNB0zGYc9M6qQCfgVrrsT8sDJK0ZR8ZLOnhxGWiHyGZAbNjnpMxLpzJk2 Iiag== X-Gm-Message-State: ANoB5pksEk7SELKnYGLvgE2xNXonnEsvlyZo4hxPIahwOgvqYDtAyjxc xyOsuDDbrJC7RCnjxowlKpdNaMfFTvc= X-Google-Smtp-Source: AA0mqf7gO61GfgZvvoseiG8zfQ7Rn25HKeDk9nvJ7sRwq9uQFV1GParPRWveQsztopB1aBrlvN5r5Q== X-Received: by 2002:a17:902:900c:b0:189:8d8b:9db0 with SMTP id a12-20020a170902900c00b001898d8b9db0mr27242684plp.34.1671048185237; Wed, 14 Dec 2022 12:03:05 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.207]) by smtp.gmail.com with ESMTPSA id w20-20020a170902ca1400b0018930dbc560sm2263831pld.96.2022.12.14.12.03.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 12:03:04 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 706FA740079; Wed, 14 Dec 2022 12:03:03 -0800 (PST) To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH v3] x86: Add a testcase for BZ #29863 Date: Wed, 14 Dec 2022 12:03:03 -0800 Message-Id: <20221214200303.4169289-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 | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 126 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..f28b80ef82 --- /dev/null +++ b/sysdeps/x86/tst-memcmp-race.c @@ -0,0 +1,118 @@ +/* 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 + { + size_t offset = a->len > 16 ? a->len - 16 : 1; + for (i = 0; i < LOOP2; i++) + buf1[offset] = (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