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

Remove some libc dependencies from src/enclave #195

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 8 additions & 6 deletions src/enclave/enclave_event_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "enclave/ticketlock.h"
#include "enclave/vio_enclave_event_channel.h"

#include "openenclave/corelibc/oemalloc.h"

static struct ticketlock** evt_chn_lock;

/* Tasks monitoring event channels */
Expand Down Expand Up @@ -88,7 +90,7 @@ static void vio_enclave_process_host_event(uint8_t* param)
uint8_t dev_id = *param;

/* release memory after extracting dev_id */
free(param);
oe_free(param);

enc_evt_channel_t* evt_channel = _enc_dev_config[dev_id].enc_evt_chn;
evt_t* evt_processed = &_enc_dev_config[dev_id].evt_processed;
Expand Down Expand Up @@ -142,27 +144,27 @@ void initialize_enclave_event_channel(
uint8_t* dev_id = NULL;
_evt_channel_num = evt_channel_num;

evt_chn_lock = (struct ticketlock**)calloc(
evt_chn_lock = (struct ticketlock**)oe_calloc(
evt_channel_num, sizeof(struct ticketlock*));

vio_tasks =
(struct lthread**)calloc(evt_channel_num, sizeof(struct lthread*));
(struct lthread**)oe_calloc(evt_channel_num, sizeof(struct lthread*));

_enc_dev_config = enc_dev_config;
for (int i = 0; i < evt_channel_num; i++)
{
evt_chn_lock[i] =
(struct ticketlock*)calloc(1, sizeof(struct ticketlock));
(struct ticketlock*)oe_calloc(1, sizeof(struct ticketlock));
memset(evt_chn_lock[i], 0, sizeof(struct ticketlock));

dev_id = (uint8_t*)calloc(1, sizeof(uint8_t));
dev_id = (uint8_t*)oe_calloc(1, sizeof(uint8_t));
*dev_id = enc_dev_config[i].dev_id;

struct lthread* lt = NULL;
if (lthread_create(
&lt, NULL, vio_enclave_process_host_event, (void*)dev_id) != 0)
{
free(vio_tasks);
oe_free(vio_tasks);
sgxlkl_fail("Failed to create lthread for event channel\n");
}
vio_tasks[i] = lt;
Expand Down
12 changes: 7 additions & 5 deletions src/enclave/enclave_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "lkl/setup.h"

#include <openenclave/internal/globals.h>
#include "openenclave/corelibc/oemalloc.h"
#include "openenclave/corelibc/oestring.h"

#include "enclave/enclave_mem.h"
#include "enclave/enclave_util.h"
Expand Down Expand Up @@ -43,22 +45,22 @@ static void __sgxlkl_enclave_copy_app_config(
char** envp = NULL;

app_config->argc = sgxlkl_config->argc;
app_config->argv = malloc((app_config->argc + 1) * sizeof(char*));
app_config->argv = oe_malloc((app_config->argc + 1) * sizeof(char*));

for (i = 0; i < app_config->argc; i++)
app_config->argv[i] = strdup(sgxlkl_config->argv[i]);
app_config->argv[i] = oe_strdup(sgxlkl_config->argv[i]);
app_config->argv[i] = NULL;

envp = sgxlkl_config->argv + sgxlkl_config->argc + 1;
for (; envp[j++] != NULL;)
;

app_config->envp = malloc((j + 1) * sizeof(char*));
app_config->envp = oe_malloc((j + 1) * sizeof(char*));
for (j = 0; envp[j] != NULL; j++)
app_config->envp[j] = strdup(envp[j]);
app_config->envp[j] = oe_strdup(envp[j]);
app_config->envp[j] = NULL;

app_config->cwd = strdup(sgxlkl_config->cwd);
app_config->cwd = oe_strdup(sgxlkl_config->cwd);

return;
}
Expand Down
8 changes: 3 additions & 5 deletions src/enclave/sgxlkl_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
#include <enclave/enclave_util.h>
#include <shared/sgxlkl_config.h>

// We can't use normal malloc at this point in time,
// so we use the OE-internal malloc here.
extern void* oe_malloc(size_t size);
extern void oe_free(void*);
#include "openenclave/corelibc/oemalloc.h"

// Duplicate a string
static int strdupz(char** to, const char* from)
Expand Down Expand Up @@ -55,7 +52,8 @@ int sgxlkl_copy_config(const sgxlkl_config_t* from, sgxlkl_config_t** to)
sgxlkl_config_t* cfg = *to;

// Catch modifications to sgxlkl_config_t early.
_Static_assert(sizeof(sgxlkl_config_t) == 472, "sgxlkl_config_t size has changed");
_Static_assert(
sizeof(sgxlkl_config_t) == 472, "sgxlkl_config_t size has changed");

cfg->max_user_threads = from->max_user_threads;
cfg->stacksize = from->stacksize;
Expand Down
6 changes: 6 additions & 0 deletions src/include/openenclave/corelibc/oestring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __OE_STRING_INCLUDED__
#define __OE_STRING_INCLUDED__

char *oe_strdup(const char *s);

#endif