
    UHjB                       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mZmZ  e	e      j                         j.                  d   Zedz  Zd	Zd
Zh 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Z(e)dk(  r e(        yy)#zLFind selector roots with opcode-shaped secondaryBranchState fill candidates.    )annotationsN)Path)read_sectionsva_to_offset)byte_athandler_entryu32_at   outiT i+T >   map1_01amap2_02dmap2_09gmap2_10gmap2_11gmap2_12hmap2_14jmap2_15jmap2_16jmap2_17hmap2_18dc                    d| dS )N0x08x values    I/home/exedev/hwanse/tools/summarize_save_selector_secondary_fill_roots.pyhex32r          c{    c                    d| dS )Nr   02xr   r   s    r   hex8r#      r   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                b    | D ]*  }|d   }||d   z   }||cxk  r|k  sn |d   ||z
  z   c S  y )Nrawraw_sizer,   r   )r+   r-   section	raw_startraw_ends        r   offset_to_var5   (   sO     8EN	gj11((4=FY$677	8
 r    c                4    |D ]  }|| kD  s	| |fc S  | | dz   fS )Ni @  r   )rootroots	candidates      r   
root_ranger:   1   s4     #	t?"# r    c                    | dz  dk7  ry | dz	  dz  }| dz	  dz  }| dz	  dz  }|dk(  ry |dkD  ry |dkD  ry t        |       t        |      t        |      t        |      dS )	N            r         )valueHexstreamPlus1HexhelperArgumentHexstreamPlus3Hex)r   r#   )r   stream_plus_1
helper_argstream_plus_3s       r   decode_candidaterI   8   s    aZ4'M2+%Jb[D(MD t%L}-!*-}-	 r    c                   i }t        |      D ]O  \  }}|j                  d      xs g D ]2  }t        |d   d      }|j                  |g       j	                  |       4 Q t        t        |            D ci c]  }|g  }}|rd}	|	dz   t        |       k  rt        j                  d| |	      d   }
|j                  |
      }|rxt        ||	      }|D ]g  }||   }d |d   j                  d	      D        \  }}||   j	                  |t        |      nd t        |
      |d uxr ||cxk  xr |k  nc d
       i |	dz  }	|	dz   t        |       k  rt        t        |            D ci c]  }|g  }}t        |      D ]  \  }}|j                  d      xs g D ch c]  }t        |d   d       }}|s8d |d   j                  d	      D        \  }}|}|dz   |k  sat        | ||      }|t        | ||      ni }|j                  d      rt        | ||dz         nd }||v rG||   j	                  t        |      |t        |      nd |j                  d      t        |      d       |dz  }|dz   |k  r g }t        |      D ]E  \  }}||   }|D cg c]  }|j                  d      s| }}||   }|j!                  |j                  d       d|j                  d       t        |      t        |      t        |      t#        |xs |      |d d |d d d       |j	                  |d   |j                  d      |j                  d      |j                  d      xs g |j                  d      xs g |j                  d      t        |      t        |      t        |      t#        |xs |      |r|d   nd |r|d   nd d       H |D cg c]
  }|d   s	| }}|D cg c]  }|j                  d      r| }}|t        |      t        |      ||dS c c}w c c}w c c}w c c}w c c}w c c}w )NfillsvaHexr=   r   r%   r&   c              3  4   K   | ]  }t        |d         ywr=   Nint.0parts     r   	<genexpr>z/fill_entry_reference_summary.<locals>.<genexpr>_   s     +fdCbM+f   rootRangeHex..)	siteVaHextargetVaHexinRootRanger
   c              3  4   K   | ]  }t        |d         ywrN   rO   rQ   s     r   rT   z/fill_entry_reference_summary.<locals>.<genexpr>l   s     Z$D"ZrU   r>   canJumpToDwordAtPlus4handlerVaHex)rX   	opcodeHexr]   rY   rZ   group:slot   )selectorfillEntryDwordRefCountfillEntryRootRangeDwordRefCountfillEntryRootBranchTargetCountfillEntryCandidateFoundfillEntryDwordRefsfillEntryRootBranchTargetsrc   rootHex	fieldMapsrouteMapOverlap	fillCount)rc   rj   rV   rk   rl   rm   rd   re   rf   rg   firstFillEntryDwordReffirstFillEntryRootBranchTargetrg   )rootEntryReferenceRowsfillEntryReferenceRootCount'routeOverlapFillEntryReferenceRootCountfillEntryReferenceRoots#routeOverlapFillEntryReferenceRoots)	enumerategetrP   
