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