Skip to content
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
15 changes: 11 additions & 4 deletions packages/std/packages/wrapper/include/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// Helper to allocate an array of n T.
#define ALLOC_N(arena, n, T) \
(T*)alloc(arena, ((size_t)(n)) * sizeof(T), _Alignof(T))
(T*)alloc_n(arena, (size_t)(n), sizeof(T), _Alignof(T))

// Helper to align `m` to `n`.
#define ALIGN(m, n) \
Expand All @@ -50,6 +50,7 @@ struct Segment {
TG_VISIBILITY void create_arena (Arena* arena, uint64_t page_size);
TG_VISIBILITY void destroy_arena (Arena* arena);
TG_VISIBILITY void* alloc (Arena* arena, size_t size, size_t alignment);
TG_VISIBILITY void* alloc_n (Arena* arena, size_t count, size_t size, size_t alignment);
TG_VISIBILITY void add_segment (Arena* arena, size_t num_pages);

// util methods that require allocation are defined here.
Expand Down Expand Up @@ -87,6 +88,7 @@ TG_VISIBILITY void destroy_arena (Arena* arena) {
TG_VISIBILITY void* alloc (Arena* arena, size_t size, size_t alignment) {
// Sanity check.
ABORT_IF((size % alignment) != 0, "internal error: misaligned allocation");
ABORT_IF(size > (SIZE_MAX >> 1), "internal error: allocation too large");

// Compute start/end of the allocation.
size_t start = ALIGN(arena->segment->offset, alignment);
Expand All @@ -99,7 +101,7 @@ TG_VISIBILITY void* alloc (Arena* arena, size_t size, size_t alignment) {
size_t min_num_pages = min_size / arena->page_size;

// The number of pages we use is the MAX(min_num_pages, DEFAULT_NUM_PAGES).
size_t num_pages = min_num_pages < DEFAULT_NUM_PAGES ? DEFAULT_NUM_PAGES : min_size;
size_t num_pages = min_num_pages < DEFAULT_NUM_PAGES ? DEFAULT_NUM_PAGES : min_num_pages;

// Add a new segment.
add_segment(arena, num_pages);
Expand All @@ -119,6 +121,11 @@ TG_VISIBILITY void* alloc (Arena* arena, size_t size, size_t alignment) {
return (void*)pointer;
}

TG_VISIBILITY void* alloc_n (Arena* arena, size_t count, size_t size, size_t alignment) {
ABORT_IF(size != 0 && count > SIZE_MAX / size, "internal error: array allocation overflow");
return alloc(arena, count * size, alignment);
}

TG_VISIBILITY void add_segment (Arena* arena, size_t num_pages) {
// Sanity check.
ABORT_IF(num_pages == 0, "internal: invalid argument");
Expand Down Expand Up @@ -184,7 +191,7 @@ TG_VISIBILITY String join (Arena* arena, String separator, String* strings, size
TG_VISIBILITY void u64_to_string (Arena* arena, uint64_t d, String* s) {
s->ptr = ALLOC_N(arena, 64, uint8_t);
do {
append_ch_to_string(s, "012345689"[d % 10], 64);
append_ch_to_string(s, "0123456789"[d % 10], 64);
d /= 10;
} while (d != 0);
reverse(s);
Expand All @@ -200,7 +207,7 @@ TG_VISIBILITY void double_to_string (Arena* arena, double d, String* s) {
ABORT_IF(frac != 0, "only integer numbers are supported");

do {
append_ch_to_string(s, "012345689"[whole % 10], 64);
append_ch_to_string(s, "0123456789"[whole % 10], 64);
whole /= 10;
} while (whole != 0);

Expand Down
4 changes: 2 additions & 2 deletions packages/std/packages/wrapper/include/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ TG_VISIBILITY int munmap (void* addr, uint64_t len) {
return (int)syscall2(__NR_munmap, (long)addr, (long)len);
}

TG_VISIBILITY int pread (int fd, void* buf, size_t count, off_t offset) {
return (int)syscall4(__NR_pread64, (long)fd, (long)buf, (long)count, (long)offset);
TG_VISIBILITY int64_t pread (int fd, void* buf, size_t count, off_t offset) {
return (int64_t)syscall4(__NR_pread64, (long)fd, (long)buf, (long)count, (long)offset);
}

TG_VISIBILITY int execve (char* pathname, char** argv, char** envp) {
Expand Down
6 changes: 3 additions & 3 deletions packages/std/packages/wrapper/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ TG_VISIBILITY bool streq (String a, String b) {
}

TG_VISIBILITY bool cstreq (String s, const char* cstr) {
for (int i = 0; i < s.len; i++) {
if (s.ptr[i] != cstr[i]) {
for (size_t i = 0; i < s.len; i++) {
if (cstr[i] == 0 || s.ptr[i] != cstr[i]) {
return false;
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ TG_VISIBILITY void read_all (int tracing, int fd, char* dst, size_t length, off_
trace("read_all length:%ld offset:%ld\n", length, offset);
}
while(length) {
int result = pread(fd, (void*)dst, length, offset);
int64_t result = pread(fd, (void*)dst, length, offset);
if (tracing) {
trace("read_all result = %d\n", result);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/std/packages/wrapper/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ int main (int argc, char** argv) {
int nphdr = -1;
int nphnum = -1;
for (int i = 0; i < stack.auxc; i++) {
if (nentry >= 0 && nbase >= 0) {
break;
}
switch(stack.auxv[i].a_type) {
case AT_PHDR: {
ABORT_IF(nphdr >= 0, "duplicate AT_PHDR");
Expand Down Expand Up @@ -485,11 +482,13 @@ TG_VISIBILITY Executable create_executable (Arena* arena, Stack* stack, Options*
data = alloc(arena, section_itr->sh_size, 1);
size = section_itr->sh_size;
offset = section_itr->sh_offset;
ABORT_IF(size < sizeof(Footer), "manifest section too small");
if (options->enable_tracing) {
trace("reading manifest at offset: %ld, size: %ld\n", offset, size);
}
read_all(options->enable_tracing, fd, data, size, offset);
memcpy((void*)&executable.footer, (void*)(data + (size - sizeof(Footer))), sizeof(Footer));
ABORT_IF(executable.footer.size > size - sizeof(Footer), "invalid footer");
break;
}
}
Expand Down Expand Up @@ -1079,7 +1078,7 @@ TG_VISIBILITY void* prepare_executable_stack (
}

// Push aux vector in reverse order.
int x = stack->auxc;
int x = stack->auxc - 1;
for (; x >= 0; x--) {
Elf64_auxv_t* v = &stack->auxv[x];
push_auxv(&sp, v);
Expand Down