refactor(manifest): logic to calculate memory region limit

This patch changes the way the limit of a memory region is recorded such
that it is an inclusive upper limit instead of an exclusive one. This
makes the check_and_record_mem_regions function easier to follow.

Also add unit test to check the case where memory region limits overlap.

Signed-off-by: Varun Wadekar <vwadekar@nvidia.com>
Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
Change-Id: I04c9569bda4cb27b212bdbc30ff27a1d9d34f549
diff --git a/src/manifest.c b/src/manifest.c
index 7f01179..be9689a 100644
--- a/src/manifest.c
+++ b/src/manifest.c
@@ -417,7 +417,7 @@
 static bool check_and_record_mem_regions(uintptr_t base_address,
 					 uint32_t page_count)
 {
-	uintptr_t limit = base_address + page_count * PAGE_SIZE;
+	uintptr_t limit = base_address + (page_count * PAGE_SIZE) - 1U;
 
 	for (size_t i = 0; i < allocated_mem_regions_index; i++) {
 		uintptr_t mem_region_base = manifest_data->mem_regions[i].base;
@@ -425,8 +425,8 @@
 			manifest_data->mem_regions[i].limit;
 
 		if ((base_address >= mem_region_base &&
-		     base_address < mem_region_limit) ||
-		    (limit < mem_region_limit && limit >= mem_region_base)) {
+		     base_address <= mem_region_limit) ||
+		    (limit <= mem_region_limit && limit >= mem_region_base)) {
 			dlog_error(
 				"Overlapping memory regions\n"
 				"New Region %#x - %#x\n"
diff --git a/src/manifest_test.cc b/src/manifest_test.cc
index cc78660..04d97cf 100644
--- a/src/manifest_test.cc
+++ b/src/manifest_test.cc
@@ -29,7 +29,7 @@
 
 using struct_manifest = struct manifest;
 
-constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 32;
+constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 64;
 
 template <typename T>
 void exec(const char *program, const char *args[], const T &stdin,
@@ -1211,6 +1211,31 @@
 		  MANIFEST_ERROR_MEM_REGION_OVERLAP);
 	manifest_dealloc();
 
+	/* clang-format off */
+	dtb = ManifestDtBuilder()
+		.FfaValidManifest()
+		.StartChild("memory-regions")
+			.Compatible({ "arm,ffa-manifest-memory-regions" })
+			.Label("rx")
+			.StartChild("rx")
+				.Description("rx-buffer")
+				.Property("base-address", "<0x7301000>")
+				.Property("pages-count", "<1>")
+				.Property("attributes", "<1>")
+			.EndChild()
+			.Label("tx")
+			.StartChild("tx")
+				.Description("tx-buffer")
+				.Property("base-address", "<0x7300000>")
+				.Property("pages-count", "<2>")
+				.Property("attributes", "<3>")
+			.EndChild()
+		.EndChild()
+		.Build();
+	/* clang-format on */
+	ASSERT_EQ(ffa_manifest_from_vec(&m, dtb),
+		  MANIFEST_ERROR_MEM_REGION_OVERLAP);
+
 	/* Different RXTX buffer sizes */
 	/* clang-format off */
 	dtb = ManifestDtBuilder()