
    @JjK                    l   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ej                  j                  d e e	e      j!                         j"                               ddlmZmZmZ ddlmZmZ ddlmZ  e	e      j!                         j6                  d	   Zed
z  ZdZdZdZ dZ!dZ"dZ#dZ$dg dddddgddddgddg ddgZ%dddd Z&d0d!Z'd1d"Z(d2d#Z)d3d$Z*d4d%Z+d5d&Z,d6d'Z-	 	 	 	 	 	 	 	 	 	 	 	 	 	 d7d(Z.d8d)Z/	 	 	 	 	 	 	 	 	 	 d9d*Z0d:d+Z1d:d,Z2d;d<d-Z3d=d.Z4e5d/k(  r e4        yy)>zBSummarize the selector leaf table around the current 2:0 frontier.    )annotationsN)Path)Any)find_cns_stringsread_sectionsva_to_offset)handler_for_opcodesection_name_for_va)trace_stream   outiT i)T i)T i*T i*T i*T i+T z'out/save_scene_selector_references.json)labelkindresourcepathHex)pathfieldsz#out/save_selector_leaf_streams.jsonleafPointerHexfieldRecordsz$out/save_selector_stream_traces.jsonstreamVaHextracezout/script_handler_table.json)handlerTableVaHexentriescodeHandlerCountdefaultHandlerCountzRruntime selector/index that chooses 0x00542a04 or 0x00542ae8 from the table windowz@wrapper 0x00542a04 execution into 0x00542ae8 on the normal routez,strict map1_01a source coordinate or hotspot)runtime-selector-indexwrapper-frontier-executionstrict-hotspotc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     G/home/exedev/hwanse/tools/summarize_save_selector_leaf_table_context.py	load_jsonr)   M   s*    ;;=::dnngn677    c                    d| dS )N0x08x values    r(   hex32r1   S   s    c{r*   c                "    | rt        | d      S d S )N   )intr/   s    r(   	parse_hexr5   W   s    "3ub>,,r*   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   <Ir   )r   lenstructunpack_from)exesectionsvaoffsets       r(   dword_atr@   [   s?    (B'F~!c#h.dC033r*   c                   t        j                  d|      }g }|D ]l  }|d   }||d   z   }| || }|j                  |      }	|	dk\  s,|j                  |d   t	        |d   |	z         d       |j                  ||	dz         }	|	dk\  rAn |S )	Nr8   rawraw_sizer   namer>   )sectionrefVaHexr   )r:   packfindappendr1   )
r<   r=   r0   needlerefsrE   startenddataposs
             r(   refs_to_valuerP   b   s    [[u%FD 
-gj))5~iiQhKK"6?!'$-#"56  ))FC!G,C Qh
- Kr*   c                    t        fd| D        d       }|sg S |j                  d      xs g D cg c]%  }|j                  d      s|j                  d      ' c}S c c}w )Nc              3  L   K   | ]  }|j                  d       k(  s|  yw)r   Nget.0itemleaf_hexs     r(   	<genexpr>z&field_maps_for_leaf.<locals>.<genexpr>t   s"     X:J1Kx1WX   $$r   map)nextrT   )leaf_streamsrX   rowrecords    `  r(   field_maps_for_leafr`   s   sU    
XXZ^
_C	,/GGN,C,Ir_&VZZX]M^FJJu___s   A	Ac                ~    t        fd| D        d       }|syt        d |j                  d      xs g D              S )Nc              3  L   K   | ]  }|j                  d       k(  s|  yw)r   NrS   rU   s     r(   rY   z(trace_contains_reader.<locals>.<genexpr>{   s!     V$((=2IX2UVrZ   Fc              3  ^   K   | ]%  }|j                  d       t        t              k(   ' yw)vaHexN)rT   r1   FRONTIER_READERrV   rW   s     r(   rY   z(trace_contains_reader.<locals>.<genexpr>~   s#     ^ttxx E/$::^s   +-r   )r\   anyrT   )stream_tracesrX   r^   s    ` r(   trace_contains_readerri   z   s<    
VVX\
]C^swwwGWG][]^^^r*   c                b   t        | ||      }t        |      |t        |      nd t        |cxk  xr	 t        k  nc d}|d|d<   |S |dz  }t	        | ||      }	t        ||      }
|j                  d|d|	j                  d      |	j                  d      |	j                  d	      d
       t        |      }t        ||      }|t        k(  rd|d<   n@|t        k(  rd|d<   n1|rd|d<   n)||v rd|d<   ||   |d<   n|
rd|d<   ||d<   |
|d<   nd|d<   |r||d<   ||d<   t        ||      |d<   |S )N)rd   valueHexinsideRootTableWindow
unreadabler      r,   02xhandlerVaHexhandlerSectionisCodeHandler)
lowByteHexlowByteHandlerVaHexlowByteHandlerSectionlowByteIsCodeHandlerwrapperLeafPointerfrontierLeafPointerleafPointercnspointertargetVaHextargetSectionscalarr   leafFieldMapstraceContainsFrontierReader)r@   r1   TABLE_STARTWRAPPER_LEAFr	   r
   updaterT   r`   FRONTIER_LEAFri   )r<   r=   stringsr>   r]   rh   r0   r^   opcodehandlerrE   	value_hex	leaf_mapss                r(   row_atr      sq    S(B'Er$)$5E%L4!,!A\!AC
 }"F
T\F h7G!(E2GJJ6#,'&{{>:!(-=!> 'O <	  eI#L)<I*F	-	+F	#F	'	FU^E
	F&M&OF )(O-B=R[-\)*Jr*   c                ~   g }t        | |||d      D ]  }|j                  |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      d
       |j                  d      s |S  |S )N   steprd   rk   	opcodeHexrp   
stopReasonbranchTargetHexfallthroughVaHex)r   rd   rk   r   rp   r   r   r   )r   rI   rT   )r<   r=   r   rL   rowsrW   s         r(   compact_tracer      s    DS(GUA> HHV$XXg&,+. HH^4((<0#xx(9: $); <	
 		 88L!K Kr*   c                   t        |       }t        | |      }t        t        t        dz   d      D cg c]  }t        | |||||       }}|D cg c]  }|j                  d      dv r| }	}t        d |	D        d       }
t        | ||t              }t        | ||t              }|D cg c]V  }|j                  d      dk(  r@|j                  d      dk(  r,|j                  d	      d
v rd|j                  d      xs g v r|X }}ddt        t        fD ci c]  }t        |      t        | ||       }}d}d}g }|s|j                  d       t        |t        t                    s|j                  d       |s|j                  d       |D cg c]  }t        j                  ||       }}d}i dddt        t               dt        t"              dt        t               dt        t               dt$        dt'        t$              dt        t              dt        t              dt        t(              d|dt'        |	      d |	d!|
r|
j                  d"      nd d#t+        |
xr |
j                  d$            d%|d&|d't'        |      |d d( ||||||d)g d*|d+
S c c}w c c}w c c}w c c}w c c}w ),Nr   r7   r   >   ry   rw   rx   c              3  J   K   | ]  }|j                  d       dk(  s|  yw)r   rx   NrS   rV   r^   s     r(   rY   z build_summary.<locals>.<genexpr>   s      ^SWWV_H]5]^s   ##r   z2:0fieldMapr   >   map1_01amap2_02d
0x005429dcr   i*T i*T Fr   r   r   a  The current selector root points at the 0x005429dc leaf table window. That window directly references 0x00542ac0 and 0x00542ad4, and it reaches the reader-bearing frontier leaf 0x00542ae8 through a small wrapper at 0x00542a04 whose second dword points at 0x00542ae8. This strengthens the evidence that the frontier leaf belongs to the current selector table, but the wrapper trace still stops on a data-backed low byte and does not prove runtime selection of 0x00542ae8. Strict source hotspot evidence is still missing.selectorrootHexrootTablePointerHextableWindowHexz..evidenceRefsevidenceRefCountwrapperLeafHexfrontierLeafHexfrontierReaderHex	tableRowsleafRefCountleafRefsfrontierLeafRefVaHexrd   %frontierLeafRefIsDirectRootTableEntryrl   wrapperTracefrontierTraceselectorPathCount   blocked)z]decode the runtime selector/index that chooses 0x00542a04 or 0x00542ae8 from the table windowzLprove the wrapper at 0x00542a04 executes into 0x00542ae8 in the normal routez1find strict map1_01a source coordinate or hotspot)
selectorPathsdirectLeafRefsruntimeSelectionProven
proofFoundfailedLeafTableGateIdsmissingEvidencestrictHotspotFoundpromotionStatusremainingProofs
conclusion)r   r   rangeWINDOW_START
WINDOW_ENDr   rT   r\   r   r   r   r1   rP   rI   ri   #LEAF_TABLE_MISSING_EVIDENCE_BY_GATECURRENT_ROOTr   EVIDENCE_REFSr9   re   bool)r<   selector_referencesr]   rh   r=   r   r>   
table_rowsr^   	leaf_refsfrontier_refwrapper_tracefrontier_traceselector_pathsr0   direct_leaf_refsruntime_selection_provenstrict_hotspot_foundfailed_leaf_table_gate_idsgate_idmissing_evidencer   s                         r(   build_summaryr      sh    S!HsH-G j1na8 	sHgr<GJ 
 "776?ZZ 	I  ^	^`deL!#x,GM"3'=IN*777u$GGFOz)GGJ#;;SWWY/526	 	N  !*m\J 	emC599   % !##"))*BC l0CD"))*FG"))*:; 2 	,//A 
	x  E 5&  	u[1  	U<01E*4E3FG	 
 	  	C.  	%-  	5/  	U?3  	Z  	I  	I  	\ 0 0 9t  	0l6p|GWGWXoGp1q  	   	! " 	S0# $ (,*":."<+2$

 !?   Us   J)J.(AJ3J8J=c                   ddd| d    d| d    dd| d	    dd
| d    dd| d    dd| d    dd| d    dd| d    d| d    dd| d    d| d    d| d    d| d    ddj                  | j                  d       xs g       xs d! dd"t        | j                  d#      xs g        d$| d%    d&| d'    d(| d)    dd| d*   dg}|j                  d+dg       |j                  d, | j                  d#      xs g D               |j                  g d-       | j                  d.      xs g D ]J  }|j	                  d/|j                  d0       d1dj                  |j                  d2      xs g               L |j                  g d3       | d4   D ]  }|j	                  d5|d6    d7|d8    d9|j                  d:       d;dj                  |j                  d<      xs g       xs d! d;|j                  d=d!       d>|j                  d?      xs d! d@|j                  dA      xs d! dB        |j                  g dC       | dD   D ]  }|j	                  dE|j                  dF       d>|j                  d6       d7|j                  d8       d7|j                  dG       d7|j                  dH       d9|j                  dI      xs d! dB        |j                  g dJ       | dK   D ]  }|j	                  dE|j                  dF       d>|j                  d6       d7|j                  d8       d7|j                  dG       d7|j                  dH       d7|j                  dL      xs d! d7|j                  dM      xs d! d9|j                  dI      xs d! dB        |j                  g dN       |j                  dO | dP   D               |j	                  d       dQj                  |      S )RNz"# Save Selector Leaf Table Context z- selector: `r   z` root `r   `z- root table pointer: `r   z- table window: `r   z- wrapper leaf: `r   z- frontier leaf: `r   z- frontier reader: `r   z- leaf refs in window: r   z- frontier leaf ref: `r   z,- frontier leaf is direct root table entry: r   z,- selector path rows for map1_01a/map2_02d: r   z- runtime selection proven: r   z- proof found: r   z- failed leaf-table gates: `, r   -z- missing evidence count: r   z- strict hotspot found: r   z- promotion status: r   z- evidence refs: `r   r   z## Missing Evidencec              3  &   K   | ]	  }d |   ywz- Nr.   rf   s     r(   rY   zmarkdown.<locals>.<genexpr>4  s     N2dVN   )r   z## Evidence Refsr   r   z- `r   z`: r   )r   z## Table Windowr   z5| va | value | kind | leaf maps | reader? | handler |z%| --- | --- | --- | --- | --- | --- |r   z| `rd   z` | `rk   z` | r   z | r   r   z | `rt   z` ru   z |)r   z## Wrapper Tracer   z/| step | va | value | opcode | handler | stop |z&| ---: | --- | --- | --- | --- | --- |r   z| r   r   rp   r   )r   z## Frontier Tracer   zM| step | va | value | opcode | handler | branch target | fallthrough | stop |z2| ---: | --- | --- | --- | --- | --- | --- | --- |r   r   r   )r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   ywr   r.   rf   s     r(   rY   zmarkdown.<locals>.<genexpr>W  s     D2dVDr   r   
)joinrT   r9   extendrI   )summarylinesrefr^   s       r(   markdownr     s   ,


+,HWY5G4HJ
!'*?"@!AC
G$456a8
G$456a8
W%678:
w':;<A>
!'."9!:;
 )?!@ AC
