Skip to content

Commit e8a9f44

Browse files
AdrianCXxtexx
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 92a83a0 commit e8a9f44

9 files changed

+318
-12
lines changed

libunwind/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ if (LIBUNWIND_ENABLE_ASSERTIONS)
270270

271271
# On Release builds cmake automatically defines NDEBUG, so we
272272
# explicitly undefine it:
273-
if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
273+
if ((NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") AND (NOT RUST_SGX))
274274
add_compile_flags(-UNDEBUG)
275275
endif()
276276
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
@@ -131,3 +131,8 @@ libunwind specific options
131131

132132
Path where built libunwind libraries should be installed. If a relative path,
133133
relative to ``CMAKE_INSTALL_PREFIX``.
134+
135+
.. option:: LIBUNWIND_ENABLE_RUST_SGX:BOOL
136+
137+
**Default**: ``OFF``
138+

libunwind/src/AddressSpace.hpp

+27
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

+49-10
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
@@ -18,14 +22,6 @@ set(LIBUNWIND_C_SOURCES
1822
Unwind-sjlj.c
1923
Unwind-wasm.c
2024
)
21-
set_source_files_properties(${LIBUNWIND_C_SOURCES}
22-
PROPERTIES
23-
# We need to set `-fexceptions` here so that key
24-
# unwinding functions, like
25-
# _UNWIND_RaiseException, are not marked as
26-
# `nounwind`, which breaks LTO builds of
27-
# libunwind. See #56825 and #120657 for context.
28-
COMPILE_FLAGS "-std=c99 -fexceptions")
2925

3026
set(LIBUNWIND_ASM_SOURCES
3127
UnwindRegistersRestore.S
@@ -65,6 +61,49 @@ if (MSVC_IDE)
6561
source_group("Header Files" FILES ${LIBUNWIND_HEADERS})
6662
endif()
6763

64+
if (RUST_SGX)
65+
# Compile Flags
66+
add_definitions(-DRUST_SGX)
67+
add_definitions(-D__NO_STRING_INLINES)
68+
add_definitions(-D__NO_MATH_INLINES)
69+
add_definitions(-D_LIBUNWIND_IS_BAREMETAL)
70+
# Can't use add_definitions because CMake will reorder these arguments
71+
list(APPEND LIBUNWIND_COMPILE_FLAGS -U_FORTIFY_SOURCE)
72+
list(APPEND LIBUNWIND_COMPILE_FLAGS -D_FORTIFY_SOURCE=0)
73+
74+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fno-stack-protector)
75+
list(APPEND LIBUNWIND_COMPILE_FLAGS -ffreestanding)
76+
list(APPEND LIBUNWIND_COMPILE_FLAGS -fexceptions)
77+
# Avoid too new relocation types being emitted, which might prevent linking
78+
# on older platforms.
79+
#
80+
# See https://github.com./rust-lang/rust/issues/34978
81+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
82+
list(APPEND LIBUNWIND_COMPILE_FLAGS -Wa,-mrelax-relocations=no)
83+
else()
84+
list(APPEND LIBUNWIND_COMPILE_FLAGS)
85+
endif()
86+
87+
# Sources
88+
list(APPEND LIBUNWIND_HEADERS UnwindRustSgx.h)
89+
list(APPEND LIBUNWIND_C_SOURCES UnwindRustSgx.c)
90+
endif()
91+
92+
93+
set_source_files_properties(${LIBUNWIND_C_SOURCES}
94+
PROPERTIES
95+
# We need to set `-fexceptions` here so that key
96+
# unwinding functions, like
97+
# _UNWIND_RaiseException, are not marked as
98+
# `nounwind`, which breaks LTO builds of
99+
# libunwind. See #56825 and #120657 for context.
100+
COMPILE_FLAGS "-std=c99 -fexceptions")
101+
102+
# See add_asm_sources() in compiler-rt for explanation of this workaround.
103+
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
104+
set_source_files_properties(${LIBUNWIND_ASM_SOURCES} PROPERTIES LANGUAGE C)
105+
endif()
106+
68107
set(LIBUNWIND_SOURCES
69108
${LIBUNWIND_CXX_SOURCES}
70109
${LIBUNWIND_C_SOURCES}
@@ -87,11 +126,11 @@ if (NOT APPLE)
87126
add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
88127
endif()
89128

90-
if (LIBUNWIND_ENABLE_THREADS AND NOT APPLE)
129+
if (LIBUNWIND_ENABLE_THREADS AND NOT APPLE AND (NOT RUST_SGX))
91130
add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
92131
endif()
93132

94-
if (LIBUNWIND_ENABLE_THREADS)
133+
if (LIBUNWIND_ENABLE_THREADS AND (NOT RUST_SGX))
95134
add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
96135
endif()
97136

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)