
    UHjO9                       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	 ej                  j                  d e e	e      j                         j                               ddlmZmZ ddlmZ  e	e      j                         j,                  d   Zedz  Zd	Zd
ZdZdZdddddZd$dZd%dZd&dZ d'dZ!d(dZ"d)dZ#d*dZ$d+dZ%d,dZ&d,dZ'd+dZ(d-d.dZd/dZ)d0dZ*d0d Z+efd1d!Z,d2d"Z-e.d#k(  r e-        yy)3zGSummarize dispatch-table entries that reach primaryBranchState writers.    )annotationsN)Path)read_sectionsva_to_offset)build_summary   outiG @   釶A ͶA i3A i>A iA imA )z0x0041dccc..0x0041df2dz0x0041e0f2..0x0041e1f6z0x0041e390..0x0041e431z0x0041fb36..0x0041fb57c                    d| dS )N0x08x )values    J/home/exedev/hwanse/tools/summarize_save_selector_branch_state_dispatch.pyhex32r       s    c{    c                L    | D ]  }|d   |cxk  r|d   |d   z   k  s|c S  ! y )Nrawraw_sizer   )sectionsoffsetsections      r   section_for_offsetr   $   s@     5>VJgenwz7J&JJN K r   c                T    | D ]#  }|d   |cxk  r|d   |d   z   k  sn |d   c S  y )Nvar   namer   )r   r   r   s      r   section_for_var   +   s?     #4=BD1D!DD6?"# r   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   <Ir   )r   lenstructunpack_from)exer   r   r   s       r   dword_atr'   2   s?    (B'F~!c#h.dC033r   c                J    t         j                  |       j                  d      S N )bytesfromhexhex)texts    r   normalize_hex_bytesr/   9   s    ==""3''r   c                N    t        ||      }|y | |||z    j                  d      S r)   )r   r-   )r&   r   r   lengthr   s        r   bytes_at_var2   =   s2    (B'F~vfvo&**3//r   c           
         t        |      }t        | ||t        t        j	                  |                  xs d}|t        |      ||||k(  dS )N )labelvaHexexpectedBytesactualBytesmatches)r/   r2   r#   r+   r,   r   )r&   r   r   expectedr5   expected_normactuals          r   
byte_checkr=   D   sQ    '1MhCh0G,HIORFr&]* r   c                    t        | |ddd      t        | |ddd      t        | |ddd	      t        | |d
dd      t        | |ddd      t        | |ddd      gS )Nr   z83 3d 80 b2 55 00 00z0loop while dispatch stop flag 0x0055b280 is zeroiA z8b 80 b0 00 00 00z2load event/object stream pointer from context+0xb0iA z8a 08zread first stream byteiA z83 f9 40z!route byte 0x40 to table dispatchiʶA z8a 48 01zread table index from stream+1r   zff 14 8d d8 f1 47 00z!call dword [index*4 + 0x0047f1d8])r=   )r&   r   s     r   dispatcher_byte_checksr?   P   su    3*.DFxy3*.ACwx3*g7OP3*j:]^3*j:Z[3*.DFij r   c                V   g }t        d |D              }|d   }|d   |d   z   }| || }t        dt        |      dz
        D ]d  }||   dk7  rt        j                  d||dz         d   }	|d	   |z   }
|
d
z   |	z   }||k(  s?|j                  t        |
      t        |      d       f |S )Nc              3  2   K   | ]  }|d    dk(  s|  yw)r   z.textNr   ).0r   s     r   	<genexpr>z%relative_call_refs.<locals>.<genexpr>]   s     NG76?g3MNs   r   r   r   r!      z<ir   r      )	callVaHextargetVaHex)nextranger#   r$   r%   appendr   )r&   r   	target_varefsr.   startendr   indexrelcall_varesolveds               r   relative_call_refsrS   [   s    DNxNNDKE
