refactor: simplify hftest test registration

The `hftest_constructed`, `hftest_count` and `hftest_list` variables
were redundant. Instead, just iterate over `hftest_begin` and
`hftest_end`.

This also means calls to `hftest_register` are unnecessary (and in fact
they do not seem to have been run, despite being registered with the
`constructor` attribute).

Change-Id: Ia0747324f9704a1225eb048251f1af4ebc8a105e
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
diff --git a/test/hftest/common.c b/test/hftest/common.c
index cc9681e..be4c44c 100644
--- a/test/hftest/common.c
+++ b/test/hftest/common.c
@@ -19,12 +19,11 @@
 #define HFTEST_CTRL_JSON_START "[hftest_ctrl:json_start]"
 #define HFTEST_CTRL_JSON_END "[hftest_ctrl:json_end]"
 
-static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS];
-static size_t hftest_count;
-static struct hftest_test *hftest_list;
-
 static struct hftest_context global_context;
 
+extern struct hftest_test hftest_begin[];
+extern struct hftest_test hftest_end[];
+
 static alignas(PAGE_SIZE) uint8_t secondary_ec_stack[MAX_CPUS][PAGE_SIZE];
 
 struct hftest_context *hftest_get_context(void)
@@ -33,43 +32,11 @@
 }
 
 /**
- * Adds the given test information to the global list, to be used by
- * `hftest_use_registered_list`.
- */
-void hftest_register(struct hftest_test test)
-{
-	if (hftest_count < HFTEST_MAX_TESTS) {
-		hftest_constructed[hftest_count++] = test;
-	} else {
-		HFTEST_FAIL(true, "Too many tests");
-	}
-}
-
-/**
- * Uses the list of tests registered by `hftest_register(...)` as the ones to
- * run.
- */
-void hftest_use_registered_list(void)
-{
-	hftest_list = hftest_constructed;
-}
-
-/**
- * Uses the given list of tests as the ones to run.
- */
-void hftest_use_list(struct hftest_test list[], size_t count)
-{
-	hftest_list = list;
-	hftest_count = count;
-}
-
-/**
  * Writes out a JSON structure describing the available tests.
  */
 void hftest_json(void)
 {
 	const char *suite = NULL;
-	size_t i;
 	size_t tests_in_suite = 0;
 
 	/* Wrap the JSON in tags for the hftest script to use. */
@@ -77,8 +44,8 @@
 
 	HFTEST_LOG("{");
 	HFTEST_LOG("  \"suites\": [");
-	for (i = 0; i < hftest_count; ++i) {
-		struct hftest_test *test = &hftest_list[i];
+	for (struct hftest_test *test = hftest_begin; test < hftest_end;
+	     ++test) {
 		if (test->suite != suite) {
 			/* Close out previously open suite. */
 			if (tests_in_suite) {
@@ -183,13 +150,11 @@
 void hftest_run(struct memiter suite_name, struct memiter test_name,
 		const struct fdt *fdt)
 {
-	size_t i;
 	hftest_test_fn suite_set_up = NULL;
 	hftest_test_fn suite_tear_down = NULL;
 
-	for (i = 0; i < hftest_count; ++i) {
-		struct hftest_test *test = &hftest_list[i];
-
+	for (struct hftest_test *test = hftest_begin; test < hftest_end;
+	     ++test) {
 		/* Check if this test is part of the suite we want. */
 		if (memiter_iseq(&suite_name, test->suite)) {
 			switch (test->kind) {
diff --git a/test/hftest/hftest_common.h b/test/hftest/hftest_common.h
index 7355dae..0879d43 100644
--- a/test/hftest/hftest_common.h
+++ b/test/hftest/hftest_common.h
@@ -9,11 +9,6 @@
 #include "hf/fdt.h"
 #include "hf/memiter.h"
 
-#include "test/hftest_impl.h"
-
-void hftest_use_registered_list(void);
-void hftest_use_list(struct hftest_test list[], size_t count);
-
 void hftest_json(void);
 void hftest_run(struct memiter suite_name, struct memiter test_name,
 		const struct fdt *fdt);
diff --git a/test/hftest/secondary_no_fdt.c b/test/hftest/secondary_no_fdt.c
index 1291b66..a384f18 100644
--- a/test/hftest/secondary_no_fdt.c
+++ b/test/hftest/secondary_no_fdt.c
@@ -20,9 +20,6 @@
 
 alignas(4096) uint8_t kstack[4096];
 
-extern struct hftest_test hftest_begin[];
-extern struct hftest_test hftest_end[];
-
 void test_main_secondary(size_t mem_size);
 
 void run_service_set_up(struct hftest_context *ctx, struct fdt *fdt)
diff --git a/test/hftest/standalone_main.c b/test/hftest/standalone_main.c
index 0a8892f..24d66bd 100644
--- a/test/hftest/standalone_main.c
+++ b/test/hftest/standalone_main.c
@@ -18,9 +18,6 @@
 
 alignas(4096) uint8_t kstack[2 * 4096];
 
-extern struct hftest_test hftest_begin[];
-extern struct hftest_test hftest_end[];
-
 void kmain(const void *fdt_ptr)
 {
 	struct fdt fdt;
@@ -40,10 +37,6 @@
 	 */
 	exception_setup(NULL, NULL);
 
-	// FIXME: find a way of doing this that isn't UB
-	// NOLINTNEXTLINE(clang-analyzer-security.PointerSub)
-	hftest_use_list(hftest_begin, hftest_end - hftest_begin);
-
 	if (!fdt_size_from_header(fdt_ptr, &fdt_len) ||
 	    !fdt_init_from_ptr(&fdt, fdt_ptr, fdt_len)) {
 		HFTEST_LOG("Unable to init FDT.");
diff --git a/test/hftest/standalone_main_secure.c b/test/hftest/standalone_main_secure.c
index 17a7ed5..e3a853a 100644
--- a/test/hftest/standalone_main_secure.c
+++ b/test/hftest/standalone_main_secure.c
@@ -25,9 +25,6 @@
 	return boot_info_header;
 }
 
-extern struct hftest_test hftest_begin[];
-extern struct hftest_test hftest_end[];
-
 static noreturn void hftest_wait(void)
 {
 	for (;;) {
@@ -56,10 +53,6 @@
 	 */
 	exception_setup(NULL, NULL);
 
-	// FIXME: find a way of doing this that isn't UB
-	// NOLINTNEXTLINE(clang-analyzer-security.PointerSub)
-	hftest_use_list(hftest_begin, hftest_end - hftest_begin);
-
 	hftest_command(&fdt);
 
 out:
diff --git a/test/inc/test/hftest_impl.h b/test/inc/test/hftest_impl.h
index 4e672f1..0f92463 100644
--- a/test/inc/test/hftest_impl.h
+++ b/test/inc/test/hftest_impl.h
@@ -70,36 +70,26 @@
 	hftest_test_ctor_##suite_name##_##test_name
 
 /* Register test functions. */
-#define HFTEST_SET_UP(suite_name)                                           \
-	static void HFTEST_SET_UP_FN(suite_name)(void);                     \
-	const struct hftest_test __attribute__((used))                      \
-	__attribute__((section(HFTEST_SET_UP_SECTION(                       \
-		suite_name)))) HFTEST_SET_UP_STRUCT(suite_name) = {         \
-		.suite = #suite_name,                                       \
-		.kind = HFTEST_KIND_SET_UP,                                 \
-		.fn = HFTEST_SET_UP_FN(suite_name),                         \
-	};                                                                  \
-	static void __attribute__((constructor)) HFTEST_SET_UP_CONSTRUCTOR( \
-		suite_name)(void)                                           \
-	{                                                                   \
-		hftest_register(HFTEST_SET_UP_STRUCT(suite_name));          \
-	}                                                                   \
+#define HFTEST_SET_UP(suite_name)                                   \
+	static void HFTEST_SET_UP_FN(suite_name)(void);             \
+	const struct hftest_test __attribute__((used))              \
+	__attribute__((section(HFTEST_SET_UP_SECTION(               \
+		suite_name)))) HFTEST_SET_UP_STRUCT(suite_name) = { \
+		.suite = #suite_name,                               \
+		.kind = HFTEST_KIND_SET_UP,                         \
+		.fn = HFTEST_SET_UP_FN(suite_name),                 \
+	};                                                          \
 	static void HFTEST_SET_UP_FN(suite_name)(void)
 
-#define HFTEST_TEAR_DOWN(suite_name)                                           \
-	static void HFTEST_TEAR_DOWN_FN(suite_name)(void);                     \
-	const struct hftest_test __attribute__((used))                         \
-	__attribute__((section(HFTEST_TEAR_DOWN_SECTION(                       \
-		suite_name)))) HFTEST_TEAR_DOWN_STRUCT(suite_name) = {         \
-		.suite = #suite_name,                                          \
-		.kind = HFTEST_KIND_TEAR_DOWN,                                 \
-		.fn = HFTEST_TEAR_DOWN_FN(suite_name),                         \
-	};                                                                     \
-	static void __attribute__((constructor)) HFTEST_TEAR_DOWN_CONSTRUCTOR( \
-		suite_name)(void)                                              \
-	{                                                                      \
-		hftest_register(HFTEST_TEAR_DOWN_STRUCT(suite_name));          \
-	}                                                                      \
+#define HFTEST_TEAR_DOWN(suite_name)                                   \
+	static void HFTEST_TEAR_DOWN_FN(suite_name)(void);             \
+	const struct hftest_test __attribute__((used))                 \
+	__attribute__((section(HFTEST_TEAR_DOWN_SECTION(               \
+		suite_name)))) HFTEST_TEAR_DOWN_STRUCT(suite_name) = { \
+		.suite = #suite_name,                                  \
+		.kind = HFTEST_KIND_TEAR_DOWN,                         \
+		.fn = HFTEST_TEAR_DOWN_FN(suite_name),                 \
+	};                                                             \
 	static void HFTEST_TEAR_DOWN_FN(suite_name)(void)
 
 #define HFTEST_TEST(suite_name, test_name, long_running, precon_fn)         \
@@ -115,11 +105,6 @@
 		.fn = HFTEST_TEST_FN(suite_name, test_name),                \
 		.precondition = (precon_fn),                                \
 	};                                                                  \
-	static void __attribute__((constructor)) HFTEST_TEST_CONSTRUCTOR(   \
-		suite_name, test_name)(void)                                \
-	{                                                                   \
-		hftest_register(HFTEST_TEST_STRUCT(suite_name, test_name)); \
-	}                                                                   \
 	static void HFTEST_TEST_FN(suite_name, test_name)(void)
 
 #define HFTEST_SERVICE_SET_UP(service_name)                                   \
@@ -330,5 +315,3 @@
 #define HFTEST_SERVICE_SEND_BUFFER() hftest_get_context()->send
 #define HFTEST_SERVICE_RECV_BUFFER() hftest_get_context()->recv
 #define HFTEST_SERVICE_MEMORY_SIZE() hftest_get_context()->memory_size
-
-void hftest_register(struct hftest_test test);