setdefaultappendranger'   r(   r)   r5   splitr   r   r   r	   r#   updatebool)r*   r+   rowsfill_targetsindexrowfillfill_varefs_by_rowr-   r   owner_indicessite_vaowner_index
root_startroot_endbranch_targets_by_rowrK   r,   opcodehandlertargetrow_summariesrefsref	root_refsbranch_targetsroots_with_entry_refsroute_roots_with_entry_refss                                r   fill_entry_reference_summaryr   N   s   )+Lo ?
sGGG$* 	?D$w-,G##GR077>	??
 BGs4yAQ)R%))RK)RqjCH$&&tS&9!<E(,,U3M&x8#0 K{+C+fc.FYF_F_`dFe+f(J,337>7JU7^PT',U|'.d':'_zW?_W_?_5  aKF qjCH$ LQQTUYQZK[3\%E2I3\3\o 
s47GGG4D4JKDT']B'KKZ#n:M:S:STX:YZ
H1f S(B/F>D>PmC6:VXG6=kkBY6ZVC262`dF%e,33!&r171Cf$+KK$?#(=	5  !GB 1f & Mo 
s5!$(CSCGGM,BSC	C.u5

777+,Acggfo->?&)$i/29~.1..A'+D,BN'C"&s)*8"*=
 	 	Jwwy)GGN3-3"ww'89?R-&)$i/29~.1..A'+D,BN'C15d1g4CQnQ.?W[
 	8 -:\SSAZ=[S\\,#77$% 	# #
 #0'*+@'A367R3S#8/J Q *S$ 4]K* D4 ]#s0   6
P")
P'P,P11P1
P6"P6,P;c                N   t        |       }t        |D ch c]'  }t        |j                  d      t              s#|d   ) c}      }g }|D ]  }|j                  d      }t        |t              s&t        ||      \  }}g }	t        ||d      D ]g  }
t        | ||
      }|t        |      }|s!|	j                  dt        |
      i||t        k(  xr	 |
t        k  |t        k(  xr	 |
t        kD  d       i |	s|j                  d      xs g }t        t        |      t        z        }|j                  |j                  d      |j                  d      |j                  d       d|j                  d       |j                  d	      xs t        |      t        |       d
t        |       ||t        |	      |	|t        k(  rdn|rdndd
        t!        | ||      }t#        d |D        d       }|D cg c]  }|j                  d      s| }}t#        d |D        i       }|d   }|d   }|D cg c]$  }|j                  d      r|j                  d      & }}t%        |      xr | }|rdn|rdnd}ddj'                  |      xs d dt        |       d|j                  d       d|j                  d       d|j                  d        
}d!}i d"t        t              d#t        t              d$t        t              d%t        |      d&t        |      d'|d'   d(|d(   d|d   d|d   d)|d*|d+|d,|d-|j                  d.      d/|j                  d      d0|j                  d      d1|j                  d       ||||d2S c c}w c c}w c c}w )3NselectedPointerr%   rL   )beforeCurrentFrontierReaderafterCurrentFrontierReaderrk   r_   ra   r`   selectedPointerHexrW   zBcurrent root; no before-frontier fill should be used for promotionzGroute-overlap root; possible inherited runtime-state producer candidateznon-route root)
r_   ra   rc   rj   rV   rk   rl   rm   rK   routeRelevancec              3  L   K   | ]  }|d    t        t              k(  s|  yw)rj   N)r   CURRENT_ROOTrR   r   s     r   rT   z build_summary.<locals>.<genexpr>   s      QC3y>U<=P+PCQs   $$rl   c              3  J   K   | ]  }|j                  d       dk(  s|  yw)rc   z1:0N)rv   r   s     r   rT   z build_summary.<locals>.<genexpr>   s!     ZSWWZ=PTY=YZs   ##rs   rt   rc   znon-route-only-entry-referencez%route-overlap-entry-reference-presentzno-entry-referencezentrySelectors=,-z; routeOverlapEntryRoots=z; predecessorRefs=rd   /re   rf   a  Opcode-shaped secondaryBranchState fills exist in multiple selector roots, including route-overlap roots. For the current root 0x00540714, the only opcode-shaped secondary fill is after the 0x00542b0c frontier reader, so it cannot initialize the map1_01a->map2_02d branch. A global fill-entry scan finds no direct dword refs or root-range branch targets into any route-overlap fill fragment, including the 1:0 predecessor; only non-route fill roots have an entry-like direct reference. The only entry-ref selector is 55:0, which has no route-map overlap. The next proof step is therefore runtime/control-flow evidence, not a static route-overlap fill-site pointer.currentRootHexfrontierReaderHex	routeMaps	rootCountrouteOverlapRootCountrq   rr   fillEntryReferenceSelectorsfillEntryReferenceNonRouteOnly!fillEntryReferenceExclusionStatus!fillEntryReferenceExclusionDetail"predecessorFillEntryCandidateFoundrg   !predecessorFillEntryDwordRefCount*predecessorFillEntryRootRangeDwordRefCount)predecessorFillEntryRootBranchTargetCount)currentRootr8   routeOverlapRoots
conclusion)r   sorted
isinstancerv   rP   r:   ry   r.   rI   rx   r   r   FRONTIER_READERset
ROUTE_MAPSr'   r   nextr|   join)r*   	selectorsr+   r   r8   r}   r7   startendrK   r,   r   decodedmapsoverlapentry_ref_summarycurrentroute_overlap_rowspredecessorentry_ref_rootsroute_entry_ref_rootsentry_ref_selectorsentry_ref_non_route_onlyentry_ref_exclusion_statusentry_ref_exclusion_detailr   s                             r   build_summaryr      s   S!Hios:cggVgNhjmCnC)*opED (ww()$$e,
ssA& 	BS(B/E}&u-GLLr 04|/C/\_H\.2l.B.[rOG[	 	 ww{#)rTZ/0WWW%GGFO777+,Acggfo->?ww34Cd$U|nBuSzl;&U <' U  _%
 	/(R 5S(DIQ4QSWXG)-L#9J1K#LLZ'9Z\^_K'(ABO-.ST #77: 	
 
  $O4R=R9R $ 	) ! 5!  #((#67>3? @""%&;"<!= >&??+CDEQ??<=
>a??;<
=		? 	8 %-U?3 	VJ' 	SY	
 	 %7!8 	&'89V'W 	23D54
 	"#45N#O 	./@Af/g 	&': 	)*B 	,-G 	,-G  	-koo>W.X!" 	,[__=U-V#$ 	5koo-7
%* 	4[__,6
+0 / 7 ] pZ Ms   $NN<NN:)N"c                   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| j                  d       d| j                  d       d| j                  d       dd| d    d| d   dddg}| d    D ]  }d!j                  d" |d#   d d$ D              }d!j                  |j                  d%      xs g       xs d}|j                  d&|j                  d'       d(|j                  d)       d*|d+    d,|d-    d(| d(|d.    d(|d/    d(|d0    d(|d1    d(| d2        |j                  d       d3j                  |      S )4Nz$# Save Selector Secondary Fill Roots z- current root: `r   `z- frontier reader: `r   z,- roots with opcode-shaped secondary fills: r   z- route-overlap roots: r   z-- roots with fill-entry refs/branch targets: rq   z;- route-overlap roots with fill-entry refs/branch targets: rr   z- fill-entry ref selectors: `r   r   r   z1- fill-entry refs restricted to non-route roots: r   z- fill-entry ref exclusion: `r   z` (r   )z*- predecessor fill-entry candidate found: r   r   zs| group | slot | root | fills | route overlap | entry refs | root refs | branch targets | relevance | first fills |zC| ---: | ---: | --- | ---: | --- | ---: | ---: | ---: | --- | --- |r8   , c              3  :   K   | ]  }d |d    d|d    d   yw)r   rL   =rB   Nr   rR   items     r   rT   zmarkdown.<locals>.<genexpr>*  s)     _dAd7m_Ad:.>-?qA_s   rK      rl   z| r_   z | ra   z | `rj   z` | rm   rd   re   rf   r   z |
)r   rv   rx   )summarylinesr   rK   r   s        r   markdownr     s&   .

G$456a8
w':;<A>
6w{7K6LM
!'*A"B!CD
7@]8^7_`
EgNwFxEyz
'=Z1[1a_a(b(ifi'jjkl
;GKKHh<i;jk
'4W(X'YY\]d]h]h  jM  ^N  ]O  OP  	Q
4W=a5b4cd

}M#E& w 
		_cRYl[]\]N^__))CGG$56<"=D!"#cggfo%6d3y>:J$sS^O_N``cis3789456c#>^:_9``c#$%Sr3	

 
LL99Ur    c                   dj                  d | d   D              }dj                  dddd| d    d	| d
    dd| d    d| d    d| d    ddt        j                  t        | j	                  d                   dt        j                  dj                  | j	                  d      xs g       xs d       d| j	                  d       ddt        j                  | d          dd|dg
      S ) Nr   c              3    K   | ]  }d |j                  d       d|j                  d       dt        j                  |d          d|d    dt        j                  dj                  |j                  d	      xs g       xs d
       d|d    d|d    d|d    dt        j                  |d          ddj                  d |d   dd D               d  yw)z<tr><td>r_   z	</td><td>ra   z</td><td><code>rj   z</code></td><td>rm   r   rl   r   rd   re   rf   r   c              3     K   | ]>  }d t        j                  |d         z   dz   t        j                  |d         z   dz    @ yw)z<code>rL   r   rB   z</code>N)htmlescaper   s     r   rT   z&html_page.<locals>.<genexpr>.<genexpr>B  sI       OuyDKKW$>>Dt{{SWXbScGddgpp  Os   AArK   Nr   z
</td></tr>)rv   r   r   r   r   s     r   rT   zhtml_page.<locals>.<genexpr>7  s<      	www  !wwv  [[Y01 2;  !{{499SWW->%?%E2FM#NO P+,- .456 7345 6{{3/012 3yy  O  ~A  BI  ~J  KM  LM  ~N  O  O  P P	s   CCr8   zV<!doctype html><meta charset="utf-8"><title>Save Selector Secondary Fill Roots</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee}table{border-collapse:collapse}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z+<h1>Save Selector Secondary Fill Roots</h1>z<p>Current root: <code>r   z </code>; frontier reader: <code>r   z</code></p>z#<p>Fill-entry refs/branch targets: rq   z roots; route-overlap roots: rr   z; predecessor candidate: r   z.</p>z#<p>Fill-entry ref exclusion: <code>r   z</code>; selectors <code>r   r   r   z</code>; non-route only r   z<p>r   z</p>z<table><thead><tr><th>group</th><th>slot</th><th>root</th><th>fills</th><th>route overlap</th><th>entry refs</th><th>root refs</th><th>branch targets</th><th>relevance</th><th>first fills</th></tr></thead><tbody>z</tbody></table>)r   r   r   strrv   )r   r}   s     r   	html_pager   6  s   99  7# D 99b 	L5
!'*:";!<<\]dex]y\z  {F  	G
-g6S.T-UUrsz  |e  tf  sg  g@  AH  Im  An  @o  ot  	u
-dkk#gkkJm>n:o.p-q  rK  LP  LW  LW  X[  X`  X`  ah  al  al  mJ  aK  aQ  OQ  XR  XY  VY  LZ  K[  [s  t{  t  t  @`  ta  sb  bg  	h
dkk',/016 	_  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_secondary_fill_roots.jsonFrA   )ensure_asciiindentr   utf-8encoding'save_selector_secondary_fill_roots.html)mkdir
write_textjsondumpsr   )r   out_dirs     r   write_outputsr   T  se    MM$M.88DD

