Author Topic: FFI & JIT Assembler  (Read 111 times)

Mr_Motley

  • Newbie
  • *
  • Posts: 22
  • Karma: +5/-1
    • View Profile
FFI & JIT Assembler
« on: Today at 02:09:44 pm »
Project: [ASM] FFI & JIT Assembler for LU
High-Performance Runtime Compilation & Native ABI Bridge



Overview
This project introduces a zero-dependency FFI and JIT engine for Liberty Unleashed, written entirely in FASM. By bypassing the official SDK and mapping the Squirrel API via raw hex offsets (e.g., SQ_GETTOP = 0x50), this module allows for register-level interaction with the VM.

Key Features

Native FFI: Invoke any Windows API (Kernel32, User32, etc.) directly from Squirrel scripts. Or simply load a dll with exported functions

JIT Assembler: Pass x86 assembly strings to the CompileAsm function to generate executable machine code at runtime.

Zero-Bloat: No C-Runtime (CRT) dependencies; pure assembly binary.



IMPORTANT: SAFETY & STABILITY WARNING
You are operating outside the Squirrel safety net. Incorrect argument counts or mismatched types will result in a Server Crash. Always test JIT-compiled logic on a local development server before deployment.



Deep-Level Example: Multi-Threaded Native UI
This example demonstrates defining a Windows UI (MiniPad) in x86 assembly, compiling it at runtime, and spawning it on its own thread via the FFI bridge.

Code: [Select]
/*
Liberty Unleashed 0.1 - Native JIT MiniPad
Developer: Robert R: Aka: Motley | Build Date: April 16 2026
Description: Deploys a multi-threaded Windows UI using native x86
assembly via the Squirrel JIT engine.
*/

class JIT_Engine {
function Compile(source) {
local ptr = CompileAsm(source);
if (ptr == 0 || ptr == null) return 0;
return ptr;
}
}

JIT <- JIT_Engine();

function onScriptLoad()
{
LoadModule("LU_Mod");
print("--- INITIATING NATIVE UI THREAD ---");

local hKern = import("kernel32.dll", "GetModuleHandleA", 0);
local pMiniPad = CompileMiniPadASM(hKern);

if (pMiniPad > 0) {
    import("kernel32.dll", "CreateThread", 0, 0, pMiniPad, 0, 0, 0);
    print("[JIT] MiniPad is live. UI thread detached successfully.");
}
}

function CompileMiniPadASM(hInst)
{
local hUser = import("kernel32.dll", "LoadLibraryA", "user32.dll");
local pReg    = import("kernel32.dll", "GetProcAddress", hUser, "RegisterClassA");
local pCreate = import("kernel32.dll", "GetProcAddress", hUser, "CreateWindowExA");
local pDef    = import("kernel32.dll", "GetProcAddress", hUser, "DefWindowProcA");
local pMsg    = import("kernel32.dll", "GetProcAddress", hUser, "GetMessageA");
local pTrans = import("kernel32.dll", "GetProcAddress", hUser, "TranslateMessage");
local pDisp  = import("kernel32.dll", "GetProcAddress", hUser, "DispatchMessageA");
local pSend  = import("kernel32.dll", "GetProcAddress", hUser, "SendMessageA");

local source = @"
    use32
    org 0
    ThreadEntry:
        push ebp
        mov ebp, esp
        sub esp, 128
        lea edi, [ebp-64]
        xor eax, eax
        mov ecx, 10
        rep stosd
        mov dword [ebp-64+4], " + pDef + @"
        mov dword [ebp-64+16], " + hInst + @"
        call .get_class
        db 'LU_JIT_PAD',0
    .get_class:
        pop eax
        mov dword [ebp-64+36], eax
        lea eax, [ebp-64]
        push eax
        mov eax, " + pReg + @"
        call eax
        push 0
        push " + hInst + @"
        push 0
        push 0
        push 350
        push 500
        push 150
        push 150
        push 0x10CF0000
        call .get_title
        db 'Liberty Unleashed 0.1 - JIT Monitor',0
    .get_title:
        call .get_class_ptr
        db 'LU_JIT_PAD',0
    .get_class_ptr:
        push 0
        mov eax, " + pCreate + @"
        call eax
        mov ebx, eax
        push 0
        push " + hInst + @"
        push 0
        push ebx
        push 300
        push 480
        push 2
        push 2
        push 0x50800004
        push 0
        call .get_edit
        db 'EDIT',0
    .get_edit:
        push 0
        mov eax, " + pCreate + @"
        call eax
        mov edi, eax
        call .get_thanks
        db '================================================',13,10
        db '    LIBERTY UNLEASHED 0.1 - JIT/FFI STARTER KIT  ',13,10
        db '================================================',13,10,13,10
        db 'Successfully executing native x86 instructions!',13,10
        db 'Running at full CPU speed via Squirrel JIT.',13,10,13,10
        db 'Special thanks for using this project.',13,10
        db 'Build Date: April 2026',0
    .get_thanks:
        push 0
        push 0x000C
        push edi
        mov eax, " + pSend + @"
        call eax
    MsgLoop:
        lea eax, [ebp-128]
        push 0
        push 0
        push 0
        push eax
        mov eax, " + pMsg + @"
        call eax
        test eax, eax
        jz ExitThread
        lea eax, [ebp-128]
        push eax
        mov eax, " + pTrans + @"
        call eax
        lea eax, [ebp-128]
        push eax
        mov eax, " + pDisp + @"
        call eax
        jmp MsgLoop
    ExitThread:
        mov esp, ebp
        pop ebp
        ret 4
";
return JIT.Compile(source);
}



Technical Implementation
The module handles EBP/ESP synchronization during the native call phase to ensure the Squirrel stack remains intact. It uses VirtualAlloc to prepare the buffer for the FASM engine to output machine code.

Download (FFI + JIT Bundle): https://www.mediafire.com/file/kyyppz18z0lfcp2/Squirrel+FFI+++JIT.zip/file

Developed by Motley (Robert R.)
« Last Edit: Today at 09:15:29 pm by Mr_Motley »

 

© Liberty Unleashed Team.