import assert from'node:assert/strict';import{cpSync,mkdtempSync,copyFileSync,readFileSync,writeFileSync}from'node:fs';import{spawnSync}from'node:child_process';import{join}from'node:path';import{artifact}from'/scripts/os-acceptance/cpu-watchdog-artifacts.mjs'; const root=process.cwd(),d=mkdtempSync('reports/os-implementation/K2100-service-user-elf-'),sources=d+'/sources';for(const name of ['os-user-runtime-program','os-shell-words','os-shell-dispatch'])cpSync(name,sources+'/'+name,{recursive:true,filter:p=>!p.split('/').includes('target')});const program=sources+'/os-user-runtime-program';copyFileSync('scripts/os-acceptance/k2100-service-user.rs',program+'/src/main.rs');writeFileSync(program+'/Cargo.toml',readFileSync(program+'/Cargo.toml','utf8')+'\n[workspace]\n[dependencies]\nturkos-os-shell-words={path="../os-shell-words"}\nturkos-os-shell-dispatch={path="../os-shell-dispatch"}\n');copyFileSync('/tmp/turkos-k2100-service-user-build.mjs',d+'/harness.mjs.txt'); const env={...process.env,CARGO_TARGET_DIR:join(root,d,'target'),RUSTFLAGS:'-C embed-bitcode=yes -A warnings'};delete env.CARGO_ENCODED_RUSTFLAGS;const build=spawnSync('cargo',['rustc','--manifest-path',program+'/Cargo.toml','--offline','--release','--target','aarch64-unknown-none','--bin','runtime-two-argv','--','-C','relocation-model=static','-C','lto=fat','-C','codegen-units=1','-C','link-arg=-zmax-page-size=0x1000','-C','link-arg=-T'+join(root,program,'linker.ld')],{env,encoding:'utf8',timeout:180000,maxBuffer:32*1024*1024});writeFileSync(d+'/build.log',(build.stdout??'')+(build.stderr??''));const r={status:build.status===0?'COMPILED_K2100_SERVICE_USER_ELF':'K2100_SERVICE_USER_ELF_COMPILE_FAILED',guestAccepted:false,productAccepted:false,scope:'Actual EL0 shell parser/dispatcher program with five bounded syscall operations; no guest execution yet.',source:artifact(program+'/src/main.rs'),manifest:artifact(program+'/Cargo.toml'),linker:artifact(program+'/linker.ld'),words:artifact(sources+'/os-shell-words/src/lib.rs'),dispatch:artifact(sources+'/os-shell-dispatch/src/lib.rs'),harness:artifact(d+'/harness.mjs.txt'),build:{exitCode:build.status,log:artifact(d+'/build.log')}}; if(build.status===0){const elf=d+'/target/aarch64-unknown-none/release/runtime-two-argv',b=readFileSync(elf),u16=o=>b.readUInt16LE(o),u32=o=>b.readUInt32LE(o),u64=o=>Number(b.readBigUInt64LE(o));assert.equal(b.subarray(0,4).toString('hex'),'7f454c46');assert.equal(u16(18),183);assert.equal(u64(24),0x80000000);assert.equal(u16(56),2);r.segments=[0,1].map(i=>{const o=u64(32)+i*u16(54),x={flags:u32(o+4),fileSize:u64(o+32),memorySize:u64(o+40),virtualAddress:u64(o+16)};assert.ok(x.memorySize<=4096);return x});assert.deepEqual(r.segments.map(x=>[x.flags,x.virtualAddress]),[[5,0x80000000],[6,0x80001000]]);assert.ok(b.includes(Buffer.from([84,82,84,65,66,73,50,0,2,0,0,0,0,0,0,0])));r.elf=artifact(elf);} writeFileSync(d+'/report.json',JSON.stringify(r,null,2)+'\n');console.log(JSON.stringify({status:r.status,report:d+'/report.json',segments:r.segments,elf:r.elf}));if(build.status!==0)process.exitCode=1;