u+Z(
(C
eC.Cq#c(Q,' 
u:  sEAI6q9t*u$Q;$y KK"7^$Y/ 
 Kr   c                    g }t        j                  d|      }d}	 | j                  ||      }|dk  r	 |S |dz   }t        ||      }|/|d   |z   |d   z
  }|j	                  |d   t        |      d|dd	       b)
Nr"   r   r   r   r   r   r   06x)r   refVaHexfileOffsetHex)r$   packfindr   rJ   r   )	r&   r   rK   rL   needlesearchhitr   ref_vas	            r   pointer_refsr^   o   s    D[[y)FF
hhvv&7 K q$Xs3?$wu~5vf!#c^
 	 r   c                    g }t        t              D ]Z  }t        |dz  z   }t        | ||      }|j	                  |d|dt        |      |t        |      nd |t        ||      nd d       \ |S )Nr!   r   02x)opcode	opcodeHex
entryVaHexhandlerVaHexhandlerSection)rI   EVENT_HANDLER_COUNTEVENT_HANDLER_TABLE_VAr'   rJ   r   r   )r&   r   entriesra   entry_va
handler_vas         r   event_handler_entriesrk      s    G+, 	)FQJ6c8X6
fS\*/1;1GE*-TFPF\nXzBbf
 		 Nr   c                   t        |       }|xs t        |       }t        | |      }t        | |      }|D ci c]  }|j	                  d      | }}g }|j	                  d      xs g D ]	  }t
        j	                  |d         }	|	t        |	      nd }
|j	                  |
      }|	t        | ||	      ng }|	t        | ||	      ng }|j                  |d   |d   |
t        t              |r|j	                  d      nd |r|j	                  d      nd |r|j	                  d      nd d|t        |      |t        |      |j	                  d	      xs i |j	                  d
      xs g |r|sdndd        dt        t              t        t        t              t        t              t        d |D              |ddd||dS c c}w )Nrd   clustersrangeHexr5   rc   rb   re   )
tableVaHexrc   rb   re   writeValueCountssourceGlobalszPDispatched through the event/object handler table, not by direct relative calls.zYDirect call evidence exists; inspect call sites before treating this as only data-driven.)r5   writerRangeHexfunctionStartHexeventHandlerTabledirectRelativeCallRefsdirectRelativeCallRefCountpointerRefspointerRefCountrp   rq   interpretationz5dispatch paths for primaryBranchState writer clustersc              3  &   K   | ]	  }|d      yw)r9   Nr   )rB   rows     r   rC   z build_summary.<locals>.<genexpr>   s     %Pc)n%Ps   a2  This .data table contains event/object VM handler function pointers. Dispatcher 0x0041b687 reads byte 0 from context+0xb0; when it is 0x40, it reads byte 1 as the index and calls dword [index*4 + 0x0047f1d8]. The branch-state writer routines are registered here by index, so their execution is data-driven.)ro   
entryCountdispatcherVaHexdispatchCallVaHexdispatcherVerifieddispatcherByteChecksmeaninga   The direct primaryBranchState writer clusters are table-dispatched event/object handlers. No direct relative call sites were found for the current writer entry points, so the next route-progress proof must identify the event/object bytecode opcode that runs before save-selector root 2:0.)scopert   
conclusionrm   tableEntries)r   build_writer_summaryrk   r?   getKNOWN_FUNCTION_STARTSr   rS   r^   rJ   rg   r#   rf   EVENT_DISPATCHER_VAEVENT_DISPATCH_CALL_VAall)r&   writer_summaryr   table_entriesdispatch_checksentryentries_by_handlerrm   clusterfunction_startfunction_start_hextable_entrycallsrL   s                 r   r   r      s   S!H#@';C'@N)#x8M,S(;OHUVu%))N3U:VVH!%%j17R .227:3FG6D6PU>2VZ(,,-?@ESE_"3.Aeg>L>X|C>:^`W%%j1 2#$:;?Jkool;PT=H[__[9dGR+//2B"CX\	" ',*-e*"4y ',> ? E2$[[9?R u cp'
 	: I 67-$%89!&'=!>"%%P%P"P$3t
