Commit af3c79f8 authored by André Almeida's avatar André Almeida Committed by Thomas Gleixner
Browse files

selftests/futex: Refactor futex_wait_unitialized_heap with kselftest_harness.h



To reduce the boilerplate code, refactor futex_wait_unitialized_heap
test to use kselftest_harness header instead of futex's logging header.

Signed-off-by: default avatarAndré Almeida <andrealmeid@igalia.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
parent f5a16834
Loading
Loading
Loading
Loading
+18 −58
Original line number Diff line number Diff line
@@ -29,95 +29,55 @@
#include <linux/futex.h>
#include <libgen.h>

#include "logging.h"
#include "futextest.h"
#include "../../kselftest_harness.h"

#define TEST_NAME "futex-wait-uninitialized-heap"
#define WAIT_US 5000000

static int child_blocked = 1;
static int child_ret;
static bool child_ret;
void *buf;

void usage(char *prog)
{
	printf("Usage: %s\n", prog);
	printf("  -c	Use color\n");
	printf("  -h	Display this help message\n");
	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
	       VQUIET, VCRITICAL, VINFO);
}

void *wait_thread(void *arg)
{
	int res;

	child_ret = RET_PASS;
	child_ret = true;
	res = futex_wait(buf, 1, NULL, 0);
	child_blocked = 0;

	if (res != 0 && errno != EWOULDBLOCK) {
		error("futex failure\n", errno);
		child_ret = RET_ERROR;
		ksft_exit_fail_msg("futex failure\n");
		child_ret = false;
	}
	pthread_exit(NULL);
}

int main(int argc, char **argv)
TEST(futex_wait_uninitialized_heap)
{
	int c, ret = RET_PASS;
	long page_size;
	pthread_t thr;

	while ((c = getopt(argc, argv, "chv:")) != -1) {
		switch (c) {
		case 'c':
			log_color(1);
			break;
		case 'h':
			usage(basename(argv[0]));
			exit(0);
		case 'v':
			log_verbosity(atoi(optarg));
			break;
		default:
			usage(basename(argv[0]));
			exit(1);
		}
	}
	int ret;

	page_size = sysconf(_SC_PAGESIZE);

	buf = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
		   MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	if (buf == (void *)-1) {
		error("mmap\n", errno);
		exit(1);
	}

	ksft_print_header();
	ksft_set_plan(1);
	ksft_print_msg("%s: Test the uninitialized futex value in FUTEX_WAIT\n",
	       basename(argv[0]));

	if (buf == (void *)-1)
		ksft_exit_fail_msg("mmap\n");

	ret = pthread_create(&thr, NULL, wait_thread, NULL);
	if (ret) {
		error("pthread_create\n", errno);
		ret = RET_ERROR;
		goto out;
	}
	if (ret)
		ksft_exit_fail_msg("pthread_create\n");

	info("waiting %dus for child to return\n", WAIT_US);
	ksft_print_dbg_msg("waiting %dus for child to return\n", WAIT_US);
	usleep(WAIT_US);

	ret = child_ret;
	if (child_blocked) {
		fail("child blocked in kernel\n");
		ret = RET_FAIL;
	}
	if (child_blocked)
		ksft_test_result_fail("child blocked in kernel\n");

 out:
	print_result(TEST_NAME, ret);
	return ret;
	if (!child_ret)
		ksft_test_result_fail("child error\n");
}

TEST_HARNESS_MAIN
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ echo
./futex_wait_wouldblock

echo
./futex_wait_uninitialized_heap $COLOR
./futex_wait_uninitialized_heap
./futex_wait_private_mapped_file $COLOR

echo