From patchwork Mon May 13 15:13:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Shuah Khan X-Patchwork-Id: 796806 Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C90A25672 for ; Mon, 13 May 2024 15:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715613231; cv=none; b=MEd5VWIF20jfQAfDuRXQaFG2Q9obFGH+Y8XMvFgGfCchemOEh8cipXCe68b1Jo6ByTta8CbmW+HPUkA+uwEMe1TCXozPnZav8QGDHTCaZ0fyS4zlaa4nOFtGDTzUmgUmj44Grnv6eUyqYzVyCPl0w4r8WMaEtegegjxfOWz61zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715613231; c=relaxed/simple; bh=bwxwLAiE81n3Z1yoB45RY2c8mTlYPK01k/Xno32pvA8=; h=Content-Type:Message-ID:Date:MIME-Version:From:Subject:To:Cc; b=bi3Ie5D3CisFveDoLXpAtCAgUB/dNoEo2gCn4L7d0nFKbB8D03X5Q9KB47cXrwJbXIoOZAuk4Oie3DHTkpIM5JYq8YCKL2ilx5QoMobo293be+2fsMKJhmn1udtjs51UYfBqtq4OgMK+b2m/j4B84goPJxDp7a1CjUhPnTeyopA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linuxfoundation.org; spf=pass smtp.mailfrom=linuxfoundation.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Ye0zy53A; arc=none smtp.client-ip=209.85.166.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linuxfoundation.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linuxfoundation.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="Ye0zy53A" Received: by mail-io1-f42.google.com with SMTP id ca18e2360f4ac-7e1d952ace4so8102839f.1 for ; Mon, 13 May 2024 08:13:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; t=1715613229; x=1716218029; darn=vger.kernel.org; h=cc:to:subject:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=q2Y34rp11DhTo88f7ZZG+euMgHjyfBULc8fRUELw0yg=; b=Ye0zy53AexST0vE5fzyAuEvsIQ6EShzTVLz4mAK8Da7Fky+T+byEH5JsSD+PwqqVp/ tnQSM/V4U6SytPIUx9eJ1d3gxHUu9pLEb1gV/z9hdfcppnhy9GREMOrN9bVpIYLmS0tG P/L+1DsIzolTSCBcERJajzX346teBVMl1iIyo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715613229; x=1716218029; h=cc:to:subject:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q2Y34rp11DhTo88f7ZZG+euMgHjyfBULc8fRUELw0yg=; b=IvgTT91XuToULjlgdvjeK6muxNdqICzRmGb2zWNNFt5VpygDi6PLDGxrWHix8AF+68 mySVFdsD5XroOHCYq38M9Ol4bwRa/QPwZ8n7bm4NIZQseNnrid+RkRz7zdnwQ12AWWf4 xD4tB1DopJP312iQQ+ob+XMYZo3Lxa7TzYIWmeOBNkgSNPYjhdkEzs6ARPlUbBGee8Nn h9V3gDEvOVNKxq6MwbR3gUNFInFwQ2BiTTRmam4L/l0cY8vao732JVv4dhFbAzM3c9cV +qDoxAbbr69QRFTjxvjzlqB4+mSMjMuvxxP1HB7yWEM596hibK/J6zKU1XK9xpCCxP62 26QQ== X-Forwarded-Encrypted: i=1; AJvYcCXmWxs1QFxCGk3tztrIcj5QWGhM22aT5/xRxJqxS8/ST2Ng/PeH0P0bywA9Wb6rANIeG7YscLeFh5dvQYB1AZGiN/qFpcqZV+b+QulcvO0g X-Gm-Message-State: AOJu0YzP81iO5Ca+x+uQ5O8gVRiFhrfLnRc0211WYjzWIBqQrvV1R0s3 2QlW+6InXloeunv8pKcZ49O3DwMIb3OSxLRtc3aOrjjCN55Y98oFgpRBZ6sHNkY= X-Google-Smtp-Source: AGHT+IGnry8IAGVge2SIgEV/sy1Si+t9AN9FCzHpGStigudBNYjWS+ogphd25zvvQ3M/+YqO1rpmAw== X-Received: by 2002:a6b:d203:0:b0:7e1:8bc8:8228 with SMTP id ca18e2360f4ac-7e1b501f23dmr1055809739f.0.1715613228793; Mon, 13 May 2024 08:13:48 -0700 (PDT) Received: from [192.168.1.128] ([38.175.170.29]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-489375c0b23sm2508947173.96.2024.05.13.08.13.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 May 2024 08:13:48 -0700 (PDT) Message-ID: Date: Mon, 13 May 2024 09:13:47 -0600 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Shuah Khan Subject: [GIT PULL] nolibc changes for Linux 6.10-rc1 To: Linus Torvalds Cc: Willy Tarreau , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , "Paul E. McKenney" , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Shuah Khan , shuah , "open list:KERNEL SELFTEST FRAMEWORK" , Linux Kernel Mailing List Hi Linus, Please pull the nolibc update for Linux 6.10-rc1. This nolibc update for Linux 6.10-rc1 - adds support for uname(2) - removes open-coded strnlen() - exports strlen() - adds tests for strlcat() and strlcpy() - fixes memory error in realloc() - fixes strlcat() return code and size usage - fixes strlcpy() return code and size usage diff is attached. thanks, -- Shuah ---------------------------------------------------------------- The following changes since commit 4cece764965020c22cff7665b18a012006359095: Linux 6.9-rc1 (2024-03-24 14:10:05 -0700) are available in the Git repository at: git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest tags/linux_kselftest-nolibc-6.10-rc1 for you to fetch changes up to 0adab2b6b7336fb6ee3c6456a432dad3b1d25647: tools/nolibc: add support for uname(2) (2024-04-14 20:28:54 +0200) ---------------------------------------------------------------- linux_kselftest-nolibc-6.10-rc1 This nolibc update for Linux 6.10-rc1 - adds support for uname(2) - removes open-coded strnlen() - exports strlen() - adds tests for strlcat() and strlcpy() - fixes memory error in realloc() - fixes strlcat() return code and size usage - fixes strlcpy() return code and size usage ---------------------------------------------------------------- Brennan Xavier McManus (1): tools/nolibc/stdlib: fix memory error in realloc() Rodrigo Campos (4): tools/nolibc/string: export strlen() tools/nolibc: Fix strlcat() return code and size usage tools/nolibc: Fix strlcpy() return code and size usage selftests/nolibc: Add tests for strlcat() and strlcpy() Thomas Weißschuh (2): tools/nolibc/string: remove open-coded strnlen() tools/nolibc: add support for uname(2) tools/include/nolibc/stdlib.h | 2 +- tools/include/nolibc/string.h | 46 +++++++++------- tools/include/nolibc/sys.h | 27 +++++++++ tools/testing/selftests/nolibc/nolibc-test.c | 82 ++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 21 deletions(-) ---------------------------------------------------------------- diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index bacfd35c5156..5be9d3c7435a 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -185,7 +185,7 @@ void *realloc(void *old_ptr, size_t new_size) if (__builtin_expect(!ret, 0)) return NULL; - memcpy(ret, heap->user_p, heap->len); + memcpy(ret, heap->user_p, user_p_len); munmap(heap, heap->len); return ret; } diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index a01c69dd495f..f9ab28421e6d 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -123,7 +123,7 @@ char *strcpy(char *dst, const char *src) * thus itself, hence the asm() statement below that's meant to disable this * confusing practice. */ -static __attribute__((unused)) +__attribute__((weak,unused,section(".text.nolibc_strlen"))) size_t strlen(const char *str) { size_t len; @@ -187,22 +187,26 @@ char *strndup(const char *str, size_t maxlen) static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { - size_t len; - char c; - - for (len = 0; dst[len]; len++) - ; - - for (;;) { - c = *src; - if (len < size) - dst[len] = c; - if (!c) + size_t len = strnlen(dst, size); + + /* + * We want len < size-1. But as size is unsigned and can wrap + * around, we use len + 1 instead. + */ + while (len + 1 < size) { + dst[len] = *src; + if (*src == '\0') break; len++; src++; } + if (len < size) + dst[len] = '\0'; + + while (*src++) + len++; + return len; } @@ -210,16 +214,18 @@ static __attribute__((unused)) size_t strlcpy(char *dst, const char *src, size_t size) { size_t len; - char c; - for (len = 0;;) { - c = src[len]; - if (len < size) - dst[len] = c; - if (!c) - break; - len++; + for (len = 0; len < size; len++) { + dst[len] = src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] = '\0'; + + while (src[len]) + len++; + return len; } diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index dda9dffd1d74..7b82bc3cf107 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -22,6 +22,7 @@ #include /* for statx() */ #include #include +#include #include "arch.h" #include "errno.h" @@ -1139,6 +1140,32 @@ int umount2(const char *path, int flags) } +/* + * int uname(struct utsname *buf); + */ + +struct utsname { + char sysname[65]; + char nodename[65]; + char release[65]; + char version[65]; + char machine[65]; + char domainname[65]; +}; + +static __attribute__((unused)) +int sys_uname(struct utsname *buf) +{ + return my_syscall1(__NR_uname, buf); +} + +static __attribute__((unused)) +int uname(struct utsname *buf) +{ + return __sysret(sys_uname(buf)); +} + + /* * int unlink(const char *path); */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 6ba4f8275ac4..94bb6e11c16f 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -600,6 +601,25 @@ int expect_strne(const char *expr, int llen, const char *cmp) return ret; } +#define EXPECT_STRBUFEQ(cond, expr, buf, val, cmp) \ + do { if (!(cond)) result(llen, SKIPPED); else ret += expect_str_buf_eq(expr, buf, val, llen, cmp); } while (0) + +static __attribute__((unused)) +int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, const char *cmp) +{ + llen += printf(" = %lu <%s> ", expr, buf); + if (strcmp(buf, cmp) != 0) { + result(llen, FAIL); + return 1; + } + if (expr != val) { + result(llen, FAIL); + return 1; + } + + result(llen, OK); + return 0; +} /* declare tests based on line numbers. There must be exactly one test per line. */ #define CASE_TEST(name) \ @@ -761,6 +781,45 @@ int test_stat_timestamps(void) return 0; } +int test_uname(void) +{ + struct utsname buf; + char osrelease[sizeof(buf.release)]; + ssize_t r; + int fd; + + memset(&buf.domainname, 'P', sizeof(buf.domainname)); + + if (uname(&buf)) + return 1; + + if (strncmp("Linux", buf.sysname, sizeof(buf.sysname))) + return 1; + + fd = open("/proc/sys/kernel/osrelease", O_RDONLY); + if (fd == -1) + return 1; + + r = read(fd, osrelease, sizeof(osrelease)); + if (r == -1) + return 1; + + close(fd); + + if (osrelease[r - 1] == '\n') + r--; + + /* Validate one of the later fields to ensure field sizes are correct */ + if (strncmp(osrelease, buf.release, r)) + return 1; + + /* Ensure the field domainname is set, it is missing from struct old_utsname */ + if (strnlen(buf.domainname, sizeof(buf.domainname)) == sizeof(buf.domainname)) + return 1; + + return 0; +} + int test_mmap_munmap(void) { int ret, fd, i, page_size; @@ -966,6 +1025,8 @@ int run_syscall(int min, int max) CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; + CASE_TEST(uname); EXPECT_SYSZR(proc, test_uname()); break; + CASE_TEST(uname_fault); EXPECT_SYSER(1, uname(NULL), -1, EFAULT); break; CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break; CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break; CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break; @@ -991,6 +1052,14 @@ int run_stdlib(int min, int max) for (test = min; test >= 0 && test <= max; test++) { int llen = 0; /* line length */ + /* For functions that take a long buffer, like strlcat() + * Add some more chars after the \0, to test functions that overwrite the buffer set + * the \0 at the exact right position. + */ + char buf[10] = "test123456"; + buf[4] = '\0'; + + /* avoid leaving empty lines below, this will insert holes into * test numbers. */ @@ -1007,6 +1076,19 @@ int run_stdlib(int min, int max) CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break; CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break; CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break; +#ifdef NOLIBC + CASE_TEST(strlcat_0); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcat_1); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 1), buf, 4, "test"); break; + CASE_TEST(strlcat_5); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 5), buf, 7, "test"); break; + CASE_TEST(strlcat_6); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 6), buf, 7, "testb"); break; + CASE_TEST(strlcat_7); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 7), buf, 7, "testba"); break; + CASE_TEST(strlcat_8); EXPECT_STRBUFEQ(1, strlcat(buf, "bar", 8), buf, 7, "testbar"); break; + CASE_TEST(strlcpy_0); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 0), buf, 3, "test"); break; + CASE_TEST(strlcpy_1); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 1), buf, 3, ""); break; + CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 2), buf, 3, "b"); break; + CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 3), buf, 3, "ba"); break; + CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(1, strlcpy(buf, "bar", 4), buf, 3, "bar"); break; +#endif CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break; CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break; CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;