
    UHjb1                    ,   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZmZmZ  ee      j!                         j"                  d   Zedz  ZdZd	Zd
Zedz
  Z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) zHScan broad block write candidates that could cover secondaryBranchState.    )annotationsN)Path)call_or_rep_rowsclassify_reffind_dword_refshex32small_immediatestext_section   outi`Y ilY i Y @   c                :    t        |        dt        |dz
         S )Nz..r   )r   )startend_exclusives     K/home/exedev/hwanse/tools/summarize_save_selector_secondary_block_writes.py	range_hexr      s"    El^2eMA$56788    c                4    t        | |      t        ||      k  S )N)maxmin)start_aend_astart_bend_bs       r   overlapr   !   s    w 3ue#444r   c                T    | t         k  rt         | z
  dz   t        | z
  fS dt        | z
  fS )Nr   )SECONDARY_STARTSECONDARY_END)addresss    r   required_sizesr    %   s4    /!(1,mg.EEEmg%%%r   c           
        t        |       \  }}g }t        t        t        dz         D ]  }t	        ||      D ]  }t        ||      }t        d||j                  d      xs dz
        }|dz   }t        |      \  }	}
|d   dk(  xr= t        |j                  d      t              xr t        |||d   z   t        t              }|d   dk(  xr	 |t        k  }t        d|t        z
        }t        t!        |      |t        z         }|r't#        ||||d	         D cg c]  }|d
   |	k\  r| c}ng }|D cg c]  }|d
   |
k\  r| }}|rt%        ||||d	         ng }|xr t'        |      xr t'        |      }|xr t'        |      xr t'        |      }|j)                  i dt+        |      dt+        |d	   |z         dt+        |d	   |z         d|d   d|d   d|d   d|d   d|	dt+        |	      d|
dt+        |
      d|d|d|d|d|d|d|i         |j-                  d        |S c c}w c c}w )Nr   r   instructionPrefixBytes   
accessKindwritewidthr   vavalue
addressHexrefVaHexinstructionVaHexinstructionKindinstructionrequiredSizeToTouchSecondaryrequiredSizeToTouchSecondaryHexrequiredSizeToCoverSecondaryrequiredSizeToCoverSecondaryHexdirectOverlapWriteaddressLikeTouchingBasenearbySizeCandidatesnearbyFullCoverSizeCandidatesnearbyTransferCandidatestouchBlockWriteCandidatefullCoverBlockWriteCandidatec                    | d   | d   fS )Nr)   r*    )rows    r   <lambda>zbuild_rows.<locals>.<lambda>[   s    s<0#j/B r   )key)r
   range
SCAN_STARTSCAN_ENDr   r   r   getr    
isinstanceintr   r   r   WINDOW_BYTESr   lenr	   r   boolappendr   sort)exetextdatarowsr   posrefinstruction_indexinstruction_endrequired_touchrequired_full_coverdirect_overlap_writeaddress_likewindow_start
window_endr;   	size_rowsfull_cover_size_rowstransfer_rowstouch_block_candidatefull_cover_block_candidates                        r   
build_rowsr\   +   s)   c"JD$DX\2 ,"41 +	CtS)C #Ascgg6N.O.TST'U V!AgO2@2I/N/L!W, ]swww/5]GWs7|%;_m\ !
 |,	9Ug>ULq"3l"BCLSY,(FGJ   0lJPTUYPZ[w<>1  $& 
  )$w<#66 $  $ ]i,T<TRVZXnpM$0$\T)_$\mI\!)5)l$?S:T)lY]^kYl&KK eGnE$t*s"23 #E$t*7H*H$I c,/	
 W "3'8#9 s=1 / 253H /0C 259L3M %&: *< '	 01E  +M!" +,A#$ /0J% 1+	,Z 	IIBICKA$s   H=5Ic                <   t        |       }|D cg c]
  }|d   s	| }}|D cg c]
  }|d   s	| }}|D cg c]
  }|d   s	| }}|D cg c]
  }|d   s	| }}|s|s|rdt        |       dnd}d| d	}nd
}i dddt        t        t              dt        t
        t        dz         dt        dt        |      dt        |      dt        |      dt        |      dt        |      d|d|d|d|d|ddd|S c c}w c c}w c c}w c c}w )Nr3   r2   r7   r8   z
 It found z address-like base ref(s) that could point at or before the table, but none has a nearby size immediate large enough to touch the table, so they are not block-write candidates.zL It found no address-like base refs that could point at or before the table.a0  No broad block write candidate was found for secondaryBranchState 0x0059e360..0x0059e36b. The scan covers dword immediates in 0x0059e300..0x0059e36b and checks address-like uses with nearby size immediates plus call/rep/stos transfer instructions, as well as direct stores that overlap the 12-byte table.z This removes the obvious static memset/memcpy-style reset shape, but it still does not prove selector runtime order or a strict map1_01a source hotspot.zAt least one secondaryBranchState broad block write candidate was found; inspect candidate rows before treating the secondary reset scope as statically narrowed.scopezZdword references to 0x0059e300..0x0059e36b with nearby size/call/rep/stos block-write scansecondaryRangeHexscanRangeHexr   windowBytesrowCountaddressLikeTouchingBaseCountdirectOverlapWriteCounttouchBlockWriteCandidateCount!fullCoverBlockWriteCandidateCountaddressLikeTouchingBasesdirectOverlapWritestouchBlockWriteCandidatesfullCoverBlockWriteCandidatesrL   promotionStatusblocked
conclusion)r\   rE   r   r   r   r?   r@   rD   )	rI   rL   r;   rT   direct_overlaptouch_candidatesfull_cover_candidatesaddress_noterm   s	            r   build_summaryrr   _   s   c?D#'JC3/H+ICJLJ%)GcS1E-FcGNG'+Os3M/NOO,0XSC8V4WSXX"2  \*+ ,| | `	 	) n T	T 	P 	mYF 		*hl; 	|	
 	CI 	'L(9 	"3~#6 	(-=)> 	,S1F-G 	#L 	~ 	$%5 	()> 	 	9  	j! 1 KGOXs+   
