Browse Source

Fix red zone issue in continue_execution(). And create a header file for shared constants between assembly code and C code.

Signed-off-by: Zhang Lili Z <lili.z.zhang@intel.com>
Zhang Lili Z 6 years ago
parent
commit
edb18459b8

+ 5 - 3
sdk/trts/linux/trts_pic.S

@@ -552,8 +552,10 @@ DECLARE_LOCAL_FUNC continue_execution
     push    %xax                       # push xax
     mov     SE_WORDSIZE*1(%xcx), %xax
     push    %xax                       # push xcx
-    mov     SE_WORDSIZE*4(%xcx), %xax
-    sub     $(SE_WORDSIZE), %xax       # xax: xsp
+    mov     SE_WORDSIZE*4(%xcx), %xax  # xax: xsp 
+# x86_64 requires a 128-bytes red zone. We need to allocate buffer to avoid touching the red zone.
+    sub     $(SE_WORDSIZE + RED_ZONE_SIZE), %xax  # allocate buffer to skip the red zone (if any) and save the xip
+                                                  # RED_ZONE_SIZE: 0(i386), 128-bytes(x86_64) 
 
 # restore registers except xax, xcx, xsp
     mov     SE_WORDSIZE*2(%xcx), %xdx
@@ -589,4 +591,4 @@ DECLARE_LOCAL_FUNC continue_execution
     pop     %xcx                       # restore xcx
     pop     %xsp                       # xsp: xax
     xchg    %xax, %xsp
-    ret
+    ret     $(RED_ZONE_SIZE)           # pop xip and red zone (if any)

+ 1 - 1
sdk/trts/linux/trts_pic.h

@@ -39,6 +39,7 @@
 
 #include "linux/linux-regs.h"
 #include "rts_cmd.h"
+#include "trts_shared_constants.h"
 
 #define SE_GUARD_PAGE_SIZE 0x10000
 
@@ -55,7 +56,6 @@
 #define SGX_ERROR_ENCLAVE_CRASHED     0x000001006 // enclave is crashed
 #define SGX_ERROR_STACK_OVERRUN       0x000001009 // enclave is running out of stack
 
-#define STATIC_STACK_SIZE   688
 
 /* Thread Data
  * c.f. data structure defintion for thread_data_t in `rts.h'.

+ 1 - 2
sdk/trts/trts_internal.h

@@ -32,8 +32,7 @@
 #define TRTS_INTERNAL_H
 
 #include "util.h"
-
-#define STATIC_STACK_SIZE   688
+#include "trts_shared_constants.h"
 
 #define TD2TCS(td) ((const void *)(((thread_data_t*)(td))->stack_base_addr + (size_t)STATIC_STACK_SIZE + (size_t)SE_GUARD_PAGE_SIZE))
 #define TCS2CANARY(addr)    ((size_t *)((size_t)(addr)-(size_t)SE_GUARD_PAGE_SIZE-(size_t)STATIC_STACK_SIZE+sizeof(size_t)))

+ 53 - 0
sdk/trts/trts_shared_constants.h

@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in
+ *     the documentation and/or other materials provided with the
+ *     distribution.
+ *   * Neither the name of Intel Corporation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/*
+ * trts_shared_constants.h:
+ *    Defines constants shared in C/C++ code and assembly code.
+ */
+
+#ifndef _TRTS_SHARED_CONSTANTS_H_
+#define _TRTS_SHARED_CONSTANTS_H_
+
+
+
+#if defined(__x86_64) || defined(__x86_64__)
+#define RED_ZONE_SIZE   128
+#else
+#define RED_ZONE_SIZE   0
+#endif
+
+
+#define STATIC_STACK_SIZE   688
+
+
+#endif
+

+ 5 - 4
sdk/trts/trts_veh.cpp

@@ -48,6 +48,8 @@
 #include "trts_inst.h"
 #include "util.h"
 #include "trts_util.h"
+#include "trts_shared_constants.h"
+
 
 typedef struct _handler_node_t
 {
@@ -337,10 +339,9 @@ extern "C" sgx_status_t trts_handle_exception(void *tcs)
     }
 
     size = 0;
-#ifdef SE_GNU64
-    size += 128; // x86_64 requires a 128-bytes red zone, which begins directly
-                 // after the return addr and includes func's arguments
-#endif
+    // x86_64 requires a 128-bytes red zone, which begins directly
+    // after the return addr and includes func's arguments
+    size += RED_ZONE_SIZE;
 
     // decrease the stack to give space for info
     size += sizeof(sgx_exception_info_t);