6w?f7g6hi
6w?R7S6TU
&w/G'H&IJ
',/01
&tyy=U1V1\Z\']'dad&eefg
$S5F)G)M2%N$OP
"7+?#@"AB
w'89:;
W%789;

-E0 
LL',-	LLN5F)G)M2NN	LL-.{{>*0b 
#''&/"#dii0A0GR&H%IJ	

 
LL   {# 
#g,uS_$5T#''&/9J#yy17R8?C@ww4c:; <-.5#6bAX9Y9`]`8aace	

 
LL  K  L~& 
!cggg&6%7uSWWZ=P<Q R$%U377>+B*C4P\H]HdadGeegi	


 
LL  v  w' 
!cggg&6%7uSWWZ=P<Q R$%U377>+B*C D)*1c2%@R8S8ZWZ7[[_ww|$+,B0	

 
LL01	LLD1B)CDD	LL99Ur*   c                   dj                  d | j                  d      xs g D              }dj                  d | d   D              }dj                  d | d   D              }dj                  d	 | d
   D              }dj                  d | d   D              }dj                  d | j                  d      xs g D              }dj                  dddd| d    d| d    d| d    d| d    d	d| d    d| d    d| d     dd!| d"    d#| d$    d%| d&    d'| d(    d)| d*    d+t        j                  d,j                  | j                  d-      xs g       xs d.       d/t	        | j                  d      xs g        d0t        j                  | d1          d2d3t        j                  | d4          d2d5| d6    d7d8| d9d:| d9d;|d<d=|d<d>|d<d?| d9g      S )@Nr   c              3     K   | ]m  }d t        j                  t        |j                  d                   dt        j                  dj	                  |j                  d      xs g              d o yw)z