D
D

DD
DD
DDc                Z    | d   D cg c]  }|d   s|d   s
|d   s|d   r| c}S c c}w )NrL   r3   r2   r7   r8   r:   )summaryr;   s     r   candidate_rowsru      sJ    v)*'(-.12 	  s   (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g}t        |       }|s|j                  d       |D ]  }dj                  d |d    D              xs d!}dj                  d" |d#   D              xs d!}|j                  d$|d%    d&|d'    d(|d)    d*|d+    d,|d-    d&|d.    d(| d*| d*|d/    d*|d0    d,|d1    d2        |j                  g d3       | d4   D ]K  }|j                  d$|d%    d&|d'    d(|d)    d*|d+    d*|d5   |d5   nd! d,|d-    d&|d.    d&|d1    d2       M |j                  d       d6j                  |      S )7Nz&# Save Selector Secondary Block Writes zScope: r^   .z- secondaryBranchState: `r_   `z- scan range: `r`   z- scan window bytes: ra   z- rows: rb   z- address-like touching bases: rc   z- direct overlap writes: rd   z - touch block write candidates: re   z%- full-cover block write candidates: rf   z- promotion status: `rk   rm   z## Candidate Rowsz|| address | ref | kind | access | touch size | cover size | sizes | transfers | touch candidate | full cover | instruction |zE| --- | --- | --- | --- | ---: | ---: | --- | --- | --- | --- | --- |z-| - | - | - | - | - | - | - | - | - | - | - |, c              3  6   K   | ]  }|d     d|d      ywvalueHex@vaHexNr:   .0items     r   	<genexpr>zmarkdown.<locals>.<genexpr>   s*      
6:tJ $w-1
   r4   -c              3  6   K   | ]  }|d     d|d      ywkindr~   r   Nr:   r   s     r   r   zmarkdown.<locals>.<genexpr>   s(      
26tF|nAd7m_-
r   r6   z| `r)   z` | `r+   z` | r,   z | r$   z | `r/   r1   r7   r8   r-   z` |)rw   z## All Referencesrw   zQ| address | ref | kind | access | width | touch size | cover size | instruction |z4| --- | --- | --- | --- | ---: | ---: | ---: | --- |rL   r&   
)ru   rG   joinextend)rt   linesrL   r;   sizes	transferss         r   markdownr      s   0

''"#1%

#G,?$@#AC
'.12!4
 678
7:&'(
)'2P*Q)RS
#G,E$F#GH
*73R+S*TU
/8[0\/]^
(9 :;1=


 	GO)E, '"DDE 
		 
>ABX>Y
 
  	 II 
