Message ID | 9c46f648cd8c784405afed565bed120f0a2f239e.1687187451.git.falcon@tinylab.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp3127914vqr; Mon, 19 Jun 2023 09:45:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6x9F1LUguoFpghxrTmjZu+ntbzJPbylWk9v+GDKmqQ7DAnUH2iERkDsImmCCQVfogOA26X X-Received: by 2002:a05:6359:6796:b0:130:e2e2:9d25 with SMTP id sq22-20020a056359679600b00130e2e29d25mr4171762rwb.13.1687193144108; Mon, 19 Jun 2023 09:45:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687193144; cv=none; d=google.com; s=arc-20160816; b=rRDOnj0U7pkstiMk1GSQCIl6lrTTsKQkh8z5c7o24DKDryqg5W5Ztf0ymgdZeO3ufc qgnlZW82li8tgK82CLrqXfVi+DoHQjQzeYUtkbuEnvINU7ytW7hy4ckO2eOFRbrOe/4M Q/m8g7etB5msgDmXIFOQA1TqVbd2b7z9OALWVeRWvgt1YHFYGwXzQ5E7fwGDaZ73ZfvW 1KVfRbat0UeFEYC6NEMaOZU+l1X+1tD092zJy9Stp/qIsieSUKoM71pA58+iqFY65Bne 484zpYXuJlfdXqmVevHNYMbgxW8GfOTE2Wy4OeKfhzQJ2nsDVOdbfJti13PeT+Dl8Ii0 ywqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=ceJr9JYregTmXC0Ro+bHlilaSDvypM0m5IX4Vo6a990=; b=I+3bUrHNyQPgiDbPRHOendEshO6u+jFAux/WBMxpJtIYpDZ+P05iMcDyP4ix7xaXg7 P9i2ij5xo917X9TrOQPww+ZSBCqgRMgXUC0OGgbVRoQsWZQv9bS6QjOH4p76YF1u4GnO e1R5k8bm5Y+P1waJeuGjdqYuDwXbytmjyBM0A8OGPhDlZzPEPV62/6e+FbSP9G/KhjPK UmdQzqoQFa5nc0ZgAwaEWyi1O5ixXigu7d74Oa4lGaAYIQH7o07EKfIV/zP59EEUQD3b 0gEh7EbTEiPmUgnngs5yRPDUpn+VRNCl7LmyJnBIHuaurwyBwJcQ73rd4GRSJU4Ttw4L pJbQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l63-20020a639142000000b005533a94a2fdsi8749168pge.466.2023.06.19.09.45.31; Mon, 19 Jun 2023 09:45:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232071AbjFSP4P (ORCPT <rfc822;duw91626@gmail.com> + 99 others); Mon, 19 Jun 2023 11:56:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229888AbjFSP4N (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 19 Jun 2023 11:56:13 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90FFE188; Mon, 19 Jun 2023 08:56:11 -0700 (PDT) X-QQ-mid: bizesmtp67t1687190155tjgcboy4 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:55:54 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 3M0okmaRx3j+DH7O25w4I49usNQKwukZko0M+fI3sghNet1qUt9QPoFPGaQxB qUMyPH1KyGxQc++m+763kfmCEtcyL49JXo2yHMQadZgrhtZ7A1HIxET4qQIWuaQOQuc0B5z vXxgUMSbD9P70U/yfA+nxAN7nnTlIGoBh+4w3niCvA+Z/2EOraI8DQpA6m6s2k1ajHZvFoE FAdBwRDieRX70M3hWobE0uCkcUfsM27XxUYZSJ731A6sywzYCHmnPH1saAHF580yKVSi5mj zZ4fd6N1vzXqxDKGes/atceNbMHFINvKZj0tmM57KNReZMT+tcf5OTPZU7XYbS6Z2xzD3im 9Tcu3DQ5VpCXjBBMGVSGOFXsL98Q4YN1yqC9dgHnKrP/0d+Ma8YNWMUUD2oOwcdwJyI9WXD X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1231696777476800510 From: Zhangjin Wu <falcon@tinylab.org> To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v4 10/10] selftests/nolibc: add mmap and munmap test cases Date: Mon, 19 Jun 2023 23:55:41 +0800 Message-Id: <9c46f648cd8c784405afed565bed120f0a2f239e.1687187451.git.falcon@tinylab.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <cover.1687187451.git.falcon@tinylab.org> References: <cover.1687187451.git.falcon@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769150238343313278?= X-GMAIL-MSGID: =?utf-8?q?1769150238343313278?= |
Series |
tools/nolibc: add a new syscall helper
|
|
Commit Message
Zhangjin Wu
June 19, 2023, 3:55 p.m. UTC
Three mmap/munmap related test cases are added:
- mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL)
The length argument must be greater than 0, otherwise, fail with -EINVAL.
- munmap((void *)-1, 4*1024), -1, EINVAL)
Invalid (void *)-1 address fail with -EINVAL.
- test_mmap_munmap(4*1024)
It finds a init file, mmap() it and then munmap().
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++
1 file changed, 31 insertions(+)
Comments
Hi, > Three mmap/munmap related test cases are added: > > - mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL) > > The length argument must be greater than 0, otherwise, fail with -EINVAL. > > - munmap((void *)-1, 4*1024), -1, EINVAL) > Sorry, this message doesn't match the code, will change it in new revison. > Invalid (void *)-1 address fail with -EINVAL. > > - test_mmap_munmap(4*1024) > > It finds a init file, mmap() it and then munmap(). > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > --- > tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ > 1 file changed, 31 insertions(+) > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index 80ab29e2887c..f7c0ca72cb28 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) > return 0; > } > > +int test_mmap_munmap(int size) > +{ > + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; > + int ret, fd, i; > + void *mem; > + > + for (i = 0; i < 5; i++) { > + ret = fd = open(init_files[i], O_RDONLY); > + if (ret < 0) > + continue; > + else > + break; > + } > + if (ret < 0) > + return ret; > + > + mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); > + if (mem == MAP_FAILED) > + return -1; > + And here need a close(fd): if (mem == MAP_FAILED) { close(fd); return -1; } > + ret = munmap(mem, size); > + if (ret < 0) > + return ret; > + And here too: if (ret < 0) { close(fd); return ret; } > + return close(fd); close(fd); return 0; Thanks, Zhangjin > +} > + > + > /* Run syscall tests between IDs <min> and <max>. > * Return 0 on success, non-zero on failure. > */ > @@ -666,6 +694,9 @@ int run_syscall(int min, int max) > CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; > CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; > CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; > + CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; > + CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)-1, 0), -1, EINVAL); break; > + CASE_TEST(mmap_good); EXPECT_SYSZR(1, test_mmap_munmap(4*1024)); break; > CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; > CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; > CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; > -- > 2.25.1
On 2023-06-19 23:55:41+0800, Zhangjin Wu wrote: > Three mmap/munmap related test cases are added: > > - mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL) > > The length argument must be greater than 0, otherwise, fail with -EINVAL. > > - munmap((void *)-1, 4*1024), -1, EINVAL) > > Invalid (void *)-1 address fail with -EINVAL. > > - test_mmap_munmap(4*1024) > > It finds a init file, mmap() it and then munmap(). > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > --- > tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ > 1 file changed, 31 insertions(+) > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index 80ab29e2887c..f7c0ca72cb28 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) > return 0; > } > > +int test_mmap_munmap(int size) > +{ > + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; Why not /proc/1/exe or even /proc/self/exe? I know your other series tries to remove the procfs dependency, but we're not there yet :-). Also does it make sense to pass a size parameter? Why not use either PAGE_SIZE or the real size of the binary from fstat(). > + int ret, fd, i; > + void *mem; > + > + for (i = 0; i < 5; i++) { > + ret = fd = open(init_files[i], O_RDONLY); > + if (ret < 0) > + continue; > + else > + break; > + } > + if (ret < 0) > + return ret; > + > + mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); > + if (mem == MAP_FAILED) > + return -1; > + > + ret = munmap(mem, size); > + if (ret < 0) > + return ret; > + > + return close(fd); > +} > + > + > /* Run syscall tests between IDs <min> and <max>. > * Return 0 on success, non-zero on failure. > */ > @@ -666,6 +694,9 @@ int run_syscall(int min, int max) > CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; > CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; > CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; > + CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; > + CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)-1, 0), -1, EINVAL); break; > + CASE_TEST(mmap_good); EXPECT_SYSZR(1, test_mmap_munmap(4*1024)); break; > CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; > CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; > CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; > -- > 2.25.1 >
Hi, Thomas > On 2023-06-19 23:55:41+0800, Zhangjin Wu wrote: > > Three mmap/munmap related test cases are added: > > > > - mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL) > > > > The length argument must be greater than 0, otherwise, fail with -EINVAL. > > > > - munmap((void *)-1, 4*1024), -1, EINVAL) > > > > Invalid (void *)-1 address fail with -EINVAL. > > > > - test_mmap_munmap(4*1024) > > > > It finds a init file, mmap() it and then munmap(). > > > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > > --- > > tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ > > 1 file changed, 31 insertions(+) > > > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > > index 80ab29e2887c..f7c0ca72cb28 100644 > > --- a/tools/testing/selftests/nolibc/nolibc-test.c > > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > > @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) > > return 0; > > } > > > > +int test_mmap_munmap(int size) > > +{ > > + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; > > Why not /proc/1/exe or even /proc/self/exe? > > I know your other series tries to remove the procfs dependency, but > we're not there yet :-). > Yeah, '/proc/self/exe' is a choice, if so, the 'has_proc' should be added ;-) > Also does it make sense to pass a size parameter? > Why not use either PAGE_SIZE or the real size of the binary from > fstat(). > Ok, as the manpage of mmap shows: For mmap(), offset must be a multiple of the underlying huge page size. The system automatically aligns length to be a multiple of the underlying huge page size. For munmap(), addr, and length must both be a multiple of the underlying huge page size. perhaps we should do further tests: * the real size/length * even > the real size * the PAGE_SIZE * not aligned with PAGE_SIZE If such tests are required, the 'size' and even 'offset' arguments could be provided to cover different combination or we do such tests internally, then, the arguments are not required. Thanks, Zhangjin > > + int ret, fd, i; > > + void *mem; > > + > > + for (i = 0; i < 5; i++) { > > + ret = fd = open(init_files[i], O_RDONLY); > > + if (ret < 0) > > + continue; > > + else > > + break; > > + } > > + if (ret < 0) > > + return ret; > > + > > + mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); > > + if (mem == MAP_FAILED) > > + return -1; > > + > > + ret = munmap(mem, size); > > + if (ret < 0) > > + return ret; > > + > > + return close(fd); > > +} > > + > > + > > /* Run syscall tests between IDs <min> and <max>. > > * Return 0 on success, non-zero on failure. > > */ > > @@ -666,6 +694,9 @@ int run_syscall(int min, int max) > > CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; > > CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; > > CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; > > + CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; > > + CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)-1, 0), -1, EINVAL); break; > > + CASE_TEST(mmap_good); EXPECT_SYSZR(1, test_mmap_munmap(4*1024)); break; > > CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; > > CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; > > CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; > > -- > > 2.25.1 > >
Hi Zhangjin, On 2023-06-23 03:32:49+0800, Zhangjin Wu wrote: > > On 2023-06-19 23:55:41+0800, Zhangjin Wu wrote: > > > Three mmap/munmap related test cases are added: > > > > > > - mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL) > > > > > > The length argument must be greater than 0, otherwise, fail with -EINVAL. > > > > > > - munmap((void *)-1, 4*1024), -1, EINVAL) > > > > > > Invalid (void *)-1 address fail with -EINVAL. > > > > > > - test_mmap_munmap(4*1024) > > > > > > It finds a init file, mmap() it and then munmap(). > > > > > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > > > --- > > > tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ > > > 1 file changed, 31 insertions(+) > > > > > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > > > index 80ab29e2887c..f7c0ca72cb28 100644 > > > --- a/tools/testing/selftests/nolibc/nolibc-test.c > > > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > > > @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) > > > return 0; > > > } > > > > > > +int test_mmap_munmap(int size) > > > +{ > > > + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; > > > > Why not /proc/1/exe or even /proc/self/exe? > > > > I know your other series tries to remove the procfs dependency, but > > we're not there yet :-). > > > > Yeah, '/proc/self/exe' is a choice, if so, the 'has_proc' should be added ;-) Currently procfs is a hard requirement. So I would leave 'has_proc' to the other series that may change this. > > Also does it make sense to pass a size parameter? > > Why not use either PAGE_SIZE or the real size of the binary from > > fstat(). > > > > Ok, as the manpage of mmap shows: > > For mmap(), offset must be a multiple of the underlying huge page > size. The system automatically aligns length to be a multiple of > the underlying huge page size. > > For munmap(), addr, and length must both be a multiple of the > underlying huge page size. > > perhaps we should do further tests: > > * the real size/length > * even > the real size > * the PAGE_SIZE > * not aligned with PAGE_SIZE > > If such tests are required, the 'size' and even 'offset' arguments could be > provided to cover different combination or we do such tests internally, then, > the arguments are not required. I think task of nolibc-test is to test the code in nolibc itself. The custom mmap implementation is trivial and directly calls the syscall. These additionally proposed tests would effectively test the core kernels implementation of mmap() and not the code of nolibc. Therefore I don't think they are necessary in nolibc-test and the functionality is hopefully already be tested in another testsuite. Note: Testing mmap is indeed useful to test the implementation of my_syscall6() by providing a bogus value in the 'offset' parameter. I think we do have such a testcase. <snip> Thomas
Hi, Thomas > Hi Zhangjin, > > On 2023-06-23 03:32:49+0800, Zhangjin Wu wrote: > > > On 2023-06-19 23:55:41+0800, Zhangjin Wu wrote: > > > > Three mmap/munmap related test cases are added: > > > > (snipped) > > > > > > > > +int test_mmap_munmap(int size) > > > > +{ > > > > + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; > > > > > > Why not /proc/1/exe or even /proc/self/exe? > > > > > > I know your other series tries to remove the procfs dependency, but > > > we're not there yet :-). > > > > > > > Yeah, '/proc/self/exe' is a choice, if so, the 'has_proc' should be added ;-) > > Currently procfs is a hard requirement. So I would leave 'has_proc' to > the other series that may change this. > Yeah, but to be consistent with the already existing 'proc' condition check, 'proc' will be used at first ;-) $ grep '(proc,' -ur tools/testing/selftests/nolibc/nolibc-test.c CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break; CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break; CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break; CASE_TEST(chroot_exe); EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break; CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/net", "/blah"), -1, EXDEV); break; Btw, for the /proc/self used in test_stat_timestamps, in the revision of the 'minimal' config support series, instead of using '/', a 'proc' should be added like above test cases. > > > Also does it make sense to pass a size parameter? > > > Why not use either PAGE_SIZE or the real size of the binary from > > > fstat(). > > > > > > > Ok, as the manpage of mmap shows: > > > > For mmap(), offset must be a multiple of the underlying huge page > > size. The system automatically aligns length to be a multiple of > > the underlying huge page size. > > > > For munmap(), addr, and length must both be a multiple of the > > underlying huge page size. > > > > perhaps we should do further tests: > > > > * the real size/length > > * even > the real size > > * the PAGE_SIZE > > * not aligned with PAGE_SIZE > > > > If such tests are required, the 'size' and even 'offset' arguments could be > > provided to cover different combination or we do such tests internally, then, > > the arguments are not required. > > I think task of nolibc-test is to test the code in nolibc itself. > The custom mmap implementation is trivial and directly calls the > syscall. These additionally proposed tests would effectively test the > core kernels implementation of mmap() and not the code of nolibc. > > Therefore I don't think they are necessary in nolibc-test and the > functionality is hopefully already be tested in another testsuite. > Ok, it is reasonable. > > Note: > > Testing mmap is indeed useful to test the implementation of > my_syscall6() by providing a bogus value in the 'offset' parameter. > I think we do have such a testcase. > Ok, we can pass a valid offset (n*PAGE_SIZE) to mmap() in test_mmap_munmap() or add a whole new mmap_offset test case with a PAGE_SIZE not aligned offset (such as 1). Thanks, Zhangjin > <snip> > > Thomas
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 80ab29e2887c..f7c0ca72cb28 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -592,6 +592,34 @@ static int test_stat_timestamps(void) return 0; } +int test_mmap_munmap(int size) +{ + char init_files[5][20] = {"/init", "/sbin/init", "/etc/init", "/bin/init", "/bin/sh"}; + int ret, fd, i; + void *mem; + + for (i = 0; i < 5; i++) { + ret = fd = open(init_files[i], O_RDONLY); + if (ret < 0) + continue; + else + break; + } + if (ret < 0) + return ret; + + mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (mem == MAP_FAILED) + return -1; + + ret = munmap(mem, size); + if (ret < 0) + return ret; + + return close(fd); +} + + /* Run syscall tests between IDs <min> and <max>. * Return 0 on success, non-zero on failure. */ @@ -666,6 +694,9 @@ int run_syscall(int min, int max) CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; + CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; + CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)-1, 0), -1, EINVAL); break; + CASE_TEST(mmap_good); EXPECT_SYSZR(1, test_mmap_munmap(4*1024)); break; CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;