[RFC] ESYS: Add esys_free helper function
Add esys_free, a wrapper around free(), to ESYS when building on Windows.
Having a wrapper around free is best practice when building libraries
on Windows; this is because there is a high chance that the caller
program will be built using a different version of the C runtime
than the library, which means that the program and the library will have
separate heaps. This is an issue because many of the ESYS functions
allocate memory for the caller and expect the caller to free the memory,
but in the case where the caller was built with a different version of
the C runtime, the caller will not be able to free the memory because
it does not have access to the esys heap. esys_free allows the caller
program to have the library free any memory that the caller didn't allocate.
Signed-off-by: David Maria <davidjmaria@fb.com>
diff --git a/include/tss2/tss2_esys.h b/include/tss2/tss2_esys.h
index 63870ed..fcd374f 100644
--- a/include/tss2/tss2_esys.h
+++ b/include/tss2/tss2_esys.h
@@ -3220,6 +3220,13 @@
ESYS_CONTEXT *esysContext,
TPM2B_DATA **outputData);
+/*
+ * TPM 2.0 ESAPI Helper Functions
+ */
+void
+esys_free(
+ void *__ptr);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/tss2-esys.def b/lib/tss2-esys.def
index 3f1e8c4..4ba875e 100644
--- a/lib/tss2-esys.def
+++ b/lib/tss2-esys.def
@@ -89,6 +89,7 @@
Esys_FirmwareRead_Async
Esys_FirmwareRead_Finish
Esys_FlushContext
+ esys_free
Esys_FlushContext_Async
Esys_FlushContext_Finish
Esys_GetCapability
diff --git a/src/tss2-esys/esys_free.c b/src/tss2-esys/esys_free.c
new file mode 100644
index 0000000..b6cb5ef
--- /dev/null
+++ b/src/tss2-esys/esys_free.c
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: BSD-2 */
+#include <stdlib.h>
+
+/*
+ * esys_free is a helper function that is a wrapper around free().
+ * This allows programs that are built using a different version
+ * of the C runtime to free memory that has been allocated by the
+ * esys library on Windows.
+ */
+void esys_free(void *__ptr) {
+ if (__ptr != NULL) {
+ free(__ptr);
+ }
+}
\ No newline at end of file
diff --git a/src/tss2-esys/tss2-esys.vcxproj b/src/tss2-esys/tss2-esys.vcxproj
index 8a56b02..bbaa28b 100644
--- a/src/tss2-esys/tss2-esys.vcxproj
+++ b/src/tss2-esys/tss2-esys.vcxproj
@@ -256,6 +256,7 @@
<ClCompile Include="esys_context.c" />
<ClCompile Include="esys_crypto.c" />
<ClCompile Include="esys_crypto_ossl.c" />
+ <ClCompile Include="esys_free.c" />
<ClCompile Include="esys_iutil.c" />
<ClCompile Include="esys_mu.c" />
<ClCompile Include="esys_tcti_default.c" />
@@ -274,4 +275,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/src/tss2-esys/tss2-esys.vcxproj.filters b/src/tss2-esys/tss2-esys.vcxproj.filters
index b458b72..0159194 100644
--- a/src/tss2-esys/tss2-esys.vcxproj.filters
+++ b/src/tss2-esys/tss2-esys.vcxproj.filters
@@ -378,6 +378,9 @@
<ClCompile Include="esys_crypto_ossl.c">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="esys_free.c">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="esys_crypto.h">