:=>X:Y
 
  	 	#l#$E#.@*A)B$sK\G]F^^a< !c*K&L%M N567tE7#i[PS-./s37U3V2W XM"#3	(	

 
LL   v 
#l#$E#.@*A)B$sK\G]F^^a< !S\5MS\SV$W X567uSAb=c<d eM"#3(	

 
LL99Ur   c                   dj                  d t        |       D              }|sd}dj                  ddddt        j                  | d          d	t        j                  | d
          dt        j                  | d          dd| d    d| d    d| d    d| d    d| d    ddt        j                  | d          dd|dg	      S )Nr   c              3  h  K   | ])  }d t        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d	          dt        j                  |d
          dt        j                  dj                  d |d   D              xs d       dt        j                  dj                  d |d   D              xs d       d|d    d|d    dt        j                  |d          d , yw)z<tr><td><code>r)   z</code></td><td><code>r+   z</code></td><td>r,   z	</td><td>r$   z</td><td><code>r/   r1   rz   c              3  8   K   | ]  }|d    dz   |d   z     ywr|   r:   r   s     r   r   z&html_page.<locals>.<genexpr>.<genexpr>   s$     $tPTT*%5%;d7m%K$t   r4   r   c              3  8   K   | ]  }|d    dz   |d   z     ywr   r:   r   s     r   r   z&html_page.<locals>.<genexpr>.<genexpr>   s"     $tDT&\C%7$w-%G$tr   r6   r7   r8   r-   z</code></td></tr>N)htmlescaper   )r   r;   s     r   r   zhtml_page.<locals>.<genexpr>   sX      	[[\!234 5[[%7!89: ;{{30123 4{{3|,-. /[[%F!GHI J[[%F!GHI J{{499$tX[\rXs$tt{x{|} ~{{499$tTWXrTs$tt{x{|} ~-./ 0123 4[[]!345 6	s   D0D2z0<tr><td colspan="11">No candidate rows</td></tr>zX<!doctype html><meta charset="utf-8"><title>Save Selector Secondary Block Writes</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1200px;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 Secondary Block Writes</h1>z<p>secondaryBranchState <code>r_   z</code>; scan <code>r`   z </code>; promotion status <code>rk   z</code>.</p>z	<p>rows: rb   z; address-like touching bases: rc   z; direct overlap writes: rd   z ; touch block write candidates: re   z%; full-cover block write candidates: rf   z.</p>z<p>rm   z</p>z<table><thead><tr><th>address</th><th>ref</th><th>kind</th><th>access</th><th>touch size</th><th>cover size</th><th>sizes</th><th>transfers</th><th>touch candidate</th><th>full cover</th><th>instruction</th></tr></thead><tbody>z</tbody></table>)r   ru   r   r   )rt   rL   s     r   	html_pager      s   99  "'* D  C99d 	G7
(W=P5Q)R(SSghlhshst{  }K  uL  iM  hN  Nn  os  oz  oz  {B  CT  {U  oV  nW  Wc  	d
GJ'((GPnHoGp  qJ  KR  Sl  Km  Jn  nN  OV  Wv  Ow  Nx  x]  ^e  fI  ^J  ]K  KP  	Q
dkk',/016 	n
 
 
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_block_writes.jsonF   )ensure_asciiindentr   zutf-8)encoding)save_selector_secondary_block_writes.html)mkdir
write_textjsondumpsr   )rt   out_dirs     r   write_outputsr      sf    MM$M.::FF

7q9D@ G  ::FFyQXGYdkFlr   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 secondary block writes -> r   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsrr   rI   
read_bytesr   r   print)parserargsrt   s      r   mainr      s    $$9F
dD=4HI
$<DDHH//12G'4<<(	,T\\<g-g,h
ijr   __main__)r   rC   r   rC   returnstr)
r   rC   r   rC   r   rC   r   rC   r   rF   )r   rC   r   ztuple[int, int])rI   bytesr   
list[dict])rI   r   r   dict)rt   r   r   r   )rt   r   r   r   )rt   r   r   r   r   None)r   r   )$r   
__future__r   r   r   r   pathlibr   3summarize_save_selector_opcode24_mode1_block_writesr   r   r   r   r	   r
   __file__resolver   r   r   r   r   r?   r@   rD   r   r   r    r\   rr   ru   r   r   r   r   __name__r:   r   r   <module>r      s    N "      H~''*
Ul
195&1h+\	7t@mk zF r   