Skip to content

Commit bb258c1

Browse files
committed
fix(pydriver): replace strtoul with inline parse_hex
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 62f6666 commit bb258c1

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

examples/python-agent-driver/hl_pydriver.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,28 @@ static const char *fc_arg0_string(const uint8_t *fc, size_t fc_len,
110110
* FC-aware callback the kernel dispatches to
111111
* on every call after it's set
112112
*/
113+
static uintptr_t parse_hex(const char *s)
114+
{
115+
uintptr_t v = 0;
116+
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
117+
s += 2;
118+
for (; *s; s++) {
119+
unsigned d;
120+
if (*s >= '0' && *s <= '9') d = *s - '0';
121+
else if (*s >= 'a' && *s <= 'f') d = *s - 'a' + 10;
122+
else if (*s >= 'A' && *s <= 'F') d = *s - 'A' + 10;
123+
else break;
124+
v = (v << 4) | d;
125+
}
126+
return v;
127+
}
128+
113129
typedef void (*hl_dispatch_fn_t)(const uint8_t *fc, size_t fc_len);
114130

115131
static const uint8_t **g_fc_bytes_slot;
116132
static size_t *g_fc_len_slot;
117133
static hl_dispatch_fn_t *g_v2_callback_slot;
118134

119-
/* Saved FS_BASE value captured right after Py_Initialize / warm-up
120-
* finishes. Restored at the head of every v2-callback invocation so
121-
* Python's TLS pointer stays valid even if something in the dispatch
122-
* preamble (dispatch_prepare's MSR restore, Hyperlight's own
123-
* save/restore of segment state) leaves FS_BASE pointing elsewhere.
124-
*/
125135
static uint64_t g_py_fsbase;
126136

127137
static inline uint64_t rdmsr_fsbase(void)
@@ -158,7 +168,6 @@ static int run_code_with_exceptions(const char *code)
158168
if (!m) return 1;
159169
PyObject *d = PyModule_GetDict(m);
160170
if (!d) return 1;
161-
162171
PyObject *result = PyRun_String(code, Py_file_input, d, d);
163172
if (result) {
164173
Py_DECREF(result);
@@ -307,14 +316,14 @@ int main(int argc, char **argv, char **envp)
307316
if (!g_fc_bytes_slot) {
308317
for (char **p = envp; p && *p; p++) {
309318
if (!strncmp(*p, "HL_FC_BYTES_PTR=", 16))
310-
g_fc_bytes_slot = (const uint8_t **)(uintptr_t)
311-
strtoul(*p + 16, NULL, 16);
319+
g_fc_bytes_slot = (const uint8_t **)
320+
parse_hex(*p + 16);
312321
else if (!strncmp(*p, "HL_FC_LEN_PTR=", 14))
313-
g_fc_len_slot = (size_t *)(uintptr_t)
314-
strtoul(*p + 14, NULL, 16);
322+
g_fc_len_slot = (size_t *)
323+
parse_hex(*p + 14);
315324
else if (!strncmp(*p, "HL_V2_CALLBACK_PTR=", 19))
316-
g_v2_callback_slot = (hl_dispatch_fn_t *)(uintptr_t)
317-
strtoul(*p + 19, NULL, 16);
325+
g_v2_callback_slot = (hl_dispatch_fn_t *)
326+
parse_hex(*p + 19);
318327
}
319328
if (!g_fc_bytes_slot || !g_fc_len_slot
320329
|| !g_v2_callback_slot) {

0 commit comments

Comments
 (0)