feat(lang): L7 memory is safe unless it says unsafe

The typed buffers are slices: words/floats/fixeds/doubles/pointers(n) make
zeroed, bounds-checked []int/[]float/... and the type names mean them. buffer(n)
is a []byte, with text_of, Fs.read_bytes/write_bytes and view(xs, start, n).
bytes(), indexing a raw pointer or bytes, free, resize, Memory.*, raw file calls,
data_of and C externs are refused outside unsafe { } / unsafe function, and a
project's own files may write unsafe only with --unsafe; the runtime and packages
are the platform. A slice passed to an extern goes as its data.

What the change found: Sync's atomics on a slice header, words(n) uninitialised,
input's fixed axes in ints, truetype's fixed outlines as ints, skin matrices
typed int, gl_shader's source table made from raw bytes. render3d gets safe
entry points (safe_api.ludic). Rendering is byte-identical; a frame costs the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-24 12:53:27 +03:00
parent 9259808f80
commit b0b0b62bce
70 changed files with 69189 additions and 64569 deletions

View file

@ -7,55 +7,57 @@
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
program TiledP0 {
entry {
# --- base64 decode: RFC 4648 test vectors (every padding remainder) ---
if Base64.decode("") == "" { print(1) }
if Base64.decode("Zg==") == "f" { print(2) }
if Base64.decode("Zm8=") == "fo" { print(3) }
if Base64.decode("Zm9v") == "foo" { print(4) }
if Base64.decode("Zm9vYg==") == "foob" { print(5) }
if Base64.decode("Zm9vYmE=") == "fooba" { print(6) }
if Base64.decode("Zm9vYmFy") == "foobar" { print(7) }
# decode ignores embedded whitespace/newlines (as TMX <data> carries)
if Base64.decode("Zm9v\n YmFy") == "foobar" { print(8) }
unsafe { # raw buffers on purpose: built with --unsafe (L7)
# --- base64 decode: RFC 4648 test vectors (every padding remainder) ---
if Base64.decode("") == "" { print(1) }
if Base64.decode("Zg==") == "f" { print(2) }
if Base64.decode("Zm8=") == "fo" { print(3) }
if Base64.decode("Zm9v") == "foo" { print(4) }
if Base64.decode("Zm9vYg==") == "foob" { print(5) }
if Base64.decode("Zm9vYmE=") == "fooba" { print(6) }
if Base64.decode("Zm9vYmFy") == "foobar" { print(7) }
# decode ignores embedded whitespace/newlines (as TMX <data> carries)
if Base64.decode("Zm9v\n YmFy") == "foobar" { print(8) }
# --- base64 encode round-trips ---
if Base64.encode("foobar") == "Zm9vYmFy" { print(9) }
if Base64.encode("f") == "Zg==" { print(10) }
if Base64.decode(Base64.encode("the quick brown fox")) == "the quick brown fox" { print(11) }
# --- base64 encode round-trips ---
if Base64.encode("foobar") == "Zm9vYmFy" { print(9) }
if Base64.encode("f") == "Zg==" { print(10) }
if Base64.decode(Base64.encode("the quick brown fox")) == "the quick brown fox" { print(11) }
# --- XML reader over a TSX element tree ---
let tsx = "<?xml version='1.0' encoding='UTF-8'?>\n<!-- a comment -->\n<tileset version='1.10' name='sheet &amp; co' tilewidth='16' tileheight='16' tilecount='4' columns='2'>\n <image source='sheet.png' width='32' height='32'/>\n <tile id='0'><properties><property name='solid' type='bool' value='true'/></properties></tile>\n <data encoding='csv'>1,2,3,4</data>\n</tileset>\n"
let root = Xml.parse(tsx)
if Xml.tag(root) == "tileset" { print(12) }
if Xml.attr(root, "name") == "sheet & co" { print(13) } # entity decode
if Xml.attr_int(root, "tilewidth", 0) == 16 { print(14) }
if Xml.attr_int(root, "tilecount", 0) == 4 { print(15) }
let img = Xml.find(root, "image")
if Xml.attr(img, "source") == "sheet.png" { print(16) }
let tile = Xml.find(root, "tile")
let props = Xml.find(tile, "properties")
let prop = Xml.find(props, "property")
if Xml.attr(prop, "value") == "true" { print(17) }
let data = Xml.find(root, "data")
if Xml.text(data) == "1,2,3,4" { print(18) }
# --- XML reader over a TSX element tree ---
let tsx = "<?xml version='1.0' encoding='UTF-8'?>\n<!-- a comment -->\n<tileset version='1.10' name='sheet &amp; co' tilewidth='16' tileheight='16' tilecount='4' columns='2'>\n <image source='sheet.png' width='32' height='32'/>\n <tile id='0'><properties><property name='solid' type='bool' value='true'/></properties></tile>\n <data encoding='csv'>1,2,3,4</data>\n</tileset>\n"
let root = Xml.parse(tsx)
if Xml.tag(root) == "tileset" { print(12) }
if Xml.attr(root, "name") == "sheet & co" { print(13) } # entity decode
if Xml.attr_int(root, "tilewidth", 0) == 16 { print(14) }
if Xml.attr_int(root, "tilecount", 0) == 4 { print(15) }
let img = Xml.find(root, "image")
if Xml.attr(img, "source") == "sheet.png" { print(16) }
let tile = Xml.find(root, "tile")
let props = Xml.find(tile, "properties")
let prop = Xml.find(props, "property")
if Xml.attr(prop, "value") == "true" { print(17) }
let data = Xml.find(root, "data")
if Xml.text(data) == "1,2,3,4" { print(18) }
# --- gzip framing: base64 -> bytes -> gunzip round-trips ---
let gz = "H4sIAAAAAAAC//NIzcnJ11EIycxJTVFIr8osUCjPL8pJUVQwMDQyNjE1M7ewVCjJSFUoLM1MzlZIKsovz1NIy6/QAwCLdXOzOAAAAA=="
let comp = bytes(256)
let clen = b64_decode(gz, comp)
let out = bytes(256)
let n = z_gunzip(comp, clen, out, 256)
out[n] = 0
if n == 56 { print(19) }
if out == "Hello, Tiled gzip world! 0123456789 the quick brown fox." { print(20) }
# --- gzip framing: base64 -> bytes -> gunzip round-trips ---
let gz = "H4sIAAAAAAAC//NIzcnJ11EIycxJTVFIr8osUCjPL8pJUVQwMDQyNjE1M7ewVCjJSFUoLM1MzlZIKsovz1NIy6/QAwCLdXOzOAAAAA=="
let comp = bytes(256)
let clen = b64_decode(gz, comp)
let out = bytes(256)
let n = z_gunzip(comp, clen, out, 256)
out[n] = 0
if n == 56 { print(19) }
if out == "Hello, Tiled gzip world! 0123456789 the quick brown fox." { print(20) }
# --- zlib still round-trips through the same path (regression) ---
let zl = "eJzzSM3JyddRCMnMSU1RSK/KLFAozy/KSVFUMDA0MjYxNTO3sFQoyUhVKCzNTM5WSCrKL89TSMuv0AMAA10SJA=="
let zc = bytes(256)
let zclen = b64_decode(zl, zc)
let zout = bytes(256)
let zn = z_uncompress(zc, zclen, zout, 256)
zout[zn] = 0
if zout == "Hello, Tiled gzip world! 0123456789 the quick brown fox." { print(21) }
# --- zlib still round-trips through the same path (regression) ---
let zl = "eJzzSM3JyddRCMnMSU1RSK/KLFAozy/KSVFUMDA0MjYxNTO3sFQoyUhVKCzNTM5WSCrKL89TSMuv0AMAA10SJA=="
let zc = bytes(256)
let zclen = b64_decode(zl, zc)
let zout = bytes(256)
let zn = z_uncompress(zc, zclen, zout, 256)
zout[zn] = 0
if zout == "Hello, Tiled gzip world! 0123456789 the quick brown fox." { print(21) }
}
}
}

