Skip to content

Commit b8fb86a

Browse files
committed
wip
1 parent 15057a3 commit b8fb86a

File tree

8 files changed

+446
-115
lines changed

8 files changed

+446
-115
lines changed

packages/std/packages/stub/include/manifest.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
#include "table.h"
88
#include "util.h"
99

10+
enum {
11+
INTERPRETER_KIND_NORMAL,
12+
INTERPRETER_KIND_LD_LINUX,
13+
INTERPRETER_KIND_LD_MUSL
14+
};
15+
1016
typedef struct {
1117
uint64_t entrypoint;
1218
String executable;
1319
String interpreter;
20+
uint64_t interpreter_kind;
1421
size_t num_library_paths;
1522
String* library_paths;
1623
size_t num_preloads;
@@ -19,6 +26,8 @@ typedef struct {
1926
String* argv;
2027
size_t interp_argc;
2128
String* interp_argv;
29+
String ld_library_path;
30+
String ld_preload;
2231
Table env;
2332
} Manifest;
2433

packages/std/packages/stub/include/table.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ static int insert (
7474
// Search for the key in the table.
7575
uint64_t index = hash % table->capacity;
7676
Node* node = table->list + index;
77-
while(node) {
77+
78+
for(;;) {
7879
// If this is an empty node, use it.
7980
if (node->key.ptr == 0) {
8081
node->key.ptr = key.ptr;
@@ -91,14 +92,16 @@ static int insert (
9192
node->val.len = val.len;
9293
return 0;
9394
}
94-
node = node->next;
95+
96+
if (node->next) {
97+
node = node->next;
98+
} else {
99+
break;
100+
}
95101
}
96102

97103
// Allocate a new node.
98-
Node* new_node = (Node*)alloc(arena, sizeof(Node), _Alignof(Node));
99-
if (!new_node) {
100-
return -1;
101-
}
104+
Node* new_node = ALLOC(arena, Node);
102105
new_node->key.ptr = key.ptr;
103106
new_node->key.len = key.len;
104107
new_node->val.ptr = val.ptr;

packages/std/packages/stub/include/util.h

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#pragma once
2-
#if BREAKPOINTS
3-
#define BREAK asm volatile ("int3");
4-
#else
5-
#define BREAK
6-
#endif
72

83
// Common includes.
94
#include <elf.h>
@@ -32,7 +27,7 @@ static size_t strlen_including_nul (const char* str) {
3227

3328
static size_t strlen (const char* s) {
3429
size_t n = 0;
35-
for (; s[n] != 0; n++) {}
30+
for (; s[n]; n++) {}
3631
return n;
3732
}
3833

@@ -44,7 +39,6 @@ struct PathComponent {
4439
String contents;
4540
};
4641

47-
4842
static String parent_dir (String path) {
4943
// Edge case: root directory.
5044
for(int i = 0; i < 2; i++) {
@@ -194,3 +188,42 @@ static void double_to_string (Arena* arena, double d, String* s) {
194188

195189
reverse(s);
196190
}
191+
192+
static uint64_t parse_uint_from_hex (String s) {
193+
ABORT_IF(s.len < 16, "invalid string");
194+
uint64_t u = 0;
195+
for (int ch = 0; ch < 16; ch++) {
196+
u <<= 4;
197+
switch(s.ptr[ch]) {
198+
case '0': u |= 0x0; break;
199+
case '1': u |= 0x1; break;
200+
case '2': u |= 0x2; break;
201+
case '3': u |= 0x3; break;
202+
case '4': u |= 0x4; break;
203+
case '5': u |= 0x5; break;
204+
case '6': u |= 0x6; break;
205+
case '7': u |= 0x7; break;
206+
case '8': u |= 0x8; break;
207+
case '9': u |= 0x9; break;
208+
case 'a': u |= 0xa; break;
209+
case 'b': u |= 0xb; break;
210+
case 'c': u |= 0xc; break;
211+
case 'd': u |= 0xd; break;
212+
case 'e': u |= 0xe; break;
213+
case 'f': u |= 0xf; break;
214+
default: exit(2);
215+
}
216+
}
217+
return u;
218+
}
219+
220+
static String serialize_uint_to_hex (Arena* arena, uint64_t u) {
221+
String s = {0};
222+
s.ptr = ALLOC_N(arena, 17, uint8_t);
223+
s.len = 16;
224+
for (uint64_t ch = 0; ch < 16; ch++) {
225+
uint8_t nibble = 0xf & (u >> 60 - (4 * ch));
226+
s.ptr[ch] = "0123456789abcdef"[nibble];
227+
}
228+
return s;
229+
}

packages/std/packages/stub/include/x86_64/debug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#ifdef SOFTWARE_BREAKPOINTS
2+
#ifdef BREAKPOINTS
33
#define BREAK do { asm volatile ("int3"); } while (0)
44
#else
55
#define BREAK

packages/std/packages/stub/src/manifest.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,40 @@ void parse_manifest (
8080
create_manifest_from_value(&cx, &value);
8181
}
8282

83+
String true_ = STRING_LITERAL("true");
84+
String clear_ld_library_path = STRING_LITERAL("TANGRAM_CLEAR_LD_LIBRARY_PATH");
85+
String clear_ld_preload = STRING_LITERAL("TANGRAM_CLEAR_LD_PRELOAD");
86+
String restore_ld_library_path = STRING_LITERAL("TANGRAM_RESTORE_LD_LIBRARY_PATH");
87+
String restore_ld_preload = STRING_LITERAL("TANGRAM_RESTORE_LD_PRELOAD");
88+
8389
// Render paths.
84-
String ld_library_path = render_ld_library_path(arena, manifest);
85-
if (ld_library_path.ptr) {
90+
manifest-> ld_library_path = render_ld_library_path(arena, manifest);
91+
if (manifest->ld_library_path.ptr) {
8692
String key = STRING_LITERAL("LD_LIBRARY_PATH");
87-
insert(arena, &manifest->env, key, ld_library_path);
93+
String val = lookup(&manifest->env, key);
94+
if (val.ptr) {
95+
String ss[2] = { val, manifest->ld_library_path };
96+
String s = STRING_LITERAL(":");
97+
manifest->ld_library_path = join(arena, s, ss, 2);
98+
insert(arena, &manifest->env, restore_ld_library_path, val);
99+
} else {
100+
insert(arena, &manifest->env, clear_ld_library_path, true_);
101+
}
102+
insert(arena, &manifest->env, key, manifest->ld_library_path);
88103
}
89-
90-
String ld_preload = render_ld_preload(arena, manifest);
91-
if (ld_preload.ptr) {
104+
manifest->ld_preload = render_ld_preload(arena, manifest);
105+
if (manifest->ld_preload.ptr) {
92106
String key = STRING_LITERAL("LD_PRELOAD");
93-
insert(arena, &manifest->env, key, ld_preload);
107+
String val = lookup(&manifest->env, key);
108+
if (val.ptr) {
109+
String ss[2] = { val, manifest->ld_preload };
110+
String s = STRING_LITERAL(":");
111+
manifest->ld_preload = join(arena, s, ss, 2);
112+
insert(arena, &manifest->env, restore_ld_preload, val);
113+
} else {
114+
insert(arena, &manifest->env, clear_ld_preload, true_);
115+
}
116+
insert(arena, &manifest->env, key, manifest->ld_preload);
94117
}
95-
96118
}
97119
#undef PATH_MAX

packages/std/packages/stub/src/manifest/json.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ static void create_interpreter (Cx* cx, JsonValue* value) {
4646
JsonValue* kind = json_get(object, "kind");
4747
ABORT_IF(!kind, "expected a kind string");
4848
ABORT_IF(kind->kind != JSON_STRING, "expected a string");
49-
if (
50-
cstreq(kind->value._string, "normal")
51-
|| cstreq(kind->value._string, "ld-musl")
52-
|| cstreq(kind->value._string, "ld-musl")
53-
) {
54-
// ok
49+
if (cstreq(kind->value._string, "normal")) {
50+
cx->manifest->interpreter_kind = INTERPRETER_KIND_NORMAL;
51+
} else if (cstreq(kind->value._string, "ld-linux")) {
52+
cx->manifest->interpreter_kind = INTERPRETER_KIND_LD_LINUX;
53+
} else if (cstreq(kind->value._string, "ld-musl")) {
54+
cx->manifest->interpreter_kind = INTERPRETER_KIND_LD_MUSL;
5555
} else if (cstreq(kind->value._string, "dyld")) {
5656
ABORT("dyld interpreter is unsupported in this context");
5757
} else {
58-
ABORT("unknown interpreter kind");
58+
char* s = cstr(cx->arena, kind->value._string);
59+
ABORT("unknown interpreter kind %s", s);
5960
}
60-
6161
JsonValue* path = json_get(object, "path");
6262
JsonValue* library_paths = json_get(object, "libraryPaths");
6363
JsonValue* preloads = json_get(object, "preloads");

0 commit comments

Comments
 (0)