Test should not reach code after an exception is expected.

There is no point looping after a FAIL either.
This fixes a mistake made in
https://hafnium-review.googlesource.com/c/hafnium/+/7920.

Bug: 140916188
Change-Id: I18157ce16fce955e7b22562c1c9d8709de3253dc
diff --git a/test/vmapi/primary_with_secondaries/memory_sharing.c b/test/vmapi/primary_with_secondaries/memory_sharing.c
index 69a43e4..17f8bc1 100644
--- a/test/vmapi/primary_with_secondaries/memory_sharing.c
+++ b/test/vmapi/primary_with_secondaries/memory_sharing.c
@@ -606,6 +606,7 @@
 	uint64_t address;
 
 	SERVICE_SELECT(SERVICE_VM1, "spci_donate_check_upper_bound", mb.send);
+	SERVICE_SELECT(SERVICE_VM2, "spci_donate_check_upper_bound", mb.send);
 
 	/* Initialise the memory before giving it. */
 	memset_s(ptr, sizeof(pages), 'b', 4 * PAGE_SIZE);
@@ -648,14 +649,18 @@
 	 */
 	pages[PAGE_SIZE] = 1;
 
+	/*
+	 * Use the second secondary VM for this test as the first is now in an
+	 * exception loop.
+	 */
 	msg_size = spci_memory_region_init(
-		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM1, constituents,
+		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM2, constituents,
 		ARRAY_SIZE(constituents), 0, 0, SPCI_MEMORY_RW_X,
 		SPCI_MEMORY_NORMAL_MEM, SPCI_MEMORY_CACHE_WRITE_BACK,
 		SPCI_MEMORY_OUTER_SHAREABLE);
 	EXPECT_EQ(spci_mem_donate(msg_size, msg_size, 0).func, SPCI_SUCCESS_32);
 
-	run_res = spci_run(SERVICE_VM1, 0);
+	run_res = spci_run(SERVICE_VM2, 0);
 	EXPECT_EQ(exception_handler_receive_exception_count(&run_res, mb.recv),
 		  1);
 }
@@ -672,6 +677,7 @@
 	uint64_t address;
 
 	SERVICE_SELECT(SERVICE_VM1, "spci_donate_check_lower_bound", mb.send);
+	SERVICE_SELECT(SERVICE_VM2, "spci_donate_check_lower_bound", mb.send);
 
 	/* Initialise the memory before donating it. */
 	memset_s(ptr, sizeof(pages), 'b', 4 * PAGE_SIZE);
@@ -714,20 +720,20 @@
 	 */
 	pages[PAGE_SIZE] = 1;
 
+	/*
+	 * Use the second secondary VM for this test as the first is now in an
+	 * exception loop.
+	 */
 	msg_size = spci_memory_region_init(
-		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM1, constituents,
+		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM2, constituents,
 		ARRAY_SIZE(constituents), 0, 0, SPCI_MEMORY_RW_X,
 		SPCI_MEMORY_NORMAL_MEM, SPCI_MEMORY_CACHE_WRITE_BACK,
 		SPCI_MEMORY_OUTER_SHAREABLE);
 	EXPECT_EQ(spci_mem_donate(msg_size, msg_size, 0).func, SPCI_SUCCESS_32);
 
-	run_res = spci_run(SERVICE_VM1, 0);
-	/*
-	 * NOTE: This generates two exceptions, one for the page fault, and one
-	 * for accessing a region past the lower bound.
-	 */
+	run_res = spci_run(SERVICE_VM2, 0);
 	EXPECT_EQ(exception_handler_receive_exception_count(&run_res, mb.recv),
-		  2);
+		  1);
 }
 
 /**
@@ -1742,7 +1748,10 @@
 	 */
 	pages[PAGE_SIZE] = 1;
 
