Skip to content

Commit

Permalink
Add proper tracing options, remove getenv calls from enclave code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph M. Wintersteiger committed Aug 19, 2020
1 parent 7716e93 commit 9979a88
Show file tree
Hide file tree
Showing 27 changed files with 339 additions and 227 deletions.
2 changes: 1 addition & 1 deletion sgx-lkl-musl
7 changes: 4 additions & 3 deletions src/enclave/enclave_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "enclave/lthread_int.h"
#include "enclave/wireguard.h"
#include "enclave/wireguard_util.h"
#include "shared/env.h"
#include "shared/util.h"

extern struct mpmcq __scheduler_queue;

Expand Down Expand Up @@ -91,7 +91,8 @@ static void init_wireguard()

static int startmain(void* args)
{
__init_libc(sgxlkl_enclave_state.elf64_stack.envp,
__init_libc(
sgxlkl_enclave_state.elf64_stack.envp,
sgxlkl_enclave_state.elf64_stack.argv[0]);
__libc_start_init();
a_barrier();
Expand Down Expand Up @@ -156,7 +157,7 @@ int __libc_init_enclave(int argc, char** argv)
max_lthreads = next_power_of_2(max_lthreads);

newmpmcq(&__scheduler_queue, max_lthreads, 0);

init_ethread_tp();

size_t espins = cfg->espins;
Expand Down
9 changes: 4 additions & 5 deletions src/enclave/enclave_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ static size_t used_pages =
0; // Tracks the number of used pages for the mmap tracing

#if DEBUG
extern int sgxlkl_trace_mmap;
static size_t mmap_max_allocated = 0; // Maximum amount of memory used thus far
#endif

Expand Down Expand Up @@ -139,7 +138,7 @@ void enclave_mman_init(const void* base, size_t num_pages, int _mmap_files)

// Base address for range of pages available to mmap calls
mmap_base = (char*)mmap_bitmap + (2 * bitmap_req_pages * PAGE_SIZE);
// Set mmap_end to one less page than we normally would to address
// Set mmap_end to one less page than we normally would to address
// https://github.com/lsds/sgx-lkl/issues/742
mmap_end = (char*)mmap_base + (mmap_num_pages - 2) * PAGE_SIZE;

Expand Down Expand Up @@ -194,7 +193,7 @@ void* enclave_mmap(
index_top = addr_to_index(addr) - (pages - 1);

#if DEBUG
if (sgxlkl_trace_mmap)
if (sgxlkl_enclave_state.config->trace.mmap)
replaced_pages = bitmap_count_set_bits(
mmap_bitmap, mmap_num_pages, index_top, pages);
#endif
Expand Down Expand Up @@ -296,7 +295,7 @@ void* enclave_mmap(
}

#if DEBUG
if (sgxlkl_trace_mmap)
if (sgxlkl_enclave_state.config->trace.mmap)
{
size_t requested = pages * PAGESIZE;
size_t total = mmap_num_pages * PAGESIZE;
Expand Down Expand Up @@ -354,7 +353,7 @@ long enclave_munmap(void* addr, size_t length)
ticket_unlock(&mmaplock);

#if DEBUG
if (sgxlkl_trace_mmap)
if (sgxlkl_enclave_state.config->trace.mmap)
{
size_t requested = pages * PAGESIZE;
size_t total = mmap_num_pages * PAGESIZE;
Expand Down
5 changes: 2 additions & 3 deletions src/enclave/enclave_oe.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdatomic.h>
#include <string.h>


#include <openenclave/bits/eeid.h>
#include <openenclave/corelibc/oemalloc.h>
#include <openenclave/corelibc/oestring.h>
Expand All @@ -13,12 +12,12 @@
#include "enclave/enclave_util.h"
#include "enclave/lthread.h"
#include "enclave/lthread_int.h"
#include "shared/env.h"
#include "shared/timer_dev.h"
#include "shared/util.h"

#define AUXV_ENTRIES 13

char *at_platform = "x86_64";
char* at_platform = "x86_64";
sgxlkl_enclave_state_t sgxlkl_enclave_state = {0};

bool sgxlkl_in_sw_debug_mode()
Expand Down
5 changes: 2 additions & 3 deletions src/enclave/enclave_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <asm-generic/ucontext.h>

#include <lkl_host.h>
#include <lkl/setup.h>
#include <lkl_host.h>
#include <string.h>

#include <openenclave/enclave.h>
Expand All @@ -16,7 +16,6 @@
#include "enclave/enclave_util.h"
#include "enclave/lthread.h"
#include "enclave/sgxlkl_t.h"
#include "shared/env.h"

#define RDTSC_OPCODE 0x310F

Expand Down Expand Up @@ -151,7 +150,7 @@ static uint64_t sgxlkl_enclave_signal_handler(
opcode);

#ifdef DEBUG
if (sgxlkl_trace_signal)
if (sgxlkl_enclave_state.config->trace.signal)
{
sgxlkl_print_backtrace((void*)oe_ctx->rbp);
}
Expand Down
2 changes: 1 addition & 1 deletion src/host_interface/virtio_blkdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <host/vio_host_event_channel.h>
#include <host/virtio_blkdev.h>
#include <host/virtio_debug.h>
#include <shared/env.h>
#include <shared/util.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
Expand Down
2 changes: 1 addition & 1 deletion src/host_interface/virtio_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <host/vio_host_event_channel.h>
#include <host/virtio_console.h>
#include <poll.h>
#include <shared/env.h>
#include <shared/util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
1 change: 0 additions & 1 deletion src/host_interface/virtio_debug.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if DEBUG && VIRTIO_TEST_HOOK

#include <host/virtio_debug.h>
#include <shared/env.h>
#include <string.h>

/* Virtio debug module enables different debug options for virtio device
Expand Down
2 changes: 1 addition & 1 deletion src/host_interface/virtio_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <host/virtio_debug.h>
#include <host/virtio_netdev.h>
#include <poll.h>
#include <shared/env.h>
#include <shared/util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
23 changes: 7 additions & 16 deletions src/include/enclave/enclave_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ uint64_t next_power_of_2(uint64_t n);

#include "openenclave/internal/print.h"

extern int sgxlkl_trace_thread;
extern int sgxlkl_trace_mmap;
extern int sgxlkl_trace_signal;
extern int sgxlkl_trace_disk;
extern int sgxlkl_trace_lkl_syscall;
extern int sgxlkl_trace_internal_syscall;
extern int sgxlkl_trace_ignored_syscall;
extern int sgxlkl_trace_unsupported_syscall;
extern int sgxlkl_trace_redirect_syscall;

#define SGXLKL_ASSERT(EXPR) \
do \
{ \
Expand All @@ -149,21 +139,22 @@ extern int sgxlkl_trace_redirect_syscall;
oe_host_printf(x, ##__VA_ARGS__); \
}
#define SGXLKL_TRACE_THREAD(x, ...) \
if (sgxlkl_trace_thread) \
if (sgxlkl_enclave_state.config->trace.thread) \
{ \
oe_host_printf("[[ THREAD ]] " x, ##__VA_ARGS__); \
}
#define SGXLKL_TRACE_MMAP(x, ...) \
if (sgxlkl_trace_mmap) \
if (sgxlkl_enclave_state.config->trace.mmap) \
{ \
oe_host_printf("[[ MMAP ]] " x, ##__VA_ARGS__); \
}
#define SGXLKL_TRACE_SYSCALL(type, x, ...) \
if ((sgxlkl_trace_lkl_syscall && type == SGXLKL_LKL_SYSCALL)) \
if ((sgxlkl_enclave_state.config->trace.lkl_syscall && \
type == SGXLKL_LKL_SYSCALL)) \
{ \
oe_host_printf("[[ LKL SYSC ]] " x, ##__VA_ARGS__); \
} \
else if ((sgxlkl_trace_internal_syscall && \
else if ((sgxlkl_enclave_state.config->trace.internal_syscall && \
type == SGXLKL_INTERNAL_SYSCALL)) \
{ \
oe_host_printf("[[ INT SYSC ]] " x, ##__VA_ARGS__); \
Expand All @@ -184,13 +175,13 @@ extern int sgxlkl_trace_redirect_syscall;
}

#define SGXLKL_TRACE_SIGNAL(x, ...) \
if (sgxlkl_trace_signal) \
if (sgxlkl_enclave_state.config->trace.signal) \
{ \
oe_host_printf("[[ SIGNAL ]] " x, ##__VA_ARGS__); \
}

#define SGXLKL_TRACE_DISK(x, ...) \
if (sgxlkl_trace_disk) \
if (sgxlkl_enclave_state.config->trace.disk) \
{ \
oe_host_printf("[[ DISK ]] " x, ##__VA_ARGS__); \
}
Expand Down
12 changes: 0 additions & 12 deletions src/include/shared/env.h → src/include/host/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "shared/oe_compat.h"

uint64_t hex_to_int(const char* digits, size_t num_digits);

uint64_t size_str_to_uint64(const char* str, uint64_t def, uint64_t max);

void size_uint64_to_str(uint64_t size, char* buf, uint64_t len);
Expand All @@ -15,14 +13,4 @@ char* getenv_str(const char* var, const char* def);

int getenv_bool(const char* var, int def);

uint64_t next_pow2(uint64_t x);

ssize_t hex_to_bytes(const char* hex, uint8_t** result);

char* bytes_to_hex(
char* str,
size_t str_size,
const void* data,
size_t data_size);

#endif /* _ENV_H */
2 changes: 1 addition & 1 deletion src/include/host/sgxlkl_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#ifndef SGXLKL_RELEASE
/* These environment variables do not have config settings, they are
* automatically passed through and imported in the enclave */
extern const char* sgxlkl_auto_passthrough[12];
extern const char* sgxlkl_auto_passthrough[1];
#endif

#endif /* SGXLKL_PARAMS_H */
7 changes: 5 additions & 2 deletions src/include/shared/oe_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#ifdef SGXLKL_ENCLAVE

/* Rewire some libc functions to oecorelibc equivalents, to avoid dependencies on
* sgx-lkl-musl in SGX-LKL kernel space. */
/* Rewire some libc functions to oecorelibc equivalents, to avoid dependencies
* on sgx-lkl-musl in SGX-LKL kernel space. */

#include <openenclave/corelibc/bits/types.h>
#include <openenclave/corelibc/oemalloc.h>
Expand All @@ -19,13 +19,16 @@
#define strlen oe_strlen
#define strcmp oe_strcmp
#define strtok_r oe_strtok_r
#define snprintf oe_snprintf
#define strtoul oe_strtoul

#else

#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down
19 changes: 19 additions & 0 deletions src/include/shared/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _SHARED_UTIL_H
#define _SHARED_UTIL_H

#include <stddef.h>
#include <stdint.h>

uint64_t hex_to_int(const char* digits, size_t num_digits);

uint64_t next_pow2(uint64_t x);

ssize_t hex_to_bytes(const char* hex, uint8_t** result);

char* bytes_to_hex(
char* str,
size_t str_size,
const void* data,
size_t data_size);

#endif
9 changes: 5 additions & 4 deletions src/lkl/lkl_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,20 @@ long __sgxlkl_log_syscall(
int params_len,
...)
{
const sgxlkl_trace_config_t* tcfg = &sgxlkl_enclave_state.config->trace;
const char* name = NULL;
char errmsg[255] = {0};

if (!sgxlkl_trace_ignored_syscall && type == SGXLKL_IGNORED_SYSCALL)
if (!tcfg->ignored_syscall && type == SGXLKL_IGNORED_SYSCALL)
return res;

if (!sgxlkl_trace_unsupported_syscall && type == SGXLKL_UNSUPPORTED_SYSCALL)
if (!tcfg->unsupported_syscall && type == SGXLKL_UNSUPPORTED_SYSCALL)
return res;

if (!sgxlkl_trace_lkl_syscall && type == SGXLKL_LKL_SYSCALL)
if (!tcfg->lkl_syscall && type == SGXLKL_LKL_SYSCALL)
return res;

if (!sgxlkl_trace_internal_syscall && type == SGXLKL_INTERNAL_SYSCALL)
if (!tcfg->internal_syscall && type == SGXLKL_INTERNAL_SYSCALL)
return res;

long params[6] = {0};
Expand Down
Loading

0 comments on commit 9979a88

Please sign in to comment.