Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting #799

Open
wants to merge 3 commits into
base: oe_port
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ if [[ $(git config --get user.name | wc -w) -lt 2 ]]; then
exit_ "Commit failed: please fix your Git user name (see docs/Contributing.md)"
fi

# TODO enable after formatting is enabled
#if ! git diff-index --check --cached HEAD --; then
# exit_ "Commit failed: please fix the conflict markers or whitespace errors"
#fi
if ! git diff-index --check --cached HEAD --; then
exit_ "Commit failed: please fix the conflict markers or whitespace errors"
fi

mapfile -t files < <(git diff --cached --name-only --diff-filter=ACMR)

Expand All @@ -38,11 +37,10 @@ fi

scripts=$(git rev-parse --show-toplevel)/scripts

# TODO enable after files have been formatted
# shellcheck disable=SC2154
#if ! "$scripts/format-c-code" --quiet --whatif --files="${files[*]}"; then
# exit_ "Commit failed: please run './scripts/format-c-code --staged' to fix the formatting"
#fi
#shellcheck disable=SC2154
if ! "$scripts/format-c-code" --quiet --whatif --files="${files[*]}"; then
exit_ "Commit failed: please run './scripts/format-c-code --staged' to fix the formatting"
fi

if ! "$scripts/format-py-code" --quiet --whatif; then
exit_ "Commit failed: please run './scripts/format-py-code' to fix the formatting"
Expand Down
5 changes: 3 additions & 2 deletions src/enclave/enclave_init.c
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/enclave/enclave_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,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
23 changes: 9 additions & 14 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 @@ -18,7 +17,7 @@

#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 Expand Up @@ -55,28 +54,25 @@ static int _strncmp(const char* x, const char* y, size_t n)
return *px == *py ? 0 : *px < *py ? -1 : +1;
}

static void
init_auxv(size_t *auxv, char* buf_ptr, char *pn)
static void init_auxv(size_t* auxv, char* buf_ptr, char* pn)
{
// By default auxv[AT_RANDOM] points to a buffer with 16 random bytes.
uint64_t *rbuf = (uint64_t*)buf_ptr;
uint64_t* rbuf = (uint64_t*)buf_ptr;
buf_ptr += 16;
// TODO Use intrinsics
// if (!_rdrand64_step(&rbuf[0]))
// goto err;
register uint64_t rd;
__asm__ volatile("rdrand %0;"
: "=r"(rd));
__asm__ volatile("rdrand %0;" : "=r"(rd));
rbuf[0] = rd;
__asm__ volatile("rdrand %0;"
: "=r"(rd));
__asm__ volatile("rdrand %0;" : "=r"(rd));
rbuf[1] = rd;

memset(auxv, 0, 2 * sizeof(size_t) * AUXV_ENTRIES);
auxv[0] = AT_CLKTCK;
auxv[1] = 100;
auxv[2] = AT_EXECFN;
auxv[3] = (size_t) pn;
auxv[3] = (size_t)pn;
auxv[4] = AT_HWCAP;
auxv[5] = 0;
auxv[6] = AT_EGID;
Expand All @@ -89,7 +85,7 @@ init_auxv(size_t *auxv, char* buf_ptr, char *pn)
auxv[13] = 0;
auxv[14] = AT_PLATFORM;
memcpy(buf_ptr, at_platform, oe_strlen(at_platform) + 1);
auxv[15] = (size_t) buf_ptr;
auxv[15] = (size_t)buf_ptr;
buf_ptr += oe_strlen(at_platform) + 1;
auxv[16] = AT_SECURE;
auxv[17] = 0;
Expand Down Expand Up @@ -146,9 +142,9 @@ static void _prepare_elf_stack()
for (size_t i = 0; i < num_imported_env; i++)
num_bytes += oe_strlen(imported_env[i]) + 1;
num_ptrs += num_imported_env + 1;
num_ptrs += 2 * AUXV_ENTRIES; // auxv vector entries
num_ptrs += 2 * AUXV_ENTRIES; // auxv vector entries
num_bytes += oe_strlen(at_platform) + 1; // AT_PLATFORM
num_bytes += 16; // AT_RANDOM
num_bytes += 16; // AT_RANDOM

elf64_stack_t* stack = &sgxlkl_enclave_state.elf64_stack;
stack->data = oe_calloc_or_die(
Expand Down Expand Up @@ -462,4 +458,3 @@ void sgxlkl_debug_dump_stack_traces(void)
lthread_dump_all_threads(false);
#endif
}

2 changes: 1 addition & 1 deletion 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 Down
7 changes: 4 additions & 3 deletions src/enclave/mpmc_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "openenclave/corelibc/oemalloc.h"
#include "enclave/enclave_util.h"
#include "enclave/mpmc_queue.h"
#include "enclave/enclave_util.h"
#include "openenclave/corelibc/oemalloc.h"

static void pause(void);
static void pause()
Expand All @@ -53,7 +53,8 @@ int newmpmcq(struct mpmcq* q, size_t buffer_size, void* buffer)
q->buffer =
buffer != 0 ? buffer : oe_calloc(sizeof(struct cell_t), buffer_size);
q->buffer_mask = (buffer_size - 1);
SGXLKL_ASSERT((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0));
SGXLKL_ASSERT(
(buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0));
for (i = 0; i != buffer_size; i += 1)
{
__atomic_store_n(&q->buffer[i].seq, i, __ATOMIC_RELAXED);
Expand Down
12 changes: 8 additions & 4 deletions src/host_interface/virtio_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ static int virtio_net_fd_net_poll(uint8_t netdev_id)
},
};


