Skip to content

Commit b2653db

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 d43049b commit b2653db

9 files changed

+322
-6
lines changed

libunwind/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ if (LIBUNWIND_ENABLE_ASSERTIONS)
264264

265265
# On Release builds cmake automatically defines NDEBUG, so we
266266
# explicitly undefine it:
267-
if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
267+
if ((NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") AND (NOT RUST_SGX))
268268
add_compile_flags(-UNDEBUG)
269269
endif()
270270
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
@@ -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

+27
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ char *getFuncNameFromTBTable(uintptr_t pc, uint16_t &NameLen,
9090
// __eh_frame_hdr_start = SIZEOF(.eh_frame_hdr) > 0 ? ADDR(.eh_frame_hdr) : 0;
9191
// __eh_frame_hdr_end = SIZEOF(.eh_frame_hdr) > 0 ? . : 0;
9292

93+
#if !defined(RUST_SGX)
9394
extern char __eh_frame_start;
9495
extern char __eh_frame_end;
9596

@@ -98,6 +99,15 @@ extern char __eh_frame_hdr_start;
9899
extern char __eh_frame_hdr_end;
99100
#endif
100101

102+
#elif defined(RUST_SGX)
103+
extern "C" char IMAGE_BASE;
104+
extern "C" uint64_t EH_FRM_HDR_OFFSET;
105+
extern "C" uint64_t EH_FRM_HDR_LEN;
106+
extern "C" uint64_t EH_FRM_OFFSET;
107+
extern "C" uint64_t EH_FRM_LEN;
108+
#endif
109+
110+
101111
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)
102112

103113
// When statically linked on bare-metal, the symbols for the EH table are looked
@@ -505,6 +515,10 @@ static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,
505515
#endif // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)
506516

507517

518+
#if defined(RUST_SGX)
519+
extern "C" char IMAGE_BASE;
520+
#endif
521+
508522
inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
509523
UnwindInfoSections &info) {
510524
#ifdef __APPLE__
@@ -522,6 +536,8 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
522536
#elif defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND) && defined(_LIBUNWIND_IS_BAREMETAL)
523537
info.dso_base = 0;
524538
// Bare metal is statically linked, so no need to ask the dynamic loader
539+
540+
#if !defined(RUST_SGX)
525541
info.dwarf_section_length = (size_t)(&__eh_frame_end - &__eh_frame_start);
526542
info.dwarf_section = (uintptr_t)(&__eh_frame_start);
527543
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %p length %p",
@@ -532,6 +548,17 @@ inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
532548
_LIBUNWIND_TRACE_UNWINDING("findUnwindSections: index section %p length %p",
533549
(void *)info.dwarf_index_section, (void *)info.dwarf_index_section_length);
534550
#endif
551+
552+
#elif defined(RUST_SGX)
553+
info.dwarf_section = (uintptr_t)EH_FRM_OFFSET + (uintptr_t)(&IMAGE_BASE);
554+
info.dwarf_section_length = (uintptr_t)EH_FRM_LEN;
555+
#if defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
556+
info.dwarf_index_section = (uintptr_t)EH_FRM_HDR_OFFSET + (uintptr_t)(&IMAGE_BASE);
557+
info.dwarf_index_section_length = (uintptr_t)EH_FRM_HDR_LEN;
558+
#endif
559+
560+
#endif
561+
535562
if (info.dwarf_section_length)
536563
return true;
537564
#elif defined(_LIBUNWIND_ARM_EHABI) && defined(_LIBUNWIND_IS_BAREMETAL)

libunwind/src/CMakeLists.txt

+43-4
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
@@ -22,9 +26,6 @@ set(LIBUNWIND_C_SOURCES
2226
UnwindLevel1-gcc-ext.c
2327
Unwind-sjlj.c
2428
)
25-
set_source_files_properties(${LIBUNWIND_C_SOURCES}
26-
PROPERTIES
27-
COMPILE_FLAGS "-std=c99")
2829

2930
set(LIBUNWIND_ASM_SOURCES
3031
UnwindRegistersRestore.S
@@ -73,6 +74,44 @@ if (MSVC_IDE)
7374
source_group("Header Files" FILES ${LIBUNWIND_HEADERS})
7475
endif()
7576

77+
if (RUST_SGX)
78+
# Compile Flags
79+
add_definitions(-DRUST_SGX)
80+
add_definitions(-D__NO_STRING_INLINES)
81+
add_definitions(-D__NO_MATH_INLINES)
82+
add_definitions(-D_LIBUNWIND_IS_BAREMETAL)
83+
# Can't use add_definitions because CMake will reorder these arguments
84+
list(APPEND LIBUNWIND_COMPILE_FLAGS -U_FORTIFY_SOURCE)
85+
list(APPEND LIBUNWIND_COMPILE_FLAGS -D_FORTIFY_SOURCE=0)
86+
87+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fno-stack-protector)
88+
list(APPEND LIBUNWIND_COMPILE_FLAGS -ffreestanding)
89+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fexceptions)
90+
# Avoid too new relocation types being emitted, which might prevent linking
91+
# on older platforms.
92+
#
93+
# See https://github.com/rust-lang/rust/issues/34978
94+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
95+
list(APPEND LIBUNWIND_COMPILE_FLAGS -Wa,-mrelax-relocations=no)
96+
else()
97+
list(APPEND LIBUNWIND_COMPILE_FLAGS)
98+
endif()
99+
100+
# Sources
101+
list(APPEND LIBUNWIND_HEADERS UnwindRustSgx.h)
102+
list(APPEND LIBUNWIND_C_SOURCES UnwindRustSgx.c)
103+
endif()
104+
105+
106+
set_source_files_properties(${LIBUNWIND_C_SOURCES}
107+
PROPERTIES
108+
COMPILE_FLAGS "-std=c99")
109+
110+
# See add_asm_sources() in compiler-rt for explanation of this workaround.
111+
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
112+
set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C)
113+
endif()
114+
76115
set(LIBUNWIND_SOURCES
77116
${LIBUNWIND_CXX_SOURCES}
78117
${LIBUNWIND_C_SOURCES}
@@ -87,7 +126,7 @@ else()
87126
add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
88127
endif()
89128
add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
90-
if (LIBUNWIND_ENABLE_THREADS)
129+
if (LIBUNWIND_ENABLE_THREADS AND (NOT RUST_SGX))
91130
add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
92131
add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
93132
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 = (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+
}

0 commit comments

Comments
 (0)