rumpk/core/npl.nim

137 lines
3.5 KiB
Nim

# MARKUS MAIWALD (ARCHITECT) | VOXIS FORGE (AI)
# RUMPK CORE // NPL FORMAT
# The Contract of Execution.
{.push stackTrace: off, lineTrace: off.}
# =========================================================
# Constants
# =========================================================
const NPL_MAGIC*: array[4, uint8] = [0x7F'u8, 0x4E'u8, 0x50'u8, 0x4C'u8]
const NPL_VERSION* = 1'u8
const ARCH_ARM64* = 0xAA'u8
const ARCH_X86_64* = 0xEE'u8
const ARCH_RISCV64* = 0x55'u8
const NPL_HEADER_SIZE* = 128
# =========================================================
# Types
# =========================================================
type
NPLHeader* {.packed.} = object
magic*: array[4, uint8]
version*: uint8
arch*: uint8
flags*: uint16
signature*: array[64, uint8]
body_size*: uint64
reserved*: array[48, uint8]
PayloadEntry* = proc() {.cdecl.}
# Global last error (avoids struct return issues)
var nplLastError*: cstring = nil
# =========================================================
# Architecture Detection
# =========================================================
proc currentArch*(): uint8 =
when defined(arm64) or defined(aarch64):
return ARCH_ARM64
elif defined(amd64) or defined(x86_64):
return ARCH_X86_64
elif defined(riscv64):
return ARCH_RISCV64
else:
return 0x00'u8
# =========================================================
# Debug output
# =========================================================
proc console_write(p: pointer, len: csize_t) {.importc, cdecl.}
proc nplPrint(s: cstring) =
var i = 0
while s[i] != '\0':
inc i
console_write(cast[pointer](s), csize_t(i))
# =========================================================
# NPL Loader (returns nil on failure, sets nplLastError)
# =========================================================
proc loadNpl*(rawPtr: pointer, maxLen: uint64): PayloadEntry =
nplLastError = nil
nplPrint("[NPL] loadNpl\n")
if rawPtr == nil:
nplLastError = "null pointer"
return nil
let header = cast[ptr NPLHeader](rawPtr)
# Check Magic
if header.magic[0] != NPL_MAGIC[0] or
header.magic[1] != NPL_MAGIC[1] or
header.magic[2] != NPL_MAGIC[2] or
header.magic[3] != NPL_MAGIC[3]:
nplLastError = "bad magic"
return nil
# Check Version
if header.version != NPL_VERSION:
nplLastError = "bad version"
return nil
# Check Architecture
if header.arch != currentArch():
nplLastError = "bad arch"
return nil
# Check Bounds
let totalSize = NPL_HEADER_SIZE.uint64 + header.body_size
if totalSize > maxLen:
nplLastError = "truncated"
return nil
# Get body pointer
let bodyPtr = cast[pointer](cast[uint64](rawPtr) + NPL_HEADER_SIZE.uint64)
# Verify Signature (dev mode: reject 0xFF)
if header.signature[0] == 0xFF'u8:
nplLastError = "sig fail"
return nil
nplPrint("[NPL] valid\n")
# Cast body to function pointer
return cast[PayloadEntry](bodyPtr)
# =========================================================
# Helper: Build Test Payload
# =========================================================
proc buildTestPayload*(buffer: ptr array[256, uint8], arch: uint8,
codeBytes: openArray[uint8]) =
# Clear buffer
for i in 0..<256:
buffer[i] = 0
let header = cast[ptr NPLHeader](buffer)
header.magic = NPL_MAGIC
header.version = NPL_VERSION
header.arch = arch
header.flags = 1
header.body_size = codeBytes.len.uint64
# Copy code to body
for i, b in codeBytes:
buffer[NPL_HEADER_SIZE + i] = b
{.pop.}