do
{
ret = poll(pfds, 2, -1);
Expand Down Expand Up @@ -536,10 +535,15 @@ int netdev_init(sgxlkl_host_state_t* host_state)
char mac[6];
// Generate a completely random MAC address
size_t b = 0;
while (b < sizeof(mac)) {
while (b < sizeof(mac))
{
ssize_t ret = getrandom(&mac[b], sizeof(mac) - b, GRND_RANDOM);
if (ret < 0) {
sgxlkl_host_fail("%s: get random MAC address failed: %s\n", __func__, strerror(errno));
if (ret < 0)
{
sgxlkl_host_fail(
"%s: get random MAC address failed: %s\n",
__func__,
strerror(errno));
return -1;
}
b += ret;
Expand Down
8 changes: 4 additions & 4 deletions src/include/enclave/enclave_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
#include "enclave/sgxlkl_t.h"

#ifndef PROT_NONE
# define PROT_NONE 0x0
#define PROT_NONE 0x0
#endif
#ifndef PROT_READ
# define PROT_READ 0x1
#define PROT_READ 0x1
#endif
#ifndef PROT_WRITE
# define PROT_WRITE 0x2
#define PROT_WRITE 0x2
#endif
#ifndef PROT_EXEC
# define PROT_EXEC 0x4
#define PROT_EXEC 0x4
#endif

void enclave_mman_init(const void* base, size_t num_pages, int _mmap_files);
Expand Down
5 changes: 3 additions & 2 deletions src/include/enclave/enclave_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ void* oe_calloc_or_die(size_t nmemb, size_t size, const char* fail_msg, ...);

/**
*
* Note that generating a stack trace by unwinding stack frames could be exploited
* by an attacker and therefore should only be possible in a DEBUG build.
* Note that generating a stack trace by unwinding stack frames could be
* exploited by an attacker and therefore should only be possible in a DEBUG
* build.
*/
#ifdef DEBUG
/**
Expand Down
52 changes: 26 additions & 26 deletions src/include/enclave/lthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@ struct futex_q

struct lthread
{
struct cpu_ctx ctx; /* cpu ctx info */
lthread_func fun; /* func lthread is running */
void* arg; /* func args passed to func */
struct lthread_attr attr; /* various attributes */
int tid; /* lthread id */
char funcname[64]; /* optional func name */
struct lthread* lt_join; /* lthread we want to join on */
void** lt_exit_ptr; /* exit ptr for lthread_join */
uint32_t ops; /* num of ops since yield */
uint64_t sleep_usecs; /* how long lthread is sleeping */
struct lthread_tls_l tls; /* pointer to TLS */
uint8_t* itls; /* image TLS */
size_t itlssz; /* size of TLS image */
uintptr_t* tp; /* thread pointer */
int err; /* errno value */
struct cpu_ctx ctx; /* cpu ctx info */
lthread_func fun; /* func lthread is running */
void* arg; /* func args passed to func */
struct lthread_attr attr; /* various attributes */
int tid; /* lthread id */
char funcname[64]; /* optional func name */
struct lthread* lt_join; /* lthread we want to join on */
void** lt_exit_ptr; /* exit ptr for lthread_join */
uint32_t ops; /* num of ops since yield */
uint64_t sleep_usecs; /* how long lthread is sleeping */
struct lthread_tls_l tls; /* pointer to TLS */
uint8_t* itls; /* image TLS */
size_t itlssz; /* size of TLS image */
uintptr_t* tp; /* thread pointer */
int err; /* errno value */
/* yield_cb_* are a callback to call after yield finished and it's arg */
/* they are required to release futex lock on FUTEX_WAIT operation */
/* and in sched_yield (see comment there) to avoid race among schedulers */
Expand Down Expand Up @@ -177,18 +177,20 @@ struct lthread_sched
};
/**
* lthread scheduler context. Pointer to this structure can be fetched by
* calling __scheduler_self().
* calling __scheduler_self().
* The structure is located towards the end of the page pointed by %gs.
*/
struct schedctx {
struct schedctx *self;
int tid;
struct lthread_sched sched;
struct schedctx
{
struct schedctx* self;
int tid;
struct lthread_sched sched;
};

/* Thread Control Block (TCB) for lthreads and lthread scheduler */
struct lthread_tcb_base {
void *self;
struct lthread_tcb_base
{
void* self;
char _pad_0[32];
// SGX-LKL does not have full stack smashing protection (SSP) support right
// now. In particular, we do not generate a random stack guard for every
Expand All @@ -198,7 +200,7 @@ struct lthread_tcb_base {
// guard/canary value at an offset 0x28 (40 bytes) from the FS segment
// base/start of the TCB (see schedctx struct above).
uint64_t stack_guard_dummy;
struct schedctx *schedctx;
struct schedctx* schedctx;
};

typedef struct lthread* lthread_t;
Expand All @@ -211,9 +213,7 @@ extern "C"
*/
void init_ethread_tp();

void lthread_sched_global_init(
size_t sleepspins,
size_t sleeptime_ns);
void lthread_sched_global_init(size_t sleepspins, size_t sleeptime_ns);

/**
* Create a new thread where the caller manages the initial thread state.
Expand Down
14 changes: 6 additions & 8 deletions src/include/enclave/lthread_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ static inline uint64_t _lthread_timespec_to_usec(const struct timespec* ts)
Gets a pointer to the schedctx struct. The pointer is maintained at offset
48 from gsbase of the ethread.
*/
static inline struct schedctx *__scheduler_self()
static inline struct schedctx* __scheduler_self()
{
struct schedctx *self;
__asm__ __volatile__ ("mov %%gs:48,%0" : "=r" (self) );
return self;
struct schedctx* self;
__asm__ __volatile__("mov %%gs:48,%0" : "=r"(self));
return self;
}


static inline struct lthread_sched*
lthread_get_sched()
static inline struct lthread_sched* lthread_get_sched()
{
struct schedctx *c = __scheduler_self();
struct schedctx* c = __scheduler_self();
return &c->sched;
}

Expand Down
3 changes: 1 addition & 2 deletions src/include/lkl/syscall-overrides-fstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#define _LKL_SYSCALL_OVERRIDES_FSTAT_H

typedef long (*syscall_fstat_handler)(int, void*);
typedef long (*syscall_newfstatat_handler)(
int, const char*, void*, int);
typedef long (*syscall_newfstatat_handler)(int, const char*, void*, int);

/**
* Register override functions that maintain compatibility
Expand Down
8 changes: 4 additions & 4 deletions src/include/openenclave/corelibc/oemalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include "openenclave/corelibc/bits/types.h"

void *oe_malloc(size_t size);
void oe_free(void *ptr);
void *oe_calloc(size_t nmemb, size_t size);
void *oe_realloc(void *ptr, size_t size);
void* oe_malloc(size_t size);
void oe_free(void* ptr);
void* oe_calloc(size_t nmemb, size_t size);
void* oe_realloc(void* ptr, size_t size);

#endif
2 changes: 1 addition & 1 deletion src/include/shared/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ unsigned char* sgxlkl_base64_encode(
size_t len,
unsigned char* out,
size_t* out_len);

unsigned char* sgxlkl_base64_decode(
const unsigned char* src,
size_t len,
Expand Down
4 changes: 2 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 Down
2 changes: 1 addition & 1 deletion src/include/shared/timer_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct timer_dev
uint64_t version;

/*
* The number of monotonic nanos that have passed outside of the
* The number of monotonic nanos that have passed outside of the
* enclave since it was started up. nanos is used in enclave_timer.c to
* update the internal clock for the passage of time.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/libjson/common/load_file.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "load_file.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

int json_load_file(
Expand Down
Loading