Commit f97b956b authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'nolibc.2024.07.15a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu

Pull nolibc updates from Paul McKenney:

 - Fix selftest printf format mismatch in expect_str_buf_eq()

 - Stop using brk() and sbrk() when testing against musl, which
   implements these two functions with ENOMEM

 - Make tests use -Werror to force failure on compiler warnings

 - Add limits for the {u,}intmax_t, ulong and {u,}llong types

 - Implement strtol() and friends

 - Add facility to skip nolibc-specific tests when running against
   non-nolibc libraries

 - Implement strerror()

 - Also use strerror() on nolibc when running kselftests

* tag 'nolibc.2024.07.15a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu:
  selftests: kselftest: also use strerror() on nolibc
  tools/nolibc: implement strerror()
  selftests/nolibc: introduce condition to run tests only on nolibc
  tools/nolibc: implement strtol() and friends
  tools/nolibc: add limits for {u,}intmax_t, ulong and {u,}llong
  selftests/nolibc: run-tests.sh: use -Werror by default
  selftests/nolibc: disable brk()/sbrk() tests on musl
  selftests/nolibc: fix printf format mismatch in expect_str_buf_eq()
parents e4b2b0b1 6ca8f2e2
......@@ -96,6 +96,10 @@ typedef uint64_t uintmax_t;
#define UINT_FAST32_MAX SIZE_MAX
#define UINT_FAST64_MAX UINT64_MAX
#define INTMAX_MIN INT64_MIN
#define INTMAX_MAX INT64_MAX
#define UINTMAX_MAX UINT64_MAX
#ifndef INT_MIN
#define INT_MIN (-__INT_MAX__ - 1)
#endif
......@@ -110,4 +114,19 @@ typedef uint64_t uintmax_t;
#define LONG_MAX __LONG_MAX__
#endif
#ifndef ULONG_MAX
#define ULONG_MAX ((unsigned long)(__LONG_MAX__) * 2 + 1)
#endif
#ifndef LLONG_MIN
#define LLONG_MIN (-__LONG_LONG_MAX__ - 1)
#endif
#ifndef LLONG_MAX
#define LLONG_MAX __LONG_LONG_MAX__
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX ((unsigned long long)(__LONG_LONG_MAX__) * 2 + 1)
#endif
#endif /* _NOLIBC_STDINT_H */
......@@ -376,6 +376,16 @@ int setvbuf(FILE *stream __attribute__((unused)),
return 0;
}
static __attribute__((unused))
const char *strerror(int errno)
{
static char buf[18] = "errno=";
i64toa_r(errno, &buf[6]);
return buf;
}
/* make sure to include all global symbols */
#include "nolibc.h"
......
......@@ -438,6 +438,115 @@ char *u64toa(uint64_t in)
return itoa_buffer;
}
static __attribute__((unused))
uintmax_t __strtox(const char *nptr, char **endptr, int base, intmax_t lower_limit, uintmax_t upper_limit)
{
const char signed_ = lower_limit != 0;
unsigned char neg = 0, overflow = 0;
uintmax_t val = 0, limit, old_val;
char c;
if (base < 0 || base > 36) {
SET_ERRNO(EINVAL);
goto out;
}
while (isspace(*nptr))
nptr++;
if (*nptr == '+') {
nptr++;
} else if (*nptr == '-') {
neg = 1;
nptr++;
}
if (signed_ && neg)
limit = -(uintmax_t)lower_limit;
else
limit = upper_limit;
if ((base == 0 || base == 16) &&
(strncmp(nptr, "0x", 2) == 0 || strncmp(nptr, "0X", 2) == 0)) {
base = 16;
nptr += 2;
} else if (base == 0 && strncmp(nptr, "0", 1) == 0) {
base = 8;
nptr += 1;
} else if (base == 0) {
base = 10;
}
while (*nptr) {
c = *nptr;
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'a' && c <= 'z')
c = c - 'a' + 10;
else if (c >= 'A' && c <= 'Z')
c = c - 'A' + 10;
else
goto out;
if (c >= base)
goto out;
nptr++;
old_val = val;
val *= base;
val += c;
if (val > limit || val < old_val)
overflow = 1;
}
out:
if (overflow) {
SET_ERRNO(ERANGE);
val = limit;
}
if (endptr)
*endptr = (char *)nptr;
return neg ? -val : val;
}
static __attribute__((unused))
long strtol(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, LONG_MIN, LONG_MAX);
}
static __attribute__((unused))
unsigned long strtoul(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, 0, ULONG_MAX);
}
static __attribute__((unused))
long long strtoll(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, LLONG_MIN, LLONG_MAX);
}
static __attribute__((unused))
unsigned long long strtoull(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, 0, ULLONG_MAX);
}
static __attribute__((unused))
intmax_t strtoimax(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, INTMAX_MIN, INTMAX_MAX);
}
static __attribute__((unused))
uintmax_t strtoumax(const char *nptr, char **endptr, int base)
{
return __strtox(nptr, endptr, base, 0, UINTMAX_MAX);
}
/* make sure to include all global symbols */
#include "nolibc.h"
......
......@@ -168,15 +168,7 @@ static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
static inline void ksft_perror(const char *msg)
{
#ifndef NOLIBC
ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
#else
/*
* nolibc doesn't provide strerror() and it seems
* inappropriate to add one, just print the errno.
*/
ksft_print_msg("%s: %d)\n", msg, errno);
#endif
}
static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
......
......@@ -152,7 +152,7 @@ CFLAGS_mips32be = -EB -mabi=32
CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))
CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \
$(call cc-option,-fno-stack-protector) \
$(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR)
$(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) $(CFLAGS_EXTRA)
LDFLAGS :=
REPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
......
......@@ -64,6 +64,14 @@ static const char *argv0;
/* will be used by constructor tests */
static int constructor_test_value;
static const int is_nolibc =
#ifdef NOLIBC
1
#else
0
#endif
;
/* definition of a series of tests */
struct test {
const char *name; /* test name */
......@@ -607,7 +615,7 @@ int expect_strne(const char *expr, int llen, const char *cmp)
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);
llen += printf(" = %lu <%s> ", (unsigned long)expr, buf);
if (strcmp(buf, cmp) != 0) {
result(llen, FAIL);
return 1;
......@@ -621,6 +629,51 @@ int expect_str_buf_eq(size_t expr, const char *buf, size_t val, int llen, const
return 0;
}
#define EXPECT_STRTOX(cond, func, input, base, expected, chars, expected_errno) \
do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strtox(llen, func, input, base, expected, chars, expected_errno); } while (0)
static __attribute__((unused))
int expect_strtox(int llen, void *func, const char *input, int base, intmax_t expected, int expected_chars, int expected_errno)
{
char *endptr;
int actual_errno, actual_chars;
intmax_t r;
errno = 0;
if (func == strtol) {
r = strtol(input, &endptr, base);
} else if (func == strtoul) {
r = strtoul(input, &endptr, base);
} else {
result(llen, FAIL);
return 1;
}
actual_errno = errno;
actual_chars = endptr - input;
llen += printf(" %lld = %lld", (long long)expected, (long long)r);
if (r != expected) {
result(llen, FAIL);
return 1;
}
if (expected_chars == -1) {
if (*endptr != '\0') {
result(llen, FAIL);
return 1;
}
} else if (expected_chars != actual_chars) {
result(llen, FAIL);
return 1;
}
if (actual_errno != expected_errno) {
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) \
case __LINE__: llen += printf("%d %s", test, #name);
......@@ -942,6 +995,7 @@ int run_syscall(int min, int max)
int ret = 0;
void *p1, *p2;
int has_gettid = 1;
int has_brk;
/* <proc> indicates whether or not /proc is mounted */
proc = stat("/proc", &stat_buf) == 0;
......@@ -954,6 +1008,9 @@ int run_syscall(int min, int max)
has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
#endif
/* on musl setting brk()/sbrk() always fails */
has_brk = brk(0) == 0;
for (test = min; test >= 0 && test <= max; test++) {
int llen = 0; /* line length */
......@@ -969,9 +1026,9 @@ int run_syscall(int min, int max)
CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break;
CASE_TEST(sbrk_0); EXPECT_PTRNE(has_brk, sbrk(0), (void *)-1); break;
CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(has_brk, (p2 == (void *)-1) || p2 == p1); break;
CASE_TEST(brk); EXPECT_SYSZR(has_brk, brk(sbrk(0))); break;
CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break;
CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break;
CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
......@@ -1076,19 +1133,17 @@ 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(strlcat_0); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 0), buf, 3, "test"); break;
CASE_TEST(strlcat_1); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 1), buf, 4, "test"); break;
CASE_TEST(strlcat_5); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 5), buf, 7, "test"); break;
CASE_TEST(strlcat_6); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 6), buf, 7, "testb"); break;
CASE_TEST(strlcat_7); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 7), buf, 7, "testba"); break;
CASE_TEST(strlcat_8); EXPECT_STRBUFEQ(is_nolibc, strlcat(buf, "bar", 8), buf, 7, "testbar"); break;
CASE_TEST(strlcpy_0); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 0), buf, 3, "test"); break;
CASE_TEST(strlcpy_1); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 1), buf, 3, ""); break;
CASE_TEST(strlcpy_2); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 2), buf, 3, "b"); break;
CASE_TEST(strlcpy_3); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 3), buf, 3, "ba"); break;
CASE_TEST(strlcpy_4); EXPECT_STRBUFEQ(is_nolibc, strlcpy(buf, "bar", 4), buf, 3, "bar"); break;
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;
......@@ -1139,6 +1194,26 @@ int run_stdlib(int min, int max)
CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, sizeof(long) == 8 ? (ptrdiff_t) 0x8000000000000000LL : (ptrdiff_t) 0x80000000); break;
CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, sizeof(long) == 8 ? (ptrdiff_t) 0x7fffffffffffffffLL : (ptrdiff_t) 0x7fffffff); break;
CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, sizeof(long) == 8 ? (size_t) 0xffffffffffffffffULL : (size_t) 0xffffffffU); break;
CASE_TEST(strtol_simple); EXPECT_STRTOX(1, strtol, "35", 10, 35, -1, 0); break;
CASE_TEST(strtol_positive); EXPECT_STRTOX(1, strtol, "+35", 10, 35, -1, 0); break;
CASE_TEST(strtol_negative); EXPECT_STRTOX(1, strtol, "-35", 10, -35, -1, 0); break;
CASE_TEST(strtol_hex_auto); EXPECT_STRTOX(1, strtol, "0xFF", 0, 255, -1, 0); break;
CASE_TEST(strtol_base36); EXPECT_STRTOX(1, strtol, "12yZ", 36, 50507, -1, 0); break;
CASE_TEST(strtol_cutoff); EXPECT_STRTOX(1, strtol, "1234567890", 8, 342391, 7, 0); break;
CASE_TEST(strtol_octal_auto); EXPECT_STRTOX(1, strtol, "011", 0, 9, -1, 0); break;
CASE_TEST(strtol_hex_00); EXPECT_STRTOX(1, strtol, "0x00", 16, 0, -1, 0); break;
CASE_TEST(strtol_hex_FF); EXPECT_STRTOX(1, strtol, "FF", 16, 255, -1, 0); break;
CASE_TEST(strtol_hex_ff); EXPECT_STRTOX(1, strtol, "ff", 16, 255, -1, 0); break;
CASE_TEST(strtol_hex_prefix); EXPECT_STRTOX(1, strtol, "0xFF", 16, 255, -1, 0); break;
CASE_TEST(strtol_trailer); EXPECT_STRTOX(1, strtol, "35foo", 10, 35, 2, 0); break;
CASE_TEST(strtol_overflow); EXPECT_STRTOX(1, strtol, "0x8000000000000000", 16, LONG_MAX, -1, ERANGE); break;
CASE_TEST(strtol_underflow); EXPECT_STRTOX(1, strtol, "-0x8000000000000001", 16, LONG_MIN, -1, ERANGE); break;
CASE_TEST(strtoul_negative); EXPECT_STRTOX(1, strtoul, "-0x1", 16, ULONG_MAX, 4, 0); break;
CASE_TEST(strtoul_overflow); EXPECT_STRTOX(1, strtoul, "0x10000000000000000", 16, ULONG_MAX, -1, ERANGE); break;
CASE_TEST(strerror_success); EXPECT_STREQ(is_nolibc, strerror(0), "errno=0"); break;
CASE_TEST(strerror_EINVAL); EXPECT_STREQ(is_nolibc, strerror(EINVAL), "errno=22"); break;
CASE_TEST(strerror_int_max); EXPECT_STREQ(is_nolibc, strerror(INT_MAX), "errno=2147483647"); break;
CASE_TEST(strerror_int_min); EXPECT_STREQ(is_nolibc, strerror(INT_MIN), "errno=-2147483648"); break;
case __LINE__:
return ret; /* must be last */
......
......@@ -15,9 +15,10 @@ download_location="${cache_dir}/crosstools/"
build_location="$(realpath "${cache_dir}"/nolibc-tests/)"
perform_download=0
test_mode=system
CFLAGS_EXTRA="-Werror"
archs="i386 x86_64 arm64 arm mips32le mips32be ppc ppc64 ppc64le riscv s390 loongarch"
TEMP=$(getopt -o 'j:d:c:b:a:m:ph' -n "$0" -- "$@")
TEMP=$(getopt -o 'j:d:c:b:a:m:peh' -n "$0" -- "$@")
eval set -- "$TEMP"
unset TEMP
......@@ -40,6 +41,7 @@ Options:
-a [ARCH] Host architecture of toolchains to use (default: ${hostarch})
-b [DIR] Build location (default: ${build_location})
-m [MODE] Test mode user/system (default: ${test_mode})
-e Disable -Werror
EOF
}
......@@ -66,6 +68,9 @@ while true; do
'-m')
test_mode="$2"
shift 2; continue ;;
'-e')
CFLAGS_EXTRA=""
shift; continue ;;
'-h')
print_usage
exit 0
......@@ -153,7 +158,7 @@ test_arch() {
exit 1
esac
printf '%-15s' "$arch:"
swallow_output "${MAKE[@]}" "$test_target" V=1
swallow_output "${MAKE[@]}" CFLAGS_EXTRA="$CFLAGS_EXTRA" "$test_target" V=1
cp run.out run.out."${arch}"
"${MAKE[@]}" report | grep passed
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment