From patchwork Mon Jun 8 09:26:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kushal Chand X-Patchwork-Id: 1305047 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=zilogic.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49gSZg2ZqMz9sRK for ; Mon, 8 Jun 2020 19:27:22 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 6F5723C291D for ; Mon, 8 Jun 2020 11:27:18 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id 8408F3C2068 for ; Mon, 8 Jun 2020 11:27:15 +0200 (CEST) Received: from mail.zilogic.com (mail.zilogic.com [45.33.14.236]) by in-6.smtp.seeweb.it (Postfix) with ESMTP id 8BEDF140122A for ; Mon, 8 Jun 2020 11:27:14 +0200 (CEST) Date: Mon, 08 Jun 2020 09:26:35 -0000 To: ltp@lists.linux.it Message-ID: <20200608092635.15248-1-kushalchand@zilogic.com> From: "Kushal Chand" Received: from localhost.localdomain (broadband.actcorp.in [49.207.136.172]) by mail.zilogic.com; Mon, 08 Jun 2020 09:26:49 -0000 X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=MSGID_FROM_MTA_HEADER, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v1] Added test case to test mmap with MAP_FIXED_NOREPLACE flag X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Vijay Kumar B ." Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" This patch adds a new test case for the mmap syscall. It tests the MAP_FIXED_NOREPLACE flag of mmap. The code checks if MAP_FIXED_NOREPLACE returns with EEXIST when mapped with an already mapped address. It does so by allocating an available address by passing NULL to first argument of mmap and tries to mmap with MAP_FIXED_NOREPLACE flag at the same address returned by the first mmap call. This fails as expected. It also does the necessary changes required to run the syscall using the runltp command after building the LTP test suite. Git Hub Issue link - https://github.com/linux-test-project/ltp/issues/299 Signed-off-by: Kushal Chand Reviewed-by: Vijay Kumar B. --- runtest/syscalls | 1 + testcases/kernel/syscalls/mmap/.gitignore | 1 + testcases/kernel/syscalls/mmap/mmap17.c | 85 +++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 testcases/kernel/syscalls/mmap/mmap17.c -- 2.20.1 diff --git a/runtest/syscalls b/runtest/syscalls index edd3e8de7..e685037c7 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -745,6 +745,7 @@ mmap14 mmap14 #mmap11 mmap11 -i 30000 mmap15 mmap15 mmap16 mmap16 +mmap17 mmap17 modify_ldt01 modify_ldt01 modify_ldt02 modify_ldt02 diff --git a/testcases/kernel/syscalls/mmap/.gitignore b/testcases/kernel/syscalls/mmap/.gitignore index 39ed2aab0..c5c083d4b 100644 --- a/testcases/kernel/syscalls/mmap/.gitignore +++ b/testcases/kernel/syscalls/mmap/.gitignore @@ -15,3 +15,4 @@ /mmap14 /mmap15 /mmap16 +/mmap17 diff --git a/testcases/kernel/syscalls/mmap/mmap17.c b/testcases/kernel/syscalls/mmap/mmap17.c new file mode 100644 index 000000000..a620f104a --- /dev/null +++ b/testcases/kernel/syscalls/mmap/mmap17.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) Zilogic Systems Pvt. Ltd., 2020 + * Email: code@zilogic.com + */ + +/* + * Test mmap with MAP_FIXED_NOREPLACE flag + * + * We are testing the MAP_FIXED_NOREPLACE flag of mmap() syscall. To check + * if an attempt to mmap at an exisiting mapping fails with EEXIST. + * The code allocates a free address by passing NULL to first mmap call + * Then tries to mmap with the same address using MAP_FIXED_NOREPLACE flag + * and the mapping fails as expected. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tst_test.h" + +static int fd_file1; +static int fd_file2; +static void *mapped_address; +static const char str[] = "Writing to mapped file"; + +#define FNAME1 "file1_to_mmap" +#define FNAME2 "file2_to_mmap" + +static void setup(void) +{ + fd_file1 = SAFE_OPEN(FNAME1, O_CREAT | O_RDWR, 0600); + fd_file2 = SAFE_OPEN(FNAME2, O_CREAT | O_RDWR, 0600); +} + +static void cleanup(void) +{ + int str_len; + + str_len = strlen(str); + + if (fd_file2 > 0) + SAFE_CLOSE(fd_file2); + if (fd_file1 > 0) + SAFE_CLOSE(fd_file1); + if (mapped_address) + SAFE_MUNMAP(mapped_address, str_len); +} + +static void test_mmap(void) +{ + int str_len; + void *address; + + str_len = strlen(str); + + SAFE_WRITE(1, fd_file1, str, str_len); + mapped_address = SAFE_MMAP(NULL, str_len, PROT_WRITE, + MAP_PRIVATE, fd_file1, 0); + + SAFE_WRITE(1, fd_file2, str, str_len); + + address = mmap(mapped_address, str_len, PROT_WRITE, + MAP_PRIVATE | MAP_FIXED_NOREPLACE, fd_file2, 0); + if (address == MAP_FAILED && errno == EEXIST) + tst_res(TPASS, "mmap set errno to EEXIST as expected"); + else + tst_res(TFAIL | TERRNO, "mmap failed, with unexpected error " + "code, expected EEXIST"); +} + +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .test_all = test_mmap, + .min_kver = "4.17", + .needs_tmpdir = 1 +};