 diff -Nur sdk/trts/trts_util.cpp sdk/trts/trts_util.cpp
 --- sdk/trts/trts_util.cpp
 +++ sdk/trts/trts_util.cpp
-@@ -34,6 +34,7 @@
+@@ -29,11 +29,19 @@
+  *
+  */
+ 
++#include <atomic>
+ 
+ #include "trts_util.h"
  #include "global_data.h"
  #include "util.h"
  #include "thread_data.h"
 +#include "trts_internal.h"
++
++std::atomic<int> ecall_count(0);
++
++void increase_ecall_count() { ecall_count++; }
++void decrease_ecall_count() { ecall_count--; }
++int get_ecall_count() { return ecall_count; }
  
  // No need to check the state of enclave or thread.
  // The functions should be called within an ECALL, so the enclave and thread must be initialized at that time.
-@@ -72,6 +73,47 @@
+@@ -72,6 +80,47 @@
      return heap_size;
  }
  

+ *      (data/bss/heap) and for current tcs(thread data/stack).
+ */
+void SGXAPI sgx_memory_layout(struct SgxMemoryLayout *memory_layout);
+
 #ifdef __cplusplus
 }

         }
         else if(index == ECMD_INIT_ENCLAVE)
         {
@@ -124,5 +123,10 @@
     {
         set_enclave_state(ENCLAVE_CRASHED);
     }
+    // The ecall has finished and is leaving the enclave.
+    if (index != ECMD_ORET)
+    {
+        decrease_ecall_count();
+    }
     return error;
 }
diff -Nur sdk/trts/trts_nsp_internal.cpp sdk/trts/trts_nsp_internal.cpp
--- sdk/trts/trts_nsp_internal.cpp
+++ sdk/trts/trts_nsp_internal.cpp
