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 d43f2b8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/compartment.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct CompConfig
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 Down
85 changes: 47 additions & 38 deletions src/compartment.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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
Expand All @@ -42,9 +44,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 @@ -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,9 @@ 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);
}
close(lib_dep_fd);
}
Expand Down Expand Up @@ -304,18 +307,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 +491,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 +526,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 +600,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 +659,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 +668,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 +704,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 +880,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 +1097,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 +1243,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 d43f2b8

Please sign in to comment.