From patchwork Sat Jul 15 18:34:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 120824 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp297405vqt; Sat, 15 Jul 2023 11:54:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlGX8z7YuAr+r2CFJtTipdBvROJVEMjK+HEoSkVnRvye3p6LGXexNOXA5gQyHbUFBo6Hn/Kv X-Received: by 2002:a05:6a20:7f90:b0:133:8229:196e with SMTP id d16-20020a056a207f9000b001338229196emr8706556pzj.35.1689447282541; Sat, 15 Jul 2023 11:54:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689447282; cv=none; d=google.com; s=arc-20160816; b=bkC6EQi4Wg6F9OSyq5oPszQplnCU4NbRpn23SXxxvtJEAbcJaC4OKVb/xlZT/MIMxw 06yc5D/+5vOMMyHvSo/4nqnq6aD1DxS+FtipPnUw/vLkc1UqX5IF7jT07IRB26C+DTpt aUAH2fmhueOInpAFjJkS7KOnFVLK+ojIQaJraG9+rgw2vroz3TvSEu4P+xH5cPETIdRq MViSaqS/jYDte6PtSbFDRAwVCI9Y2rftNih93LAt9uM4wfduEOAKN8o0HqeV9NrZFsmp 7rbVex6RpOctnBMMhlFMpI4TWLHYx3pSlqnDa49I6c2pQ9ninMEeymhF07Eo0QEB/1KZ sRPQ== 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=xM2wp6uISDuurLs+zAuEAJX8yThVn8wLsBiWLDF3wo4=; fh=sXngqCw1oXONrSSImRxZmGyzcK67QhJYp1RgY40tBsg=; b=KdAZTsUo9VyFNUnBgjLCxN/tp/6pYi/47R/HMPUgz+2SWb5fMRT/U7RPIp2GYi57EB DT728uE7kH40IHawsCue3kIuK28WHSi5UChyvDKTXuiFwM7vecxcudHhR0z2Dpaw5KcN pwbnzYDTc+pzxto6/WEWH8hGPsw/wlhvyUJjKNjKifg63BzhDdPkdqRqG2GnK2RNtusZ KuhXaTXgT478Dh48I5S2gS8+8seCJK3C0P2MCJm6JRUhAxG1UbS7VCM0FUbXzHyQgW0E WFAbcRG4SWIjOgX+FsRTVgcODs9UDcl+0/cpzRFeDOOpIKjlc8Y8Kib9Ik2N20fvLb6J /7bg== 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 y66-20020a626445000000b0067e34495f2dsi8871599pfb.143.2023.07.15.11.54.25; Sat, 15 Jul 2023 11:54:42 -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 S230037AbjGOSeq (ORCPT + 99 others); Sat, 15 Jul 2023 14:34:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjGOSep (ORCPT ); Sat, 15 Jul 2023 14:34:45 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0ACE271E; Sat, 15 Jul 2023 11:34:43 -0700 (PDT) X-QQ-mid: bizesmtp86t1689446074thn87dje Received: from linux-lab-host.localdomain ( [119.123.131.162]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 16 Jul 2023 02:34:33 +0800 (CST) X-QQ-SSF: 01200000002000D0W000B00A0000000 X-QQ-FEAT: wPivafIDGPNnE4pgBteeGjfO4VitGccVNTEgGVrjyg0S3lkSPswFvUfEC3oYD lR/yUIvjnDtW9NVv0uFM2R+RdQk8VwJyFfBHaiQxbmxNK7GvzScYpqPtEbdH8hf8UaZ6ocU W9OIDjPXpG7lEOK8SZdxqOMXUWqRFMvzFxOtqBN1CcbXNmz1bj3D3b6vZ5/stbHDzDaynkm mb6T48L4NKMzGDgW3F46hFNKVVZQ2NGVpPn4kM7dWVJ38RkRIEOXwQJRAiEHS3xi8FNJeYl xZaLvacE8y2isu6V+Z7ViWKjJFmvUKQmedpKBzUtE89FJGx4mJn3Ssw1KhDN3XIFJoAs0vW jhrKySOJPajkDqNyEwWGXqPNd8pl0FJRIHjdZooVyEoUkvrDuX38US3435PeLzETAE2q1hT X-QQ-GoodBg: 0 X-BIZMAIL-ID: 1900254662054636365 From: Zhangjin Wu To: w@1wt.eu Cc: arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, thomas@t-8ch.de Subject: [PATCH v4 16/18] selftests/nolibc: add testcases for startup code Date: Sun, 16 Jul 2023 02:34:32 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz5a-1 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771513873385569719 X-GMAIL-MSGID: 1771513873385569719 The startup code is critical to get the right argc, argv, envp/environ and _auxv, let's add a startup test group and the corresponding testcases. The "environ" test case is also moved from the stdlib test group to this new startup test group and it is renamed to "environ_envp". Since argv0 has been used by many other test cases, let's add testcases to gurantee it too. Signed-off-by: Zhangjin Wu --- tools/testing/selftests/nolibc/nolibc-test.c | 56 +++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 3ff706078fbd..03b1d30f5507 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -15,6 +15,7 @@ #include #ifndef _NOLIBC_STDIO_H /* standard libcs need more includes */ +#include #include #include #include @@ -47,6 +48,12 @@ /* will be used to test initialization of environ */ static char **test_envp; +/* will be used to test initialization of argv */ +static char **test_argv; + +/* will be used to test initialization of argc */ +static int test_argc; + /* will be used by some test cases as readable file, please don't write it */ static const char *argv0; @@ -561,6 +568,51 @@ static int expect_strne(const char *expr, int llen, const char *cmp) #define CASE_TEST(name) \ case __LINE__: llen += printf("%d %s", test, #name); +int run_startup(int min, int max) +{ + int test; + int ret = 0; + /* kernel at least passes HOME and TERM, shell passes more */ + int env_total = 2; + /* checking NULL for argv/argv0, environ and _auxv is not enough, let's compare with sbrk(0) or &end */ + extern char end; + char *brk = sbrk(0) != (void *)-1 ? sbrk(0) : &end; + /* differ from nolibc, both glibc and musl have no global _auxv */ + const unsigned long *test_auxv = (void *)-1; +#ifdef NOLIBC + test_auxv = _auxv; +#endif + + for (test = min; test >= 0 && test <= max; test++) { + int llen = 0; /* line length */ + + /* avoid leaving empty lines below, this will insert holes into + * test numbers. + */ + switch (test + __LINE__ + 1) { + CASE_TEST(argc); EXPECT_GE(1, test_argc, 1); break; + CASE_TEST(argv_addr); EXPECT_PTRGT(1, test_argv, brk); break; + CASE_TEST(argv_environ); EXPECT_PTRLT(1, test_argv, environ); break; + CASE_TEST(argv_total); EXPECT_EQ(1, environ - test_argv - 1, test_argc ?: 1); break; + CASE_TEST(argv0_addr); EXPECT_PTRGT(1, argv0, brk); break; + CASE_TEST(argv0_str); EXPECT_STRNZ(1, argv0 > brk ? argv0 : NULL); break; + CASE_TEST(argv0_len); EXPECT_GE(1, argv0 > brk ? strlen(argv0) : 0, 1); break; + CASE_TEST(environ_addr); EXPECT_PTRGT(1, environ, brk); break; + CASE_TEST(environ_envp); EXPECT_PTREQ(1, environ, test_envp); break; + CASE_TEST(environ_auxv); EXPECT_PTRLT(test_auxv != (void *)-1, environ, test_auxv); break; + CASE_TEST(environ_total); EXPECT_GE(test_auxv != (void *)-1, (void *)test_auxv - (void *)environ - 1, env_total); break; + CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break; + CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break; + CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break; + CASE_TEST(auxv_AT_PAGESZ); EXPECT_GE(1, getauxval(AT_PAGESZ), 4096); break; + case __LINE__: + return ret; /* must be last */ + /* note: do not set any defaults so as to permit holes above */ + } + } + return ret; +} + /* used by some syscall tests below */ int test_getdents64(const char *dir) @@ -844,7 +896,6 @@ int run_stdlib(int min, int max) * test numbers. */ switch (test + __LINE__ + 1) { - CASE_TEST(environ); EXPECT_PTREQ(1, environ, test_envp); break; CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break; CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break; CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break; @@ -1128,6 +1179,7 @@ int prepare(void) /* This is the definition of known test names, with their functions */ static const struct test test_names[] = { /* add new tests here */ + { .name = "startup", .func = run_startup }, { .name = "syscall", .func = run_syscall }, { .name = "stdlib", .func = run_stdlib }, { .name = "vfprintf", .func = run_vfprintf }, @@ -1174,6 +1226,8 @@ int main(int argc, char **argv, char **envp) char *test; argv0 = argv[0]; + test_argc = argc; + test_argv = argv; test_envp = envp; /* when called as init, it's possible that no console was opened, for