-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhl_pydriver.c
More file actions
370 lines (337 loc) · 11.1 KB
/
Copy pathhl_pydriver.c
File metadata and controls
370 lines (337 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* hl_pydriver — Python-runtime driver for Hyperlight.
*
* First call enters via main() (standard deferred-dispatch path that
* app-elfloader sets up). main() does the one-time Py_Initialize and
* warm-up imports, then installs an FC-aware dispatch callback via
* hyperlight_dispatch_register_v2(). It also handles the first call's
* user code so the caller gets their result.
*
* Every subsequent call goes through the v2 callback directly —
* dispatch_inner sees g_dispatch_callback != NULL and invokes it
* without touching the legacy g_run_callback that would otherwise
* re-enter main(). Python interpreter state (sys.modules, the GIL,
* heap allocations for numpy/pandas types) persists across calls.
*
* Flow:
* host: call("run", <code string>) ┐
* guest dispatch → deferred_run → main() │
* main reads HL_FC_{BYTES,LEN}_PTR from env │
* Py_Initialize() + warm-up imports │ ~2 s, once
* hyperlight_dispatch_register_v2(run_code) │
* run_code(fc_bytes, fc_len) ┘
* main returns, VM halts, host sees result
*
* host: call("run", <code string>) ┐
* guest dispatch → run_code(fc_bytes, fc_len)│ ~50 ms, subsequent
* PyRun_SimpleString(user code) │
* VM halts, host sees result ┘
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
/* -- Minimal FlatBuffers reader for the incoming FunctionCall ---------
* hl_pydriver only cares about the first string parameter (the user's
* Python source to exec). Hand-rolled so we don't depend on flatcc
* or the kernel's fb.h. Format: size-prefixed FunctionCall table
* with a parameters vector of Parameter(value_type=hlstring).
*/
static inline uint32_t fb_u32(const uint8_t *b, size_t o)
{
return b[o] | ((uint32_t)b[o+1] << 8) |
((uint32_t)b[o+2] << 16) | ((uint32_t)b[o+3] << 24);
}
static inline uint16_t fb_u16(const uint8_t *b, size_t o)
{
return b[o] | ((uint16_t)b[o+1] << 8);
}
static inline size_t fb_vtable(const uint8_t *b, size_t tbl)
{
return tbl - (int32_t)fb_u32(b, tbl);
}
static inline uint16_t fb_field(const uint8_t *b, size_t tbl, uint16_t vt)
{
size_t v = fb_vtable(b, tbl);
uint16_t vs = fb_u16(b, v);
return vt >= vs ? 0 : fb_u16(b, v + vt);
}
static inline size_t fb_follow(const uint8_t *b, size_t tbl, uint16_t vt)
{
uint16_t f = fb_field(b, tbl, vt);
if (!f) return 0;
size_t p = tbl + f;
return p + fb_u32(b, p);
}
/* Extract the first parameter as an hlstring (ParameterValue union
* discriminant = 7). Returns pointer into `fc` + length, or NULL on
* error or when arg 0 isn't a string.
*/
static const char *fc_arg0_string(const uint8_t *fc, size_t fc_len,
size_t *out_len)
{
if (fc_len < 8) return NULL;
size_t root = 4 + fb_u32(fc, 4);
size_t params = fb_follow(fc, root, 6);
if (!params) return NULL;
if (fb_u32(fc, params) == 0) return NULL;
size_t p0_pos = params + 4;
size_t p0 = p0_pos + fb_u32(fc, p0_pos);
uint16_t tf = fb_field(fc, p0, 4);
if (!tf) return NULL;
if (fc[p0 + tf] != 7 /* hlstring */) return NULL;
size_t hs = fb_follow(fc, p0, 6);
if (!hs) return NULL;
size_t s = fb_follow(fc, hs, 4);
if (!s || s + 4 > fc_len) return NULL;
uint32_t slen = fb_u32(fc, s);
if (s + 4 + slen > fc_len) return NULL;
*out_len = slen;
return (const char *)(fc + s + 4);
}
/* -- Kernel plumbing ------------------------------------------------
* Slot addresses injected by app-elfloader into our environ at boot.
* All kernel-side state, read/written by dereferencing.
*
* HL_FC_BYTES_PTR: kernel global (const uint8_t *) — bytes of
* the current incoming FunctionCall flatbuffer
* HL_FC_LEN_PTR: kernel global (size_t) — length of above
* HL_V2_CALLBACK_PTR: kernel global (hl_dispatch_fn_t) — the
* FC-aware callback the kernel dispatches to
* on every call after it's set
*/
static uintptr_t parse_hex(const char *s)
{
uintptr_t v = 0;
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
s += 2;
for (; *s; s++) {
unsigned d;
if (*s >= '0' && *s <= '9') d = *s - '0';
else if (*s >= 'a' && *s <= 'f') d = *s - 'a' + 10;
else if (*s >= 'A' && *s <= 'F') d = *s - 'A' + 10;
else break;
v = (v << 4) | d;
}
return v;
}
typedef void (*hl_dispatch_fn_t)(const uint8_t *fc, size_t fc_len);
static const uint8_t **g_fc_bytes_slot;
static size_t *g_fc_len_slot;
static hl_dispatch_fn_t *g_v2_callback_slot;
static uint64_t g_py_fsbase;
static inline uint64_t rdmsr_fsbase(void)
{
uint32_t lo, hi;
__asm__ volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(0xC0000100));
return ((uint64_t)hi << 32) | lo;
}
static inline void wrmsr_fsbase(uint64_t v)
{
uint32_t lo = (uint32_t)v, hi = (uint32_t)(v >> 32);
__asm__ volatile("wrmsr" : : "c"(0xC0000100), "a"(lo), "d"(hi));
}
/* -- Core work ------------------------------------------------------- */
static void report_exit_code(int code)
{
char req[128];
int n = snprintf(req, sizeof(req),
"{\"name\":\"__hl_exit\",\"args\":{\"code\":%d}}", code);
int fd = open("/dev/hcall", O_RDWR);
if (fd < 0)
return;
write(fd, req, n);
char resp[128];
read(fd, resp, sizeof(resp));
close(fd);
}
static int run_code_with_exceptions(const char *code)
{
PyObject *m = PyImport_AddModule("__main__");
if (!m) return 1;
PyObject *d = PyModule_GetDict(m);
if (!d) return 1;
PyObject *result = PyRun_String(code, Py_file_input, d, d);
if (result) {
Py_DECREF(result);
return 0;
}
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
PyObject *type, *value, *tb;
PyErr_Fetch(&type, &value, &tb);
PyErr_NormalizeException(&type, &value, &tb);
int exit_code = 1;
if (value) {
PyObject *ca = PyObject_GetAttrString(value, "code");
if (ca) {
if (PyLong_Check(ca)) {
exit_code = (int)PyLong_AsLong(ca);
} else if (ca == Py_None) {
exit_code = 0;
} else {
PyObject *s = PyObject_Str(ca);
if (s) {
const char *msg = PyUnicode_AsUTF8(s);
if (msg)
fprintf(stderr, "%s\n", msg);
Py_DECREF(s);
}
}
Py_DECREF(ca);
}
}
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(tb);
return exit_code;
}
PyErr_Print();
return 1;
}
static void py_run_user_code(const uint8_t *fc, size_t fc_len)
{
if (g_py_fsbase)
wrmsr_fsbase(g_py_fsbase);
size_t code_len = 0;
const char *code = fc_arg0_string(fc, fc_len, &code_len);
if (!code)
return;
char stack_buf[4096];
char *buf;
if (code_len < sizeof(stack_buf)) {
memcpy(stack_buf, code, code_len);
stack_buf[code_len] = '\0';
buf = stack_buf;
} else {
buf = malloc(code_len + 1);
if (!buf)
return;
memcpy(buf, code, code_len);
buf[code_len] = '\0';
}
int exit_code = run_code_with_exceptions(buf);
if (buf != stack_buf)
free(buf);
if (exit_code != 0)
report_exit_code(exit_code);
}
static void py_initialize_once(void)
{
Py_UTF8Mode = 1;
Py_Initialize();
PyRun_SimpleString(
"import sys\n"
"sys.argv = ['hl_pydriver']\n");
/* Pre-import the python-agent stack so every subsequent call
* through the v2 callback sees a warm sys.modules and pays only
* the user's own code cost. Best-effort: an import that fails
* just warns — the user's PyRun_SimpleString will raise its own
* traceback if they actually need that module. */
PyRun_SimpleString(
"import sys, importlib\n"
"for _mod in ("
" 'numpy', 'pandas', 'pydantic', 'yaml', 'jinja2',"
" 'bs4', 'tabulate', 'click', 'tenacity', 'tqdm',"
" 'openpyxl', 'pypdf', 'markdown_it', 'PIL', 'lxml',"
" 'cryptography', 'dateutil', 'dotenv',"
" 'docx', 'pptx'):\n"
" try:\n"
" importlib.import_module(_mod)\n"
" except Exception as _e:\n"
" sys.stderr.write(f'warn: preload {_mod} failed: {_e}\\n')\n");
/* Monkey-patch zipfile.ZipInfo.__init__ to clamp pre-1980 timestamps.
* Unikraft's ramfs reports epoch-0 (1970) for file mtimes; Python's
* zipfile rejects timestamps before 1980, breaking openpyxl's XLSX
* writer (XLSX is a ZIP container). */
PyRun_SimpleString(
"import zipfile as _zf\n"
"def _patch_zipinfo():\n"
" _orig = _zf.ZipInfo.__init__\n"
" def _safe(self, filename='NoName', date_time=(1980,1,1,0,0,0)):\n"
" if date_time[0] < 1980:\n"
" date_time = (1980,1,1,0,0,0)\n"
" _orig(self, filename, date_time)\n"
" _zf.ZipInfo.__init__ = _safe\n"
"_patch_zipinfo()\n"
"del _patch_zipinfo, _zf\n");
/* Monkey-patch time.sleep to call the host via /dev/hcall.
* Unikraft's cooperative scheduler on Hyperlight has no timer
* interrupt, so the kernel-level nanosleep is a no-op. This
* routes the sleep to the host thread which actually blocks. */
PyRun_SimpleString(
"import time as _hl_time\n"
"def _hl_sleep(secs):\n"
" if secs <= 0:\n"
" return\n"
" import json\n"
" fd = open('/dev/hcall', 'r+b', buffering=0)\n"
" fd.write(json.dumps("
"{'name':'__hl_sleep','args':{'ns':int(secs*1e9)}}).encode())\n"
" fd.read()\n"
" fd.close()\n"
"_hl_time.sleep = _hl_sleep\n"
"del _hl_time, _hl_sleep\n");
}
/* -- Entry points --------------------------------------------------- */
int main(int argc, char **argv, char **envp)
{
static int py_initialized;
(void)argc; (void)argv;
/* Resolve the slot addresses once. Injected by app-elfloader as
* env vars — we just parse the hex addresses. */
if (!g_fc_bytes_slot) {
for (char **p = envp; p && *p; p++) {
if (!strncmp(*p, "HL_FC_BYTES_PTR=", 16))
g_fc_bytes_slot = (const uint8_t **)
parse_hex(*p + 16);
else if (!strncmp(*p, "HL_FC_LEN_PTR=", 14))
g_fc_len_slot = (size_t *)
parse_hex(*p + 14);
else if (!strncmp(*p, "HL_V2_CALLBACK_PTR=", 19))
g_v2_callback_slot = (hl_dispatch_fn_t *)
parse_hex(*p + 19);
}
if (!g_fc_bytes_slot || !g_fc_len_slot
|| !g_v2_callback_slot) {
fprintf(stderr,
"hl_pydriver: HL_* env vars missing\n");
return 1;
}
}
if (!py_initialized) {
py_initialize_once();
/* Capture FS_BASE now — this is the TLS pointer Python's
* internals have wired themselves up against. Future v2
* callback entries will restore it before touching any
* Python state. */
g_py_fsbase = rdmsr_fsbase();
py_initialized = 1;
/* Install ourselves as the FC-aware dispatch callback. */
*g_v2_callback_slot = py_run_user_code;
}
const uint8_t *fc = *g_fc_bytes_slot;
size_t fc_len = *g_fc_len_slot;
if (fc && fc_len)
py_run_user_code(fc, fc_len);
fflush(stdout);
fflush(stderr);
/* Hand-rolled exit_group via inline syscall: skips glibc's
* exit() atexit chain AND any TLS state glibc's syscall()
* wrapper might touch (seen the latter corrupt Python's TLS
* between first-call halt and second-call re-entry in
* testing). The kernel's exit_group handler on
* Unikraft-Hyperlight halts the VM cleanly — same end effect
* as a normal return, just without the destructive cleanup.
*/
register long rax __asm__("rax") = 231; /* SYS_exit_group */
register long rdi __asm__("rdi") = 0;
__asm__ volatile("syscall" : : "r"(rax), "r"(rdi)
: "rcx", "r11", "memory");
/* not reached */
return 0;
}