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: 61944 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 498D7382AB5A for ; Wed, 14 Dec 2022 20:03:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 498D7382AB5A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671048209; 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=DuGmNCAJprU83EUN+MtD6Cikxlkf9XcmG1G1mmbIyoZAik8aSkg0MybDCMZ4EZpTp 8SRd/iEAj8l+7P8ElEBV8SHASA/v5QOy3aVptFILC58Yv16Rm4Da6afH+v8XH8NkCO I7cAC7dbc6ru9NjRBx2U0Wi4RqpjuGCZ6eLyHb8s= 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+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 | 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