f %- = Ws   Gc                h    dj                  d t        | j                               D              xs dS )N, c              3  0   K   | ]  \  }}| d |   yw):Nr   )rB   keyr   s      r   rC   z format_counts.<locals>.<genexpr>   s     O*#uuAeW%Os   -)joinsorteditems)countss    r   format_countsr      s(    99Ov||~8NOOVSVVr   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g}| d   D ]l  }|d   }|j                  d|d    d|d    d|j                  d      xs d d|j                  d      xs d d|d    dt        |d          d|d     d!       n |j                  g d"       | d   D ]A  }d#j	                  d$ |d%   D              xs d}|j                  d&|d    d'|d    d(|        C |j                  g d)       | d   d*   D ],  }|j                  d&|d+    d'|d    d(|d,    d-|d.    d/	       . |j                  d       d0j	                  |      S )1Nz%# Save Selector Branch State Dispatchr4   zBDispatch evidence for direct `primaryBranchState` writer clusters.z- event/object handler table: `rt   ro   `z- dispatcher: `r}   z
` / call `r~   z#- dispatcher byte checks verified: r   z- scanned entries: r|   r   z\| cluster | function | table entry | opcode/index | direct calls | writes | interpretation |z,| --- | --- | --- | --- | ---: | --- | --- |rm   z| r5   z | `rs   z` | `rc   r   rb   z` | rv   z | rp   ry   z |)r4   z## Pointer Referencesr4   r   c              3  8   K   | ]  }|d     d|d    d  yw)r   z `rV   r   Nr   rB   refs     r   rC   zmarkdown.<locals>.<genexpr>   s'     cSC	N+2c*o->a@c   rw   z- `z` z: )r4   z## Dispatcher Byte Checksr4   r   r6   r9   z (`r8   z`)
)rJ   r   r   extendr   )summarylinesr   tablerL   checks         r   markdownr      s\   /
L

)'2E*F|*T)UUVW
'"567HIJ*U\]pUq  sF  VG  UH  HI  	J
-g6I.JK_.`-ab
g&9:<HIJ

f6E :& 
+,!"$w/A'B&C D		,'.3/uUYY{5K5Rs4SSW345SwOaGb9c8ddg'()-	

 
LL23:& VyycGTaLbccjgjs7#567r'':J9K2dVTUV 
LL67,-.DE 
%.!E'N#32eI6F5G H}%&b*	


 
LL99Ur   c                 l   g }| d   D ]  }|d   }|j                  dt        j                  |d          dt        j                  |d   xs d       dt        j                  |j                  d	      xs d       dt        j                  |j                  d
      xs d       d|d    dt        j                  t	        |d                dt        j                  |d          d        g }| d   D ]l  }dj                  d |d   D              xs d}|j                  dt        j                  |d   xs d       dt        j                  |d          d| d       n g }| d   d   D ]e  }|j                  dt        j                  |d          dt        j                  |d          d|d    dt        j                  |d          d	       g dj                  dddddd d!d"d#d$d%d&d'd(d)d*d+t        j                  | d   d,          d-d.t        j                  | d   d/          d0t        j                  | d   d1          d2| d   d3    d4d5t        j                  | d6          d7d8d9d:j                  |       d;d<d=d9d:j                  |       d;d>d?d9d:j                  |       d;d@dAd:g      S )BNrm   rt   z<tr><td>r5   z</td><td><code>rs   r   z</code></td><td><code>rc   rb   z</code></td><td>rv   z	</td><td>rp   ry   z
</td></tr>r   c              3  8   K   | ]  }|d     d|d    d  yw)r   z <code>rV   z</code>Nr   r   s     r   rC   zhtml_page.<locals>.<genexpr>  s(     nPSC	N+73z?2C7Knr   rw   z<tr><td><code>r   r6   r9   r8   z</code></td></tr>r   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">z4  <title>Save Selector Branch State Dispatch</title>z	  <style>z^    body { margin: 24px; background: #101010; color: #eee; font: 14px system-ui, sans-serif; }zJ    table { border-collapse: collapse; width: 100%; margin-bottom: 24px; }zM    th, td { border: 1px solid #333; padding: 6px 8px; vertical-align: top; }z    th { background: #1d1d1d; }z    code { color: #f5d76e; }z
  </style>z</head>z<body>z.  <h1>Save Selector Branch State Dispatch</h1>z'  <p>Event/object handler table: <code>ro   z</code>.</p>z  <p>Dispatcher: <code>r}   z</code>, call <code>r~   z*</code>; dispatcher byte checks verified: r   z.</p>z  <p>r   z</p>z  <table><thead><tr><th>cluster</th><th>function</th><th>table entry</th><th>opcode/index</th><th>direct calls</th><th>writes</th><th>interpretation</th></tr></thead>z	  <tbody>r4   z</tbody></table>z  <h2>Pointer References</h2>zO  <table><thead><tr><th>function</th><th>cluster</th><th>refs</th></tr></thead>z!  <h2>Dispatcher Byte Checks</h2>z_  <table><thead><tr><th>VA</th><th>check</th><th>matches</th><th>actual bytes</th></tr></thead>z</body>z</html>)rJ   htmlescaper   r   r   )r   rowsr   r   ref_rowsrL   
check_rowsr   s           r   	html_pager      s   D:& 
+,;;ww/01 2W-?%@%GCHI JUYY|%<%CDE FUYY{%;%BsCD E789 :;;}W5G-HIJK L;;w'789: ;
	

 H:& 
yynW^_lWmnnuruW-?%@%GCHI J;;ww/01 2& 	

 J,-.DE 
U7^45 6;;uW~./ 0#$ %U=%9:; <		

 99"P>hTW)&8
1$++gFY>Z[g>h2i1jjvw
!$++g6I.JK\.]"^!__stxtt  AH  I\  A]  ^q  Ar  us  tt  t^  _f  gz  _{  |P  _Q  ^R  RW  	X
GL12348 	q
BGGDM?"23'Y
BGGH%&&67+i
BGGJ'((89
=  r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz(save_selector_branch_state_dispatch.jsonF   )ensure_asciiindentr   zutf-8)encoding(save_selector_branch_state_dispatch.html)mkdir
write_textjsondumpsr   )r   out_dirs     r   write_outputsr   ?  sf    MM$M.99EE

7q9D@ F  99EEiPWFXcjEkr   c                 v   t        j                  t              } | j                  dt        t
        dz         | j                  dt        t               | j                         }t        |j                  j                               }t        ||j                         t        d|j                  dz          y )N)descriptionz--exezHwanse2.exe)typedefaultz	--out-dirz-wrote save selector branch state dispatch -> r   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsr   r&   
read_bytesr   r   print)parserargsr   s      r   mainr   H  s    $$9F
dD=4HI
$<DDHH//12G'4<<(	9$,,Is:s9t
uvr   __main__)r   intreturnstr)r   
list[dict]r   r   r   dict | None)r   r   r   r   r   
str | None)r&   r+   r   r   r   r   r   z
int | None)r.   r   r   r   )
r&   r+   r   r   r   r   r1   r   r   r   )r&   r+   r   r   r   r   r:   r   r5   r   r   dict)r&   r+   r   r   r   r   )r&   r+   r   r   rK   r   r   r   )N)r&   r+   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )/r   
__future__r   r   r   r   r$   syspathlibr   pathinsertr   __file__resolveparentprobe_exe_scene_tablesr   r   ,summarize_save_selector_branch_state_writersr   r   r   r   r   rg   rf   r   r   r   r   r   r   r'   r/   r2   r=   r?   rS   r^   rk   r   r   r   r   r   __name__r   r   r   <module>r      s   M "     
  3tH~--/667 8 > ^ H~''*
Ul#    # ((((	 4(0	(*:zW#LBJ 25 lw zF r   