Batch 29 (Zephyr and NuttX fixes) commit 2bb22bb gave NuttX mounted-path transactions and Zephyr NVS recovery compaction as HARD and NuttX ELF module bindings as MEDIUM. Batch 28 commit b804230 gave Mynewt JSON arrays as MEDIUM; four of its tasks are being topped up. Each task comes from the commit Qwen measured, its digest matches, and it is still present at the batch branch head. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
10 KiB
Python
212 lines
10 KiB
Python
"""Private parent oracle: synthetic ELF inputs and raw child observations."""
|
|
import errno
|
|
import json
|
|
from pathlib import Path
|
|
import random
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
|
|
def image(case):
|
|
"""64-bit little endian ELF; the configured observer never executes it."""
|
|
data = bytearray(8192)
|
|
etype = case.get('etype', 3)
|
|
fdpic = case.get('fdpic', True)
|
|
name = case.get('name', 'console_puts')
|
|
section_value = case.get('value', 0x90)
|
|
section = case.get('section', 2 if section_value >= 1024 else 1)
|
|
stype = case.get('stype', 3)
|
|
binding = case.get('binding', 0)
|
|
stringtab = b'\0' + name.encode() + b'\0nx_stacksize\0'
|
|
symname = 0 if stype == 3 else 1
|
|
syms = [(0,0,0,0,0,0), (symname,(binding<<4)|stype,case.get('other',0),section,section_value,17),
|
|
(1,(case.get('weak',False) and 2 or 1)<<4|2,0,0,0,0),
|
|
(len(name)+2,0x11,0,1,0x170,4)]
|
|
for i, sym in enumerate(syms):
|
|
struct.pack_into('<IBBHQQ',data,0x400+24*i,*sym)
|
|
data[0x800:0x800+len(stringtab)] = stringtab
|
|
sections=[(0,0,0,0,0,0,0,0,0,0)]
|
|
names=b'\0.text\0.data\0.dynsym\0.symtab\0.dynamic\0.strtab\0.rel.dyn\0.rel.plt\0.shstrtab\0'
|
|
ns=lambda s:names.index(s.encode()+b'\0')
|
|
sections += [(ns('.text'),1,6,0,0x1800,512,0,0,8,0),
|
|
(ns('.data'),1,3,1024,0x1c00,512,0,0,8,0),
|
|
(ns('.dynsym'),11,0,0,0x400,96,6,2,8,24),
|
|
(ns('.symtab'),2,0,0,0x400,96,6,2,8,24)]
|
|
rels=[]
|
|
indices=case.get('indices',[case.get('sym',2)])
|
|
rela=case.get('rela',False)
|
|
plt=case.get('plt',False)
|
|
for i,idx in enumerate(indices):
|
|
offset=32*i
|
|
v=case.get('word',0x95)+i*2
|
|
struct.pack_into('<Q',data,0x1c00+offset,v)
|
|
add=case.get('addend',8) if rela else 0
|
|
if rela:
|
|
struct.pack_into('<Q',data,0x1c00+offset+add,v)
|
|
info=(idx<<32)|(case.get('rtype',7)+i)
|
|
roff=offset if etype==1 else 1024+offset
|
|
rels.append(struct.pack('<QQq',roff,info,add) if rela else struct.pack('<QQ',roff,info))
|
|
relblob=b''.join(rels)
|
|
relpos=0x1200 if plt else 0x1000
|
|
data[relpos:relpos+len(relblob)]=relblob
|
|
if plt:
|
|
dyn=[(23,relpos),(2,len(relblob)),(20,7 if rela else 17)]
|
|
else:
|
|
dyn=[(17,relpos),(18,len(relblob)),(19,16)]
|
|
dyn += [(6,0x400),(5,0x800),(0,0)]
|
|
for i,(tag,val) in enumerate(dyn):struct.pack_into('<qQ',data,0xc00+i*16,tag,val)
|
|
sections += [(ns('.dynamic'),6,0,0,0xc00,len(dyn)*16,6,0,8,16),
|
|
(ns('.strtab'),3,0,0,0x800,len(stringtab),0,0,1,0),
|
|
(ns('.rel.dyn'),4 if rela else 9,0x40 if etype==1 and not plt else 0,0,relpos,len(relblob) if not plt else 0,4,2,8,24 if rela else 16),
|
|
(ns('.rel.plt'),4 if rela else 9,0x40 if etype==1 and plt else 0,0,relpos,len(relblob) if plt else 0,4,2,8,24 if rela else 16),
|
|
(ns('.shstrtab'),3,0,0,0x1400,len(names),0,0,1,0)]
|
|
data[0x1400:0x1400+len(names)]=names
|
|
ident=b'\x7fELF'+bytes([2,1,1,65 if fdpic else 0])+bytes(8)
|
|
phnum=2 if etype==3 else 0
|
|
struct.pack_into('<16sHHIQQQIHHHHHH',data,0,ident,etype,62,1,0,64,256,0,64,56,phnum,64,len(sections),9)
|
|
if phnum:
|
|
struct.pack_into('<IIQQQQQQ',data,64,1,5,0x1800,0,0,512,512,8)
|
|
struct.pack_into('<IIQQQQQQ',data,120,1,6,0x1c00,1024,0,512,576,8)
|
|
for i,s in enumerate(sections):struct.pack_into('<IIQQQQIIQQ',data,256+i*64,*s)
|
|
return bytes(data)
|
|
|
|
|
|
def cases():
|
|
rng=random.Random(0x411d9)
|
|
out=[]
|
|
def add(**kw):
|
|
c={'name':'import_'+str(rng.getrandbits(44)), 'export':rng.randrange(0x4000,0x10000),
|
|
'global':0, 'nexports':2,'fdpic':True,'etype':3,'fail':0,'find':'-',
|
|
'sym':2,'word':0x95,'value':0x90,'binding':0,'stype':3,'other':0,'plt':False,'rela':False}
|
|
c.update(kw);out.append(c)
|
|
# Dynamic imports: global table precedence, private caller tables and bounds.
|
|
for fdpic in (True,False):
|
|
for plt in (False,True):
|
|
add(fdpic=fdpic,plt=plt)
|
|
add(fdpic=fdpic,plt=plt,global_value=1)
|
|
add(fdpic=fdpic,plt=plt,nexports=1)
|
|
add(fdpic=fdpic,plt=plt,nexports=0)
|
|
add(fdpic=fdpic,plt=plt,nexports=-1)
|
|
add(fdpic=fdpic,plt=plt,nexports=0,weak=True)
|
|
add(fdpic=fdpic,plt=plt,nexports=-1,weak=True)
|
|
add(name='api_'+('abcdefghij'*10),indices=[2,2,2,2,2,2])
|
|
add(export=0,weak=True)
|
|
add(export=(1<<48)+rng.getrandbits(30))
|
|
add(global_value=1,global_high=True)
|
|
# Defined FDPIC symbols: text and independently placed data, REL addends.
|
|
for plt in (False,True):
|
|
for value in (0,0x7f,0x90,1024,1033,1100):
|
|
add(plt=plt,sym=1,value=value,word=rng.randrange(1,255)|1)
|
|
add(plt=plt,indices=[1,1,1,1,1,1],value=0xa0,word=0x95)
|
|
add(plt=plt,sym=1,stype=2,binding=1,other=2,value=0xb1)
|
|
add(plt=plt,indices=[1,1,1],fail=-errno.EIO)
|
|
# Existing symbol-free path, non-FDPIC defined-symbol policy, PLT RELA.
|
|
add(sym=0,word=0x84)
|
|
add(sym=0,word=1033)
|
|
add(sym=1,fdpic=False)
|
|
add(fdpic=False,rela=True,plt=True,sym=0,word=0x84)
|
|
add(fdpic=False,rela=True,plt=True,sym=2)
|
|
add(fdpic=True,rela=True,plt=True,sym=1)
|
|
# ET_REL regression paths: REL and RELA exports and section symbols.
|
|
for rela in (False,True):
|
|
add(etype=1,fdpic=False,rela=rela,sym=2)
|
|
add(etype=1,fdpic=False,rela=rela,sym=1,value=0x30)
|
|
add(etype=1,fdpic=False,rela=rela,sym=1,value=9,section=2)
|
|
add(etype=1,fdpic=False,rela=rela,sym=1,fail=-errno.EIO)
|
|
# Optional symbol lookup walks valid null/section symbols without error logs.
|
|
add(sym=2,find='nx_stacksize')
|
|
add(sym=2,find='absent_optional_symbol')
|
|
for c in out:
|
|
if c.pop('global_value',0):c['global']=rng.randrange(0x11000,0x19000)
|
|
if c.pop('global_high',False):c['global']+=(1<<47)
|
|
return out
|
|
|
|
|
|
def expected(c,obs):
|
|
# All expected values are computed here, never in candidate address space.
|
|
for k,v in [('init',0),('load',0),('fdpic',int(c['fdpic'])),('dynamic',int(c['etype']==3))]:
|
|
if obs.get(k)!=v:return k
|
|
text=obs.get('text'); data=obs.get('data')
|
|
if type(text)!=int or type(data)!=int or text<=0 or data<=0 or text==data:return 'allocations'
|
|
if obs.get('datasec')!=(1024 if c['etype']==3 else 0):return 'segment link base'
|
|
def mapped(v):return data+v-1024 if v>=1024 else text+v
|
|
indices=c.get('indices',[c['sym']]);events=[];words=[0]*64;ret=0
|
|
rela=c.get('rela',False);plt=c.get('plt',False)
|
|
for i,idx in enumerate(indices):
|
|
value=c['word']+i*2;off=i*32;words[off//8]=value
|
|
if rela:words[(off+c.get('addend',8))//8]=value
|
|
if c['fdpic'] and rela and plt and c['etype']==3:
|
|
ret=-errno.ENOEXEC
|
|
else:
|
|
for i,idx in enumerate(indices):
|
|
off=i*32;value=c['word']+i*2;where=data+off
|
|
roff=off if c['etype']==1 else 1024+off
|
|
info=(idx<<32)|(c.get('rtype',7)+i)
|
|
addend=c.get('addend',8) if rela else 0
|
|
if c['etype']==3 and idx==2:
|
|
resolved=c['global'] or (c['export'] if c['nexports']>=2 else 0)
|
|
if not resolved and not c.get('weak',False):ret=-errno.EINVAL;break
|
|
if rela:where+=addend
|
|
words[(off+addend)//8]=resolved
|
|
continue
|
|
if c['etype']==3 and idx==1 and not c['fdpic']:continue
|
|
if c['etype']==3 and rela:where+=addend
|
|
if idx==1:
|
|
symvalue=mapped(c['value']) if c['etype']==3 else (data+c['value'] if c.get('section',2 if c['value']>=1024 else 1)==2 else text+c['value'])
|
|
stinfo=(c['binding']<<4)|c['stype'];name=0 if c['stype']==3 else 1
|
|
other=c['other'];shndx=c.get('section',2 if c['value']>=1024 else 1);size=17
|
|
elif idx==2:
|
|
symvalue=c['export'];stinfo=(2 if c.get('weak') else 1)<<4|2
|
|
name=1;other=0;shndx=0;size=0
|
|
else:
|
|
symvalue=mapped(value);stinfo=0;name=0;other=0;shndx=0;size=0
|
|
events.append([int(rela and c['etype']==1),1,symvalue,where,roff,info,value,
|
|
addend if c['etype']==1 else 0,name,stinfo,other,shndx,size])
|
|
if c['fail']:
|
|
ret=c['fail'];break
|
|
for k,v in [('bind',ret),('events',events),('words',words)]:
|
|
if obs.get(k)!=v:return k+' expected='+str(v)[:400]+' got='+str(obs.get(k))[:400]
|
|
if c['find']!='-':
|
|
wanted=0 if c['find']=='nx_stacksize' else -errno.ENOENT
|
|
if obs.get('found')!=wanted:return 'optional symbol result'
|
|
if wanted==0 and obs.get('found_value')!=0x170:return 'optional symbol value'
|
|
if obs.get('scan_errors')!=0:return 'nameless symbol emitted error diagnostics'
|
|
return None
|
|
|
|
|
|
def main():
|
|
candidate=Path(sys.argv[1]);tests=cases()
|
|
with tempfile.TemporaryDirectory(prefix='elf-bind-grade-') as tmp:
|
|
work=Path(tmp)
|
|
# Everything is materialized before compilation or candidate execution.
|
|
with tarfile.open(Path(__file__).with_name('support.tar'),'r') as ar:
|
|
for member in ar.getmembers():
|
|
if not member.isfile():continue
|
|
path=work/member.name;path.parent.mkdir(parents=True,exist_ok=True)
|
|
path.write_bytes(ar.extractfile(member).read())
|
|
for name in ('libs/libc/elf/elf_bind.c','libs/libc/elf/elf_symbols.c'):
|
|
(work/name).write_bytes((candidate/name).read_bytes())
|
|
for i,c in enumerate(tests):(work/('module-%d.elf'%i)).write_bytes(image(c))
|
|
build=subprocess.run(['make','observe'],cwd=work,text=True,capture_output=True,timeout=35)
|
|
if build.returncode:
|
|
print('FAIL build');return 1
|
|
count=0
|
|
for i,c in enumerate(tests):
|
|
request='%s %d %d %d %d %s\n'%(c['name'],c['export'],c['nexports'],c['global'],c['fail'],c['find'])
|
|
r=subprocess.run([str(work/'observe'),str(work/('module-%d.elf'%i))],input=request,cwd=work,text=True,capture_output=True,timeout=4)
|
|
if r.returncode:
|
|
print('FAIL child %d exit %d'%(i,r.returncode));return 1
|
|
try:obs=json.loads(r.stdout)
|
|
except (ValueError,TypeError):print('FAIL observation %d'%i);return 1
|
|
failure=expected(c,obs)
|
|
if failure:
|
|
print('FAIL case %d %s'%(i,failure));return 1
|
|
count+=1
|
|
print('checked %d ELF scenarios'%count)
|
|
print('PASS');return 0
|
|
|
|
if __name__=='__main__':raise SystemExit(main())
|