Skip to content

Commit

Permalink
fixup! Add some useful script and various changes
Browse files Browse the repository at this point in the history
Clang format
  • Loading branch information
0152la committed Oct 31, 2024
1 parent ed582b7 commit e478eb3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 54 deletions.
8 changes: 2 additions & 6 deletions include/compartment.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ struct CompConfig
size_t stack_size;
struct CompEntryPointDef *entry_points;
size_t entry_point_count;
void *base_address;

char** env_ptr;
char **env_ptr;
size_t env_ptr_sz;
unsigned short env_ptr_count;
};
Expand All @@ -187,9 +186,6 @@ struct Compartment
void *__capability ddc;
// ELF data
size_t size; // size of compartment in memory
void *base; // address where to load compartment
void *mem_top;
bool mapped;

// Environ
char **environ_ptr;
Expand Down Expand Up @@ -221,7 +217,7 @@ entry_point_cmp(const void *, const void *);
struct Compartment *
comp_from_elf(char *, struct CompConfig *); // char **, size_t, void *);
void
comp_map(struct Compartment *);
comp_map(struct Compartment *, void *);
void
comp_unmap(struct Compartment *);
void
Expand Down
2 changes: 2 additions & 0 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct Compartment *
register_new_comp(char *, bool);
int64_t
exec_comp(struct Compartment *, char *, char **);
void
map_comp(struct Compartment *, void *);

union arg_holder
{
Expand Down
94 changes: 52 additions & 42 deletions src/compartment.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ lib_init();
static struct LibDependency *
parse_lib_file(char *, struct Compartment *);
static void
parse_lib_segs(Elf64_Ehdr *, void*, struct LibDependency *, struct Compartment *);
parse_lib_segs(
Elf64_Ehdr *, void *, struct LibDependency *, struct Compartment *);
static void
parse_lib_symtb(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *);
parse_lib_symtb(Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *);
static void
parse_lib_rela(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *);
parse_lib_rela(Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *);
static void
parse_lib_dynamic_deps(Elf64_Shdr *, Elf64_Ehdr *, void*, struct LibDependency *);
parse_lib_dynamic_deps(
Elf64_Shdr *, Elf64_Ehdr *, void *, struct LibDependency *);
static void
map_comp_entry_points(struct Compartment *);
static void
resolve_rela_syms(struct Compartment *);
static void
find_tls_lookup_func(struct Compartment *);
static void
prepare_compartment_mapping(struct Compartment *);

static bool
check_lib_dep_sym(lib_symbol *, const unsigned short);
Expand All @@ -42,9 +46,9 @@ eval_sym_tls_offset(struct Compartment *, const comp_symbol *);
static ssize_t
do_pread(int, void *, size_t, off_t);
static void
get_lib_data(void*, void*, size_t, off_t);
static void*
seek_lib_data(void*, off_t);
get_lib_data(void *, void *, size_t, off_t);
static void *
seek_lib_data(void *, off_t);
static char *
find_in_dir(const char *, char *);
static void
Expand Down Expand Up @@ -79,8 +83,6 @@ comp_init()
new_comp->ddc = NULL;

new_comp->size = 0;
new_comp->base = NULL;
new_comp->mem_top = NULL;
new_comp->mapped = false;

new_comp->scratch_mem_base = NULL;
Expand Down Expand Up @@ -138,8 +140,6 @@ comp_from_elf(char *filename, struct CompConfig *cc)
{
struct Compartment *new_comp = comp_init();
new_comp->cc = cc;
new_comp->base = cc->base_address; // TODO reuse `cc` base
new_comp->mem_top = cc->base_address;

unsigned short libs_to_parse_count = 1;
unsigned short libs_parsed_count = 0;
Expand Down Expand Up @@ -204,6 +204,8 @@ comp_from_elf(char *filename, struct CompConfig *cc)
assert(new_comp->environ_sz + new_comp->total_tls_size
== new_comp->scratch_mem_extra);

prepare_compartment_mapping(new_comp);

return new_comp;
}

Expand All @@ -220,9 +222,10 @@ comp_map(struct Compartment *to_map)
struct SegmentMap lib_dep_seg;
int lib_dep_fd;

void* map_result = mmap(to_map->base, (intptr_t) ((char*) to_map->mem_top - (char*) to_map->base),
PROT_READ | PROT_WRITE | PROT_EXEC, // TODO fix
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
void *map_result = mmap(to_map->base,
(intptr_t) ((char *) to_map->mem_top - (char *) to_map->base),
PROT_READ | PROT_WRITE | PROT_EXEC, // TODO fix
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (map_result == MAP_FAILED)
{
err(1, "Error mapping compartment %zu data", to_map->id);
Expand All @@ -236,9 +239,10 @@ comp_map(struct Compartment *to_map)
{
lib_dep_seg = lib_dep->lib_segs[j];
do_pread(lib_dep_fd,
(char*) lib_dep->lib_mem_base
+ (uintptr_t) lib_dep_seg.mem_bot,
lib_dep_seg.file_sz, lib_dep_seg.offset);
(char *) lib_dep->lib_mem_base
+ (uintptr_t) lib_dep_seg.mem_bot,
lib_dep_seg.file_sz, lib_dep_seg.offset);
// TODO mprotect
}
close(lib_dep_fd);
}
Expand Down Expand Up @@ -304,18 +308,19 @@ comp_map(struct Compartment *to_map)
}

void
comp_unmap(struct Compartment* to_unmap)
comp_unmap(struct Compartment *to_unmap)
{
int res;

res = munmap(to_unmap->base, (intptr_t) ((char*) to_unmap->mem_top - (char*) to_unmap->base));
res = munmap(to_unmap->base,
(intptr_t) ((char *) to_unmap->mem_top - (char *) to_unmap->base));
if (res == -1)
{
err(1, "Error unmapping compartment %zu data", to_unmap->id);
}

res
= munmap((void *) to_unmap->scratch_mem_base, to_unmap->scratch_mem_size);
res = munmap(
(void *) to_unmap->scratch_mem_base, to_unmap->scratch_mem_size);
if (res == -1)
{
err(1, "Error unmapping compartment %zu scratch memory", to_unmap->id);
Expand Down Expand Up @@ -487,8 +492,8 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp)
{
err(1, "Error accessing data for file %s", lib_path);
}
void* lib_data = mmap(NULL, lib_fd_stat.st_size, PROT_READ, MAP_PRIVATE,
lib_fd, 0);
void *lib_data
= mmap(NULL, lib_fd_stat.st_size, PROT_READ, MAP_PRIVATE, lib_fd, 0);
close(lib_fd);

// Read ELF headers
Expand Down Expand Up @@ -522,7 +527,7 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp)
Elf64_Shdr shstrtab_hdr;
get_lib_data(&shstrtab_hdr, lib_data, sizeof(Elf64_Shdr),
lib_ehdr.e_shoff + lib_ehdr.e_shstrndx * sizeof(Elf64_Shdr));
char* shstrtab = (char*) seek_lib_data(lib_data, shstrtab_hdr.sh_offset);
char *shstrtab = (char *) seek_lib_data(lib_data, shstrtab_hdr.sh_offset);

// XXX The string table is read in `strtab` as a sequence of
// variable-length strings. Then, symbol names are obtained by indexing at
Expand Down Expand Up @@ -596,8 +601,8 @@ parse_lib_file(char *lib_name, struct Compartment *new_comp)
}