View file

@ -6,11 +6,11 @@ program UdpDemo {
let pa = Udp.port(a)
let pb = Udp.port(b)
let lo = Udp.ip("127.0.0.1")
let out = Memory.bytes(16)
Memory.poke(out, 0, 104)
Memory.poke(out, 1, 105)
let out = buffer(16)
out[0] = 104
out[1] = 105
let sent = Udp.send(a, lo, pb, out, 2)
let inb = Memory.bytes(64)
let inb = buffer(64)
var got = 0
var tries = 0
while got == 0 and tries < 200000 { got = Udp.recv(b, inb, 64); tries += 1 }
@ -20,7 +20,7 @@ program UdpDemo {
tries = 0
while back == 0 and tries < 200000 { back = Udp.recv(a, out, 16); tries += 1 }
let bad = Udp.ip("1.2.3") == 0 and Udp.ip("300.1.1.1") == 0
print(`{sent} {got} {Memory.peek(inb, 0)} {Memory.peek(inb, 1)} {from_ok} {back} {Udp.ip_text(lo)} {bad} {Udp.recv(a, out, 16)} {pa > 0}`)
print(`{sent} {got} {inb[0]} {inb[1]} {from_ok} {back} {Udp.ip_text(lo)} {bad} {Udp.recv(a, out, 16)} {pa > 0}`)
Udp.close(a)
Udp.close(b)
}

View file

@ -8,17 +8,19 @@
# them back through the loopback: prints 4, then 10 20 30 42.
program NetEcho {
entry {
let out = bytes(4)
out[0] = '\n'
out[1] = 20
out[2] = 30
out[3] = '*'
net_send(0, out, 4) # onto the wire (the built-in loopback)
unsafe { # raw buffers on purpose: built with --unsafe (L7)
let out = bytes(4)
out[0] = '\n'
out[1] = 20
out[2] = 30
out[3] = '*'
net_send(0, out, 4) # onto the wire (the built-in loopback)
let inb = bytes(64)
let n = net_poll(inb, 64) # take the next datagram back off
print(n) # 4
var i = 0
while i < n { print(inb[i]); i += 1 } # 10 20 30 42
let inb = bytes(64)
let n = net_poll(inb, 64) # take the next datagram back off
print(n) # 4
var i = 0
while i < n { print(inb[i]); i += 1 } # 10 20 30 42
}
}
}

View file

@ -15,15 +15,17 @@ program NetSnapshot {
model Unit { Health }
entry {
spawn Unit { Health { hp: 50, max: 100 } }
let buf = bytes(world_size())
for (Health) in query [Health, {Unit}] {
print(Health.hp) # 50
let n = world_save(buf) # snapshot the whole world
Health.hp = 7
print(Health.hp) # 7
world_load(buf, n) # roll the world back
print(Health.hp) # 50 — restored from bytes
unsafe { # raw buffers on purpose: built with --unsafe (L7)
spawn Unit { Health { hp: 50, max: 100 } }
let buf = bytes(world_size())
for (Health) in query [Health, {Unit}] {
print(Health.hp) # 50
let n = world_save(buf) # snapshot the whole world
Health.hp = 7
print(Health.hp) # 7
world_load(buf, n) # roll the world back
print(Health.hp) # 50 — restored from bytes
}
}
}
}

View file

@ -22,15 +22,15 @@ program VkCompute {
var dev: pointer = null
function fail(what: string, r: int) -> void { print(`vulkan: {what} failed, VkResult {r}`); quit() }
function handle(out: bytes) -> long { return vk_get_i64(out, 0) }
function has_ext(props: bytes, n: int, want: string) -> bool {
unsafe function fail(what: string, r: int) -> void { print(`vulkan: {what} failed, VkResult {r}`); quit() }
unsafe function handle(out: bytes) -> long { return vk_get_i64(out, 0) }
unsafe function has_ext(props: bytes, n: int, want: string) -> bool {
for i in 0 .. n {
if string(vk_at(props, i * VkExtensionProperties_sizeof + VkExtensionProperties_extensionName)) == want { return true }
}
return false
}
function read_all(path: string) -> bytes {
unsafe function read_all(path: string) -> bytes {
let f = file_open(path, "rb")
if f == null { return null }
file_seek(f, 0, 2)

View file

@ -16,11 +16,11 @@ program VkProbe {
const API_1_0: int = 4194304 # VK_MAKE_API_VERSION(0, 1, 0, 0)
function api_text(v: int) -> string { return `{(v >> 22) & 127}.{(v >> 12) & 1023}.{v & 4095}` }
function yes(b: bool) -> string { if b { return "yes" }; return "no " }
unsafe function api_text(v: int) -> string { return `{(v >> 22) & 127}.{(v >> 12) & 1023}.{v & 4095}` }
unsafe function yes(b: bool) -> string { if b { return "yes" }; return "no " }
# a name in an array of VkExtensionProperties
function has_ext(props: bytes, n: int, want: string) -> bool {
unsafe function has_ext(props: bytes, n: int, want: string) -> bool {
for i in 0 .. n {
let at = vk_at(props, i * VkExtensionProperties_sizeof + VkExtensionProperties_extensionName)
if string(at) == want { return true }

View file

@ -14,7 +14,7 @@ program VkResources {
model Anchor { Marker }
var failures: int = 0
function check(what: string, ok: bool) -> void {
unsafe function check(what: string, ok: bool) -> void {
if ok { print(`vulkan: {what} ok`) } else { print(`vulkan: {what} FAILED`); failures += 1 }
}

View file

@ -21,16 +21,16 @@ program VkTriangle {
const H: int = 240
const FMT: int = VK_FORMAT_R8G8B8A8_UNORM
function fail(what: string, r: int) -> void { print(`vulkan: {what} failed, VkResult {r}`); quit() }
function handle(out: bytes) -> long { return vk_get_i64(out, 0) }
function has_ext(props: bytes, n: int, want: string) -> bool {
unsafe function fail(what: string, r: int) -> void { print(`vulkan: {what} failed, VkResult {r}`); quit() }
unsafe function handle(out: bytes) -> long { return vk_get_i64(out, 0) }
unsafe function has_ext(props: bytes, n: int, want: string) -> bool {
for i in 0 .. n {
if string(vk_at(props, i * VkExtensionProperties_sizeof + VkExtensionProperties_extensionName)) == want { return true }
}
return false
}
var spv_len: int = 0
function read_all(path: string) -> bytes {
unsafe function read_all(path: string) -> bytes {
let f = file_open(path, "rb")
if f == null { return null }
file_seek(f, 0, 2)
@ -43,7 +43,7 @@ program VkTriangle {
return b
}
# the first memory type the resource allows that has every property wanted
function mem_type(mp: bytes, allowed: int, want: int) -> int {
unsafe function mem_type(mp: bytes, allowed: int, want: int) -> int {
for t in 0 .. vk_get_i32(mp, VkPhysicalDeviceMemoryProperties_memoryTypeCount) {
let pf = vk_get_i32(mp, VkPhysicalDeviceMemoryProperties_memoryTypes + t * VkMemoryType_sizeof + VkMemoryType_propertyFlags)
if ((allowed >> t) & 1) == 1 and (pf & want) == want { return t }
@ -51,14 +51,14 @@ program VkTriangle {
return -1
}
# one colour image, one mip, one layer
function color_range(at: bytes, off: int) -> void {
unsafe function color_range(at: bytes, off: int) -> void {
vk_put_i32(at, off + VkImageSubresourceRange_aspectMask, VK_IMAGE_ASPECT_COLOR_BIT)
vk_put_i32(at, off + VkImageSubresourceRange_baseMipLevel, 0)
vk_put_i32(at, off + VkImageSubresourceRange_levelCount, 1)
vk_put_i32(at, off + VkImageSubresourceRange_baseArrayLayer, 0)
vk_put_i32(at, off + VkImageSubresourceRange_layerCount, 1)
}
function barrier(cb: pointer, image: long, old_layout: int, new_layout: int, src_access: int, dst_access: int, src_stage: int, dst_stage: int) -> void {
unsafe function barrier(cb: pointer, image: long, old_layout: int, new_layout: int, src_access: int, dst_access: int, src_stage: int, dst_stage: int) -> void {
let b = bytes(VkImageMemoryBarrier_sizeof)
vk_zero(b, VkImageMemoryBarrier_sizeof)
vk_put_i32(b, VkImageMemoryBarrier_sType, VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER)
@ -72,7 +72,7 @@ program VkTriangle {
color_range(b, VkImageMemoryBarrier_subresourceRange)
vk_cmd_pipeline_barrier(cb, src_stage, dst_stage, 0, 0, null, 0, null, 1, b)
}
function shader_module(dev: pointer, path: string) -> long {
unsafe function shader_module(dev: pointer, path: string) -> long {
let spv = read_all(path)
if spv == null { print(`vulkan: no SPIR-V at {path} (compile vk_triangle.slang with slangc)`); quit() }
let smci = bytes(VkShaderModuleCreateInfo_sizeof)