883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
81 lines
3.7 KiB
Python
81 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Only the new owned scratch lifetime/reentry and ordinary-return boundary."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from engine_registration_realm_test import FIXTURE, ROOT
|
|
|
|
|
|
class RealmScratchBoundary(unittest.TestCase):
|
|
def test_reentry_release_and_nonlocal_fail_closed(self):
|
|
fixture = FIXTURE.split('int main(void){', 1)[0]
|
|
fixture = '#include <setjmp.h>\n' + fixture
|
|
fixture = fixture.replace('static int scenario,calls;', '''
|
|
static int scenario,calls,reenter_source,reenter_marshalling,jump_marshalling;
|
|
static jmp_buf escape;
|
|
''')
|
|
fixture = fixture.replace('++calls;assert(capacity==64);', '''
|
|
++calls;assert(capacity==64);
|
|
if(reenter_source){
|
|
reenter_source=0;int previous=lua.count;
|
|
assert(gw_registration_get_realm_snapshot(&lua)==1);
|
|
assert(lua.count==previous+1&&top(&lua)->kind==NUMBER&&top(&lua)->number==REALM_SNAPSHOT_BUSY);
|
|
lua.count=previous;
|
|
}
|
|
''')
|
|
fixture = fixture.replace('struct Lua*s=L;assert(array==0', '''
|
|
if(jump_marshalling){jump_marshalling=0;longjmp(escape,1);}
|
|
if(reenter_marshalling){
|
|
reenter_marshalling=0;struct Lua*s=L;int previous=s->count;
|
|
assert(gw_registration_get_realm_snapshot(L)==1);
|
|
assert(s->count==previous+1&&top(L)->kind==NUMBER&&top(L)->number==REALM_SNAPSHOT_BUSY);
|
|
s->count=previous;
|
|
}
|
|
struct Lua*s=L;assert(array==0''')
|
|
fixture += r'''
|
|
int main(void){
|
|
gw_registration_data.lua_state=(u64)(uintptr_t)&lua;
|
|
gw_registration_realm_binding.module=0x1000;
|
|
gw_registration_realm_binding.snapshot=(u64)(uintptr_t)source_snapshot;
|
|
/* Binding refusal is an ordinary return: the next valid capture must work. */
|
|
assert(run()==1&&top(&lua)->number==REALM_BINDING_HELD&&calls==0);
|
|
gw_registration_realm_binding.ready=1;
|
|
reenter_source=1;reenter_marshalling=1;
|
|
assert(run()==1&&calls==1&&lua.count==1&&top(&lua)->kind==TABLE);
|
|
struct Table*t=top(&lua)->table;
|
|
assert(field(t,"r0")->kind==TABLE&&!maybe_field(t,"password"));
|
|
assert(field(field(t,"r0")->table,"wire_id")->number==5);
|
|
assert(reenter_source==0&&reenter_marshalling==0);
|
|
scenario=2;assert(run()==1&&top(&lua)->number==-19&&calls==2);
|
|
scenario=0;assert(run()==1&&top(&lua)->kind==TABLE&&calls==3);
|
|
/* Model a nonlocal exit across an original Lua helper. The subsequent call
|
|
* must fail closed without recapturing or using partially marshalled rows. */
|
|
reset();jump_marshalling=1;
|
|
if(!setjmp(escape)){(void)gw_registration_get_realm_snapshot(&lua);assert(!"Expected nonlocal exit");}
|
|
int previous=calls;reset();
|
|
assert(gw_registration_get_realm_snapshot(&lua)==1);
|
|
assert(lua.count==1&&top(&lua)->number==REALM_SNAPSHOT_BUSY&&calls==previous);
|
|
reset();assert(gw_registration_request_realm(&lua)==1);
|
|
assert(top(&lua)->number==REALM_SELECT_HELD&&calls==previous);
|
|
assert(gw_registration_get_realm_snapshot(&other_lua)==0);
|
|
puts("PASS new scratch lifetime: capture/marshal reentry held, ordinary returns release, nonlocal exit fails closed");
|
|
return 0;
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
directory = Path(temporary)
|
|
source, binary = directory / 'scratch.c', directory / 'scratch'
|
|
source.write_text(fixture)
|
|
compiled = subprocess.run(['cc', '-std=c11', '-O2', '-Wall', '-Wextra', '-Werror',
|
|
'-Wno-missing-field-initializers', '-I', str(ROOT / 'native'), str(source), '-o', str(binary)],
|
|
capture_output=True, text=True)
|
|
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
|
result = subprocess.run([str(binary)], capture_output=True, text=True)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn('PASS new scratch lifetime', result.stdout)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|