infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
executor.c
Go to the documentation of this file.
1
38#include "common/utility.h"
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43// Platform-Specific Includes
44#if defined(INFIX_OS_WINDOWS)
45#include <windows.h>
46#else
47#include <errno.h>
48#include <fcntl.h>
49#include <pthread.h>
50#include <sys/mman.h>
51#include <sys/types.h>
52#include <unistd.h>
53#endif
54#if defined(INFIX_OS_MACOS)
55#include <dlfcn.h>
56#include <libkern/OSCacheControl.h>
57#endif
58// Polyfills for mmap flags for maximum POSIX compatibility.
59#if defined(INFIX_ENV_POSIX) && !defined(INFIX_OS_WINDOWS)
60#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
61#define MAP_ANON MAP_ANONYMOUS
62#endif
63static pthread_mutex_t g_dwarf_mutex = PTHREAD_MUTEX_INITIALIZER;
64#endif
65
66#if defined(INFIX_OS_WINDOWS) && defined(INFIX_ARCH_X64)
67// SEH Unwind Info Opcodes and Structures for JIT code on Windows x64.
68// These are defined in winnt.h but we redefine them here for clarity and to ensure availability.
69#define UWOP_PUSH_NONVOL 0
70#define UWOP_ALLOC_LARGE 1
71#define UWOP_ALLOC_SMALL 2
72#define UWOP_SET_FPREG 3
73
74#pragma pack(push, 1)
75typedef struct _UNWIND_CODE {
76 uint8_t CodeOffset;
77 uint8_t UnwindOp : 4;
78 uint8_t OpInfo : 4;
79} UNWIND_CODE;
80
81typedef struct _UNWIND_INFO {
82 uint8_t Version : 3;
83 uint8_t Flags : 5;
84 uint8_t SizeOfPrologue;
85 uint8_t CountOfCodes;
86 uint8_t FrameRegister : 4;
87 uint8_t FrameOffset : 4;
88 UNWIND_CODE UnwindCode[1]; // Variable length array
89} UNWIND_INFO;
90#pragma pack(pop)
91
92// We reserve 512 bytes at the end of every JIT block for SEH metadata.
93#define INFIX_SEH_METADATA_SIZE 256
94#elif defined(INFIX_OS_WINDOWS) && defined(INFIX_ARCH_AARCH64)
95#pragma pack(push, 1)
96typedef struct _UNWIND_INFO_ARM64 {
97 uint32_t FunctionLength : 18;
98 uint32_t Version : 2;
99 uint32_t X : 1;
100 uint32_t E : 1;
101 uint32_t EpilogueCount : 5;
102 uint32_t CodeWords : 5;
103} UNWIND_INFO_ARM64;
104#pragma pack(pop)
105#define INFIX_SEH_METADATA_SIZE 256
106#else
107#define INFIX_SEH_METADATA_SIZE 0
108#endif
109
110// macOS JIT Security Hardening Logic
111#if defined(INFIX_OS_MACOS)
126typedef const struct __CFString * CFStringRef;
127typedef const void * CFTypeRef;
128typedef struct __SecTask * SecTaskRef;
129typedef struct __CFError * CFErrorRef;
130#define kCFStringEncodingUTF8 0x08000100
131// A struct to hold dynamically loaded function pointers from macOS frameworks.
132static struct {
133 void (*CFRelease)(CFTypeRef);
134 bool (*CFBooleanGetValue)(CFTypeRef boolean);
135 CFStringRef (*CFStringCreateWithCString)(CFTypeRef allocator, const char * cStr, uint32_t encoding);
136 CFTypeRef kCFAllocatorDefault;
137 SecTaskRef (*SecTaskCreateFromSelf)(CFTypeRef allocator);
138 CFTypeRef (*SecTaskCopyValueForEntitlement)(SecTaskRef task, CFStringRef entitlement, CFErrorRef * error);
139 void (*pthread_jit_write_protect_np)(int enabled);
140 void (*sys_icache_invalidate)(void * start, size_t len);
141} g_macos_apis;
149static void initialize_macos_apis(void) {
150 // We don't need to link against these frameworks, which makes building simpler.
151 void * cf = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
152 void * sec = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY);
153
154 // Hardened Runtime helpers found in libSystem/libpthread
155 g_macos_apis.pthread_jit_write_protect_np = dlsym(RTLD_DEFAULT, "pthread_jit_write_protect_np");
156 g_macos_apis.sys_icache_invalidate = dlsym(RTLD_DEFAULT, "sys_icache_invalidate");
157
158 if (!cf || !sec) {
159 INFIX_DEBUG_PRINTF("Warning: Could not dlopen macOS frameworks. JIT security features will be degraded.");
160 if (cf)
161 dlclose(cf);
162 if (sec)
163 dlclose(sec);
164 return;
165 }
166 g_macos_apis.CFRelease = dlsym(cf, "CFRelease");
167 g_macos_apis.CFBooleanGetValue = dlsym(cf, "CFBooleanGetValue");
168 g_macos_apis.CFStringCreateWithCString = dlsym(cf, "CFStringCreateWithCString");
169 void ** pAlloc = (void **)dlsym(cf, "kCFAllocatorDefault");
170 if (pAlloc)
171 g_macos_apis.kCFAllocatorDefault = *pAlloc;
172 g_macos_apis.SecTaskCreateFromSelf = dlsym(sec, "SecTaskCreateFromSelf");
173 g_macos_apis.SecTaskCopyValueForEntitlement = dlsym(sec, "SecTaskCopyValueForEntitlement");
174 dlclose(cf);
175 dlclose(sec);
176}
182static bool has_jit_entitlement(void) {
183 // Use pthread_once to ensure the dynamic loading happens exactly once, thread-safely.
184 static pthread_once_t init_once = PTHREAD_ONCE_INIT;
185 pthread_once(&init_once, initialize_macos_apis);
186
187 // Secure JIT path on macOS requires both the entitlement check and the toggle API.
188 if (!g_macos_apis.pthread_jit_write_protect_np)
189 return false;
190
191 if (!g_macos_apis.SecTaskCopyValueForEntitlement || !g_macos_apis.CFStringCreateWithCString)
192 return false;
193 bool result = false;
194 SecTaskRef task = g_macos_apis.SecTaskCreateFromSelf(g_macos_apis.kCFAllocatorDefault);
195 if (!task)
196 return false;
197 CFStringRef key = g_macos_apis.CFStringCreateWithCString(
198 g_macos_apis.kCFAllocatorDefault, "com.apple.security.cs.allow-jit", kCFStringEncodingUTF8);
199 CFTypeRef value = nullptr;
200 if (key) {
201 // This is the core check: ask the system for the value of the entitlement.
202 value = g_macos_apis.SecTaskCopyValueForEntitlement(task, key, nullptr);
203 g_macos_apis.CFRelease(key);
204 }
205 g_macos_apis.CFRelease(task);
206 if (value) {
207 // The value of the entitlement is a CFBoolean, so we must extract its value.
208 if (g_macos_apis.CFBooleanGetValue && g_macos_apis.CFBooleanGetValue(value))
209 result = true;
210 g_macos_apis.CFRelease(value);
211 }
212 return result;
213}
214#endif // INFIX_OS_MACOS
215// Hardened POSIX Anonymous Shared Memory Allocator (for Dual-Mapping W^X)
216#if !defined(INFIX_OS_WINDOWS) && !defined(INFIX_OS_MACOS) && !defined(INFIX_OS_ANDROID) && !defined(INFIX_OS_OPENBSD)
217#include <fcntl.h>
218#include <stdint.h>
219#if defined(INFIX_OS_LINUX) && defined(_GNU_SOURCE)
220#include <sys/syscall.h>
221#endif
222
232static int create_anonymous_file(void) {
233#if defined(INFIX_OS_LINUX) && defined(MFD_CLOEXEC)
234 // Strategy 1: memfd_create (Linux 3.17+)
235 // MFD_CLOEXEC ensures the FD isn't leaked to child processes.
236 int linux_fd = memfd_create("infix_jit", MFD_CLOEXEC);
237 if (linux_fd >= 0)
238 return linux_fd;
239 // If it fails (e.g. old kernel, ENOSYS), fall through to shm_open.
240#endif
241
242#if defined(__FreeBSD__) && defined(SHM_ANON)
243 // Strategy 2: SHM_ANON (FreeBSD)
244 int bsd_fd = shm_open(SHM_ANON, O_RDWR | O_CREAT | O_EXCL, 0600);
245 if (bsd_fd >= 0)
246 return bsd_fd;
247#endif
248
249 // Strategy 3: shm_open with randomized name (Legacy POSIX)
250 char shm_name[64];
251 uint64_t random_val = 0;
252 // Generate a sufficiently random name to avoid collisions if multiple processes
253 // are running this code simultaneously. Using /dev/urandom is a robust way to do this.
254 int rand_fd = open("/dev/urandom", O_RDONLY);
255 if (rand_fd < 0)
256 return -1;
257 ssize_t bytes_read = read(rand_fd, &random_val, sizeof(random_val));
258 close(rand_fd);
259 if (bytes_read != sizeof(random_val))
260 return -1;
261
262 snprintf(shm_name, sizeof(shm_name), "/infix-jit-%d-%llx", getpid(), (unsigned long long)random_val);
263 // Create the shared memory object exclusively.
264 int fd = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0600);
265 if (fd >= 0) {
266 // Unlink immediately. The name is removed, but the inode persists until close().
267 shm_unlink(shm_name);
268 return fd;
269 }
270 return -1;
271}
272#endif
273// Public API: Executable Memory Management
282#if defined(INFIX_OS_WINDOWS)
283 infix_executable_t exec = {
284 .rx_ptr = nullptr, .rw_ptr = nullptr, .size = 0, .handle = nullptr, .seh_registration = nullptr};
285#else
286 infix_executable_t exec = {.rx_ptr = nullptr, .rw_ptr = nullptr, .size = 0, .shm_fd = -1, .eh_frame_ptr = nullptr};
287#endif
288 if (size == 0)
289 return exec;
290
291#if defined(INFIX_OS_WINDOWS)
292 // Add headroom for SEH metadata on Windows.
293 size_t total_size = size + INFIX_SEH_METADATA_SIZE;
294
295 // Windows: Single-mapping W^X. Allocate as RW, later change to RX via VirtualProtect.
296 void * code = VirtualAlloc(nullptr, total_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
297 if (code == nullptr) {
300 return exec;
301 }
302 exec.rw_ptr = code;
303 exec.rx_ptr = code;
304#elif defined(INFIX_OS_MACOS) || defined(INFIX_OS_ANDROID) || defined(INFIX_OS_OPENBSD) || defined(INFIX_OS_DRAGONFLY)
305 // Single-mapping POSIX platforms. Allocate as RW, later change to RX via mprotect.
306 void * code = MAP_FAILED;
307#if defined(MAP_ANON)
308 int flags = MAP_PRIVATE | MAP_ANON;
309#if defined(INFIX_OS_MACOS)
310 // On macOS, we perform a one-time check for JIT support.
311 static bool g_use_secure_jit_path = false;
312 static bool g_checked_jit_support = false;
313 if (!g_checked_jit_support) {
314 g_use_secure_jit_path = has_jit_entitlement();
315 INFIX_DEBUG_PRINTF("macOS JIT check: Entitlement found = %s. Using %s API.",
316 g_use_secure_jit_path ? "yes" : "no",
317 g_use_secure_jit_path ? "secure (MAP_JIT)" : "legacy (mprotect)");
318 g_checked_jit_support = true;
319 }
320 // If entitled, use the modern, more secure MAP_JIT flag.
321 if (g_use_secure_jit_path)
322 flags |= MAP_JIT;
323#endif // INFIX_OS_MACOS
324 code = mmap(nullptr, size, PROT_READ | PROT_WRITE, flags, -1, 0);
325#if defined(INFIX_OS_MACOS)
326 if (code != MAP_FAILED && g_use_secure_jit_path) {
327 // Switch thread to Write mode. enabled=0 means Write allowed.
328 g_macos_apis.pthread_jit_write_protect_np(0);
329 }
330#endif
331#endif // MAP_ANON
332 if (code == MAP_FAILED) { // Fallback for older systems without MAP_ANON
333 int fd = open("/dev/zero", O_RDWR);
334 if (fd != -1) {
335 code = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
336 close(fd);
337 }
338 }
339 if (code == MAP_FAILED) {
341 return exec;
342 }
343 exec.rw_ptr = code;
344 exec.rx_ptr = code;
345#else
346 // Dual-mapping POSIX platforms (e.g., Linux, FreeBSD). Create two separate views of the same memory.
348 if (exec.shm_fd < 0) {
350 INFIX_CATEGORY_ALLOCATION, INFIX_CODE_EXECUTABLE_MEMORY_FAILURE, errno, "create_anonymous_file failed");
351 return exec;
352 }
353 if (ftruncate(exec.shm_fd, size) != 0) {
356 close(exec.shm_fd);
357 exec.shm_fd = -1; // Ensure clean state
358 return exec;
359 }
360 // The RW mapping.
361 exec.rw_ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, exec.shm_fd, 0);
362 // The RX mapping of the exact same physical memory.
363 exec.rx_ptr = mmap(nullptr, size, PROT_READ | PROT_EXEC, MAP_SHARED, exec.shm_fd, 0);
364 // If either mapping fails, clean up both and return an error.
365 if (exec.rw_ptr == MAP_FAILED || exec.rx_ptr == MAP_FAILED) {
366 int err = errno; // Capture errno before cleanup
367 if (exec.rw_ptr != MAP_FAILED)
368 munmap(exec.rw_ptr, size);
369 if (exec.rx_ptr != MAP_FAILED)
370 munmap(exec.rx_ptr, size);
371 close(exec.shm_fd);
373 return (infix_executable_t){.rx_ptr = nullptr, .rw_ptr = nullptr, .size = 0, .shm_fd = -1};
374 }
375
376 // The mmap mappings hold a reference to the shared memory object, so we don't
377 // need the FD anymore. Keeping it open consumes a file descriptor per trampoline,
378 // causing "shm_open failed" after ~1024 trampolines.
379 close(exec.shm_fd);
380 exec.shm_fd = -1;
381#endif
382 exec.size = size;
383 INFIX_DEBUG_PRINTF("Allocated JIT memory. RW at %p, RX at %p", exec.rw_ptr, exec.rx_ptr);
384 return exec;
385}
386
387#if defined(INFIX_OS_WINDOWS)
398static EXCEPTION_DISPOSITION _infix_seh_personality_routine(PEXCEPTION_RECORD ExceptionRecord,
399 void * EstablisherFrame,
400 c23_maybe_unused PCONTEXT ContextRecord,
401 void * DispatcherContext) {
402 PDISPATCHER_CONTEXT dc = (PDISPATCHER_CONTEXT)DispatcherContext;
403
404 // If we are already unwinding, don't do anything.
405 if (ExceptionRecord->ExceptionFlags & (EXCEPTION_UNWINDING | EXCEPTION_EXIT_UNWIND))
406 return ExceptionContinueSearch;
407
408 // Set the thread-local error.
410
411 // Retrieve the target epilogue IP from our HandlerData.
412 // The HandlerData points to the 4-byte epilogue offset we stored in UNWIND_INFO.
413 uint32_t epilogue_offset = *(uint32_t *)dc->HandlerData;
414 void * target_ip = (void *)(dc->ImageBase + epilogue_offset);
415
416 // Perform a non-local unwind to the epilogue.
417 RtlUnwind(EstablisherFrame, target_ip, ExceptionRecord, nullptr);
418
419 return ExceptionContinueSearch; // Unreachable
420}
421
422#if defined(INFIX_ARCH_X64)
423// Internal: Populates and registers SEH metadata for a Windows x64 JIT block.
424static void _infix_register_seh_windows_x64(infix_executable_t * exec,
426 uint32_t prologue_size,
427 uint32_t epilogue_offset) {
428 // metadata_ptr starts after the machine code.
429 uint8_t * metadata_base = (uint8_t *)exec->rw_ptr + exec->size;
430
431 // RUNTIME_FUNCTION (PDATA) - Must be 4-byte aligned.
432 RUNTIME_FUNCTION * rf = (RUNTIME_FUNCTION *)_infix_align_up((size_t)metadata_base, 4);
433
434 // UNWIND_INFO (XDATA) - Follows PDATA.
435 UNWIND_INFO * ui = (UNWIND_INFO *)_infix_align_up((size_t)(rf + 1), 2);
436
437 ui->Version = 1;
438 ui->Flags = 0;
439 if (category == INFIX_EXECUTABLE_SAFE_FORWARD)
440 ui->Flags |= UNW_FLAG_EHANDLER;
441 ui->FrameRegister = 5; // RBP
442 ui->FrameOffset = 0;
443 ui->SizeOfPrologue = (uint8_t)prologue_size;
444
445 if (category == INFIX_EXECUTABLE_REVERSE) {
446 // Reverse Trampoline: push rbp, push rsi, push rdi, mov rbp, rsp, and rsp -mask, [sub rsp, alloc]
447 ui->CountOfCodes = 4;
448 ui->UnwindCode[0].CodeOffset = 6; // After mov rbp, rsp
449 ui->UnwindCode[0].UnwindOp = UWOP_SET_FPREG;
450 ui->UnwindCode[0].OpInfo = 0;
451
452 ui->UnwindCode[1].CodeOffset = 3; // After push rdi
453 ui->UnwindCode[1].UnwindOp = UWOP_PUSH_NONVOL;
454 ui->UnwindCode[1].OpInfo = 7; // RDI
455
456 ui->UnwindCode[2].CodeOffset = 2; // After push rsi
457 ui->UnwindCode[2].UnwindOp = UWOP_PUSH_NONVOL;
458 ui->UnwindCode[2].OpInfo = 6; // RSI
459
460 ui->UnwindCode[3].CodeOffset = 1; // After push rbp
461 ui->UnwindCode[3].UnwindOp = UWOP_PUSH_NONVOL;
462 ui->UnwindCode[3].OpInfo = 5; // RBP
463 }
464 else {
465 // Forward or Direct Trampoline: push rbp, push r12-r15, mov rbp, rsp, and rsp -16, [sub rsp, alloc]
466 ui->CountOfCodes = 6;
467 // Opcodes in reverse order:
468 ui->UnwindCode[0].CodeOffset = 12; // After mov rbp, rsp
469 ui->UnwindCode[0].UnwindOp = UWOP_SET_FPREG;
470 ui->UnwindCode[0].OpInfo = 0;
471
472 ui->UnwindCode[1].CodeOffset = 9; // After push r15
473 ui->UnwindCode[1].UnwindOp = UWOP_PUSH_NONVOL;
474 ui->UnwindCode[1].OpInfo = 15; // R15
475
476 ui->UnwindCode[2].CodeOffset = 7; // After push r14
477 ui->UnwindCode[2].UnwindOp = UWOP_PUSH_NONVOL;
478 ui->UnwindCode[2].OpInfo = 14; // R14
479
480 ui->UnwindCode[3].CodeOffset = 5; // After push r13
481 ui->UnwindCode[3].UnwindOp = UWOP_PUSH_NONVOL;
482 ui->UnwindCode[3].OpInfo = 13; // R13
483
484 ui->UnwindCode[4].CodeOffset = 3; // After push r12
485 ui->UnwindCode[4].UnwindOp = UWOP_PUSH_NONVOL;
486 ui->UnwindCode[4].OpInfo = 12; // R12
487
488 ui->UnwindCode[5].CodeOffset = 1; // After push rbp
489 ui->UnwindCode[5].UnwindOp = UWOP_PUSH_NONVOL;
490 ui->UnwindCode[5].OpInfo = 5; // RBP
491 }
492
493 // 3. Personality Routine Stub - Follows UNWIND_INFO.
494 // The ExceptionHandler field is at offset: 4 + ((CountOfCodes + 1) & ~1) * 2
495 uint32_t * eh_field_ptr = (uint32_t *)&ui->UnwindCode[(ui->CountOfCodes + 1) & ~1];
496
497 // Position the stub AFTER the ExceptionHandler RVA and HandlerData (8 bytes total).
498 uint8_t * stub = (uint8_t *)_infix_align_up((size_t)(eh_field_ptr + 2), 16);
499
500 stub[0] = 0x48;
501 stub[1] = 0xB8; // mov rax, imm64
502 *(uint64_t *)(stub + 2) = (uint64_t)_infix_seh_personality_routine;
503 stub[10] = 0xFF;
504 stub[11] = 0xE0; // jmp rax
505
506 // BaseAddress should be 64KB aligned for maximum compatibility.
507 DWORD64 base_address = (DWORD64)exec->rx_ptr & ~0xFFFF;
508 DWORD rva_offset = (DWORD)((uint8_t *)exec->rx_ptr - (uint8_t *)base_address);
509
510 rf->BeginAddress = rva_offset; // Relative to BaseAddress
511 // EndAddress covers the entire code block.
512 rf->EndAddress = rva_offset + (DWORD)exec->size;
513 rf->UnwindData = rva_offset + (DWORD)((uint8_t *)ui - (uint8_t *)exec->rx_ptr);
514
515 if (ui->Flags & UNW_FLAG_EHANDLER) {
516 // ExceptionHandler RVA points to our absolute jump stub.
517 eh_field_ptr[0] = rva_offset + (uint32_t)(stub - (uint8_t *)exec->rx_ptr);
518 // HandlerData field stores our target epilogue offset.
519 eh_field_ptr[1] = epilogue_offset;
520 }
521
522 if (RtlAddFunctionTable(rf, 1, base_address)) {
523 exec->seh_registration = rf;
525 "Registered SEH PDATA at %p (XDATA at %p, Stub at %p) for JIT code at %p", rf, ui, stub, exec->rx_ptr);
526 }
527 else {
528 INFIX_DEBUG_PRINTF("infix: RtlAddFunctionTable failed! GetLastError=%lu\n", GetLastError());
529 }
530}
531#elif defined(INFIX_ARCH_AARCH64)
532// Internal: Populates and registers SEH metadata for a Windows ARM64 JIT block.
533static void _infix_register_seh_windows_arm64(infix_executable_t * exec,
535 uint32_t prologue_size,
536 uint32_t epilogue_offset) {
537 uint8_t * metadata_base = (uint8_t *)exec->rw_ptr + exec->size;
538
539 // RUNTIME_FUNCTION (PDATA) - Must be 4-byte aligned.
540 // On ARM64, we use two entries: one for the function and a sentinel for the end.
541 RUNTIME_FUNCTION * rf = (RUNTIME_FUNCTION *)_infix_align_up((size_t)metadata_base, 4);
542
543 // UNWIND_INFO (XDATA) - Follows PDATA.
544 UNWIND_INFO_ARM64 * ui = (UNWIND_INFO_ARM64 *)_infix_align_up((size_t)(rf + 2), 4);
545 infix_memset(ui, 0, sizeof(UNWIND_INFO_ARM64));
546
547 ui->FunctionLength = (uint32_t)(exec->size / 4);
548 ui->Version = 0;
549 ui->X = (category == INFIX_EXECUTABLE_SAFE_FORWARD);
550 ui->E = 0;
551 ui->EpilogueCount = 1;
552
553 uint8_t * unwind_codes = (uint8_t *)(ui + 1);
554 uint32_t code_idx = 0;
555
556 if (category == INFIX_EXECUTABLE_REVERSE) {
557 // Reverse Prologue: stp x29, x30, [sp, #-16]!; mov x29, sp; sub sp, sp, #alloc
558 // Opcodes in REVERSE order:
559 unwind_codes[code_idx++] = 0xE1; // mov x29, sp
560 unwind_codes[code_idx++] = 0xC8; // stp x29, x30, [sp, #-16]!
561 unwind_codes[code_idx++] = 0xE4; // end
562 }
563 else {
564 // Forward or Direct Prologue: stp x29, x30, [sp, #-16]!; stp x19, x20, ...; stp x21, x22, ...; mov x29, sp; sub
565 // sp, sp, #alloc
566 unwind_codes[code_idx++] = 0xE1; // mov x29, sp
567 unwind_codes[code_idx++] = 0xD4; // stp x21, x22, [sp, #-16]!
568 unwind_codes[code_idx++] = 0xD2; // stp x19, x20, [sp, #-16]!
569 unwind_codes[code_idx++] = 0xC8; // stp x29, x30, [sp, #-16]!
570 unwind_codes[code_idx++] = 0xE4; // end
571 }
572
573 ui->CodeWords = (code_idx + 3) / 4;
574
575 // On ARM64, if X=1, the Exception Handler RVA and Handler Data follow the epilogue scopes
576 // and unwind codes.
577 // XDATA layout: [Header] [Epilogue Scopes] [Unwind Codes] [Padding] [Handler RVA] [Handler Data]
578
579 uint32_t * epilogue_scopes = (uint32_t *)(ui + 1);
580 // Each epilogue scope is 4 bytes. We have ui->EpilogueCount of them.
581 epilogue_scopes[0] = (epilogue_offset / 4); // Epilogue Start Index (instructions)
582
583 uint8_t * unwind_codes_ptr = (uint8_t *)(epilogue_scopes + ui->EpilogueCount);
584 // Clear and then copy the codes
585 infix_memset(unwind_codes_ptr, 0, ui->CodeWords * 4);
586 infix_memcpy(unwind_codes_ptr, unwind_codes, code_idx);
587
588 // Handler info must follow unwind codes (which are already padded to 4 bytes by ui->CodeWords).
589 uint32_t * handler_info_ptr = (uint32_t *)(unwind_codes_ptr + ui->CodeWords * 4);
590
591 uint8_t * stub = (uint8_t *)_infix_align_up((size_t)(handler_info_ptr + 2), 16);
592
593 // stub:
594 // ldr x9, personality_addr
595 // br x9
596 // personality_addr: .quad _infix_seh_personality_routine
597 *(uint32_t *)stub = 0x58000049; // ldr x9, #8
598 *(uint32_t *)(stub + 4) = 0xD61F0120; // br x9
599 *(uint64_t *)(stub + 8) = (uint64_t)_infix_seh_personality_routine;
600
601 DWORD64 base_address = (DWORD64)exec->rx_ptr & ~0xFFFF;
602 DWORD rva_offset = (DWORD)((uint8_t *)exec->rx_ptr - (uint8_t *)base_address);
603
604 rf[0].BeginAddress = rva_offset;
605 rf[0].UnwindData = rva_offset + (DWORD)((uint8_t *)ui - (uint8_t *)exec->rx_ptr);
606
607 // Sentinel entry defines the end of the previous function
608 rf[1].BeginAddress = rva_offset + (DWORD)exec->size;
609 rf[1].UnwindData = 0;
610
611 if (ui->X) {
612 // According to the spec, the Exception Handler RVA and Handler Data
613 // are located at the end of the XDATA, which is 4-byte aligned.
614 handler_info_ptr[0] = rva_offset + (uint32_t)(stub - (uint8_t *)exec->rx_ptr);
615 handler_info_ptr[1] = epilogue_offset;
616 }
617
618 if (RtlAddFunctionTable(rf, 2, base_address)) {
619 exec->seh_registration = rf;
621 "Registered SEH PDATA at %p (XDATA at %p, Stub at %p) for JIT code at %p", rf, ui, stub, exec->rx_ptr);
622 }
623 else {
624 INFIX_DEBUG_PRINTF("infix: RtlAddFunctionTable failed! GetLastError=%lu\n", GetLastError());
625 }
626}
627#endif
628#endif
629
630#if defined(INFIX_OS_LINUX) && defined(INFIX_ARCH_X64)
639static c23_maybe_unused void _infix_register_eh_frame_linux_x64(infix_executable_t * exec, infix_executable_category_t category) {
640 // Simplified .eh_frame layout: [ CIE | FDE | Terminator ]
641 const size_t cie_size = 32;
642 const size_t fde_size = 64;
643 const size_t total_size = cie_size + fde_size + 4; // +4 for null terminator
644
645 uint8_t * eh = infix_malloc(total_size);
646 if (!eh)
647 return;
648 infix_memset(eh, 0, total_size);
649
650 uint8_t * p = eh;
651
652 // CIE
653 *(uint32_t *)p = (uint32_t)(cie_size - 4);
654 p += 4;
655 *(uint32_t *)p = 0;
656 p += 4;
657 *p++ = 1; // version
658 *p++ = '\0'; // augmentation
659 *p++ = 1; // code align
660 *p++ = 0x78; // data align (-8)
661 *p++ = 16; // ret reg (rip)
662
663 // Initial state: CFA = rsp + 8, rip at CFA - 8
664 *p++ = 0x0c;
665 *p++ = 0x07;
666 *p++ = 0x08;
667 *p++ = 0x90;
668 *p++ = 0x01;
669 while ((size_t)(p - eh) < cie_size)
670 *p++ = 0;
671
672 // FDE
673 uint8_t * fde_start = eh + cie_size;
674 p = fde_start;
675 *(uint32_t *)p = (uint32_t)(fde_size - 4);
676 p += 4;
677 *(uint32_t *)p = (uint32_t)(p - eh);
678 p += 4; // back-offset
679
680 *(void **)p = exec->rx_ptr;
681 p += 8;
682 *(uint64_t *)p = (uint64_t)exec->size;
683 p += 8;
684 *p++ = 0; // aug data len
685
686 // Instructions:
687 if (category == INFIX_EXECUTABLE_REVERSE) {
688 // push rbp; mov rbp, rsp; push rsi; push rdi
689 *p++ = 0x41; // loc +1 (after push rbp)
690 *p++ = 0x0e;
691 *p++ = 16; // def_cfa_offset 16
692 *p++ = 0x86;
693 *p++ = 0x02; // offset rbp (6), 2
694 *p++ = 0x43; // loc +3 (after mov rbp, rsp)
695 *p++ = 0x0d;
696 *p++ = 0x06; // def_cfa_register rbp (6)
697 *p++ = 0x41; // loc +1 (after push rsi)
698 *p++ = 0x84;
699 *p++ = 0x03; // offset rsi (4), 3
700 *p++ = 0x41; // loc +1 (after push rdi)
701 *p++ = 0x85;
702 *p++ = 0x04; // offset rdi (5), 4
703 }
704 else {
705 // push rbp; mov rbp, rsp; push r12; push r13; push r14; push r15
706 *p++ = 0x41; // loc +1 (after push rbp)
707 *p++ = 0x0e;
708 *p++ = 16; // def_cfa_offset 16
709 *p++ = 0x86;
710 *p++ = 0x02; // offset rbp (6), 2
711 *p++ = 0x43; // loc +3 (after mov rbp, rsp)
712 *p++ = 0x0d;
713 *p++ = 0x06; // def_cfa_register rbp (6)
714 *p++ = 0x42; // loc +2 (after push r12)
715 *p++ = 0x8c;
716 *p++ = 0x03; // offset r12, 3
717 *p++ = 0x42; // loc +2 (after push r13)
718 *p++ = 0x8d;
719 *p++ = 0x04; // offset r13, 4
720 *p++ = 0x42; // loc +2 (after push r14)
721 *p++ = 0x8e;
722 *p++ = 0x05; // offset r14, 5
723 *p++ = 0x42; // loc +2 (after push r15)
724 *p++ = 0x8f;
725 *p++ = 0x06; // offset r15, 6
726 }
727
728 while ((size_t)(p - eh) < (cie_size + fde_size))
729 *p++ = 0;
730 *(uint32_t *)p = 0; // Terminator
731
732 extern void __register_frame(void *);
733 pthread_mutex_lock(&g_dwarf_mutex);
734 __register_frame(eh);
735 pthread_mutex_unlock(&g_dwarf_mutex);
736
737 exec->eh_frame_ptr = eh;
738 INFIX_DEBUG_PRINTF("Registered DWARF .eh_frame at %p for JIT code at %p", (void *)eh, exec->rx_ptr);
739}
740#elif defined(INFIX_OS_LINUX) && defined(INFIX_ARCH_AARCH64)
749static void _infix_register_eh_frame_arm64(infix_executable_t * exec, infix_executable_category_t category) {
750 // Simplified .eh_frame layout: [ CIE | FDE | Terminator ]
751 const size_t cie_size = 32;
752 const size_t fde_size = 64;
753 const size_t total_size = cie_size + fde_size + 4; // +4 for null terminator
754
755 uint8_t * eh = infix_malloc(total_size);
756 if (!eh)
757 return;
758 infix_memset(eh, 0, total_size);
759
760 uint8_t * p = eh;
761
762 // CIE (Common Information Entry)
763 *(uint32_t *)p = (uint32_t)(cie_size - 4);
764 p += 4; // length
765 *(uint32_t *)p = 0;
766 p += 4; // cie_id (0)
767 *p++ = 1; // version
768 *p++ = '\0'; // augmentation string ("")
769 *p++ = 4; // code_alignment_factor (AArch64 instructions are 4 bytes)
770 *p++ = 0x78; // data_alignment_factor (-8 in SLEB128)
771 *p++ = 30; // return_address_register (30 = lr on arm64)
772
773 // CIE Instructions: Initial state
774 // DW_CFA_def_cfa sp, 0
775 *p++ = 0x0c;
776 *p++ = 31;
777 *p++ = 0;
778 while ((size_t)(p - eh) < cie_size)
779 *p++ = 0;
780
781 // FDE (Frame Description Entry)
782 uint8_t * fde_start = eh + cie_size;
783 p = fde_start;
784 *(uint32_t *)p = (uint32_t)(fde_size - 4);
785 p += 4; // length
786 *(uint32_t *)p = (uint32_t)(p - eh);
787 p += 4; // cie_pointer (back-offset)
788
789 *(void **)p = exec->rx_ptr;
790 p += 8; // pc_begin (absolute)
791 *(uint64_t *)p = (uint64_t)exec->size;
792 p += 8; // pc_range (absolute)
793 *p++ = 0; // aug data len
794
795 // Instructions: match our trampoline prologue
796 if (category == INFIX_EXECUTABLE_REVERSE) {
797 // stp x29, x30, [sp, #-16]!; mov x29, sp
798 *p++ = 0x41; // loc +1 (4 bytes, after stp)
799 *p++ = 0x0e;
800 *p++ = 16; // def_cfa_offset 16
801 *p++ = 0x9d;
802 *p++ = 2; // offset r29 (x29), 2 (CFA - 16)
803 *p++ = 0x9e;
804 *p++ = 1; // offset r30 (x30/lr), 1 (CFA - 8)
805 *p++ = 0x41; // loc +1 (4 bytes, after mov)
806 *p++ = 0x0d;
807 *p++ = 29; // def_cfa_register r29
808 }
809 else {
810 // stp x29, x30, [sp, #-16]!; stp x19, x20, ...; stp x21, x22, ...; mov x29, sp
811 *p++ = 0x41; // after stp x29, x30
812 *p++ = 0x0e;
813 *p++ = 16;
814 *p++ = 0x9d;
815 *p++ = 2; // x29 at CFA - 16
816 *p++ = 0x9e;
817 *p++ = 1; // x30 at CFA - 8
818 *p++ = 0x41; // after stp x19, x20
819 *p++ = 0x0e;
820 *p++ = 32;
821 *p++ = 0x93;
822 *p++ = 4; // x19 at CFA - 32
823 *p++ = 0x94;
824 *p++ = 3; // x20 at CFA - 24
825 *p++ = 0x41; // after stp x21, x22
826 *p++ = 0x0e;
827 *p++ = 48;
828 *p++ = 0x95;
829 *p++ = 6; // x21 at CFA - 48
830 *p++ = 0x96;
831 *p++ = 5; // x22 at CFA - 40
832 *p++ = 0x41; // after mov x29, sp
833 *p++ = 0x0d;
834 *p++ = 29; // def_cfa_register x29 (offset remains 48)
835 }
836
837 while ((size_t)(p - eh) < (cie_size + fde_size))
838 *p++ = 0;
839 *(uint32_t *)p = 0; // Terminator
840
841 // Register the frame with the runtime.
842 extern void __register_frame(void *);
843 pthread_mutex_lock(&g_dwarf_mutex);
844 __register_frame(eh);
845 pthread_mutex_unlock(&g_dwarf_mutex);
846
847 exec->eh_frame_ptr = eh;
848 INFIX_DEBUG_PRINTF("Registered ARM64 DWARF .eh_frame at %p for JIT code at %p", (void *)eh, exec->rx_ptr);
849}
850#endif
851
865 if (exec.size == 0)
866 return;
867#if defined(INFIX_OS_WINDOWS)
868#if defined(INFIX_ARCH_X64) || defined(INFIX_ARCH_AARCH64)
869 if (exec.seh_registration)
870 RtlDeleteFunctionTable((PRUNTIME_FUNCTION)exec.seh_registration);
871#endif
872 if (exec.rw_ptr) {
873 // Change protection to NOACCESS to catch use-after-free bugs immediately.
874 if (!VirtualProtect(exec.rw_ptr, exec.size, PAGE_NOACCESS, &(DWORD){0}))
875 INFIX_DEBUG_PRINTF("WARNING: VirtualProtect failed to set PAGE_NOACCESS guard page.");
876 VirtualFree(exec.rw_ptr, 0, MEM_RELEASE);
877 }
878#elif defined(INFIX_OS_MACOS)
879 // On macOS with MAP_JIT, the memory is managed with special thread-local permissions.
880 // We only need to unmap the single mapping.
881 if (exec.rw_ptr) {
882 // Creating a guard page before unmapping is good practice.
883 mprotect(exec.rw_ptr, exec.size, PROT_NONE);
884 munmap(exec.rw_ptr, exec.size);
885 }
886#elif defined(INFIX_OS_ANDROID) || defined(INFIX_OS_OPENBSD) || defined(INFIX_OS_DRAGONFLY)
887 // Other single-mapping POSIX systems.
888 if (exec.rw_ptr) {
889 //mprotect(exec.rw_ptr, exec.size, PROT_NONE);
890 munmap(exec.rw_ptr, exec.size);
891 }
892#else
893 // Dual-mapping POSIX: protect and unmap both views.
894 if (exec.eh_frame_ptr) {
895 extern void __deregister_frame(void *);
896 pthread_mutex_lock(&g_dwarf_mutex);
897 __deregister_frame(exec.eh_frame_ptr);
898 pthread_mutex_unlock(&g_dwarf_mutex);
900 }
901 if (exec.rx_ptr)
902 mprotect(exec.rx_ptr, exec.size, PROT_NONE);
903 if (exec.rw_ptr)
904 munmap(exec.rw_ptr, exec.size);
905 if (exec.rx_ptr && exec.rx_ptr != exec.rw_ptr) // rw_ptr might be same as rx_ptr on some platforms
906 munmap(exec.rx_ptr, exec.size);
907 if (exec.shm_fd >= 0)
908 close(exec.shm_fd);
909#endif
910}
911
931 c23_maybe_unused uint32_t prologue_size,
932 c23_maybe_unused uint32_t epilogue_offset) {
933 if (exec->rw_ptr == nullptr || exec->size == 0)
934 return false;
935
936 // On AArch64 (and other RISC architectures), the instruction and data caches can be
937 // separate. We must explicitly flush the D-cache (where the JIT wrote the code)
938 // and invalidate the I-cache so the CPU fetches the new instructions.
939 // We might as well do it on x64 too.
940 // NOTE: key on the OS, not the compiler. clang on Windows defines `__clang__`
941 // (checked before `_MSC_VER`), so `INFIX_COMPILER_MSVC` is not set for it; the
942 // `dc cvau`/`ic ivau` inline-asm path below is illegal at EL0 on Windows NT and
943 // SIGILLs every JIT call (STATUS_ILLEGAL_INSTRUCTION, 0xC000001D).
944#if defined(INFIX_OS_WINDOWS)
945 // Use the Windows-specific API.
946 FlushInstructionCache(GetCurrentProcess(), exec->rw_ptr, exec->size);
947#elif defined(INFIX_OS_MACOS)
948 // Use the Apple-specific API if available (required for Apple Silicon correctness)
949 if (g_macos_apis.sys_icache_invalidate)
950 g_macos_apis.sys_icache_invalidate(exec->rw_ptr, exec->size);
951 else
952 __builtin___clear_cache((char *)exec->rw_ptr, (char *)exec->rw_ptr + exec->size);
953#elif defined(INFIX_ARCH_AARCH64)
954 // Robust manual cache clearing for AArch64 Linux/BSD.
955 // We clean the D-cache to point of unification and invalidate the I-cache.
956 uintptr_t start = (uintptr_t)exec->rw_ptr;
957 uintptr_t end = start + exec->size;
958 uintptr_t ctr_el0;
959 __asm__ __volatile__("mrs %0, ctr_el0" : "=r"(ctr_el0));
960
961 // D-cache line size is in bits [19:16] as log2 of number of words.
962 uintptr_t d_line_size = 4 << ((ctr_el0 >> 16) & 0xf);
963 for (uintptr_t addr = start & ~(d_line_size - 1); addr < end; addr += d_line_size)
964 __asm__ __volatile__("dc cvau, %0" ::"r"(addr) : "memory");
965 __asm__ __volatile__("dsb ish" ::: "memory");
966
967 // I-cache line size is in bits [3:0] as log2 of number of words.
968 uintptr_t i_line_size = 4 << (ctr_el0 & 0xf);
969 for (uintptr_t addr = start & ~(i_line_size - 1); addr < end; addr += i_line_size)
970 __asm__ __volatile__("ic ivau, %0" ::"r"(addr) : "memory");
971 __asm__ __volatile__("dsb ish\n\tisb" ::: "memory");
972#elif defined(INFIX_ARCH_RISCV)
973 // `__builtin___clear_cache` expands to `fence.i`, which lives in the Zifencei
974 // extension. Some RISC-V toolchains (e.g. OpenBSD's egcc with a newer binutils)
975 // do not enable Zifencei in the default `-march` and reject the opcode, so
976 // enable it explicitly for the flush.
977 __asm__ __volatile__(".option arch, +zifencei\n\tfence.i" ::: "memory");
978#else
979 // Use the GCC/Clang built-in for other platforms.
980 __builtin___clear_cache((char *)exec->rw_ptr, (char *)exec->rw_ptr + exec->size);
981#endif
982
983 bool result = false;
984#if defined(INFIX_OS_WINDOWS)
985 // On Windows, we register SEH unwind info before making the memory executable.
986#if defined(INFIX_ARCH_X64)
987 _infix_register_seh_windows_x64(exec, category, prologue_size, epilogue_offset);
988#elif defined(INFIX_ARCH_AARCH64)
989 _infix_register_seh_windows_arm64(exec, category, prologue_size, epilogue_offset);
990#endif
991 // Finalize permissions to Read+Execute.
992 // We include the SEH metadata in the protected region.
993 result = VirtualProtect(exec->rw_ptr, exec->size + INFIX_SEH_METADATA_SIZE, PAGE_EXECUTE_READ, &(DWORD){0});
994 if (!result)
996#elif defined(INFIX_OS_MACOS)
997 static bool g_use_secure_jit_path = false;
998 static bool g_checked_jit_support = false;
999 if (!g_checked_jit_support) {
1000 g_use_secure_jit_path = has_jit_entitlement();
1001 g_checked_jit_support = true;
1002 }
1003
1004 if (g_use_secure_jit_path && g_macos_apis.pthread_jit_write_protect_np) {
1005 // Switch thread state to Execute allowed (enabled=1)
1006 g_macos_apis.pthread_jit_write_protect_np(1);
1007 result = true;
1008 }
1009 else {
1010 result = (mprotect(exec->rw_ptr, exec->size, PROT_READ | PROT_EXEC) == 0);
1011 }
1012 if (!result)
1014#elif defined(INFIX_OS_ANDROID) || defined(INFIX_OS_OPENBSD) || defined(INFIX_OS_DRAGONFLY)
1015 // Other single-mapping POSIX platforms use mprotect.
1016 result = (mprotect(exec->rw_ptr, exec->size, PROT_READ | PROT_EXEC) == 0);
1017 if (!result)
1019#else
1020 // Dual-mapping POSIX (Linux, FreeBSD).
1021 // The RX mapping is already executable.
1022#if defined(INFIX_OS_LINUX) && defined(INFIX_ARCH_X64)
1023 _infix_register_eh_frame_linux_x64(exec, category);
1024#elif defined(INFIX_OS_LINUX) && defined(INFIX_ARCH_AARCH64)
1025 _infix_register_eh_frame_arm64(exec, category);
1026#endif
1027 // SECURITY CRITICAL: We MUST unmap the RW view now. If we leave it mapped,
1028 // an attacker with a heap disclosure could find it and overwrite the JIT code,
1029 // bypassing W^X.
1030 if (munmap(exec->rw_ptr, exec->size) == 0) {
1031 exec->rw_ptr = nullptr; // Clear the pointer to prevent double-free or misuse.
1032 result = true;
1033 }
1034 else {
1036 INFIX_CATEGORY_ALLOCATION, INFIX_CODE_PROTECTION_FAILURE, errno, "munmap of RW view failed");
1037 result = false;
1038 }
1039#endif
1040 if (result)
1041 INFIX_DEBUG_PRINTF("Memory at %p is now executable.", exec->rx_ptr);
1042 return result;
1043}
1044// Public API: Protected (Read-Only) Memory
1057 infix_protected_t prot = {.rw_ptr = nullptr, .size = 0};
1058 if (size == 0)
1059 return prot;
1060#if defined(INFIX_OS_WINDOWS)
1061 prot.rw_ptr = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
1062 if (!prot.rw_ptr)
1064#else
1065#if defined(MAP_ANON)
1066 prot.rw_ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
1067#else
1068 int fd = open("/dev/zero", O_RDWR);
1069 if (fd == -1)
1070 prot.rw_ptr = MAP_FAILED;
1071 else {
1072 prot.rw_ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
1073 close(fd);
1074 }
1075#endif
1076 if (prot.rw_ptr == MAP_FAILED) {
1078 prot.rw_ptr = nullptr;
1079 }
1080#endif
1081 if (prot.rw_ptr)
1082 prot.size = size;
1083 return prot;
1084}
1085
1091 if (prot.size == 0)
1092 return;
1093#if defined(INFIX_OS_WINDOWS)
1094 VirtualFree(prot.rw_ptr, 0, MEM_RELEASE);
1095#else
1096 munmap(prot.rw_ptr, prot.size);
1097#endif
1098}
1099
1111 if (prot.size == 0)
1112 return false;
1113 bool result = false;
1114#if defined(INFIX_OS_WINDOWS)
1115 result = VirtualProtect(prot.rw_ptr, prot.size, PAGE_READONLY, &(DWORD){0});
1116 if (!result)
1118#else
1119 result = (mprotect(prot.rw_ptr, prot.size, PROT_READ) == 0);
1120 if (!result)
1122#endif
1123 return result;
1124}
1125// Universal Reverse Call Dispatcher
1149void infix_internal_dispatch_callback_fn_impl(infix_reverse_t * context, void * return_value_ptr, void ** args_array) {
1150 INFIX_DEBUG_PRINTF("REVERSE_DISPATCH: context=%p ret=%p args=%p num_args=%zu\n",
1151 (void *)context,
1152 return_value_ptr,
1153 (void *)args_array,
1154 context->num_args);
1155 if (args_array) {
1156 for (size_t i = 0; i < context->num_args; i++) {
1157 INFIX_DEBUG_PRINTF(" args[%zu] = %p", i, args_array[i]);
1158 if (args_array[i])
1159 INFIX_DEBUG_PRINTF(" (val32: %d %d)", ((int *)args_array[i])[0], ((int *)args_array[i])[1]);
1160 INFIX_DEBUG_PRINTF("\n");
1161 }
1162 }
1163 INFIX_DEBUG_PRINTF("Dispatching reverse call. Context: %p, User Fn: %p, ret=%p, args=%p",
1164 (void *)context,
1165 context->user_callback_fn,
1166 return_value_ptr,
1167 (void *)args_array);
1168 if (args_array) {
1169 for (size_t i = 0; i < context->num_args; i++) {
1171 " args[%zu] = %p (val: 0x%04X)", i, args_array[i], args_array[i] ? *(uint16_t *)args_array[i] : 0);
1172 }
1173 }
1174 if (context->user_callback_fn == nullptr) {
1175 // If no handler is set, do nothing. If the function has a return value,
1176 // it's good practice to zero it out to avoid returning garbage.
1177 if (return_value_ptr && context->return_type->size > 0)
1178 infix_memset(return_value_ptr, 0, context->return_type->size);
1179 return;
1180 }
1181 if (context->cached_forward_trampoline != nullptr) {
1182 // Path 1: Type-safe "callback". Use the pre-generated forward trampoline to
1183 // call the user's C function with the correct signature. This is efficient
1184 // and provides a clean interface for the C developer.
1186 cif_func(return_value_ptr, args_array);
1187 }
1188 else {
1189 // Path 2: Generic "closure". Directly call the user's generic handler.
1190 // This path is more flexible and is intended for language bindings where the
1191 // handler needs access to the context and raw argument pointers.
1193 handler(context, return_value_ptr, args_array);
1194 }
1195 INFIX_DEBUG_PRINTF("Exiting reverse call dispatcher.");
1196}
clock_t start
Definition 901_call_overhead.c:48
clock_t end
Definition 901_call_overhead.c:48
char * p
Definition 904_registry_benchmark.c:25
#define c23_nodiscard
Internal alias for the public INFIX_NODISCARD macro.
Definition compat_c23.h:92
#define c23_maybe_unused
A compatibility macro for the C23 [[maybe_unused]] attribute.
Definition compat_c23.h:156
c23_nodiscard bool infix_executable_make_executable(infix_executable_t *exec, c23_maybe_unused infix_executable_category_t category, c23_maybe_unused uint32_t prologue_size, c23_maybe_unused uint32_t epilogue_offset)
Definition executor.c:929
void infix_protected_free(infix_protected_t prot)
Frees a block of protected memory.
Definition executor.c:1090
void infix_executable_free(infix_executable_t exec)
Frees a block of executable memory and applies guard pages to prevent use-after-free.
Definition executor.c:864
static int create_anonymous_file(void)
Definition executor.c:232
c23_nodiscard infix_protected_t infix_protected_alloc(size_t size)
Allocates a block of standard memory for later protection.
Definition executor.c:1056
c23_nodiscard infix_executable_t infix_executable_alloc(size_t size)
Allocates a block of executable memory using the platform's W^X strategy.
Definition executor.c:281
#define INFIX_SEH_METADATA_SIZE
Definition executor.c:107
c23_nodiscard bool infix_protected_make_readonly(infix_protected_t prot)
Makes a block of memory read-only for security hardening.
Definition executor.c:1110
void infix_internal_dispatch_callback_fn_impl(infix_reverse_t *context, void *return_value_ptr, void **args_array)
The universal C entry point for all reverse call trampolines.
Definition executor.c:1149
@ INFIX_CODE_PROTECTION_FAILURE
Definition infix.h:1423
@ INFIX_CODE_EXECUTABLE_MEMORY_FAILURE
Definition infix.h:1422
@ INFIX_CODE_NATIVE_EXCEPTION
Definition infix.h:1418
@ INFIX_CODE_OUT_OF_MEMORY
Definition infix.h:1421
@ INFIX_CATEGORY_ABI
Definition infix.h:1407
@ INFIX_CATEGORY_ALLOCATION
Definition infix.h:1405
void(* infix_cif_func)(void *, void **)
A function pointer type for a bound forward trampoline.
Definition infix.h:470
size_t size
Definition infix.h:279
void(* infix_closure_handler_fn)(infix_context_t *, void *, void **)
A function pointer type for a generic closure handler.
Definition infix.h:482
INFIX_API INFIX_NODISCARD infix_cif_func infix_forward_get_code(infix_forward_t *)
Gets the callable function pointer from a bound forward trampoline.
Definition trampoline.c:293
#define infix_free(ptr)
A macro that can be defined to override infix's allocator at compile time.
Definition infix.h:435
#define infix_malloc(n)
A macro that can be defined to override infix's allocator at compile time.
Definition infix.h:423
#define infix_memcpy
A macro that can be defined to override the default memcpy function.
Definition infix.h:439
#define infix_memset
A macro that can be defined to override the default memset function.
Definition infix.h:443
Internal data structures, function prototypes, and constants.
infix_executable_category_t
Definition infix_internals.h:706
@ INFIX_EXECUTABLE_REVERSE
Definition infix_internals.h:709
@ INFIX_EXECUTABLE_SAFE_FORWARD
Definition infix_internals.h:708
static size_t _infix_align_up(size_t value, size_t alignment)
Aligns a value up to the next multiple of a power-of-two alignment.
Definition infix_internals.h:775
INFIX_INTERNAL void _infix_set_system_error(infix_error_category_t category, infix_error_code_t code, long system_code, const char *msg)
Sets the thread-local error state for a system-level error.
Definition error.c:244
INFIX_INTERNAL void _infix_set_error(infix_error_category_t category, infix_error_code_t code, size_t position)
Sets the thread-local error state with detailed information.
Definition error.c:175
Internal representation of an executable memory block for JIT code.
Definition infix_internals.h:56
size_t size
Definition infix_internals.h:66
void * rw_ptr
Definition infix_internals.h:65
void * rx_ptr
Definition infix_internals.h:64
void * eh_frame_ptr
Definition infix_internals.h:62
int shm_fd
Definition infix_internals.h:61
Internal representation of a memory block that will be made read-only.
Definition infix_internals.h:77
size_t size
Definition infix_internals.h:79
void * rw_ptr
Definition infix_internals.h:78
Internal definition of a reverse trampoline (callback/closure) handle.
Definition infix_internals.h:119
infix_type * return_type
Definition infix_internals.h:123
void * user_callback_fn
Definition infix_internals.h:128
size_t num_args
Definition infix_internals.h:125
infix_forward_t * cached_forward_trampoline
Definition infix_internals.h:133
A header for conditionally compiled debugging utilities.
#define INFIX_DEBUG_PRINTF(...)
Definition utility.h:97