fix: vm unit tests heap allocation

From this change [1] vCPUs array within a VM is dynamically allocated
from the page pool.
For the VM unit tests the page pool is reinitialized for each test in
the test suite by the SetUp function. A VM created in one test has vCPUs
allocated from start of the page pool, and the subsequent test creating
a VM has its vCPUs allocated from the same range since the page pool has
been reinitialized. This is mainly because vm.vm_boot_order re-uses the
VM array as it is left by the vm.vm_unmap_hypervisor_not_mapped test.
This works for static data but not for data allocated from heap.
Fix by letting the first test from vm suite allocate the heap once, and
subsequent tests re-use it from where it was left leaving last test
execution.

[1] https://review.trustedfirmware.org/c/hafnium/hafnium/+/16778

Change-Id: If8b659239df653edfacc3173a7bb7d6b84bf1423
Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
Signed-off-by: J-Alves <joao.alves@arm.com>
diff --git a/src/vm_test.cc b/src/vm_test.cc
index 4b588c9..c3d8a4a 100644
--- a/src/vm_test.cc
+++ b/src/vm_test.cc
@@ -32,27 +32,30 @@
 using struct_vm = struct vm;
 using struct_vm_locked = struct vm_locked;
 
-constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 32;
+constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 64;
 const int TOP_LEVEL = arch_mm_stage2_max_level();
 
 class vm : public ::testing::Test
 {
+       protected:
+	static std::unique_ptr<uint8_t[]> test_heap;
+
+	struct mpool ppool;
+
 	void SetUp() override
 	{
-		/*
-		 * TODO: replace with direct use of stdlib allocator so
-		 * sanitizers are more effective.
-		 */
-		test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
-		mpool_init(&ppool, sizeof(struct mm_page_table));
-		mpool_add_chunk(&ppool, test_heap.get(), TEST_HEAP_SIZE);
+		if (!test_heap) {
+			/*
+			 * TODO: replace with direct use of stdlib allocator so
+			 * sanitizers are more effective.
+			 */
+			test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
+			mpool_init(&ppool, sizeof(struct mm_page_table));
+			mpool_add_chunk(&ppool, test_heap.get(),
+					TEST_HEAP_SIZE);
+		}
 	}
 
-	std::unique_ptr<uint8_t[]> test_heap;
-
-       protected:
-	struct mpool ppool;
-
        public:
 	static bool BootOrderSmallerThan(struct_vm *vm1, struct_vm *vm2)
 	{
@@ -60,6 +63,8 @@
 	}
 };
 
+std::unique_ptr<uint8_t[]> vm::test_heap;
+
 /**
  * If nothing is mapped, unmapping the hypervisor has no effect.
  */
@@ -88,7 +93,7 @@
 	struct_vm *vm_cur;
 	std::list<struct_vm *> expected_final_order;
 
-	EXPECT_FALSE(vm_get_first_boot());
+	EXPECT_TRUE(vm_get_first_boot() == NULL);
 
 	/*
 	 * Insertion when no call to "vm_update_boot" has been made yet.