Add memory leak checking

Add checks that the vboot library does not leak memory. This works by
tracking VbExMalloc() calls and making sure that they have an associated
VbExFree().

Adjust host_signature to use VbExFree() instead of free(), so that this
scheme works correctly for existing code.

BUG=chrome-os-partner:21115
BRANCH=pit
TEST=FEATURES=test emerge-peach_pit vboot_reference

Change-Id: I6ccccfbcc162fc43fb75862cd0eddad78ce8b18a
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/66175
diff --git a/tests/test_common.h b/tests/test_common.h
index f5d73d1..4acf588 100644
--- a/tests/test_common.h
+++ b/tests/test_common.h
@@ -50,4 +50,7 @@
 #define COL_RED "\x1b[0;31m"
 #define COL_STOP "\x1b[m"
 
+/* Check that all memory allocations were freed */
+int vboot_api_stub_check_memory(void);
+
 #endif  /* VBOOT_REFERENCE_TEST_COMMON_H_ */
diff --git a/tests/vboot_api_devmode_tests.c b/tests/vboot_api_devmode_tests.c
index 61f0de1..925a146 100644
--- a/tests/vboot_api_devmode_tests.c
+++ b/tests/vboot_api_devmode_tests.c
@@ -328,6 +328,8 @@
 
   if (!gTestSuccess)
     error_code = 255;
+  if (vboot_api_stub_check_memory())
+    error_code = 255;
 
   return error_code;
 }
diff --git a/tests/vboot_api_firmware_tests.c b/tests/vboot_api_firmware_tests.c
index 4a97b93..9826e55 100644
--- a/tests/vboot_api_firmware_tests.c
+++ b/tests/vboot_api_firmware_tests.c
@@ -249,6 +249,8 @@
 
   VbSelectFirmwareTest();
 
+  if (vboot_api_stub_check_memory())
+    error_code = 255;
   if (!gTestSuccess)
     error_code = 255;
 
diff --git a/tests/vboot_api_kernel2_tests.c b/tests/vboot_api_kernel2_tests.c
index 323f5f6..1cb2d83 100644
--- a/tests/vboot_api_kernel2_tests.c
+++ b/tests/vboot_api_kernel2_tests.c
@@ -566,5 +566,8 @@
 	VbBootDevTest();
 	VbBootRecTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_api_kernel3_tests.c b/tests/vboot_api_kernel3_tests.c
index d885125..fa13d4e 100644
--- a/tests/vboot_api_kernel3_tests.c
+++ b/tests/vboot_api_kernel3_tests.c
@@ -361,5 +361,8 @@
 {
 	VbSoftwareSyncTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_api_kernel4_tests.c b/tests/vboot_api_kernel4_tests.c
index 82b1501..bb82702 100644
--- a/tests/vboot_api_kernel4_tests.c
+++ b/tests/vboot_api_kernel4_tests.c
@@ -245,5 +245,8 @@
 {
 	VbSlkTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_api_kernel_tests.c b/tests/vboot_api_kernel_tests.c
index 8cfc1cb..bccad0d 100644
--- a/tests/vboot_api_kernel_tests.c
+++ b/tests/vboot_api_kernel_tests.c
@@ -313,5 +313,8 @@
 {
 	VbTryLoadKernelTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_audio_tests.c b/tests/vboot_audio_tests.c
index d674931..bc6a3ef 100644
--- a/tests/vboot_audio_tests.c
+++ b/tests/vboot_audio_tests.c
@@ -224,5 +224,8 @@
   if (!gTestSuccess)
     error_code = 255;
 
+  if (vboot_api_stub_check_memory())
+    error_code = 255;
+
   return error_code;
 }
diff --git a/tests/vboot_common2_tests.c b/tests/vboot_common2_tests.c
index 564518a..220391c 100644
--- a/tests/vboot_common2_tests.c
+++ b/tests/vboot_common2_tests.c
@@ -102,7 +102,7 @@
 
 	RSAPublicKeyFree(rsa);
 	free(sig);
-	free(digest);
+	VbExFree(digest);
 }
 
 static void ReSignKernelPreamble(VbKernelPreambleHeader *h,
@@ -277,5 +277,8 @@
 		return -1;
 	}
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_common3_tests.c b/tests/vboot_common3_tests.c
index b5499f6..b5c0cc9 100644
--- a/tests/vboot_common3_tests.c
+++ b/tests/vboot_common3_tests.c
@@ -24,7 +24,7 @@
 				    SHA512_DIGEST_ALGORITHM);
 	Memcpy(GetSignatureData(&h->key_block_checksum), newchk,
 	       SHA512_DIGEST_SIZE);
-	free(newchk);
+	VbExFree(newchk);
 }
 
 static void KeyBlockVerifyTest(const VbPublicKey *public_key,
@@ -376,5 +376,8 @@
 		return -1;
 	}
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_common_tests.c b/tests/vboot_common_tests.c
index 852be79..7cfea90 100644
--- a/tests/vboot_common_tests.c
+++ b/tests/vboot_common_tests.c
@@ -237,5 +237,8 @@
 	PublicKeyTest();
 	VbSharedDataTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_display_tests.c b/tests/vboot_display_tests.c
index 8086d31..63f6890 100644
--- a/tests/vboot_display_tests.c
+++ b/tests/vboot_display_tests.c
@@ -264,5 +264,8 @@
 	DisplayKeyTest();
 	FontTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_firmware_tests.c b/tests/vboot_firmware_tests.c
index 724e7f8..651197c 100644
--- a/tests/vboot_firmware_tests.c
+++ b/tests/vboot_firmware_tests.c
@@ -451,6 +451,8 @@
 
   LoadFirmwareTest();
 
+  if (vboot_api_stub_check_memory())
+    error_code = 255;
   if (!gTestSuccess)
     error_code = 255;
 
diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c
index fdf81b7..ee164ce 100644
--- a/tests/vboot_kernel_tests.c
+++ b/tests/vboot_kernel_tests.c
@@ -567,5 +567,8 @@
 	InvalidParamsTest();
 	LoadKernelTest();
 
+	if (vboot_api_stub_check_memory())
+		return 255;
+
 	return gTestSuccess ? 0 : 255;
 }
diff --git a/tests/vboot_nvstorage_test.c b/tests/vboot_nvstorage_test.c
index 7ffec61..63e93d9 100644
--- a/tests/vboot_nvstorage_test.c
+++ b/tests/vboot_nvstorage_test.c
@@ -176,6 +176,8 @@
 
   VbNvStorageTest();
 
+  if (vboot_api_stub_check_memory())
+    error_code = 255;
   if (!gTestSuccess)
     error_code = 255;