Skip to content

Commit ad24251

Browse files
AdrianCXnikic
authored andcommitted
[rust] Changes needed for 'x86_64-fortanix-unknown-sgx' nightly target.
Code is guarded via defines to enable only if 'RUST_SGX' is present. Main logic is in libunwind/src/AddressSpace.hpp We use 6 symbols to figure out where eh_frame / eh_frame_hdr is at runtime when loaded in an SGX enclave. (EH symbols + IMAGE base) These are set by 'fortanix-sgx-tools'. As notes: - Target above at the moment uses a pre-compiled libunwind.a from forked repo. - Goal of these changes is to use official llvm with patch. - Changes in rust-lang to use this are planned if/when this is accepted. - Ticket: fortanix/rust-sgx#174 - Original ported changes: llvm/llvm-project@release/5.x...fortanix:release/5.x
1 parent 6be9ad8 commit ad24251

9 files changed

+312
-6
lines changed

libunwind/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ if (LIBUNWIND_ENABLE_ASSERTIONS)
236236

237237
# On Release builds cmake automatically defines NDEBUG, so we
238238
# explicitly undefine it:
239-
if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
239+
if ((NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") AND (NOT RUST_SGX))
240240
add_compile_flags(-UNDEBUG)
241241
endif()
242242
else()

libunwind/README_RUST_SGX.md

Lines changed: 22 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,8 @@ libunwind specific options
148148

149149
Path where built libunwind libraries should be installed. If a relative path,
150150
relative to ``CMAKE_INSTALL_PREFIX``.
151+
152+
.. option:: LIBUNWIND_ENABLE_RUST_SGX:BOOL
153+
154+
**Default**: ``OFF``
155+

libunwind/src/AddressSpace.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ namespace libunwind {
9494
// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;
9595
// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;
9696

97+
#if !defined(RUST_SGX)
9798
extern char __eh_frame_start;
9899
extern char __eh_frame_end;
99100

@@ -102,6 +103,15 @@ extern char __eh_frame_hdr_start;
102103
extern char __eh_frame_hdr_end;
103104
#endif
104105

106+
#elif defined(RUST_SGX)
107+
extern "C" char IMAGE_BASE;
108+
extern "C" uint64_t EH_FRM_HDR_OFFSET;
109+
extern "C" uint64_t EH_FRM_HDR_LEN;
110+
extern "C" uint64_t EH_FRM_OFFSET;
111+
extern "C" uint64_t EH_FRM_LEN;
112+
#endif
113+
114+
105115
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
106116

107117
// When statically linked on bare-metal, the symbols for the EH table are looked
@@ -487,6 +497,10 @@ static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,
487497
#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
488498

489499

500+
#if defined(RUST_SGX)
501+
extern "C" char IMAGE_BASE;
502+
#endif
503+
490504
inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
491505
UnwindInfoSections &info) {
492506
#ifdef __APPLE__
@@ -520,6 +534,8 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
520534
#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
521535
info.dso_base = 0;
522536
// Bare metal is statically linked, so no need to ask the dynamic loader
537+
538+
#if !defined(RUST_SGX)
523539
info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start);
524540
info.dwarf_section = (uintptr_t)(&__eh_frame_start);
525541
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
@@ -530,6 +546,17 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
530546
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
531547
(void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
532548
#endif
549+
550+
#elif defined(RUST_SGX)
551+
info.dwarf_section = (uintptr_t)EH_FRM_OFFSET + (uintptr_t)(&IMAGE_BASE);
552+
info.dwarf_section_length = (uintptr_t)EH_FRM_LEN;
553+
#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
554+
info.dwarf_index_section = (uintptr_t)EH_FRM_HDR_OFFSET + (uintptr_t)(&IMAGE_BASE);
555+
info.dwarf_index_section_length = (uintptr_t)EH_FRM_HDR_LEN;
556+
#endif
557+
558+
#endif
559+
533560
if (info.dwarf_section_length)
534561
return true;
535562
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)

libunwind/src/CMakeLists.txt

Lines changed: 43 additions & 4 deletions
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
@@ -17,9 +21,6 @@ set(LIBUNWIND_C_SOURCES
1721
UnwindLevel1-gcc-ext.c
1822
Unwind-sjlj.c
1923
)
20-
set_source_files_properties(${LIBUNWIND_C_SOURCES}
21-
PROPERTIES
22-
COMPILE_FLAGS "-std=c99")
2324

2425
set(LIBUNWIND_ASM_SOURCES
2526
UnwindRegistersRestore.S
@@ -59,6 +60,44 @@ if (MSVC_IDE)
5960
source_group("Header Files" FILES ${LIBUNWIND_HEADERS})
6061
endif()
6162

63+
if (RUST_SGX)
64+
# Compile Flags
65+
add_definitions(-DRUST_SGX)
66+
add_definitions(-D__NO_STRING_INLINES)
67+
add_definitions(-D__NO_MATH_INLINES)
68+
add_definitions(-D_LIBUNWIND_IS_BAREMETAL)
69+
# Can't use add_definitions because CMake will reorder these arguments
70+
list(APPEND LIBUNWIND_COMPILE_FLAGS -U_FORTIFY_SOURCE)
71+
list(APPEND LIBUNWIND_COMPILE_FLAGS -D_FORTIFY_SOURCE=0)
72+
73+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fno-stack-protector)
74+
list(APPEND LIBUNWIND_COMPILE_FLAGS -ffreestanding)
75+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fexceptions)
76+
# Avoid too new relocation types being emitted, which might prevent linking
77+
# on older platforms.
78+
#
79+
# See https://github.com/rust-lang/rust/issues/34978
80+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
81+
list(APPEND LIBUNWIND_COMPILE_FLAGS -Wa,-mrelax-relocations=no)
82+
else()
83+
list(APPEND LIBUNWIND_COMPILE_FLAGS)
84+
endif()
85+
86+
# Sources
87+
list(APPEND LIBUNWIND_HEADERS UnwindRustSgx.h)
88+
list(APPEND LIBUNWIND_C_SOURCES UnwindRustSgx.c)
89+
endif()
90+
91+
92+
set_source_files_properties(${LIBUNWIND_C_SOURCES}
93+
PROPERTIES
94+
COMPILE_FLAGS "-std=c99")
95+
96+
# See add_asm_sources() in compiler-rt for explanation of this workaround.
97+
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
98+
set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C)
99+
endif()
100+
62101
set(LIBUNWIND_SOURCES
63102
${LIBUNWIND_CXX_SOURCES}
64103
${LIBUNWIND_C_SOURCES}
@@ -73,7 +112,7 @@ else()
73112
add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
74113
endif()
75114
add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
76-
if (LIBUNWIND_ENABLE_THREADS)
115+
if (LIBUNWIND_ENABLE_THREADS AND (NOT RUST_SGX))
77116
add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
78117
add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
79118
endif()

libunwind/src/RWMutex.hpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 125 additions & 0 deletions
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 = (struct _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+
}

libunwind/src/UnwindRustSgx.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===--------------------- UnwindRustSgx.h ----------------------------------===//
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+
#if !defined(UNWIND_RUST_SGX_H)
12+
#define UNWIND_RUST_SGX_H
13+
14+
#ifdef RUST_SGX
15+
16+
#undef _GNU_SOURCE
17+
#define _GNU_SOURCE
18+
#include <link.h>
19+
#include <stdarg.h>
20+
#include <string.h>
21+
#include <stdint.h>
22+
23+
// We have to use RWLock from rust repo, it is defined in:
24+
// src/libstd/sys/sgx/rwlock.rs.
25+
// rwlock.rs has a compile time check to ensure the size and alignment matches
26+
// the Rust definition.
27+
typedef struct {
28+
void *opaque;
29+
} RWLock;
30+
31+
#define RWLOCK_INIT \
32+
{ (void *)0 }
33+
34+
// These are the functions exposed by SGX-Rust.
35+
// The rust changes are available at:
36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
int __rust_rwlock_rdlock(RWLock *rwlock);
40+
int __rust_rwlock_wrlock(RWLock *rwlock);
41+
int __rust_rwlock_unlock(RWLock *rwlock);
42+
unsigned char *__rust_c_alloc(size_t, size_t);
43+
void __rust_c_dealloc(unsigned char *, size_t, size_t);
44+
__attribute__((noreturn)) void __rust_abort(void);
45+
unsigned char *__rust_encl_address(size_t);
46+
47+
#ifndef NDEBUG
48+
void __rust_print_err(uint8_t *m, int s);
49+
#endif
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif
54+
55+
#define abort __rust_abort
56+
57+
#undef pthread_rwlock_t
58+
#undef pthread_rwlock_rdlock
59+
#undef pthread_rwlock_wrlock
60+
#undef pthread_rwlock_unlock
61+
#undef PTHREAD_RWLOCK_INITIALIZER
62+
63+
#define pthread_rwlock_t RWLock
64+
#define pthread_rwlock_rdlock __rust_rwlock_rdlock
65+
#define pthread_rwlock_wrlock __rust_rwlock_wrlock
66+
#define pthread_rwlock_unlock __rust_rwlock_unlock
67+
#define PTHREAD_RWLOCK_INITIALIZER RWLOCK_INIT
68+
69+
#define malloc libuw_malloc
70+
#define free libuw_free
71+
72+
#ifdef __cplusplus
73+
extern "C" {
74+
#endif
75+
76+
void *libuw_malloc(size_t size);
77+
void libuw_free(void *p);
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
83+
#endif
84+
#endif

0 commit comments

Comments
 (0)