use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
pub const AOT_MAGIC: &[u8; 8] = b"STRK_AOT";
pub const AOT_VERSION: u32 = 1;
pub const TRAILER_LEN: u64 = 32;
#[derive(Debug, Clone)]
pub struct EmbeddedScript {
pub name: String,
pub source: String,
}
fn encode_payload(name: &str, source: &str) -> Vec<u8> {
let mut out = Vec::with_capacity(4 + name.len() + source.len());
let name_len = u32::try_from(name.len()).expect("script name length fits in u32");
out.extend_from_slice(&name_len.to_le_bytes());
out.extend_from_slice(name.as_bytes());
out.extend_from_slice(source.as_bytes());
out
}
fn decode_payload(bytes: &[u8]) -> Option<EmbeddedScript> {
if bytes.len() < 4 {
return None;
}
let name_len = u32::from_le_bytes(bytes[0..4].try_into().ok()?) as usize;
if 4 + name_len > bytes.len() {
return None;
}
let name = std::str::from_utf8(&bytes[4..4 + name_len])
.ok()?
.to_string();
let source = std::str::from_utf8(&bytes[4 + name_len..])
.ok()?
.to_string();
Some(EmbeddedScript { name, source })
}
fn build_trailer(compressed_len: u64, uncompressed_len: u64) -> [u8; 32] {
let mut trailer = [0u8; 32];
trailer[0..8].copy_from_slice(&compressed_len.to_le_bytes());
trailer[8..16].copy_from_slice(&uncompressed_len.to_le_bytes());
trailer[16..20].copy_from_slice(&AOT_VERSION.to_le_bytes());
trailer[24..32].copy_from_slice(AOT_MAGIC);
trailer
}
pub fn append_embedded_script(out_path: &Path, name: &str, source: &str) -> io::Result<()> {
let payload = encode_payload(name, source);
let compressed = zstd::stream::encode_all(&payload[..], 3)?;
let mut f = OpenOptions::new().append(true).open(out_path)?;
f.write_all(&compressed)?;
let trailer = build_trailer(compressed.len() as u64, payload.len() as u64);
f.write_all(&trailer)?;
f.sync_all()?;
Ok(())
}
pub fn try_load_embedded(exe: &Path) -> Option<EmbeddedScript> {
let mut f = File::open(exe).ok()?;
let size = f.metadata().ok()?.len();
if size < TRAILER_LEN {
return None;
}
f.seek(SeekFrom::End(-(TRAILER_LEN as i64))).ok()?;
let mut trailer = [0u8; TRAILER_LEN as usize];
f.read_exact(&mut trailer).ok()?;
if &trailer[24..32] != AOT_MAGIC {
return None;
}
let compressed_len = u64::from_le_bytes(trailer[0..8].try_into().ok()?);
let uncompressed_len = u64::from_le_bytes(trailer[8..16].try_into().ok()?);
let version = u32::from_le_bytes(trailer[16..20].try_into().ok()?);
if version != AOT_VERSION {
return None;
}
if compressed_len == 0 || compressed_len > size - TRAILER_LEN {
return None;
}
let payload_start = size - TRAILER_LEN - compressed_len;
f.seek(SeekFrom::Start(payload_start)).ok()?;
let mut compressed = vec![0u8; compressed_len as usize];
f.read_exact(&mut compressed).ok()?;
let payload = zstd::stream::decode_all(&compressed[..]).ok()?;
if payload.len() != uncompressed_len as usize {
return None;
}
decode_payload(&payload)
}
pub fn build(script_path: &Path, out_path: &Path) -> Result<PathBuf, String> {
let source = fs::read_to_string(script_path)
.map_err(|e| format!("stryke build: cannot read {}: {}", script_path.display(), e))?;
let script_name = script_path
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("script.pl")
.to_string();
crate::parse_with_file(&source, &script_name).map_err(|e| format!("{}", e))?;
let exe = std::env::current_exe()
.map_err(|e| format!("stryke build: locating current executable: {}", e))?;
copy_exe_without_trailer(&exe, out_path).map_err(|e| {
format!(
"stryke build: copy {} -> {}: {}",
exe.display(),
out_path.display(),
e
)
})?;
append_embedded_script(out_path, &script_name, &source)
.map_err(|e| format!("stryke build: write trailer: {}", e))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = fs::metadata(out_path) {
let mut p = meta.permissions();
p.set_mode(p.mode() | 0o111);
let _ = fs::set_permissions(out_path, p);
}
}
Ok(out_path.to_path_buf())
}
fn copy_exe_without_trailer(src: &Path, dst: &Path) -> io::Result<()> {
let mut sf = File::open(src)?;
let size = sf.metadata()?.len();
let keep = if size >= TRAILER_LEN {
sf.seek(SeekFrom::End(-(TRAILER_LEN as i64)))?;
let mut trailer = [0u8; TRAILER_LEN as usize];
if sf.read_exact(&mut trailer).is_ok() && &trailer[24..32] == AOT_MAGIC {
let compressed_len = u64::from_le_bytes(trailer[0..8].try_into().unwrap());
if compressed_len > 0 && compressed_len <= size - TRAILER_LEN {
size - TRAILER_LEN - compressed_len
} else {
size
}
} else {
size
}
} else {
size
};
sf.seek(SeekFrom::Start(0))?;
let _ = fs::remove_file(dst);
let mut df = File::create(dst)?;
let mut remaining = keep;
let mut buf = vec![0u8; 64 * 1024];
while remaining > 0 {
let n = std::cmp::min(remaining as usize, buf.len());
sf.read_exact(&mut buf[..n])?;
df.write_all(&buf[..n])?;
remaining -= n as u64;
}
df.sync_all()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn tmp_path(tag: &str) -> PathBuf {
let dir = std::env::temp_dir();
dir.join(format!(
"stryke-aot-test-{}-{}-{}",
std::process::id(),
tag,
rand::random::<u32>()
))
}
#[test]
fn payload_roundtrips_name_and_source() {
let payload = encode_payload("hello.pl", "print \"hi\\n\";\n");
let decoded = decode_payload(&payload).expect("decode");
assert_eq!(decoded.name, "hello.pl");
assert_eq!(decoded.source, "print \"hi\\n\";\n");
}
#[test]
fn append_and_load_trailer_roundtrips_on_plain_file() {
let path = tmp_path("roundtrip");
fs::write(
&path,
b"not really an ELF, but good enough for trailer tests",
)
.unwrap();
append_embedded_script(&path, "script.pl", "my $x = 1 + 2;").unwrap();
let loaded = try_load_embedded(&path).expect("load");
assert_eq!(loaded.name, "script.pl");
assert_eq!(loaded.source, "my $x = 1 + 2;");
fs::remove_file(&path).ok();
}
#[test]
fn load_returns_none_for_file_without_trailer() {
let path = tmp_path("no-trailer");
fs::write(&path, b"plain binary, no magic").unwrap();
assert!(try_load_embedded(&path).is_none());
fs::remove_file(&path).ok();
}
#[test]
fn load_returns_none_for_short_file() {
let path = tmp_path("short");
fs::write(&path, b"abc").unwrap();
assert!(try_load_embedded(&path).is_none());
fs::remove_file(&path).ok();
}
#[test]
fn copy_without_trailer_strips_embedded_script() {
let src = tmp_path("src");
let mid = tmp_path("mid");
let dst = tmp_path("dst");
fs::write(&src, b"pretend stryke binary bytes").unwrap();
fs::copy(&src, &mid).unwrap();
append_embedded_script(&mid, "a.pl", "say 1;").unwrap();
copy_exe_without_trailer(&mid, &dst).unwrap();
append_embedded_script(&dst, "b.pl", "say 2;").unwrap();
let loaded = try_load_embedded(&dst).expect("load layer 2");
assert_eq!(loaded.name, "b.pl");
assert_eq!(loaded.source, "say 2;");
let original = fs::read(&src).unwrap();
let mut stripped_dst = fs::read(&dst).unwrap();
stripped_dst.truncate(original.len());
assert_eq!(stripped_dst, original);
fs::remove_file(&src).ok();
fs::remove_file(&mid).ok();
fs::remove_file(&dst).ok();
}
#[test]
fn bad_magic_is_ignored() {
let path = tmp_path("bad-magic");
let mut bytes = vec![0u8; 200];
let tail = &mut bytes[200 - 32..];
tail[0..8].copy_from_slice(&10u64.to_le_bytes()); tail[8..16].copy_from_slice(&20u64.to_le_bytes());
tail[16..20].copy_from_slice(&1u32.to_le_bytes());
tail[24..32].copy_from_slice(b"NOTPERLZ");
fs::write(&path, &bytes).unwrap();
assert!(try_load_embedded(&path).is_none());
fs::remove_file(&path).ok();
}
}