Skip to content

Commit 5992729

Browse files
authored
Merge pull request #57 from AdrianCX/rustc/9.0-2019-12-19-rust-sgx-libunwind
libunwind changes needed to run code in sgx environment via rust-sgx.
2 parents e6e8011 + 2eeda8e commit 5992729

9 files changed

+319
-10
lines changed

libunwind/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ if (LIBUNWIND_ENABLE_ASSERTIONS)
335335

336336
# On Release builds cmake automatically defines NDEBUG, so we
337337
# explicitly undefine it:
338-
if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
338+
if ((uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") AND (NOT RUST_SGX))
339339
add_compile_flags(-UNDEBUG)
340340
endif()
341341
else()

libunwind/README_RUST_SGX.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Libunwind customizations for linking with x86_64-fortanix-unknown-sgx Rust target.
2+
3+
## Description
4+
### Initial Fork
5+
Initial Fork has been made from 5.0 release of llvm (commit: 6a075b6de4)
6+
### Detailed Description
7+
#### Header files that we do not include for this target
8+
1. pthread.h
9+
#### Library that we do not link to for this target.
10+
1. pthread (Locks used by libunwind is provided by rust stdlib for this target)
11+
12+
## Building unwind for rust-sgx target
13+
### Generate Make files:
14+
* `cd where you want to build libunwind`
15+
* `mkdir build`
16+
* `cd build`
17+
* `cmake -DCMAKE_BUILD_TYPE="RELEASE" -DRUST_SGX=1 -G "Unix Makefiles" -DLLVM_ENABLE_WARNINGS=1 -DLIBUNWIND_ENABLE_PEDANTIC=0 -DLLVM_PATH=<path/to/llvm> <path/to/libunwind>`
18+
* `"DEBUG"` could be used instead of `"RELEASE"` to enable debug logs of libunwind.
19+
20+
### Build:
21+
* `make unwind_static`
22+
* `build/lib/` will have the built library.

libunwind/docs/BuildingLibunwind.rst

+5
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,8 @@ libunwind specific options
159159
.. option:: LIBUNWIND_SYSROOT
160160

161161
Sysroot for cross compiling
162+
163+
.. option:: LIBUNWIND_ENABLE_RUST_SGX:BOOL
164+
165+
**Default**: ``OFF``
166+

libunwind/src/AddressSpace.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ namespace libunwind {
124124
// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;
125125
// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;
126126

127+
#if !defined(RUST_SGX)
127128
extern char __eh_frame_start;
128129
extern char __eh_frame_end;
129130

@@ -132,6 +133,15 @@ extern char __eh_frame_hdr_start;
132133
extern char __eh_frame_hdr_end;
133134
#endif
134135

136+
#elif defined(RUST_SGX)
137+
extern "C" char IMAGE_BASE;
138+
extern "C" uint64_t EH_FRM_HDR_OFFSET;
139+
extern "C" uint64_t EH_FRM_HDR_LEN;
140+
extern "C" uint64_t EH_FRM_OFFSET;
141+
extern "C" uint64_t EH_FRM_LEN;
142+
#endif
143+
144+
135145
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
136146

137147
// When statically linked on bare-metal, the symbols for the EH table are looked
@@ -392,6 +402,10 @@ LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
392402
return result;
393403
}
394404

405+
#if defined(RUST_SGX)
406+
extern "C" char IMAGE_BASE;
407+
#endif
408+
395409
inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
396410
UnwindInfoSections &info) {
397411
#ifdef __APPLE__
@@ -408,6 +422,8 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
408422
}
409423
#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
410424
// Bare metal is statically linked, so no need to ask the dynamic loader
425+
426+
#if !defined(RUST_SGX)
411427
info.dwarf_section_length = (uintptr_t)(&__eh_frame_end - &__eh_frame_start);
412428
info.dwarf_section = (uintptr_t)(&__eh_frame_start);
413429
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
@@ -418,6 +434,17 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
418434
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
419435
(void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
420436
#endif
437+
438+
#elif defined(RUST_SGX)
439+
info.dwarf_section = (uintptr_t)EH_FRM_OFFSET + (uintptr_t)(&IMAGE_BASE);
440+
info.dwarf_section_length = (uintptr_t)EH_FRM_LEN;
441+
#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
442+
info.dwarf_index_section = (uintptr_t)EH_FRM_HDR_OFFSET + (uintptr_t)(&IMAGE_BASE);
443+
info.dwarf_index_section_length = (uintptr_t)EH_FRM_HDR_LEN;
444+
#endif
445+
446+
#endif
447+
421448
if (info.dwarf_section_length)
422449
return true;
423450
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)

libunwind/src/CMakeLists.txt

+41-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Get sources
22

3+
enable_language(C CXX ASM)
4+
5+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6+
37
set(LIBUNWIND_CXX_SOURCES
48
libunwind.cpp
59
Unwind-EHABI.cpp
@@ -16,17 +20,11 @@ set(LIBUNWIND_C_SOURCES
1620
UnwindLevel1-gcc-ext.c
1721
Unwind-sjlj.c
1822
)
19-
set_source_files_properties(${LIBUNWIND_C_SOURCES}
20-
PROPERTIES
21-
COMPILE_FLAGS "-std=c99")
2223

2324
set(LIBUNWIND_ASM_SOURCES
2425
UnwindRegistersRestore.S
2526
UnwindRegistersSave.S
2627
)
27-
set_source_files_properties(${LIBUNWIND_ASM_SOURCES}
28-
PROPERTIES
29-
LANGUAGE C)
3028

3129
set(LIBUNWIND_HEADERS
3230
AddressSpace.hpp
@@ -55,6 +53,42 @@ if (MSVC_IDE)
5553
source_group("Header Files" FILES ${LIBUNWIND_HEADERS})
5654
endif()
5755

56+
if (RUST_SGX)
57+
# Compile Flags
58+
add_definitions(-DRUST_SGX)
59+
add_definitions(-D__NO_STRING_INLINES)
60+
add_definitions(-D__NO_MATH_INLINES)
61+
add_definitions(-D_LIBUNWIND_IS_BAREMETAL)
62+
# Can't use add_definitions because CMake will reorder these arguments
63+
list(APPEND LIBUNWIND_COMPILE_FLAGS -U_FORTIFY_SOURCE)
64+
list(APPEND LIBUNWIND_COMPILE_FLAGS -D_FORTIFY_SOURCE=0)
65+
66+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fno-stack-protector)
67+
list(APPEND LIBUNWIND_COMPILE_FLAGS -ffreestanding)
68+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fexceptions)
69+
# Avoid too new relocation types being emitted, which might prevent linking
70+
# on older platforms.
71+
#
72+
# See https://github.com/rust-lang/rust/issues/34978
73+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
74+
list(APPEND LIBUNWIND_COMPILE_FLAGS -Wa,-mrelax-relocations=no)
75+
else()
76+
list(APPEND LIBUNWIND_COMPILE_FLAGS)
77+
endif()
78+
79+
# Sources
80+
list(APPEND LIBUNWIND_HEADERS UnwindRustSgx.h)
81+
list(APPEND LIBUNWIND_C_SOURCES UnwindRustSgx.c)
82+
endif()
83+
84+
85+
set_source_files_properties(${LIBUNWIND_C_SOURCES}
86+
PROPERTIES
87+
COMPILE_FLAGS "-std=c99")
88+
set_source_files_properties(${LIBUNWIND_ASM_SOURCES}
89+
PROPERTIES
90+
LANGUAGE ASM)
91+
5892
set(LIBUNWIND_SOURCES
5993
${LIBUNWIND_CXX_SOURCES}
6094
${LIBUNWIND_C_SOURCES}
@@ -69,7 +103,7 @@ else()
69103
add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
70104
endif()
71105
add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
72-
if (LIBUNWIND_ENABLE_THREADS)
106+
if (LIBUNWIND_ENABLE_THREADS AND (NOT RUST_SGX))
73107
add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
74108
add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
75109
endif()

libunwind/src/RWMutex.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#if defined(_WIN32)
1717
#include <windows.h>
18-
#elif !defined(_LIBUNWIND_HAS_NO_THREADS)
18+
#elif !defined(_LIBUNWIND_HAS_NO_THREADS) && !defined(RUST_SGX)
1919
#include <pthread.h>
2020
#if defined(__ELF__) && defined(_LIBUNWIND_LINK_PTHREAD_LIB)
2121
#pragma comment(lib, "pthread")

libunwind/src/UnwindRustSgx.c

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//===--------------------- UnwindRustSgx.c ----------------------------------===//
2+
//
3+
//// The LLVM Compiler Infrastructure
4+
////
5+
//// This file is dual licensed under the MIT and the University of Illinois Open
6+
//// Source Licenses. See LICENSE.TXT for details.
7+
////
8+
////
9+
////===----------------------------------------------------------------------===//
10+
11+
#define _GNU_SOURCE
12+
#include <link.h>
13+
14+
#include <elf.h>
15+
#include <stdarg.h>
16+
#include <stdio.h>
17+
#include <stddef.h>
18+
#include "UnwindRustSgx.h"
19+
20+
#define max_log 256
21+
22+
__attribute__((weak)) struct _IO_FILE *stderr = (_IO_FILE *)-1;
23+
24+
static int vwrite_err(const char *format, va_list ap)
25+
{
26+
int len = 0;
27+
#ifndef NDEBUG
28+
char s[max_log];
29+
s[0]='\0';
30+
len = vsnprintf(s, max_log, format, ap);
31+
__rust_print_err((uint8_t *)s, len);
32+
#endif
33+
return len;
34+
}
35+
36+
static int write_err(const char *format, ...)
37+
{
38+
int ret;
39+
va_list args;
40+
va_start(args, format);
41+
ret = vwrite_err(format, args);
42+
va_end(args);
43+
44+
45+
return ret;
46+
}
47+
48+
__attribute__((weak)) int fprintf (FILE *__restrict __stream,
49+
const char *__restrict __format, ...)
50+
{
51+
52+
int ret;
53+
if (__stream != stderr) {
54+
write_err("Rust SGX Unwind supports only writing to stderr\n");
55+
return -1;
56+
} else {
57+
va_list args;
58+
ret = 0;
59+
va_start(args, __format);
60+
ret += vwrite_err(__format, args);
61+
va_end(args);
62+
}
63+
64+
return ret;
65+
}
66+
67+
__attribute__((weak)) int fflush (FILE *__stream)
68+
{
69+
// We do not need to do anything here.
70+
return 0;
71+
}
72+
73+
__attribute__((weak)) void __assert_fail(const char * assertion,
74+
const char * file,
75+
unsigned int line,
76+
const char * function)
77+
{
78+
write_err("%s:%d %s %s\n", file, line, function, assertion);
79+
abort();
80+
}
81+
82+
// We do not report stack over flow detected.
83+
// Calling write_err uses more stack due to the way we have implemented it.
84+
// With possible enabling of stack probes, we should not
85+
// get into __stack_chk_fail() at all.
86+
__attribute__((weak)) void __stack_chk_fail() {
87+
abort();
88+
}
89+
90+
/*
91+
* Below are defined for all executibles compiled for
92+
* x86_64-fortanix-unknown-sgx rust target.
93+
* Ref: rust/src/libstd/sys/sgx/abi/entry.S
94+
*/
95+
96+
struct libwu_rs_alloc_meta {
97+
size_t alloc_size;
98+
// Should we put a signatre guard before ptr for oob access?
99+
unsigned char ptr[0];
100+
};
101+
102+
#define META_FROM_PTR(__PTR) (struct libwu_rs_alloc_meta *) \
103+
((unsigned char *)__PTR - offsetof(struct libwu_rs_alloc_meta, ptr))
104+
105+
void *libuw_malloc(size_t size)
106+
{
107+
struct libwu_rs_alloc_meta *meta;
108+
size_t alloc_size = size + sizeof(struct libwu_rs_alloc_meta);
109+
meta = (void *)__rust_c_alloc(alloc_size, sizeof(size_t));
110+
if (!meta) {
111+
return NULL;
112+
}
113+
meta->alloc_size = alloc_size;
114+
return (void *)meta->ptr;
115+
}
116+
117+
void libuw_free(void *p)
118+
{
119+
struct libwu_rs_alloc_meta *meta;
120+
if (!p) {
121+
return;
122+
}
123+
meta = META_FROM_PTR(p);
124+
__rust_c_dealloc((unsigned char *)meta, meta->alloc_size, sizeof(size_t));
125+
}

0 commit comments

Comments
 (0)