From patchwork Mon Oct 31 22:01:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 620263 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp3128515pvb; Mon, 31 Oct 2022 15:04:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM48yjo4jCM+2g+61+in5VjlQShYOH/LGLrfv27UU9ZyOgmHfQ/lE4MVc2A/Z1MIuZY+9ts9 X-Received: by 2002:a05:620a:490a:b0:6fa:28b1:6a84 with SMTP id ed10-20020a05620a490a00b006fa28b16a84mr5940929qkb.536.1667253842132; Mon, 31 Oct 2022 15:04:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667253842; cv=none; d=google.com; s=arc-20160816; b=MN70TdM/O79YHD/SUx42YvgME6k1Vwubg49CDZbU05DBH4FjKNpkCKttSSAsWhUoR3 gB+jikTxffyeA0nrPufoW1QuVevFTtNrVXwm1Arg06+EK0I8I3pLkl7gYWptTVKVxpQN GvcSFednKKIfcQ93qdKSdn9eHpB0lmVjF6waG5pdnKmI0xsTyjCBjA7PHd1ApMyw9tD5 bNjH/7fTID0jMHhEwM2Sf5z300gnK+DIPJX9aFFBXjNPIiOxkL0T5iQiFF95VmjPy70u qLecwBrHOhxkuk1P1n1u9VN1a3B+CoOkWQTUlMzldGLFRqGX8zKCKHThsig0LvGapn54 ju9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=p5pSoZP8flpR+zy8VVBNx48l3eYCAiXbKfe8EtMZICo=; b=A2HOiByAPEXqYeSxDJscMV0eiq/OY1GXMaeCygL+9m2CIFKNfRrNiNPi/jxHeKJPQR lk62F2YuzHUEHLxLFTCIyP4Z+gnlYxCgCDLIq6BVXU2oAUDsc2j3JIWxkbzCE3r7WmZw eb6rJ1aULwbN30VykslLu3P7PWZ9w5g9ZNwDP+sB5VowI4t02325sGEjR3VvooLerQ56 s5ZzJhfogI9c1Qt1TVggrNy5dRANMCwFnxjgdDak9Bsx3B+hq98q7rskXglRm6LBq6jO EDXWsVeabzsxaW/mq6bFFlVBRZaEiSMBwv+AFGc6r15LCgD3uvA9hjZHcSFpol2l5dzG nySA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RbS5xbnE; spf=pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id k4-20020ac84744000000b0039cd5aed4afsi4575902qtp.217.2022.10.31.15.04.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 31 Oct 2022 15:04:02 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RbS5xbnE; spf=pass (google.com: domain of qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opcrH-00064V-Dx; Mon, 31 Oct 2022 18:02:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opcqz-0005YB-IZ for qemu-devel@nongnu.org; Mon, 31 Oct 2022 18:01:59 -0400 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1opcqj-0001Ms-KR for qemu-devel@nongnu.org; Mon, 31 Oct 2022 18:01:49 -0400 Received: by mail-io1-xd2f.google.com with SMTP id p184so10915588iof.11 for ; Mon, 31 Oct 2022 15:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p5pSoZP8flpR+zy8VVBNx48l3eYCAiXbKfe8EtMZICo=; b=RbS5xbnEvbXKRZtRdJP78X+Rzst6xzsKb35fcVvbGfAFYJhGU7hUvqqwa5kZiVWWLu IEYlteBIXC7qUaI+YUw/rNk+fwR/Ipv7EKkxbjdII3e2GMCMoml3zmglSSGCRa4xqYMV rEqFzZ3Nu8N6EQSuSVjLjipTrYzdRuYXn4urUvZ84QlcobVGgV+0YyKQqQqsC5hWd+ev Zt8+N1s0qVT3D2boppH+hXnk0D5R3BMhHk+Yba8gl/TWrQbvVHtnJmg1s9bCakgxCcHG mqPPcd1PqmO/PrEqq1/RcIrWouxDCbNXiAULCr89h+YQ55UItfixLPp/CBiOseaiEX2K UnUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p5pSoZP8flpR+zy8VVBNx48l3eYCAiXbKfe8EtMZICo=; b=Uk8iHM2omifycXW+mbkpeLexKzBMoxo5wG+A1GkfIksnaVairjSym0vMTtKTF4BL7k HOfMpVO/mcq9ewpL3PVKPSNniXJ1axH04nCvwQfARxNOM2okGluQMUD2cSZv9bKSMDtX U5ogmmcJMBSX69Y/mkyfK+KrAX9a29elctjkaDi163brLrgoS7XzCkW/1oWjS3YNp/Ng /JHJQ7PVdJz3WaaWlWqA2AzexJlPyRb7hQLQDVzAn+P0qKIcTEHNs68H3rDWdCnZKcRH 0FFj4JdejyGD3pNjj35Sq9i13iGWuXoiGDvFstQXnqmuDLhIgI2NmPkuHn2IdV5d3lF/ OgXA== X-Gm-Message-State: ACrzQf2jqV6AZW7xl/RdEkFdweqTFWGUQwb2SP2g6wgH0PwnnaDTuOd0 mA+zHmR+fph2yg0m87EXpUlx1DJYhQY6hw== X-Received: by 2002:a05:6638:f13:b0:375:4ae5:6be7 with SMTP id h19-20020a0566380f1300b003754ae56be7mr7114147jas.57.1667253700361; Mon, 31 Oct 2022 15:01:40 -0700 (PDT) Received: from stoup.. ([172.58.176.58]) by smtp.gmail.com with ESMTPSA id s7-20020a02c507000000b00363ec4dcaacsm3203039jam.22.2022.10.31.15.01.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 15:01:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: stefanha@gmail.com, Ilya Leoshkevich Subject: [PULL v2 13/13] tests/tcg/multiarch: Add munmap-pthread.c Date: Tue, 1 Nov 2022 09:01:13 +1100 Message-Id: <20221031220113.414796-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221031220113.414796-1-richard.henderson@linaro.org> References: <20221031215723.414467-1-richard.henderson@linaro.org> <20221031220113.414796-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::d2f; envelope-from=richard.henderson@linaro.org; helo=mail-io1-xd2f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+peter.griffin=linaro.org+patch=linaro.org@nongnu.org From: Ilya Leoshkevich Add a test to detect races between munmap() and creating new threads. Signed-off-by: Ilya Leoshkevich Message-Id: <20221028124227.2354792-3-iii@linux.ibm.com> [rth: add more return insns] Signed-off-by: Richard Henderson --- tests/tcg/multiarch/munmap-pthread.c | 79 ++++++++++++++++++++++++++++ tests/tcg/multiarch/Makefile.target | 3 ++ 2 files changed, 82 insertions(+) create mode 100644 tests/tcg/multiarch/munmap-pthread.c diff --git a/tests/tcg/multiarch/munmap-pthread.c b/tests/tcg/multiarch/munmap-pthread.c new file mode 100644 index 0000000000..d7143b00d5 --- /dev/null +++ b/tests/tcg/multiarch/munmap-pthread.c @@ -0,0 +1,79 @@ +/* Test that munmap() and thread creation do not race. */ +#include +#include +#include +#include +#include +#include +#include + +static const char nop_func[] = { +#if defined(__aarch64__) + 0xc0, 0x03, 0x5f, 0xd6, /* ret */ +#elif defined(__alpha__) + 0x01, 0x80, 0xFA, 0x6B, /* ret */ +#elif defined(__arm__) + 0x1e, 0xff, 0x2f, 0xe1, /* bx lr */ +#elif defined(__riscv) + 0x67, 0x80, 0x00, 0x00, /* ret */ +#elif defined(__s390__) + 0x07, 0xfe, /* br %r14 */ +#elif defined(__i386__) || defined(__x86_64__) + 0xc3, /* ret */ +#endif +}; + +static void *thread_mmap_munmap(void *arg) +{ + volatile bool *run = arg; + char *p; + int ret; + + while (*run) { + p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + assert(p != MAP_FAILED); + + /* Create a small translation block. */ + memcpy(p, nop_func, sizeof(nop_func)); + ((void(*)(void))p)(); + + ret = munmap(p, getpagesize()); + assert(ret == 0); + } + + return NULL; +} + +static void *thread_dummy(void *arg) +{ + return NULL; +} + +int main(void) +{ + pthread_t mmap_munmap, dummy; + volatile bool run = true; + int i, ret; + + /* Without a template, nothing to test. */ + if (sizeof(nop_func) == 0) { + return EXIT_SUCCESS; + } + + ret = pthread_create(&mmap_munmap, NULL, thread_mmap_munmap, (void *)&run); + assert(ret == 0); + + for (i = 0; i < 1000; i++) { + ret = pthread_create(&dummy, NULL, thread_dummy, NULL); + assert(ret == 0); + ret = pthread_join(dummy, NULL); + assert(ret == 0); + } + + run = false; + ret = pthread_join(mmap_munmap, NULL); + assert(ret == 0); + + return EXIT_SUCCESS; +} diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target index 78104f9bbb..5f0fee1aad 100644 --- a/tests/tcg/multiarch/Makefile.target +++ b/tests/tcg/multiarch/Makefile.target @@ -36,6 +36,9 @@ threadcount: LDFLAGS+=-lpthread signals: LDFLAGS+=-lrt -lpthread +munmap-pthread: CFLAGS+=-pthread +munmap-pthread: LDFLAGS+=-pthread + # We define the runner for test-mmap after the individual # architectures have defined their supported pages sizes. If no # additional page sizes are defined we only run the default test.