-	/* Use the secondary VM for this test as the first is now aborted. */
+	/*
+	 * Use the second secondary VM for this test as the first is now in an
+	 * exception loop.
+	 */
 	msg_size = spci_memory_region_init(
 		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM2, constituents,
 		ARRAY_SIZE(constituents), 0, 0, SPCI_MEMORY_RW_X,
@@ -1810,7 +1819,10 @@
 	 */
 	pages[PAGE_SIZE] = 1;
 
-	/* Use the secondary VM for this test as the first is now aborted. */
+	/*
+	 * Use the second secondary VM for this test as the first is now in an
+	 * exception loop.
+	 */
 	msg_size = spci_memory_region_init(
 		mb.send, HF_PRIMARY_VM_ID, SERVICE_VM2, constituents,
 		ARRAY_SIZE(constituents), 0, 0, SPCI_MEMORY_RW_X,
diff --git a/test/vmapi/primary_with_secondaries/services/memory.c b/test/vmapi/primary_with_secondaries/services/memory.c
index b45c1f0..1ffabc4 100644
--- a/test/vmapi/primary_with_secondaries/services/memory.c
+++ b/test/vmapi/primary_with_secondaries/services/memory.c
@@ -114,127 +114,111 @@
 
 TEST_SERVICE(spci_memory_return)
 {
+	struct spci_value ret = spci_msg_wait();
+	uint8_t *ptr;
+	uint32_t msg_size;
+	size_t i;
+	void *recv_buf = SERVICE_RECV_BUFFER();
+	void *send_buf = SERVICE_SEND_BUFFER();
+	struct spci_memory_region *memory_region;
+	struct spci_memory_region_constituent *constituents;
+
 	exception_setup(NULL, exception_handler_yield_data_abort);
 
-	/* Loop, giving memory back to the sender. */
-	for (;;) {
-		struct spci_value ret = spci_msg_wait();
-		uint8_t *ptr;
-		uint32_t msg_size;
-		size_t i;
-		void *recv_buf = SERVICE_RECV_BUFFER();
-		void *send_buf = SERVICE_SEND_BUFFER();
-		struct spci_memory_region *memory_region;
-		struct spci_memory_region_constituent *constituents;
+	/* The memory may have been sent in one of several different ways. */
+	EXPECT_TRUE(ret.func == SPCI_MEM_DONATE_32 ||
+		    ret.func == SPCI_MEM_LEND_32 ||
+		    ret.func == SPCI_MEM_SHARE_32);
 
-		/*
-		 * The memory may have been sent in one of several different
-		 * ways.
-		 */
-		EXPECT_TRUE(ret.func == SPCI_MEM_DONATE_32 ||
-			    ret.func == SPCI_MEM_LEND_32 ||
-			    ret.func == SPCI_MEM_SHARE_32);
+	memory_region = (struct spci_memory_region *)recv_buf;
+	constituents = spci_memory_region_get_constituents(memory_region);
+	ptr = (uint8_t *)spci_memory_region_constituent_get_address(
+		&constituents[0]);
 
-		memory_region = (struct spci_memory_region *)recv_buf;
-		constituents =
-			spci_memory_region_get_constituents(memory_region);
-		ptr = (uint8_t *)spci_memory_region_constituent_get_address(
-			&constituents[0]);
-
-		/* Check that one has access to the shared region. */
-		for (i = 0; i < PAGE_SIZE; ++i) {
-			ptr[i]++;
-		}
-
-		/* Give the memory back and notify the sender. */
-		msg_size = spci_memory_region_init(
-			send_buf, hf_vm_get_id(), memory_region->sender,
-			constituents, memory_region->constituent_count, 0, 0,
-			SPCI_MEMORY_RW_X, SPCI_MEMORY_NORMAL_MEM,
-			SPCI_MEMORY_CACHE_WRITE_BACK,
-			SPCI_MEMORY_OUTER_SHAREABLE);
-		EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
-		EXPECT_EQ(spci_mem_donate(msg_size, msg_size, 0).func,
-			  SPCI_SUCCESS_32);
-
-		/*
-		 * Try and access the memory which will cause a fault unless the
-		 * memory has been shared back again.
-		 */
-		ptr[0] = 123;
-
-		FAIL("Exception not generated by invalid access.");
+	/* Check that one has access to the shared region. */
+	for (i = 0; i < PAGE_SIZE; ++i) {
+		ptr[i]++;
 	}
+
+	/* Give the memory back and notify the sender. */
+	msg_size = spci_memory_region_init(
+		send_buf, hf_vm_get_id(), memory_region->sender, constituents,
+		memory_region->constituent_count, 0, 0, SPCI_MEMORY_RW_X,
+		SPCI_MEMORY_NORMAL_MEM, SPCI_MEMORY_CACHE_WRITE_BACK,
+		SPCI_MEMORY_OUTER_SHAREABLE);
+	EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
+	EXPECT_EQ(spci_mem_donate(msg_size, msg_size, 0).func, SPCI_SUCCESS_32);
+
+	/*
+	 * Try and access the memory which will cause a fault unless the memory
+	 * has been shared back again.
+	 */
+	ptr[0] = 123;
+
+	FAIL("Exception not generated by invalid access.");
 }
 
 TEST_SERVICE(spci_donate_check_upper_bound)
 {
+	struct spci_value ret = spci_msg_wait();
+	uint8_t *ptr;
+	uint8_t index;
+	void *recv_buf = SERVICE_RECV_BUFFER();
+	struct spci_memory_region *memory_region;
+	struct spci_memory_region_constituent *constituents;
+
 	exception_setup(NULL, exception_handler_yield_data_abort);
 
-	for (;;) {
-		struct spci_value ret = spci_msg_wait();
-		uint8_t *ptr;
-		uint8_t index;
-		void *recv_buf = SERVICE_RECV_BUFFER();
-		struct spci_memory_region *memory_region;
-		struct spci_memory_region_constituent *constituents;
+	EXPECT_EQ(ret.func, SPCI_MEM_DONATE_32);
 
-		exception_handler_reset();
+	memory_region = (struct spci_memory_region *)recv_buf;
+	constituents = spci_memory_region_get_constituents(memory_region);
 
-		EXPECT_EQ(ret.func, SPCI_MEM_DONATE_32);
+	/* Choose which constituent we want to test. */
+	index = *(uint8_t *)spci_memory_region_constituent_get_address(
+		&constituents[0]);
+	ptr = (uint8_t *)spci_memory_region_constituent_get_address(
+		&constituents[index]);
+	EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
 
-		memory_region = (struct spci_memory_region *)recv_buf;
-		constituents =
-			spci_memory_region_get_constituents(memory_region);
+	/*
+	 * Check that one cannot access out of bounds after donated
+	 * region. This should trigger the exception handler.
+	 */
+	ptr[PAGE_SIZE]++;
 
-		/* Choose which constituent we want to test. */
-		index = *(uint8_t *)spci_memory_region_constituent_get_address(
-			&constituents[0]);
-		ptr = (uint8_t *)spci_memory_region_constituent_get_address(
-			&constituents[index]);
-		EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
-
-		/*
-		 * Check that one cannot access out of bounds after donated
-		 * region. This should trigger the exception handler.
-		 */
-		ptr[PAGE_SIZE]++;
-	}
+	FAIL("Exception not generated by access out of bounds.");
 }
 
 TEST_SERVICE(spci_donate_check_lower_bound)
 {
+	struct spci_value ret = spci_msg_wait();
+	uint8_t *ptr;
+	uint8_t index;
+	void *recv_buf = SERVICE_RECV_BUFFER();
+	struct spci_memory_region *memory_region;
+	struct spci_memory_region_constituent *constituents;
+
 	exception_setup(NULL, exception_handler_yield_data_abort);
 
-	for (;;) {
-		struct spci_value ret = spci_msg_wait();
-		uint8_t *ptr;
-		uint8_t index;
-		void *recv_buf = SERVICE_RECV_BUFFER();
-		struct spci_memory_region *memory_region;
-		struct spci_memory_region_constituent *constituents;
+	EXPECT_EQ(ret.func, SPCI_MEM_DONATE_32);
 
-		exception_handler_reset();
+	memory_region = (struct spci_memory_region *)recv_buf;
+	constituents = spci_memory_region_get_constituents(memory_region);
 
-		EXPECT_EQ(ret.func, SPCI_MEM_DONATE_32);
+	/* Choose which constituent we want to test. */
+	index = *(uint8_t *)spci_memory_region_constituent_get_address(
+		&constituents[0]);
+	ptr = (uint8_t *)spci_memory_region_constituent_get_address(
+		&constituents[index]);
+	EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
 
-		memory_region = (struct spci_memory_region *)recv_buf;
-		constituents =
-			spci_memory_region_get_constituents(memory_region);
-
-		/* Choose which constituent we want to test. */
-		index = *(uint8_t *)spci_memory_region_constituent_get_address(
-			&constituents[0]);
-		ptr = (uint8_t *)spci_memory_region_constituent_get_address(
-			&constituents[index]);
-		EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32);
-
-		/*
-		 * Check that one cannot access out of bounds after donated
-		 * region. This should trigger the exception handler.
-		 */
-		ptr[-1]++;
-	}
+	/*
+	 * Check that one cannot access out of bounds after donated
+	 * region. This should trigger the exception handler.
+	 */
+	ptr[-1]++;
+	FAIL("Exception not generated by access out of bounds.");
 }
 
 /**