From: Alex Bennée Date: Fri, 17 Sep 2021 16:23:24 +0000 (+0100) Subject: tests/tcg: move some multiarch files and make conditional X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=5343a837cdb0e10db05310e0da5a89843539b400;p=qemu.git tests/tcg: move some multiarch files and make conditional We had some messy code to filter out stuff we can't build. Lets junk that and simplify the logic by pushing some stuff into subdirs. In particular we move: float_helpers into libs - not a standalone test linux-test into linux - so we only build on Linux hosts This allows for at least some of the tests to be nominally usable by *BSD user builds. Signed-off-by: Alex Bennée Cc: Warner Losh Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Warner Losh Message-Id: <20210917162332.3511179-4-alex.bennee@linaro.org> --- diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target index 3f283eabe6..6ccb592aac 100644 --- a/tests/tcg/multiarch/Makefile.target +++ b/tests/tcg/multiarch/Makefile.target @@ -8,18 +8,23 @@ MULTIARCH_SRC=$(SRC_PATH)/tests/tcg/multiarch # Set search path for all sources -VPATH += $(MULTIARCH_SRC) -MULTIARCH_SRCS =$(notdir $(wildcard $(MULTIARCH_SRC)/*.c)) -MULTIARCH_TESTS =$(filter-out float_helpers, $(MULTIARCH_SRCS:.c=)) +VPATH += $(MULTIARCH_SRC) +MULTIARCH_SRCS = $(notdir $(wildcard $(MULTIARCH_SRC)/*.c)) +ifneq ($(CONFIG_LINUX),) +VPATH += $(MULTIARCH_SRC)/linux +MULTIARCH_SRCS += $(notdir $(wildcard $(MULTIARCH_SRC)/linux/*.c)) +endif +MULTIARCH_TESTS = $(MULTIARCH_SRCS:.c=) +$(info SRCS=${MULTIARCH_SRCS} and ${MULTIARCH_TESTS}) # # The following are any additional rules needed to build things # float_%: LDFLAGS+=-lm -float_%: float_%.c float_helpers.c - $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< $(MULTIARCH_SRC)/float_helpers.c -o $@ $(LDFLAGS) +float_%: float_%.c libs/float_helpers.c + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< $(MULTIARCH_SRC)/libs/float_helpers.c -o $@ $(LDFLAGS) run-float_%: float_% $(call run-test,$<, $(QEMU) $(QEMU_OPTS) $<,"$< on $(TARGET_NAME)") diff --git a/tests/tcg/multiarch/float_helpers.c b/tests/tcg/multiarch/float_helpers.c deleted file mode 100644 index bc530e5732..0000000000 --- a/tests/tcg/multiarch/float_helpers.c +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Common Float Helpers - * - * This contains a series of useful utility routines and a set of - * floating point constants useful for exercising the edge cases in - * floating point tests. - * - * Copyright (c) 2019 Linaro - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -/* we want additional float type definitions */ -#define __STDC_WANT_IEC_60559_BFP_EXT__ -#define __STDC_WANT_IEC_60559_TYPES_EXT__ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include - -#include "float_helpers.h" - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - -/* - * Half Precision Numbers - * - * Not yet well standardised so we return a plain uint16_t for now. - */ - -/* no handy defines for these numbers */ -static uint16_t f16_numbers[] = { - 0xffff, /* -NaN / AHP -Max */ - 0xfcff, /* -NaN / AHP */ - 0xfc01, /* -NaN / AHP */ - 0xfc00, /* -Inf */ - 0xfbff, /* -Max */ - 0xc000, /* -2 */ - 0xbc00, /* -1 */ - 0x8001, /* -MIN subnormal */ - 0x8000, /* -0 */ - 0x0000, /* +0 */ - 0x0001, /* MIN subnormal */ - 0x3c00, /* 1 */ - 0x7bff, /* Max */ - 0x7c00, /* Inf */ - 0x7c01, /* NaN / AHP */ - 0x7cff, /* NaN / AHP */ - 0x7fff, /* NaN / AHP +Max*/ -}; - -static const int num_f16 = ARRAY_SIZE(f16_numbers); - -int get_num_f16(void) -{ - return num_f16; -} - -uint16_t get_f16(int i) -{ - return f16_numbers[i % num_f16]; -} - -/* only display as hex */ -char *fmt_16(uint16_t num) -{ - char *fmt; - asprintf(&fmt, "f16(%#04x)", num); - return fmt; -} - -/* - * Single Precision Numbers - */ - -#ifndef SNANF -/* Signaling NaN macros, if supported. */ -# define SNANF (__builtin_nansf ("")) -# define SNAN (__builtin_nans ("")) -# define SNANL (__builtin_nansl ("")) -#endif - -static float f32_numbers[] = { - -SNANF, - -NAN, - -INFINITY, - -FLT_MAX, - -0x1.1874b2p+103, - -0x1.c0bab6p+99, - -0x1.31f75p-40, - -0x1.505444p-66, - -FLT_MIN, - 0.0, - FLT_MIN, - 0x1p-25, - 0x1.ffffe6p-25, /* min positive FP16 subnormal */ - 0x1.ff801ap-15, /* max subnormal FP16 */ - 0x1.00000cp-14, /* min positive normal FP16 */ - 1.0, - 0x1.004p+0, /* smallest float after 1.0 FP16 */ - 2.0, - M_E, M_PI, - 0x1.ffbep+15, - 0x1.ffcp+15, /* max FP16 */ - 0x1.ffc2p+15, - 0x1.ffbfp+16, - 0x1.ffcp+16, /* max AFP */ - 0x1.ffc1p+16, - 0x1.c0bab6p+99, - FLT_MAX, - INFINITY, - NAN, - SNANF -}; - -static const int num_f32 = ARRAY_SIZE(f32_numbers); - -int get_num_f32(void) -{ - return num_f32; -} - -float get_f32(int i) -{ - return f32_numbers[i % num_f32]; -} - -char *fmt_f32(float num) -{ - uint32_t single_as_hex = *(uint32_t *) # - char *fmt; - asprintf(&fmt, "f32(%02.20a:%#010x)", num, single_as_hex); - return fmt; -} - - -/* This allows us to initialise some doubles as pure hex */ -typedef union { - double d; - uint64_t h; -} test_doubles; - -static test_doubles f64_numbers[] = { - {SNAN}, - {-NAN}, - {-INFINITY}, - {-DBL_MAX}, - {-FLT_MAX-1.0}, - {-FLT_MAX}, - {-1.111E+31}, - {-1.111E+30}, /* half prec */ - {-2.0}, {-1.0}, - {-DBL_MIN}, - {-FLT_MIN}, - {0.0}, - {FLT_MIN}, - {2.98023224e-08}, - {5.96046E-8}, /* min positive FP16 subnormal */ - {6.09756E-5}, /* max subnormal FP16 */ - {6.10352E-5}, /* min positive normal FP16 */ - {1.0}, - {1.0009765625}, /* smallest float after 1.0 FP16 */ - {DBL_MIN}, - {1.3789972848607228e-308}, - {1.4914738736681624e-308}, - {1.0}, {2.0}, - {M_E}, {M_PI}, - {65503.0}, - {65504.0}, /* max FP16 */ - {65505.0}, - {131007.0}, - {131008.0}, /* max AFP */ - {131009.0}, - {.h = 0x41dfffffffc00000 }, /* to int = 0x7fffffff */ - {FLT_MAX}, - {FLT_MAX + 1.0}, - {DBL_MAX}, - {INFINITY}, - {NAN}, - {.h = 0x7ff0000000000001}, /* SNAN */ - {SNAN}, -}; - -static const int num_f64 = ARRAY_SIZE(f64_numbers); - -int get_num_f64(void) -{ - return num_f64; -} - -double get_f64(int i) -{ - return f64_numbers[i % num_f64].d; -} - -char *fmt_f64(double num) -{ - uint64_t double_as_hex = *(uint64_t *) # - char *fmt; - asprintf(&fmt, "f64(%02.20a:%#020" PRIx64 ")", num, double_as_hex); - return fmt; -} - -/* - * Float flags - */ -char *fmt_flags(void) -{ - int flags = fetestexcept(FE_ALL_EXCEPT); - char *fmt; - - if (flags) { - asprintf(&fmt, "%s%s%s%s%s", - flags & FE_OVERFLOW ? "OVERFLOW " : "", - flags & FE_UNDERFLOW ? "UNDERFLOW " : "", - flags & FE_DIVBYZERO ? "DIV0 " : "", - flags & FE_INEXACT ? "INEXACT " : "", - flags & FE_INVALID ? "INVALID" : ""); - } else { - asprintf(&fmt, "OK"); - } - - return fmt; -} diff --git a/tests/tcg/multiarch/libs/float_helpers.c b/tests/tcg/multiarch/libs/float_helpers.c new file mode 100644 index 0000000000..4e68d2b659 --- /dev/null +++ b/tests/tcg/multiarch/libs/float_helpers.c @@ -0,0 +1,228 @@ +/* + * Common Float Helpers + * + * This contains a series of useful utility routines and a set of + * floating point constants useful for exercising the edge cases in + * floating point tests. + * + * Copyright (c) 2019 Linaro + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +/* we want additional float type definitions */ +#define __STDC_WANT_IEC_60559_BFP_EXT__ +#define __STDC_WANT_IEC_60559_TYPES_EXT__ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "../float_helpers.h" + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +/* + * Half Precision Numbers + * + * Not yet well standardised so we return a plain uint16_t for now. + */ + +/* no handy defines for these numbers */ +static uint16_t f16_numbers[] = { + 0xffff, /* -NaN / AHP -Max */ + 0xfcff, /* -NaN / AHP */ + 0xfc01, /* -NaN / AHP */ + 0xfc00, /* -Inf */ + 0xfbff, /* -Max */ + 0xc000, /* -2 */ + 0xbc00, /* -1 */ + 0x8001, /* -MIN subnormal */ + 0x8000, /* -0 */ + 0x0000, /* +0 */ + 0x0001, /* MIN subnormal */ + 0x3c00, /* 1 */ + 0x7bff, /* Max */ + 0x7c00, /* Inf */ + 0x7c01, /* NaN / AHP */ + 0x7cff, /* NaN / AHP */ + 0x7fff, /* NaN / AHP +Max*/ +}; + +static const int num_f16 = ARRAY_SIZE(f16_numbers); + +int get_num_f16(void) +{ + return num_f16; +} + +uint16_t get_f16(int i) +{ + return f16_numbers[i % num_f16]; +} + +/* only display as hex */ +char *fmt_16(uint16_t num) +{ + char *fmt; + asprintf(&fmt, "f16(%#04x)", num); + return fmt; +} + +/* + * Single Precision Numbers + */ + +#ifndef SNANF +/* Signaling NaN macros, if supported. */ +# define SNANF (__builtin_nansf ("")) +# define SNAN (__builtin_nans ("")) +# define SNANL (__builtin_nansl ("")) +#endif + +static float f32_numbers[] = { + -SNANF, + -NAN, + -INFINITY, + -FLT_MAX, + -0x1.1874b2p+103, + -0x1.c0bab6p+99, + -0x1.31f75p-40, + -0x1.505444p-66, + -FLT_MIN, + 0.0, + FLT_MIN, + 0x1p-25, + 0x1.ffffe6p-25, /* min positive FP16 subnormal */ + 0x1.ff801ap-15, /* max subnormal FP16 */ + 0x1.00000cp-14, /* min positive normal FP16 */ + 1.0, + 0x1.004p+0, /* smallest float after 1.0 FP16 */ + 2.0, + M_E, M_PI, + 0x1.ffbep+15, + 0x1.ffcp+15, /* max FP16 */ + 0x1.ffc2p+15, + 0x1.ffbfp+16, + 0x1.ffcp+16, /* max AFP */ + 0x1.ffc1p+16, + 0x1.c0bab6p+99, + FLT_MAX, + INFINITY, + NAN, + SNANF +}; + +static const int num_f32 = ARRAY_SIZE(f32_numbers); + +int get_num_f32(void) +{ + return num_f32; +} + +float get_f32(int i) +{ + return f32_numbers[i % num_f32]; +} + +char *fmt_f32(float num) +{ + uint32_t single_as_hex = *(uint32_t *) # + char *fmt; + asprintf(&fmt, "f32(%02.20a:%#010x)", num, single_as_hex); + return fmt; +} + + +/* This allows us to initialise some doubles as pure hex */ +typedef union { + double d; + uint64_t h; +} test_doubles; + +static test_doubles f64_numbers[] = { + {SNAN}, + {-NAN}, + {-INFINITY}, + {-DBL_MAX}, + {-FLT_MAX-1.0}, + {-FLT_MAX}, + {-1.111E+31}, + {-1.111E+30}, /* half prec */ + {-2.0}, {-1.0}, + {-DBL_MIN}, + {-FLT_MIN}, + {0.0}, + {FLT_MIN}, + {2.98023224e-08}, + {5.96046E-8}, /* min positive FP16 subnormal */ + {6.09756E-5}, /* max subnormal FP16 */ + {6.10352E-5}, /* min positive normal FP16 */ + {1.0}, + {1.0009765625}, /* smallest float after 1.0 FP16 */ + {DBL_MIN}, + {1.3789972848607228e-308}, + {1.4914738736681624e-308}, + {1.0}, {2.0}, + {M_E}, {M_PI}, + {65503.0}, + {65504.0}, /* max FP16 */ + {65505.0}, + {131007.0}, + {131008.0}, /* max AFP */ + {131009.0}, + {.h = 0x41dfffffffc00000 }, /* to int = 0x7fffffff */ + {FLT_MAX}, + {FLT_MAX + 1.0}, + {DBL_MAX}, + {INFINITY}, + {NAN}, + {.h = 0x7ff0000000000001}, /* SNAN */ + {SNAN}, +}; + +static const int num_f64 = ARRAY_SIZE(f64_numbers); + +int get_num_f64(void) +{ + return num_f64; +} + +double get_f64(int i) +{ + return f64_numbers[i % num_f64].d; +} + +char *fmt_f64(double num) +{ + uint64_t double_as_hex = *(uint64_t *) # + char *fmt; + asprintf(&fmt, "f64(%02.20a:%#020" PRIx64 ")", num, double_as_hex); + return fmt; +} + +/* + * Float flags + */ +char *fmt_flags(void) +{ + int flags = fetestexcept(FE_ALL_EXCEPT); + char *fmt; + + if (flags) { + asprintf(&fmt, "%s%s%s%s%s", + flags & FE_OVERFLOW ? "OVERFLOW " : "", + flags & FE_UNDERFLOW ? "UNDERFLOW " : "", + flags & FE_DIVBYZERO ? "DIV0 " : "", + flags & FE_INEXACT ? "INEXACT " : "", + flags & FE_INVALID ? "INVALID" : ""); + } else { + asprintf(&fmt, "OK"); + } + + return fmt; +} diff --git a/tests/tcg/multiarch/linux-test.c b/tests/tcg/multiarch/linux-test.c deleted file mode 100644 index 019d8175ca..0000000000 --- a/tests/tcg/multiarch/linux-test.c +++ /dev/null @@ -1,545 +0,0 @@ -/* - * linux and CPU test - * - * Copyright (c) 2003 Fabrice Bellard - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define STACK_SIZE 16384 - -static void error1(const char *filename, int line, const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - fprintf(stderr, "%s:%d: ", filename, line); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); - exit(1); -} - -static int __chk_error(const char *filename, int line, int ret) -{ - if (ret < 0) { - error1(filename, line, "%m (ret=%d, errno=%d/%s)", - ret, errno, strerror(errno)); - } - return ret; -} - -#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__) - -#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret)) - -/*******************************************************/ - -#define FILE_BUF_SIZE 300 - -static void test_file(void) -{ - int fd, i, len, ret; - uint8_t buf[FILE_BUF_SIZE]; - uint8_t buf2[FILE_BUF_SIZE]; - uint8_t buf3[FILE_BUF_SIZE]; - char cur_dir[1024]; - struct stat st; - struct utimbuf tbuf; - struct iovec vecs[2]; - DIR *dir; - struct dirent64 *de; - /* TODO: make common tempdir creation for tcg tests */ - char template[] = "/tmp/linux-test-XXXXXX"; - char *tmpdir = mkdtemp(template); - - assert(tmpdir); - - if (getcwd(cur_dir, sizeof(cur_dir)) == NULL) - error("getcwd"); - - chk_error(chdir(tmpdir)); - - /* open/read/write/close/readv/writev/lseek */ - - fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644)); - for(i=0;i < FILE_BUF_SIZE; i++) - buf[i] = i; - len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2)); - if (len != (FILE_BUF_SIZE / 2)) - error("write"); - vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2); - vecs[0].iov_len = 16; - vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16; - vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16; - len = chk_error(writev(fd, vecs, 2)); - if (len != (FILE_BUF_SIZE / 2)) - error("writev"); - chk_error(close(fd)); - - chk_error(rename("file1", "file2")); - - fd = chk_error(open("file2", O_RDONLY)); - - len = chk_error(read(fd, buf2, FILE_BUF_SIZE)); - if (len != FILE_BUF_SIZE) - error("read"); - if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0) - error("memcmp"); - -#define FOFFSET 16 - ret = chk_error(lseek(fd, FOFFSET, SEEK_SET)); - if (ret != 16) - error("lseek"); - vecs[0].iov_base = buf3; - vecs[0].iov_len = 32; - vecs[1].iov_base = buf3 + 32; - vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32; - len = chk_error(readv(fd, vecs, 2)); - if (len != FILE_BUF_SIZE - FOFFSET) - error("readv"); - if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0) - error("memcmp"); - - chk_error(close(fd)); - - /* access */ - chk_error(access("file2", R_OK)); - - /* stat/chmod/utime/truncate */ - - chk_error(chmod("file2", 0600)); - tbuf.actime = 1001; - tbuf.modtime = 1000; - chk_error(truncate("file2", 100)); - chk_error(utime("file2", &tbuf)); - chk_error(stat("file2", &st)); - if (st.st_size != 100) - error("stat size"); - if (!S_ISREG(st.st_mode)) - error("stat mode"); - if ((st.st_mode & 0777) != 0600) - error("stat mode2"); - if (st.st_atime != 1001 || - st.st_mtime != 1000) - error("stat time"); - - chk_error(stat(tmpdir, &st)); - if (!S_ISDIR(st.st_mode)) - error("stat mode"); - - /* fstat */ - fd = chk_error(open("file2", O_RDWR)); - chk_error(ftruncate(fd, 50)); - chk_error(fstat(fd, &st)); - chk_error(close(fd)); - - if (st.st_size != 50) - error("stat size"); - if (!S_ISREG(st.st_mode)) - error("stat mode"); - - /* symlink/lstat */ - chk_error(symlink("file2", "file3")); - chk_error(lstat("file3", &st)); - if (!S_ISLNK(st.st_mode)) - error("stat mode"); - - /* getdents */ - dir = opendir(tmpdir); - if (!dir) - error("opendir"); - len = 0; - for(;;) { - de = readdir64(dir); - if (!de) - break; - if (strcmp(de->d_name, ".") != 0 && - strcmp(de->d_name, "..") != 0 && - strcmp(de->d_name, "file2") != 0 && - strcmp(de->d_name, "file3") != 0) - error("readdir"); - len++; - } - closedir(dir); - if (len != 4) - error("readdir"); - - chk_error(unlink("file3")); - chk_error(unlink("file2")); - chk_error(chdir(cur_dir)); - chk_error(rmdir(tmpdir)); -} - -static void test_fork(void) -{ - int pid, status; - - pid = chk_error(fork()); - if (pid == 0) { - /* child */ - sleep(2); - exit(2); - } - chk_error(waitpid(pid, &status, 0)); - if (!WIFEXITED(status) || WEXITSTATUS(status) != 2) - error("waitpid status=0x%x", status); -} - -static void test_time(void) -{ - struct timeval tv, tv2; - struct timespec ts, rem; - struct rusage rusg1, rusg2; - int ti, i; - - chk_error(gettimeofday(&tv, NULL)); - rem.tv_sec = 1; - ts.tv_sec = 0; - ts.tv_nsec = 20 * 1000000; - chk_error(nanosleep(&ts, &rem)); - if (rem.tv_sec != 1) - error("nanosleep"); - chk_error(gettimeofday(&tv2, NULL)); - ti = tv2.tv_sec - tv.tv_sec; - if (ti >= 2) - error("gettimeofday"); - - chk_error(getrusage(RUSAGE_SELF, &rusg1)); - for(i = 0;i < 10000; i++); - chk_error(getrusage(RUSAGE_SELF, &rusg2)); - if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 || - (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0) - error("getrusage"); -} - -static int server_socket(void) -{ - int val, fd; - struct sockaddr_in sockaddr = {}; - - /* server socket */ - fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); - - val = 1; - chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))); - - sockaddr.sin_family = AF_INET; - sockaddr.sin_port = htons(0); /* choose random ephemeral port) */ - sockaddr.sin_addr.s_addr = 0; - chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); - chk_error(listen(fd, 0)); - return fd; - -} - -static int client_socket(uint16_t port) -{ - int fd; - struct sockaddr_in sockaddr = {}; - - /* server socket */ - fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); - sockaddr.sin_family = AF_INET; - sockaddr.sin_port = htons(port); - inet_aton("127.0.0.1", &sockaddr.sin_addr); - chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); - return fd; -} - -static const char socket_msg[] = "hello socket\n"; - -static void test_socket(void) -{ - int server_fd, client_fd, fd, pid, ret, val; - struct sockaddr_in sockaddr; - struct sockaddr_in server_addr; - socklen_t len, socklen; - uint16_t server_port; - char buf[512]; - - server_fd = server_socket(); - /* find out what port we got */ - socklen = sizeof(server_addr); - ret = getsockname(server_fd, (struct sockaddr *)&server_addr, &socklen); - chk_error(ret); - server_port = ntohs(server_addr.sin_port); - - /* test a few socket options */ - len = sizeof(val); - chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len)); - if (val != SOCK_STREAM) - error("getsockopt"); - - pid = chk_error(fork()); - if (pid == 0) { - client_fd = client_socket(server_port); - send(client_fd, socket_msg, sizeof(socket_msg), 0); - close(client_fd); - exit(0); - } - len = sizeof(sockaddr); - fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len)); - - ret = chk_error(recv(fd, buf, sizeof(buf), 0)); - if (ret != sizeof(socket_msg)) - error("recv"); - if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0) - error("socket_msg"); - chk_error(close(fd)); - chk_error(close(server_fd)); -} - -#define WCOUNT_MAX 512 - -static void test_pipe(void) -{ - fd_set rfds, wfds; - int fds[2], fd_max, ret; - uint8_t ch; - int wcount, rcount; - - chk_error(pipe(fds)); - chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK)); - chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK)); - wcount = 0; - rcount = 0; - for(;;) { - FD_ZERO(&rfds); - fd_max = fds[0]; - FD_SET(fds[0], &rfds); - - FD_ZERO(&wfds); - FD_SET(fds[1], &wfds); - if (fds[1] > fd_max) - fd_max = fds[1]; - - ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL)); - if (ret > 0) { - if (FD_ISSET(fds[0], &rfds)) { - chk_error(read(fds[0], &ch, 1)); - rcount++; - if (rcount >= WCOUNT_MAX) - break; - } - if (FD_ISSET(fds[1], &wfds)) { - ch = 'a'; - chk_error(write(fds[1], &ch, 1)); - wcount++; - } - } - } - chk_error(close(fds[0])); - chk_error(close(fds[1])); -} - -static int thread1_res; -static int thread2_res; - -static int thread1_func(void *arg) -{ - int i; - for(i=0;i<5;i++) { - thread1_res++; - usleep(10 * 1000); - } - return 0; -} - -static int thread2_func(void *arg) -{ - int i; - for(i=0;i<6;i++) { - thread2_res++; - usleep(10 * 1000); - } - return 0; -} - -static void wait_for_child(pid_t pid) -{ - int status; - chk_error(waitpid(pid, &status, 0)); -} - -/* For test_clone we must match the clone flags used by glibc, see - * CLONE_THREAD_FLAGS in the QEMU source code. - */ -static void test_clone(void) -{ - uint8_t *stack1, *stack2; - pid_t pid1, pid2; - - stack1 = malloc(STACK_SIZE); - pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE, - CLONE_VM | CLONE_FS | CLONE_FILES | - CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, - "hello1")); - - stack2 = malloc(STACK_SIZE); - pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE, - CLONE_VM | CLONE_FS | CLONE_FILES | - CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, - "hello2")); - - wait_for_child(pid1); - free(stack1); - wait_for_child(pid2); - free(stack2); - - if (thread1_res != 5 || - thread2_res != 6) - error("clone"); -} - -/***********************************/ - -volatile int alarm_count; -jmp_buf jmp_env; - -static void sig_alarm(int sig) -{ - if (sig != SIGALRM) - error("signal"); - alarm_count++; -} - -static void sig_segv(int sig, siginfo_t *info, void *puc) -{ - if (sig != SIGSEGV) - error("signal"); - longjmp(jmp_env, 1); -} - -static void test_signal(void) -{ - struct sigaction act; - struct itimerval it, oit; - - /* timer test */ - - alarm_count = 0; - - act.sa_handler = sig_alarm; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - chk_error(sigaction(SIGALRM, &act, NULL)); - - it.it_interval.tv_sec = 0; - it.it_interval.tv_usec = 10 * 1000; - it.it_value.tv_sec = 0; - it.it_value.tv_usec = 10 * 1000; - chk_error(setitimer(ITIMER_REAL, &it, NULL)); - chk_error(getitimer(ITIMER_REAL, &oit)); - - while (alarm_count < 5) { - usleep(10 * 1000); - getitimer(ITIMER_REAL, &oit); - } - - it.it_interval.tv_sec = 0; - it.it_interval.tv_usec = 0; - it.it_value.tv_sec = 0; - it.it_value.tv_usec = 0; - memset(&oit, 0xff, sizeof(oit)); - chk_error(setitimer(ITIMER_REAL, &it, &oit)); - - /* SIGSEGV test */ - act.sa_sigaction = sig_segv; - sigemptyset(&act.sa_mask); - act.sa_flags = SA_SIGINFO; - chk_error(sigaction(SIGSEGV, &act, NULL)); - if (setjmp(jmp_env) == 0) { - /* - * clang requires volatile or it will turn this into a - * call to abort() instead of forcing a SIGSEGV. - */ - *(volatile uint8_t *)0 = 0; - } - - act.sa_handler = SIG_DFL; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - chk_error(sigaction(SIGSEGV, &act, NULL)); - - if (sigaction(SIGKILL, &act, NULL) == 0) { - error("sigaction(SIGKILL, &act, NULL) must not succeed"); - } - if (sigaction(SIGSTOP, &act, NULL) == 0) { - error("sigaction(SIGSTOP, &act, NULL) must not succeed"); - } - chk_error(sigaction(SIGKILL, NULL, &act)); - chk_error(sigaction(SIGSTOP, NULL, &act)); -} - -#define SHM_SIZE 32768 - -static void test_shm(void) -{ - void *ptr; - int shmid; - - shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777)); - ptr = shmat(shmid, NULL, 0); - if (ptr == (void *)-1) { - error("shmat"); - } - - memset(ptr, 0, SHM_SIZE); - - chk_error(shmctl(shmid, IPC_RMID, 0)); - chk_error(shmdt(ptr)); -} - -int main(int argc, char **argv) -{ - test_file(); - test_pipe(); - test_fork(); - test_time(); - test_socket(); - - if (argc > 1) { - printf("test_clone still considered buggy\n"); - test_clone(); - } - - test_signal(); - test_shm(); - return 0; -} diff --git a/tests/tcg/multiarch/linux/linux-test.c b/tests/tcg/multiarch/linux/linux-test.c new file mode 100644 index 0000000000..019d8175ca --- /dev/null +++ b/tests/tcg/multiarch/linux/linux-test.c @@ -0,0 +1,545 @@ +/* + * linux and CPU test + * + * Copyright (c) 2003 Fabrice Bellard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define STACK_SIZE 16384 + +static void error1(const char *filename, int line, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "%s:%d: ", filename, line); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); + exit(1); +} + +static int __chk_error(const char *filename, int line, int ret) +{ + if (ret < 0) { + error1(filename, line, "%m (ret=%d, errno=%d/%s)", + ret, errno, strerror(errno)); + } + return ret; +} + +#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__) + +#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret)) + +/*******************************************************/ + +#define FILE_BUF_SIZE 300 + +static void test_file(void) +{ + int fd, i, len, ret; + uint8_t buf[FILE_BUF_SIZE]; + uint8_t buf2[FILE_BUF_SIZE]; + uint8_t buf3[FILE_BUF_SIZE]; + char cur_dir[1024]; + struct stat st; + struct utimbuf tbuf; + struct iovec vecs[2]; + DIR *dir; + struct dirent64 *de; + /* TODO: make common tempdir creation for tcg tests */ + char template[] = "/tmp/linux-test-XXXXXX"; + char *tmpdir = mkdtemp(template); + + assert(tmpdir); + + if (getcwd(cur_dir, sizeof(cur_dir)) == NULL) + error("getcwd"); + + chk_error(chdir(tmpdir)); + + /* open/read/write/close/readv/writev/lseek */ + + fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644)); + for(i=0;i < FILE_BUF_SIZE; i++) + buf[i] = i; + len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2)); + if (len != (FILE_BUF_SIZE / 2)) + error("write"); + vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2); + vecs[0].iov_len = 16; + vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16; + vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16; + len = chk_error(writev(fd, vecs, 2)); + if (len != (FILE_BUF_SIZE / 2)) + error("writev"); + chk_error(close(fd)); + + chk_error(rename("file1", "file2")); + + fd = chk_error(open("file2", O_RDONLY)); + + len = chk_error(read(fd, buf2, FILE_BUF_SIZE)); + if (len != FILE_BUF_SIZE) + error("read"); + if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0) + error("memcmp"); + +#define FOFFSET 16 + ret = chk_error(lseek(fd, FOFFSET, SEEK_SET)); + if (ret != 16) + error("lseek"); + vecs[0].iov_base = buf3; + vecs[0].iov_len = 32; + vecs[1].iov_base = buf3 + 32; + vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32; + len = chk_error(readv(fd, vecs, 2)); + if (len != FILE_BUF_SIZE - FOFFSET) + error("readv"); + if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0) + error("memcmp"); + + chk_error(close(fd)); + + /* access */ + chk_error(access("file2", R_OK)); + + /* stat/chmod/utime/truncate */ + + chk_error(chmod("file2", 0600)); + tbuf.actime = 1001; + tbuf.modtime = 1000; + chk_error(truncate("file2", 100)); + chk_error(utime("file2", &tbuf)); + chk_error(stat("file2", &st)); + if (st.st_size != 100) + error("stat size"); + if (!S_ISREG(st.st_mode)) + error("stat mode"); + if ((st.st_mode & 0777) != 0600) + error("stat mode2"); + if (st.st_atime != 1001 || + st.st_mtime != 1000) + error("stat time"); + + chk_error(stat(tmpdir, &st)); + if (!S_ISDIR(st.st_mode)) + error("stat mode"); + + /* fstat */ + fd = chk_error(open("file2", O_RDWR)); + chk_error(ftruncate(fd, 50)); + chk_error(fstat(fd, &st)); + chk_error(close(fd)); + + if (st.st_size != 50) + error("stat size"); + if (!S_ISREG(st.st_mode)) + error("stat mode"); + + /* symlink/lstat */ + chk_error(symlink("file2", "file3")); + chk_error(lstat("file3", &st)); + if (!S_ISLNK(st.st_mode)) + error("stat mode"); + + /* getdents */ + dir = opendir(tmpdir); + if (!dir) + error("opendir"); + len = 0; + for(;;) { + de = readdir64(dir); + if (!de) + break; + if (strcmp(de->d_name, ".") != 0 && + strcmp(de->d_name, "..") != 0 && + strcmp(de->d_name, "file2") != 0 && + strcmp(de->d_name, "file3") != 0) + error("readdir"); + len++; + } + closedir(dir); + if (len != 4) + error("readdir"); + + chk_error(unlink("file3")); + chk_error(unlink("file2")); + chk_error(chdir(cur_dir)); + chk_error(rmdir(tmpdir)); +} + +static void test_fork(void) +{ + int pid, status; + + pid = chk_error(fork()); + if (pid == 0) { + /* child */ + sleep(2); + exit(2); + } + chk_error(waitpid(pid, &status, 0)); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 2) + error("waitpid status=0x%x", status); +} + +static void test_time(void) +{ + struct timeval tv, tv2; + struct timespec ts, rem; + struct rusage rusg1, rusg2; + int ti, i; + + chk_error(gettimeofday(&tv, NULL)); + rem.tv_sec = 1; + ts.tv_sec = 0; + ts.tv_nsec = 20 * 1000000; + chk_error(nanosleep(&ts, &rem)); + if (rem.tv_sec != 1) + error("nanosleep"); + chk_error(gettimeofday(&tv2, NULL)); + ti = tv2.tv_sec - tv.tv_sec; + if (ti >= 2) + error("gettimeofday"); + + chk_error(getrusage(RUSAGE_SELF, &rusg1)); + for(i = 0;i < 10000; i++); + chk_error(getrusage(RUSAGE_SELF, &rusg2)); + if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 || + (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0) + error("getrusage"); +} + +static int server_socket(void) +{ + int val, fd; + struct sockaddr_in sockaddr = {}; + + /* server socket */ + fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); + + val = 1; + chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))); + + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(0); /* choose random ephemeral port) */ + sockaddr.sin_addr.s_addr = 0; + chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); + chk_error(listen(fd, 0)); + return fd; + +} + +static int client_socket(uint16_t port) +{ + int fd; + struct sockaddr_in sockaddr = {}; + + /* server socket */ + fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(port); + inet_aton("127.0.0.1", &sockaddr.sin_addr); + chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); + return fd; +} + +static const char socket_msg[] = "hello socket\n"; + +static void test_socket(void) +{ + int server_fd, client_fd, fd, pid, ret, val; + struct sockaddr_in sockaddr; + struct sockaddr_in server_addr; + socklen_t len, socklen; + uint16_t server_port; + char buf[512]; + + server_fd = server_socket(); + /* find out what port we got */ + socklen = sizeof(server_addr); + ret = getsockname(server_fd, (struct sockaddr *)&server_addr, &socklen); + chk_error(ret); + server_port = ntohs(server_addr.sin_port); + + /* test a few socket options */ + len = sizeof(val); + chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len)); + if (val != SOCK_STREAM) + error("getsockopt"); + + pid = chk_error(fork()); + if (pid == 0) { + client_fd = client_socket(server_port); + send(client_fd, socket_msg, sizeof(socket_msg), 0); + close(client_fd); + exit(0); + } + len = sizeof(sockaddr); + fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len)); + + ret = chk_error(recv(fd, buf, sizeof(buf), 0)); + if (ret != sizeof(socket_msg)) + error("recv"); + if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0) + error("socket_msg"); + chk_error(close(fd)); + chk_error(close(server_fd)); +} + +#define WCOUNT_MAX 512 + +static void test_pipe(void) +{ + fd_set rfds, wfds; + int fds[2], fd_max, ret; + uint8_t ch; + int wcount, rcount; + + chk_error(pipe(fds)); + chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK)); + chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK)); + wcount = 0; + rcount = 0; + for(;;) { + FD_ZERO(&rfds); + fd_max = fds[0]; + FD_SET(fds[0], &rfds); + + FD_ZERO(&wfds); + FD_SET(fds[1], &wfds); + if (fds[1] > fd_max) + fd_max = fds[1]; + + ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL)); + if (ret > 0) { + if (FD_ISSET(fds[0], &rfds)) { + chk_error(read(fds[0], &ch, 1)); + rcount++; + if (rcount >= WCOUNT_MAX) + break; + } + if (FD_ISSET(fds[1], &wfds)) { + ch = 'a'; + chk_error(write(fds[1], &ch, 1)); + wcount++; + } + } + } + chk_error(close(fds[0])); + chk_error(close(fds[1])); +} + +static int thread1_res; +static int thread2_res; + +static int thread1_func(void *arg) +{ + int i; + for(i=0;i<5;i++) { + thread1_res++; + usleep(10 * 1000); + } + return 0; +} + +static int thread2_func(void *arg) +{ + int i; + for(i=0;i<6;i++) { + thread2_res++; + usleep(10 * 1000); + } + return 0; +} + +static void wait_for_child(pid_t pid) +{ + int status; + chk_error(waitpid(pid, &status, 0)); +} + +/* For test_clone we must match the clone flags used by glibc, see + * CLONE_THREAD_FLAGS in the QEMU source code. + */ +static void test_clone(void) +{ + uint8_t *stack1, *stack2; + pid_t pid1, pid2; + + stack1 = malloc(STACK_SIZE); + pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE, + CLONE_VM | CLONE_FS | CLONE_FILES | + CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, + "hello1")); + + stack2 = malloc(STACK_SIZE); + pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE, + CLONE_VM | CLONE_FS | CLONE_FILES | + CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, + "hello2")); + + wait_for_child(pid1); + free(stack1); + wait_for_child(pid2); + free(stack2); + + if (thread1_res != 5 || + thread2_res != 6) + error("clone"); +} + +/***********************************/ + +volatile int alarm_count; +jmp_buf jmp_env; + +static void sig_alarm(int sig) +{ + if (sig != SIGALRM) + error("signal"); + alarm_count++; +} + +static void sig_segv(int sig, siginfo_t *info, void *puc) +{ + if (sig != SIGSEGV) + error("signal"); + longjmp(jmp_env, 1); +} + +static void test_signal(void) +{ + struct sigaction act; + struct itimerval it, oit; + + /* timer test */ + + alarm_count = 0; + + act.sa_handler = sig_alarm; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + chk_error(sigaction(SIGALRM, &act, NULL)); + + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 10 * 1000; + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 10 * 1000; + chk_error(setitimer(ITIMER_REAL, &it, NULL)); + chk_error(getitimer(ITIMER_REAL, &oit)); + + while (alarm_count < 5) { + usleep(10 * 1000); + getitimer(ITIMER_REAL, &oit); + } + + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 0; + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 0; + memset(&oit, 0xff, sizeof(oit)); + chk_error(setitimer(ITIMER_REAL, &it, &oit)); + + /* SIGSEGV test */ + act.sa_sigaction = sig_segv; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + chk_error(sigaction(SIGSEGV, &act, NULL)); + if (setjmp(jmp_env) == 0) { + /* + * clang requires volatile or it will turn this into a + * call to abort() instead of forcing a SIGSEGV. + */ + *(volatile uint8_t *)0 = 0; + } + + act.sa_handler = SIG_DFL; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + chk_error(sigaction(SIGSEGV, &act, NULL)); + + if (sigaction(SIGKILL, &act, NULL) == 0) { + error("sigaction(SIGKILL, &act, NULL) must not succeed"); + } + if (sigaction(SIGSTOP, &act, NULL) == 0) { + error("sigaction(SIGSTOP, &act, NULL) must not succeed"); + } + chk_error(sigaction(SIGKILL, NULL, &act)); + chk_error(sigaction(SIGSTOP, NULL, &act)); +} + +#define SHM_SIZE 32768 + +static void test_shm(void) +{ + void *ptr; + int shmid; + + shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777)); + ptr = shmat(shmid, NULL, 0); + if (ptr == (void *)-1) { + error("shmat"); + } + + memset(ptr, 0, SHM_SIZE); + + chk_error(shmctl(shmid, IPC_RMID, 0)); + chk_error(shmdt(ptr)); +} + +int main(int argc, char **argv) +{ + test_file(); + test_pipe(); + test_fork(); + test_time(); + test_socket(); + + if (argc > 1) { + printf("test_clone still considered buggy\n"); + test_clone(); + } + + test_signal(); + test_shm(); + return 0; +} diff --git a/tests/tcg/x86_64/Makefile.target b/tests/tcg/x86_64/Makefile.target index 2151ea6302..d7a7385583 100644 --- a/tests/tcg/x86_64/Makefile.target +++ b/tests/tcg/x86_64/Makefile.target @@ -8,8 +8,12 @@ include $(SRC_PATH)/tests/tcg/i386/Makefile.target +ifneq ($(CONFIG_LINUX),) X86_64_TESTS += vsyscall TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64 +else +TESTS=$(MULTIARCH_TESTS) +endif QEMU_OPTS += -cpu max test-x86_64: LDFLAGS+=-lm -lc