Skip to content

Commit 0f45cad

Browse files
authored
Merge pull request #231 from lzha101/trts_red_zone
Fix red zone issue in continue_execution() Signed-off-by: Zhang Lili Z <lili.z.zhang@intel.com>
2 parents a31b0b6 + edb1845 commit 0f45cad

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

sdk/trts/linux/trts_pic.S

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,10 @@ DECLARE_LOCAL_FUNC continue_execution
552552
push %xax # push xax
553553
mov SE_WORDSIZE*1(%xcx), %xax
554554
push %xax # push xcx
555-
mov SE_WORDSIZE*4(%xcx), %xax
556-
sub $(SE_WORDSIZE), %xax # xax: xsp
555+
mov SE_WORDSIZE*4(%xcx), %xax # xax: xsp
556+
# x86_64 requires a 128-bytes red zone. We need to allocate buffer to avoid touching the red zone.
557+
sub $(SE_WORDSIZE + RED_ZONE_SIZE), %xax # allocate buffer to skip the red zone (if any) and save the xip
558+
# RED_ZONE_SIZE: 0(i386), 128-bytes(x86_64)
557559

558560
# restore registers except xax, xcx, xsp
559561
mov SE_WORDSIZE*2(%xcx), %xdx
@@ -589,4 +591,4 @@ DECLARE_LOCAL_FUNC continue_execution
589591
pop %xcx # restore xcx
590592
pop %xsp # xsp: xax
591593
xchg %xax, %xsp
592-
ret
594+
ret $(RED_ZONE_SIZE) # pop xip and red zone (if any)

sdk/trts/linux/trts_pic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include "linux/linux-regs.h"
4141
#include "rts_cmd.h"
42+
#include "trts_shared_constants.h"
4243

4344
#define SE_GUARD_PAGE_SIZE 0x10000
4445

@@ -55,7 +56,6 @@
5556
#define SGX_ERROR_ENCLAVE_CRASHED 0x000001006 // enclave is crashed
5657
#define SGX_ERROR_STACK_OVERRUN 0x000001009 // enclave is running out of stack
5758

58-
#define STATIC_STACK_SIZE 688
5959

6060
/* Thread Data
6161
* c.f. data structure defintion for thread_data_t in `rts.h'.

sdk/trts/trts_internal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
#define TRTS_INTERNAL_H
3333

3434
#include "util.h"
35-
36-
#define STATIC_STACK_SIZE 688
35+
#include "trts_shared_constants.h"
3736

3837
#define TD2TCS(td) ((const void *)(((thread_data_t*)(td))->stack_base_addr + (size_t)STATIC_STACK_SIZE + (size_t)SE_GUARD_PAGE_SIZE))
3938
#define TCS2CANARY(addr) ((size_t *)((size_t)(addr)-(size_t)SE_GUARD_PAGE_SIZE-(size_t)STATIC_STACK_SIZE+sizeof(size_t)))

sdk/trts/trts_shared_constants.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Intel Corporation nor the names of its
15+
* contributors may be used to endorse or promote products derived
16+
* from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
*/
31+
32+
/*
33+
* trts_shared_constants.h:
34+
* Defines constants shared in C/C++ code and assembly code.
35+
*/
36+
37+
#ifndef _TRTS_SHARED_CONSTANTS_H_
38+
#define _TRTS_SHARED_CONSTANTS_H_
39+
40+
41+
42+
#if defined(__x86_64) || defined(__x86_64__)
43+
#define RED_ZONE_SIZE 128
44+
#else
45+
#define RED_ZONE_SIZE 0
46+
#endif
47+
48+
49+
#define STATIC_STACK_SIZE 688
50+
51+
52+
#endif
53+

sdk/trts/trts_veh.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include "trts_inst.h"
4949
#include "util.h"
5050
#include "trts_util.h"
51+
#include "trts_shared_constants.h"
52+
5153

5254
typedef struct _handler_node_t
5355
{
@@ -337,10 +339,9 @@ extern "C" sgx_status_t trts_handle_exception(void *tcs)
337339
}
338340

339341
size = 0;
340-
#ifdef SE_GNU64
341-
size += 128; // x86_64 requires a 128-bytes red zone, which begins directly
342-
// after the return addr and includes func's arguments
343-
#endif
342+
// x86_64 requires a 128-bytes red zone, which begins directly
343+
// after the return addr and includes func's arguments
344+
size += RED_ZONE_SIZE;
344345

345346
// decrease the stack to give space for info
346347
size += sizeof(sgx_exception_info_t);

0 commit comments

Comments
 (0)