<li><code>r   z	</code>: r   r   </li>NhtmlescapestrrT   r   )rV   r   s     r(   rY   zhtml_page.<locals>.<genexpr>]  sf      
 		S1239;;tyy!2!8b9:
;	s   A3A5r   r   c              3    K   | ]@  }d t        j                  |d          dt        j                  t        |d                dt        j                  t        |j                  d                   dt        j                  dj	                  |j                  d      xs g       xs d	       dt        j                  t        |j                  d
d	                   dt        j                  t        |j                  d      xs d	             dt        j                  t        |j                  d      xs d	             d C yw)z<tr><td><code>rd   </code></td><td><code>rk   </code></td><td>r   z	</td><td>r   r   r   r   </td><td><code>rt   z</code> ru   
</td></tr>Nr   r   s     r(   rY   zhtml_page.<locals>.<genexpr>d  s=     
 	[[W./ 0[[S_!567 8{{3swwv/01 2{{499SWW_%=%CDKLM N{{3sww'DcJKLM N[[SWW-B%C%Js!KLMXVZVaVabefifmfm  oF  gG  gN  KN  cO  WP  VQ Q	
s   EE	r   c              3    K   | ]  }d |j                  d       dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d	      xs d
             d  yw)<tr><td>r   r   rd   r   rk   r   rp   r   r   r   r   NrT   r   r   r   r   s     r(   rY   zhtml_page.<locals>.<genexpr>o  s      
 	wwv  [[SWWW%5!678 9[[SWWZ%8!9:; <[[SWW[%9!:;< =[[SWW^%<!=>? @{{3sww|4;<=> ?	
s   DDr   c              3    K   | ]h  }d |j                  d       dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d      xs d	             dt        j                  t        |j                  d
      xs d	             dt        j                  t        |j                  d      xs d	             d k yw)r   r   r   rd   r   rk   r   rp   r   r   r   r   r   r   Nr   r   s     r(   rY   zhtml_page.<locals>.<genexpr>z  s4      	wwv  [[SWWW%5!678 9[[SWWZ%8!9:; <[[SWW[%9!:;< =[[SWW^%<!=>? @[[SWW->%?%F3!GHI J[[SWW-?%@%GC!HIJ K{{3sww|4;<=> ?		s   E/E1r   c              3  N   K   | ]  }d t        j                  |       d  ywz<li>r   Nr   r   rf   s     r(   rY   zhtml_page.<locals>.<genexpr>  s#     \tDKK-.e4\   #%r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   rf   s     r(   rY   zhtml_page.<locals>.<genexpr>  s(      ,0$t{{4 !'r   r   zT<!doctype html><meta charset="utf-8"><title>Save Selector Leaf Table Context</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1100px;margin:24px auto}table{border-collapse:collapse;width:100%;margin:18px 0}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z)<h1>Save Selector Leaf Table Context</h1>z<p>selector <code>r   z</code> root <code>r   z"</code>; root table pointer <code>r   z</code>; table window <code>r   z</code>.</p>z<p>wrapper leaf <code>r   z</code>; frontier leaf <code>r   z</code>; frontier reader <code>r   z<p>leaf refs in window: r   z; frontier leaf ref <code>r   z3</code>; frontier leaf is direct root table entry: r   z; runtime selection proven: r   z; proofFound=r   z; failedLeafTableGates=<code>,r   r   z</code>; missingEvidenceCount=z; promotion status: r   z</p>z<p>r   z<p><b>Evidence refs:</b> r   z.</p>z<h2>Missing Evidence</h2><ul>z</ul>z<h2>Evidence Refs</h2><ul>z<h2>Table Window</h2><table><thead><tr><th>va</th><th>value</th><th>kind</th><th>leaf maps</th><th>reader?</th><th>handler</th></tr></thead><tbody>z</tbody></table>z<h2>Wrapper Trace</h2><table><thead><tr><th>step</th><th>va</th><th>value</th><th>opcode</th><th>handler</th><th>stop</th></tr></thead><tbody>z<h2>Frontier Trace</h2><table><thead><tr><th>step</th><th>va</th><th>value</th><th>opcode</th><th>handler</th><th>branch target</th><th>fallthrough</th><th>stop</th></tr></thead><tbody>z<h2>Remaining Proofs</h2><ul>)r   rT   r   r   r9   )r   evidence_refsr   wrapper_rowsfrontier_rowsproofsmissing_itemss          r(   	html_pager   \  s   GG 
 ;;~.4" M  
 ;'
 
J 99 
 >*
 
L II  ?+ M WW\IZA[\\FGG 4;KK@Q4R4XVX M 99` 	G3
WZ011DWYEWDXXz  |C  DY  |Z  {[  [w  x  @P  xQ  wR  R^  	_
 )9!: ;;XY`arYsXt  uT  U\  ]p  Uq  Tr  r~  	
"7>#:";;UV]^tVuUv  wj  kr  sZ  k[  j\  \x  y@  AY  yZ  x[  [h  ip  q}  i~  h  \  ]a  ]h  ]h  il  iq  iq  ry  r}  r}  ~V  rW  r]  []  i^  ie  be  ]f  \g  gE  FI  JQ  JU  JU  Vg  Jh  Jn  ln  Fo  Ep  pD  EI  EP  EP  QX  Yj  Qk  El  Dm  mq  	r
dkk',/016
#G,>$?#@F
'e<
$]O59 	^ 	Y 	D
'xu5)  r*   c                   |j                  dd       |dz  }|j                  t        j                  | dd      dz   d	       |9|j                  j                  dd       |j                  t        |       d	       |S )
NT)parentsexist_okz%save_selector_leaf_table_context.jsonF   )ensure_asciiindentr   r    r!   )mkdir
write_textr$   dumpsparentr   )r   out_dirhtml_outjson_outs       r(   write_outputsr    s    MM$M.@@H

7q9D@   dT:Ig.AOr*   c            	        t        j                         } | j                  dt        t        dz         | j                  dt        t
        dz         | j                  dt        t
        dz         | j                  dt        t
        d	z         | j                  d
t        t
               | j                  dt               | j                         }t        |j                  j                         t        |j                  g       t        |j                  g       t        |j                  g             }t        ||j                  |j                         }t#        d|        y )Nz--exezHwanse2.exe)typedefaultz--selector-refsz#save_scene_selector_references.jsonz--leaf-streamszsave_selector_leaf_streams.jsonz--stream-tracesz save_selector_stream_traces.jsonz	--out-dirz
--html-out)r  z*wrote save selector leaf table context -> )argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr   r<   
read_bytesr)   selector_refsr]   rh   r  r  r  print)parserargsr   r  s       r(   mainr    s&   $$&F
dD=4HI
)cDi>ij
(tSCd=de
)cDf>fg
$<
40D$$$b)$##R($$$b)	G WdllDMMBH	6xj
ABr*   __main__)r   r   r'   r   returnr   )r0   r4   r  r   )r0   z
str | Noner  
int | None)r<   bytesr=   
list[dict]r>   r4   r  r  )r<   r  r=   r  r0   r4   r  r  )r]   r  rX   r   r  z	list[str])rh   r  rX   r   r  r   )r<   r  r=   r  r   dict[int, str]r>   r4   r]   r  rh   r  r  dict)
r<   r  r=   r  r   r  rL   r4   r  r  )
r<   r  r   r  r]   r  rh   r  r  r  )r   r  r  r   )N)r   r  r  r   r  zPath | Noner  r   )r  None)6__doc__
__future__r   r
  r   r$   r:   syspathlibr   typingr   r   insertr   __file__resolver  probe_exe_scene_tablesr   r   r   summarize_script_handler_tabler	   r
   %summarize_save_selector_stream_tracesr   r   r  r  r   r   r   r   r   r   re   r   r   r)   r1   r5   r@   rP   r`   ri   r   r   r   r   r   r  r  __name__r.   r*   r(   <module>r*     s   H "     
   3tH~--/667 8 P P R > H~''*
Ul
 :
 6
 7
 0
1!J 	] 	KD' #8-4"`_/	// / 		/
 / / 
/d$R	R#R R 	R
 
Rj?DDN
C& zF r*   