static void
parse_lib_segs(Elf64_Ehdr *lib_ehdr, void* lib_data, struct LibDependency *lib_dep,
struct Compartment *new_comp)
parse_lib_segs(Elf64_Ehdr *lib_ehdr, void *lib_data,
struct LibDependency *lib_dep, struct Compartment *new_comp)
{
// Get segment data
Elf64_Phdr lib_phdr;
Expand Down Expand Up @@ -655,7 +660,7 @@ parse_lib_segs(Elf64_Ehdr *lib_ehdr, void* lib_data, struct LibDependency *lib_d
}

static void
parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data,
parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void *lib_data,
struct LibDependency *lib_dep)
{
// Get symbol table
Expand All @@ -664,8 +669,9 @@ parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data,
get_lib_data(&link_shdr, lib_data, sizeof(Elf64_Shdr),
lib_ehdr->e_shoff + symtb_shdr->sh_link * sizeof(Elf64_Shdr));

Elf64_Sym* sym_tb = (Elf64_Sym*) seek_lib_data(lib_data, symtb_shdr->sh_offset);
char* str_tb = (char*) seek_lib_data(lib_data, link_shdr.sh_offset);
Elf64_Sym *sym_tb
= (Elf64_Sym *) seek_lib_data(lib_data, symtb_shdr->sh_offset);
char *str_tb = (char *) seek_lib_data(lib_data, link_shdr.sh_offset);

size_t lib_syms_count = symtb_shdr->sh_size / sizeof(Elf64_Sym);
size_t actual_syms = 0;
Expand Down Expand Up @@ -699,23 +705,25 @@ parse_lib_symtb(Elf64_Shdr *symtb_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data,
}

static void
parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data,
parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void *lib_data,
struct LibDependency *lib_dep)
{
// Traverse `.rela.plt`, so we can see which function addresses we need
// to eagerly load
Elf64_Rela* rela_sec = (Elf64_Rela*) seek_lib_data(lib_data, rela_shdr->sh_offset);
Elf64_Rela *rela_sec
= (Elf64_Rela *) seek_lib_data(lib_data, rela_shdr->sh_offset);
size_t rela_count = rela_shdr->sh_size / sizeof(Elf64_Rela);

Elf64_Shdr dyn_sym_hdr;
get_lib_data(&dyn_sym_hdr, lib_data, sizeof(Elf64_Shdr),
lib_ehdr->e_shoff + rela_shdr->sh_link * sizeof(Elf64_Shdr));
Elf64_Sym *dyn_sym_tbl = (Elf64_Sym*) seek_lib_data(lib_data, dyn_sym_hdr.sh_offset);
Elf64_Sym *dyn_sym_tbl
= (Elf64_Sym *) seek_lib_data(lib_data, dyn_sym_hdr.sh_offset);

Elf64_Shdr dyn_str_hdr;
get_lib_data(&dyn_str_hdr, lib_data, sizeof(Elf64_Shdr),
lib_ehdr->e_shoff + dyn_sym_hdr.sh_link * sizeof(Elf64_Shdr));
char *dyn_str_tbl = (char*) seek_lib_data(lib_data, dyn_str_hdr.sh_offset);
char *dyn_str_tbl = (char *) seek_lib_data(lib_data, dyn_str_hdr.sh_offset);

// XXX Since TLSDESC entries might resolve to two relocation slots, we
// ensure we have enough space by doubling the expected relocation counts
Expand Down Expand Up @@ -873,14 +881,15 @@ parse_lib_rela(Elf64_Shdr *rela_shdr, Elf64_Ehdr *lib_ehdr, void* lib_data,

static void
parse_lib_dynamic_deps(Elf64_Shdr *dynamic_shdr, Elf64_Ehdr *lib_ehdr,
void* lib_data, struct LibDependency *lib_dep)
void *lib_data, struct LibDependency *lib_dep)
{
// Find additional library dependencies
Elf64_Dyn *dyn_entries = (Elf64_Dyn*) seek_lib_data(lib_data, dynamic_shdr->sh_offset);
Elf64_Dyn *dyn_entries
= (Elf64_Dyn *) seek_lib_data(lib_data, dynamic_shdr->sh_offset);
Elf64_Shdr dynstr_shdr;
get_lib_data(&dynstr_shdr, lib_data, sizeof(Elf64_Shdr),
lib_ehdr->e_shoff + dynamic_shdr->sh_link * sizeof(Elf64_Shdr));
char *dynstr_tbl = (char*) seek_lib_data(lib_data, dynstr_shdr.sh_offset);
char *dynstr_tbl = (char *) seek_lib_data(lib_data, dynstr_shdr.sh_offset);

for (size_t i = 0; i < dynamic_shdr->sh_size / sizeof(Elf64_Dyn); ++i)
{
Expand Down Expand Up @@ -1089,15 +1098,15 @@ do_pread(int fd, void *buf, size_t count, off_t offset)
}

static void
get_lib_data(void* buf, void* lib_file_addr, size_t data_sz, off_t offset)
get_lib_data(void *buf, void *lib_file_addr, size_t data_sz, off_t offset)
{
memcpy(buf, (char*) lib_file_addr + offset, data_sz);
memcpy(buf, (char *) lib_file_addr + offset, data_sz);
}

static void*
seek_lib_data(void* lib_data, off_t offset)
static void *
seek_lib_data(void *lib_data, off_t offset)
{
return (void*) ((char*) lib_data + offset);
return (void *) ((char *) lib_data + offset);
}

static void *
Expand Down Expand Up @@ -1235,7 +1244,8 @@ setup_environ(struct Compartment *new_comp)
{
assert(new_comp->cc->env_ptr != NULL); // TODO consider optional check
new_comp->environ_sz
= align_up(new_comp->cc->env_ptr_sz, new_comp->page_size) + new_comp->page_size;
= align_up(new_comp->cc->env_ptr_sz, new_comp->page_size)
+ new_comp->page_size;
new_comp->environ_ptr = get_extra_scratch_region_base(new_comp);
adjust_comp_scratch_mem(new_comp, new_comp->environ_sz);
}
Expand Down
4 changes: 2 additions & 2 deletions src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,14 @@ prepare_compartment_environ()
// We only save offsets for the pointers, since they'll be relocated
// relative to the compartment base address
proc_env_ptr[proc_env_count] = (char *) (vals_offset + proc_env_ptr_sz);
strcpy((char *) proc_env_ptr + vals_offset + proc_env_ptr_sz, *curr_env);
strcpy(
(char *) proc_env_ptr + vals_offset + proc_env_ptr_sz, *curr_env);

proc_env_count += 1;
proc_env_ptr_sz += strlen(*curr_env) + 1;
}
proc_env_ptr_sz += vals_offset;
proc_env_ptr = realloc(proc_env_ptr, proc_env_ptr_sz);

}

static void *
Expand Down
7 changes: 4 additions & 3 deletions tests/manager_caller_multiple.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
int
main(int argc, char **argv)
{
const char* count_env_name = "EXECUTE_COUNT";
const char* count_env_val = getenv(count_env_name);
const char *count_env_name = "EXECUTE_COUNT";
const char *count_env_val = getenv(count_env_name);
const unsigned int comps_count_default = 100;
unsigned int comps_count = count_env_val ? atoi(count_env_val) : comps_count_default;
unsigned int comps_count
= count_env_val ? atoi(count_env_val) : comps_count_default;

// Initial setup
manager_ddc = cheri_ddc_get();
Expand Down
1 change: 0 additions & 1 deletion tests/test_map_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ main()
comp_clean(hw_comp);
return 0;
}

0 comments on commit e478eb3

Please sign in to comment.