Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1,68 @@
-- Linux x86_64 with GNAT 15.1.0
with Ada.Unchecked_Conversion;
with Ada.Text_IO;
with Interfaces.C;
with System;
-- Pull in primitive operations for unsigned int types to combine
-- flags with "or"
use type Interfaces.C.unsigned;
procedure Machine_Code_Add is
-- The mmap interface provided by GNAT is limited, expecting only
-- reading and writing files rather than grabbing anonymous
-- executable memory regions. At that point it's easier just to
-- define our own interface.
PROT_READ : constant Interfaces.C.unsigned := 1;
PROT_WRITE : constant Interfaces.C.unsigned := 2;
PROT_EXEC : constant Interfaces.C.unsigned := 4;
MAP_PRIVATE : constant Interfaces.C.unsigned := 2;
MAP_ANON : constant Interfaces.C.unsigned := 32;
function Mmap (addr : System.Address;
len : Interfaces.C.size_t;
prot : Interfaces.C.unsigned;
flags : Interfaces.C.unsigned;
fd : Interfaces.C.unsigned;
off : Interfaces.C.long) return System.Address;
pragma Import (C, Mmap, "mmap");
function Munmap (addr : System.Address;
len : Interfaces.C.size_t) return Interfaces.C.int;
pragma Import (C, Munmap, "munmap");
function Memcpy (dest, src : System.Address; n : Interfaces.C.size_t)
return System.Address;
pragma Import (C, Memcpy, "memcpy");
type Add_Fn is access
function (A, B : Interfaces.C.long) return Interfaces.C.long
with Convention => C;
function To_Add_Fn is
new Ada.Unchecked_Conversion (System.Address, Add_Fn);
Code : constant array (Positive range 1 .. 7)
of Interfaces.C.unsigned_char :=
(16#48#, 16#89#, 16#F8#, 16#48#, 16#01#, 16#F0#, 16#C3#);
Len : constant Interfaces.C.size_t := Code'Length;
Mem : constant System.Address :=
Mmap (System.Null_Address,
Len,
PROT_READ or PROT_WRITE or PROT_EXEC,
MAP_PRIVATE or MAP_ANON,
-1,
0);
Add : constant Add_Fn := To_Add_Fn (Mem);
Dummy : System.Address := Memcpy (Mem, Code'Address, Len);
Dummy2 : Interfaces.C.int;
Sum : Interfaces.C.long := Add (12, 7);
begin
Ada.Text_IO.Put_Line ("12 + 7 =" & Interfaces.C.long'Image (Sum));
Dummy2 := Munmap (Mem, Len);
end Machine_Code_Add;

View file

@ -0,0 +1,23 @@
/* mclib.c */
// gcc -c -fpic mclib.c
// gcc -shared -o mclib.so mclib.o
#include <string.h>
#include <sys/mman.h>
typedef unsigned char uchar;
extern uchar run_machine_code(const char *code, uchar a, uchar b, int len) {
void *buf;
uchar c;
/* copy code to executable buffer */
buf = mmap (0, len, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1, 0);
memcpy (buf, code, len);
/* run code */
c = ((uchar (*) (uchar, uchar))buf)(a, b);
/* free buffer */
munmap (buf, len);
return c;
}

View file

@ -0,0 +1,19 @@
local ffi = require "pluto:ffi"
local lib = ffi.open("./mclib.so")
lib:cdef[[
unsigned char run_machine_code(const char *code, unsigned char a, unsigned char b, int len);
]]
local a = 7
local b = 12
-- x64 opcodes for this task
local m = {
0x55, 0x48, 0x89, 0xe5, 0x89, 0x7d,
0xfc, 0x89, 0x75, 0xf8, 0x8b, 0x75,
0xfc, 0x03, 0x75, 0xf8, 0x89, 0x75,
0xf4, 0x8b, 0x45, 0xf4, 0x5d, 0xc3
}
local code = m:mapped(|byte| -> string.char(byte)):concat("")
print($"{a} + {b} = {lib.run_machine_code(code, a, b, #m)}")