forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
6540553
commit 44b3fd3
Showing
9 changed files
with
312 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Libunwind customizations for linking with x86_64-fortanix-unknown-sgx Rust target. | ||
|
||
## Description | ||
### Initial Fork | ||
Initial Fork has been made from 5.0 release of llvm (commit: 6a075b6de4) | ||
### Detailed Description | ||
#### Header files that we do not include for this target | ||
1. pthread.h | ||
#### Library that we do not link to for this target. | ||
1. pthread (Locks used by libunwind is provided by rust stdlib for this target) | ||
|
||
## Building unwind for rust-sgx target | ||
### Generate Make files: | ||
* `cd where you want to build libunwind` | ||
* `mkdir build` | ||
* `cd build` | ||
* `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>` | ||
* `"DEBUG"` could be used instead of `"RELEASE"` to enable debug logs of libunwind. | ||
|
||
### Build: | ||
* `make unwind_static` | ||
* `build/lib/` will have the built library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//===--------------------- UnwindRustSgx.c ----------------------------------===// | ||
// | ||
//// The LLVM Compiler Infrastructure | ||
//// | ||
//// This file is dual licensed under the MIT and the University of Illinois Open | ||
//// Source Licenses. See LICENSE.TXT for details. | ||
//// | ||
//// | ||
////===----------------------------------------------------------------------===// | ||
|
||
#define _GNU_SOURCE | ||
#include <link.h> | ||
|
||
#include <elf.h> | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stddef.h> | ||
#include "UnwindRustSgx.h" | ||
|
||
#define max_log 256 | ||
|
||
__attribute__((weak)) struct _IO_FILE *stderr = (struct _IO_FILE *)-1; | ||
|
||
static int vwrite_err(const char *format, va_list ap) | ||
{ | ||
int len = 0; | ||
#ifndef NDEBUG | ||
char s[max_log]; | ||
s[0]='\0'; | ||
len = vsnprintf(s, max_log, format, ap); | ||
__rust_print_err((uint8_t *)s, len); | ||
#endif | ||
return len; | ||
} | ||
|
||
static int write_err(const char *format, ...) | ||
{ | ||
int ret; | ||
va_list args; | ||
va_start(args, format); | ||
ret = vwrite_err(format, args); | ||
va_end(args); | ||
|
||
|
||
return ret; | ||
} | ||
|
||
__attribute__((weak)) int fprintf (FILE *__restrict __stream, | ||
const char *__restrict __format, ...) | ||
{ | ||
|
||
int ret; | ||
if (__stream != stderr) { | ||
write_err("Rust SGX Unwind supports only writing to stderr\n"); | ||
return -1; | ||
} else { | ||
va_list args; | ||
ret = 0; | ||
va_start(args, __format); | ||
ret += vwrite_err(__format, args); | ||
va_end(args); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
__attribute__((weak)) int fflush (FILE *__stream) | ||
{ | ||
// We do not need to do anything here. | ||
return 0; | ||
} | ||
|
||
__attribute__((weak)) void __assert_fail(const char * assertion, | ||
const char * file, | ||
unsigned int line, | ||
const char * function) | ||
{ | ||
write_err("%s:%d %s %s\n", file, line, function, assertion); | ||
abort(); | ||
} | ||
|
||
// We do not report stack over flow detected. | ||
// Calling write_err uses more stack due to the way we have implemented it. | ||
// With possible enabling of stack probes, we should not | ||
// get into __stack_chk_fail() at all. | ||
__attribute__((weak)) void __stack_chk_fail() { | ||
abort(); | ||
} | ||
|
||
/* | ||
* Below are defined for all executibles compiled for | ||
* x86_64-fortanix-unknown-sgx rust target. | ||
* Ref: rust/src/libstd/sys/sgx/abi/entry.S | ||
*/ | ||
|
||
struct libwu_rs_alloc_meta { | ||
size_t alloc_size; | ||
// Should we put a signatre guard before ptr for oob access? | ||
unsigned char ptr[0]; | ||
}; | ||
|
||
#define META_FROM_PTR(__PTR) (struct libwu_rs_alloc_meta *) \ | ||
((unsigned char *)__PTR - offsetof(struct libwu_rs_alloc_meta, ptr)) | ||
|
||
void *libuw_malloc(size_t size) | ||
{ | ||
struct libwu_rs_alloc_meta *meta; | ||
size_t alloc_size = size + sizeof(struct libwu_rs_alloc_meta); | ||
meta = (void *)__rust_c_alloc(alloc_size, sizeof(size_t)); | ||
if (!meta) { | ||
return NULL; | ||
} | ||
meta->alloc_size = alloc_size; | ||
return (void *)meta->ptr; | ||
} | ||
|
||
void libuw_free(void *p) | ||
{ | ||
struct libwu_rs_alloc_meta *meta; | ||
if (!p) { | ||
return; | ||
} | ||
meta = META_FROM_PTR(p); | ||
__rust_c_dealloc((unsigned char *)meta, meta->alloc_size, sizeof(size_t)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//===--------------------- UnwindRustSgx.h ----------------------------------===// | ||
// | ||
//// The LLVM Compiler Infrastructure | ||
//// | ||
//// This file is dual licensed under the MIT and the University of Illinois Open | ||
//// Source Licenses. See LICENSE.TXT for details. | ||
//// | ||
//// | ||
////===----------------------------------------------------------------------===// | ||
|
||
#if !defined(UNWIND_RUST_SGX_H) | ||
#define UNWIND_RUST_SGX_H | ||
|
||
#ifdef RUST_SGX | ||
|
||
#undef _GNU_SOURCE | ||
#define _GNU_SOURCE | ||
#include <link.h> | ||
#include <stdarg.h> | ||
#include <string.h> | ||
#include <stdint.h> | ||
|
||
// We have to use RWLock from rust repo, it is defined in: | ||
// src/libstd/sys/sgx/rwlock.rs. | ||
// rwlock.rs has a compile time check to ensure the size and alignment matches | ||
// the Rust definition. | ||
typedef struct { | ||
void *opaque; | ||
} RWLock; | ||
|
||
#define RWLOCK_INIT \ | ||
{ (void *)0 } | ||
|
||
// These are the functions exposed by SGX-Rust. | ||
// The rust changes are available at: | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
int __rust_rwlock_rdlock(RWLock *rwlock); | ||
int __rust_rwlock_wrlock(RWLock *rwlock); | ||
int __rust_rwlock_unlock(RWLock *rwlock); | ||
unsigned char *__rust_c_alloc(size_t, size_t); | ||
void __rust_c_dealloc(unsigned char *, size_t, size_t); | ||
__attribute__((noreturn)) void __rust_abort(void); | ||
unsigned char *__rust_encl_address(size_t); | ||
|
||
#ifndef NDEBUG | ||
void __rust_print_err(uint8_t *m, int s); | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#define abort __rust_abort | ||
|
||
#undef pthread_rwlock_t | ||
#undef pthread_rwlock_rdlock | ||
#undef pthread_rwlock_wrlock | ||
#undef pthread_rwlock_unlock | ||
#undef PTHREAD_RWLOCK_INITIALIZER | ||
|
||
#define pthread_rwlock_t RWLock | ||
#define pthread_rwlock_rdlock __rust_rwlock_rdlock | ||
#define pthread_rwlock_wrlock __rust_rwlock_wrlock | ||
#define pthread_rwlock_unlock __rust_rwlock_unlock | ||
#define PTHREAD_RWLOCK_INITIALIZER RWLOCK_INIT | ||
|
||
#define malloc libuw_malloc | ||
#define free libuw_free | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void *libuw_malloc(size_t size); | ||
void libuw_free(void *p); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
#endif |
Oops, something went wrong.