Browse Source

[Pal/Linux-SGX] Keep 8MB of enclave heap for internal PAL objects

This commit adds a hack to keep some heap (currently 8MB) for internal
PAL objects allocated at runtime (recall that LibOS does not keep track
of PAL memory, so without this hack it could overwrite internal PAL
memory). This hack is probabilistic and brittle, but sufficient for now.
Dmitrii Kuvaiskii 4 years ago
parent
commit
50316ff984
1 changed files with 11 additions and 1 deletions
  1. 11 1
      Pal/src/host/Linux-SGX/db_main.c

+ 11 - 1
Pal/src/host/Linux-SGX/db_main.c

@@ -63,8 +63,18 @@ void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end,
 {
     *start = (PAL_PTR)pal_sec.heap_min;
     *end   = (PAL_PTR)get_enclave_heap_top();
+
+    /* FIXME: hack to keep some heap for internal PAL objects allocated at runtime (recall that
+     * LibOS does not keep track of PAL memory, so without this hack it could overwrite internal
+     * PAL memory). This hack is probabilistic and brittle. */
+    *end = SATURATED_P_SUB(*end, 2 * 1024 * g_page_size, *start);  /* 8MB reserved for PAL stuff */
+    if (*end <= *start) {
+        SGX_DBG(DBG_E, "Not enough enclave memory, please increase enclave size!\n");
+        ocall_exit(1, /*is_exitgroup=*/true);
+    }
+
     *hole_start = SATURATED_P_SUB(pal_sec.exec_addr, MEMORY_GAP, *start);
-    *hole_end = SATURATED_P_ADD(pal_sec.exec_addr + pal_sec.exec_size, MEMORY_GAP, *end);
+    *hole_end   = SATURATED_P_ADD(pal_sec.exec_addr + pal_sec.exec_size, MEMORY_GAP, *end);
 }
 
 PAL_NUM _DkGetProcessId (void)