From patchwork Wed Dec 14 21:24:15 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: 61952 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 888CE383EB2B for ; Wed, 14 Dec 2022 21:24:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 888CE383EB2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671053083; bh=pw6SrWtS8h4TU94zvCWgki1toZey1Oz2fcNiaivetV0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=t646VQZtLpvBB3uya97dptwq7mggg0gdXkkTVZiEK/SsYLBJAOaLd6SbC9Zv8YEMM 7zA3ecx0KsbNCesQKjy/Ia0uNYBgn/22Xd6UaJjUnuX+yNyMRpaEjPjyy3GAn0U7yO DO/zLK1AWQg1Hk8SfpccBTmxvH4W1e6c+f8iHIwI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by sourceware.org (Postfix) with ESMTPS id C2641383FD67 for ; Wed, 14 Dec 2022 21:24:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C2641383FD67 Received: by mail-pj1-x102b.google.com with SMTP id u5so8322473pjy.5 for ; Wed, 14 Dec 2022 13:24:19 -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=pw6SrWtS8h4TU94zvCWgki1toZey1Oz2fcNiaivetV0=; b=s2GTIXqXLpzZhR5StBgqTVIe+rpFtuXo0YJftwlvmaLmMCAgNkYvPo/b8ogj6h7Zd3 l8JxlF5yA0tUDU/H6uaoAL3H8XC4tLv90s2qLrrhs9on7/2AbdmdBZFtxiH7VjJeZGeN +cweHl4rSvSJFd5NKnqStj6nI9tCeR/XELIw+mA2lkVWcI+Gctkk/Kr/tK0l5c5B5NYM stbDX5z/NSlmnM+xdHE5Lj6fe9G79c/p/4i/WXV7VgAfk/+ka5gWhbzAU/vQGN/M3Ss+ LSriXDzNCA/qTsEB59a9RoQKPFXegCYK9VTWruxjW+Guh+Omq9y0n+B/lA6kEHrPmE0E VbYg== X-Gm-Message-State: ANoB5pmkWCapVpbEg7cdbBeF84O2DMHvkNxnXEMS08Mg+RHNMkINysLu wMZryHdN4SVnSoDh1fUOKQ5UKnR7aoM= X-Google-Smtp-Source: AA0mqf61/d7yoT2+mOBDZnCxrYzi1BIJhFzx8WyYngwheLFybBClB6vPrSRkLqn+EESzg19zGv4PEw== X-Received: by 2002:a05:6a21:9181:b0:a4:150f:8ddc with SMTP id tp1-20020a056a21918100b000a4150f8ddcmr30957777pzb.52.1671053058582; Wed, 14 Dec 2022 13:24:18 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.207]) by smtp.gmail.com with ESMTPSA id o10-20020a635d4a000000b0047712e4bc51sm269558pgm.55.2022.12.14.13.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 13:24:17 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id D70A7740079; Wed, 14 Dec 2022 13:24:15 -0800 (PST) To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH v4] x86: Add a testcase for BZ #29863 Date: Wed, 14 Dec 2022 13:24:15 -0800 Message-Id: <20221214212415.4170229-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 | 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..ec78d150c7 --- /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 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] = 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