7q9D@ E  88DDYwEWbiDjr    c                    t        j                         } | j                  dt        t        dz         | j                  dt        t
        dz         | j                  dt        t
               | j                         }t        |j                  j                         t        j                  |j                  j                  d                  }t        ||j                         t!        d	|j                  d
z          y )Nz--exezHwanse2.exe)typedefaultz--selectorszsave_scene_selectors.jsonz	--out-dirr   r   zwrote secondary fill roots -> r   )argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr   r*   
read_bytesr   loadsr   	read_textr   r   print)parserargsr   s      r   mainr   ]  s    $$&F
dD=4HI
D#@[:[\
$<D

4>>++W+=>G '4<<(	*4<<:c+c*d
efr    __main__)r   rP   returnr   )r*   bytesr+   
list[dict]r,   rP   r   
int | None)r+   r  r-   rP   r   r  )r7   rP   r8   z	list[int]r   ztuple[int, int])r   rP   r   zdict | None)r*   r   r+   r  r}   r  r   dict)r*   r   r   r  r   r  )r   r  r   r   )r   r  r   r   r   None)r   r  )*__doc__
__future__r   r   r   r   r(   syspathlibr   pathinsertr   __file__resolveparentprobe_exe_scene_tablesr   r   %summarize_save_selector_stream_tracesr   r   r	   r   r   r   r   r   r   r   r#   r.   r5   r:   rI   r   r   r   r   r   r   __name__r   r    r   <module>r     s    R "     
  3tH~--/667 8 > P P H~''*
Ul R
4,Upl^B<kg zF r    