
    nJj.                    8   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Z	 ee
      j                         j                  d   Zedz  ZdZdZeZe	j$                  Zg d	Zg d
ZdddddddddgZddZddZddZddZddZedfddZddZedk(  r e        yy) zLScan broad block write candidates that could cover opcode 0x24 runtime flag.    )annotationsN)Path   outiMY i Y )z runtime-enabled-flag-block-writez covering-size-transfer-candidatezruntime-watchpoint-trace)zPbroad block write candidate covering opcode 0x24 runtime-enabled flag 0x0059e34dzZnearby covering size and transfer instruction proving a static runtime-flag block producerz8runtime watchpoint trace proving the 0x0059e34d producerHwanse2.exez<dword-reference block-write scan over 0x0059e300..0x0059e34d)pathdescriptionz7out/save_selector_opcode24_runtime_enabled_context.jsonzruntime-enabled flag proof gapz"out/runtime_trace_feasibility.jsonz-runtime watchpoint trace availability blockerc                    d| dS )N0x08x )values    Z/home/exedev/hwanse/tools/summarize_save_selector_opcode24_runtime_enabled_block_writes.pyhex32r   $   s    c{    c                   t         j                  t         j                  t         j                  f}	 t        t         _        t        t         _        t        t         _        t        j
                  |       }|\  t         _        t         _        t         _        |D ]*  }|j                  d      |d<   |j                  d      |d<   , |S # |\  t         _        t         _        t         _        w xY w)NrequiredSizeToCoverMode1requiredSizeToCoverRuntimeFlagrequiredSizeToCoverMode1Hex!requiredSizeToCoverRuntimeFlagHex)
block_scanMODE1_SOURCEBLOCK_SCAN_STARTBLOCK_SCAN_ENDRUNTIME_ENABLED_FLAG
build_rowsget)exe
old_valuesrowsrows       r   	scan_rowsr"   (   s    ##!!J

"6
&6
#$2
!$$S) 		
#'% Z038R0S,-3677;X3Y/0Z K	 		
#'%s   AC #C(c                D   t        |       }|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              dt        t               dt        t
               dt        dt        |      dt        |      dt        |      dt        |      d|d|d|d|dddddt        dt        dt        t        t              d|dS c c}w c c}w c c}w ) NaddressLikeCoveringBasedirectCoveringWriteblockWriteCandidatez It did find zz address-like covering base ref(s), but none has a nearby covering size immediate, so they are not block-write candidates.z2 It found no address-like covering base candidate.a&  No broad block write candidate was found for runtime flag 0x0059e34d. The scan covers dword immediates in 0x0059e300..0x0059e34d and looks for address-like uses with nearby covering size immediates and call/rep transfer instructions; it also checks direct wide stores that could cover the flag.z This does not replace a runtime trace, but it removes the obvious static memset/memcpy-style runtime-flag producer shape from the current evidence.zAt least one broad block write candidate was found; inspect candidate rows before treating 0x0059e34d as an unresolved runtime flag.scopezUdword references to 0x0059e300..0x0059e34d with nearby size/call/rep block-write scanruntimeEnabledFlagHexscanRangeHexz..windowBytesrowCountaddressLikeCoveringBaseCountdirectCoveringWriteCountblockWriteCandidateCountaddressLikeCoveringBasesdirectCoveringWritesblockWriteCandidatesr    
proofFoundF*opcode24RuntimeEnabledBlockWriteProofFound-failedOpcode24RuntimeEnabledBlockWriteGateIdsmissingEvidenceevidenceRefsblocked)evidenceRefCountpromotionStatus
conclusion)
r"   lenr   r   r   r   WINDOW_BYTES4FAILED_OPCODE24_RUNTIME_ENABLED_BLOCK_WRITE_GATE_IDS5OPCODE24_RUNTIME_ENABLED_BLOCK_WRITE_MISSING_EVIDENCE2OPCODE24_RUNTIME_ENABLED_BLOCK_WRITE_EVIDENCE_REFS)r   r    r!   address_likedirect_coveringblock_candidatesaddress_noter:   s           r   build_summaryrD   ?   s   S>D#'JC3/H+ICJLJ&*Isc2G.HsIOI'+Js3H/IJJ#3  C-. /O O F	 	 %~ &YY 	8 	h';!< 	5!1232eN6K5LM 	|	
 	CI 	'L(9 	#C$8 	#C(8$9 	#L 	 	 0 	 	e 	5e 	89m  	P!" 	J#$   RS$ ) - KIJs   
DD
DD
DD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g}|j                  d | d   D               |j                  g d       |j                  d  | d!   D               |j                  g d"       |j                  d# | d$   D               |j                  g d%       | d&   D cg c]  }|d'   s
|d(   s|d)   r| }}|D ]  }d*j                  d+ |d,   D              xs d-}d*j                  d. |d/   D              xs d-}|j                  d0|d1    d2|d3    d4|d5    d6|d7    d8|d9    d4| d6| d6|d)    d8|d:    d;        |s|j                  d<       |j                  d       d=j                  |      S c c}w )>Nz8# Save Selector Opcode 0x24 Runtime Enabled Block Writes zScope: r'   .z- runtime enabled flag: `r(   `z- scan range: `r)   z- scan window bytes: r*   z- rows: r+   z- address-like covering bases: r,   z- direct covering writes: r-   z- block write candidates: r.   z- proofFound: `r2   z/- opcode24RuntimeEnabledBlockWriteProofFound: `r3   z- promotion status: `r9   r:   z## Failed Gatesc              3  (   K   | ]
  }d | d  yw)- `rH   Nr   .0items     r   	<genexpr>zmarkdown.<locals>.<genexpr>   s     d43tfAds   r4   )rF   z## Missing EvidencerF   c              3  &   K   | ]	  }d |   yw)z- Nr   rK   s     r   rN   zmarkdown.<locals>.<genexpr>   s     D2dVDs   r5   )rF   z## Evidence RefsrF   c              3  8   K   | ]  }d |d    d|d      yw)rJ   r   z`: r	   Nr   rL   r!   s     r   rN   zmarkdown.<locals>.<genexpr>   s/       c&k]#c-012s   r6   )rF   z## Candidate RowsrF   ze| address | ref | kind | access | required size | sizes | transfers | block candidate | instruction |z8| --- | --- | --- | --- | ---: | --- | --- | --- | --- |r    r$   r%   r&   , c              3  6   K   | ]  }|d     d|d      ywvalueHex@vaHexNr   rK   s     r   rN   zmarkdown.<locals>.<genexpr>   %     jDT*-.aW?j   coveringSizeCandidates-c              3  6   K   | ]  }|d     d|d      ywkindrV   rW   Nr   rK   s     r   rN   zmarkdown.<locals>.<genexpr>   #     fDfaW?frY   transferCandidatesz| `
addressHexz` | `instructionVaHexz` | instructionKindz | 
accessKindz | `r   instructionz` |z%| - | - | - | - | - | - | - | - | - |
)extendjoinappend)summarylinesr!   candidate_rowssizes	transferss         r   markdownro   o   s   B

''"#1%

#G,C$D#EQG
'.12!4
 678
7:&'(
)'2P*Q)RS
$W-G%H$IJ
$W-G%H$IJ
',/02
9'Bn:o9ppqr
(9 :;1=


'E* 
LLd73b+cdd	LL  
 
LLD1B)CDD	LL  
 
LL >*  
LL   v()S1F-G3OdKe 	N   
		jCPhLijjqnqIIfCPdLeffmjm	#l#$E#.@*A)B$sK\G]F^^a< !c*M&N%OtTYSZZ]^g]hhk()*$s=/A.B#G	

 <=	LL99Us   2Gc                4   g }| d   D ]+  }|d   s|d   s|d   sdj                  d |d   D              xs d}dj                  d	 |d
   D              xs d}|j                  dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |       dt        j                  |       d|d    dt        j                  |d          d       . dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }dj                  dd d!d"t        j                  | d#          d$d%d&t        j                  | d'          d(d)t        j                  | d*          d(d+| d,    d-d.| d/    d-d0| d1    d-d2| d3    d-d4| d5    d-d6t        j                  t	        | d7                d(d8t        j                  | d9          d(d:d;t        j                  | d<          d=d>d%| d:d?d%| d:d@d%| d:dAdBdj                  |      xs dCdDg      S )ENr    r$   r%   r&   rR   c              3  6   K   | ]  }|d     d|d      ywrT   r   rK   s     r   rN   zhtml_page.<locals>.<genexpr>   rX   rY   rZ   r[   c              3  6   K   | ]  }|d     d|d      ywr]   r   rK   s     r   rN   zhtml_page.<locals>.<genexpr>   r_   rY   r`   z<tr><td><code>ra   z</code></td><td><code>rb   z</code></td><td>rc   z	</td><td>rd   z</td><td><code>r   re   z</code></td></tr>rF   c              3  N   K   | ]  }d t        j                  |       d  yw)
<li><code></code></li>NhtmlescaperK   s     r   rN   zhtml_page.<locals>.<genexpr>   s+       T[[&'|4   #%r4   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li></li>Nrv   rK   s     r   rN   zhtml_page.<locals>.<genexpr>   s+       t{{4 !'ry   r5   c              3     K   | ]9  }d t        j                  |d          dt        j                  |d          d ; yw)rt   r   z	</code>: r	   r{   Nrv   rQ   s     r   rN   zhtml_page.<locals>.<genexpr>   sE       T[[V-.iCDV8W7XX]^s   ?Ar6   rf   zj<!doctype html><meta charset="utf-8"><title>Save Selector Opcode 0x24 Runtime Enabled Block Writes</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1180px;margin:24px auto}table{border-collapse:collapse}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z?<h1>Save Selector Opcode 0x24 Runtime Enabled Block Writes</h1>z
<p>Scope: r'   z.</p>z<ul>z <li>runtime enabled flag: <code>r(   ru   z<li>scan range: <code>r)   z<li>scan window bytes: r*   r{   z
<li>rows: r+   z!<li>address-like covering bases: r,   z<li>direct covering writes: r-   z<li>block write candidates: r.   z<li>proofFound: <code>r2   z<li>promotion status: <code>r9   z</ul>z<p>r:   z</p>z<h2>Failed Gates</h2>z<h2>Missing Evidence</h2>z<h2>Evidence Refs</h2>z<h2>Candidate Rows</h2>z<table><thead><tr><th>address</th><th>ref</th><th>kind</th><th>access</th><th>required size</th><th>sizes</th><th>transfers</th><th>block candidate</th><th>instruction</th></tr></thead><tbody>z8<tr><td colspan="9">No block write candidates.</td></tr>z</tbody></table>)rh   ri   rw   rx   str)rj   rl   r!   rm   rn   failed_gatesmissing_evidenceevidence_refss           r   	html_pager      sJ   Nv 
-.#6K2LPSTiPj		jCPhLijjqnqIIfCPdLeffmjm	S%678 9S);%<=> ?;;s#4567 8;;s<012 3S)L%MNO P;;u%& ';;y)* +,-. /S%789 :
	

$ 77 KL L ww -.  GG >* M 99v 	nI
T[[!12359
*4;;w?V7W+X*YYef
 W^-D!E FlS
!'-"8!9?
WZ()/
+G4R,S+TTYZ
&w/I'J&K5Q
&w/I'J&K5Q
 S1F-G!H IV
&t{{7;L3M'N&O|\
dkk',/016
|nE"#
 & 
}oU#! 	K		.!_%_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_okz8save_selector_opcode24_runtime_enabled_block_writes.jsonF   )ensure_asciiindentrf   zutf-8)encoding)mkdir
write_textjsondumpsparentr   )rj   out_dirhtml_outjson_outs       r   write_outputsr      s    MM$M.SSH

7q9D@   dT:Ig.AOr   c                    t        j                  t              } | j                  dt        t
        dz         | j                  dt        t               | j                  dt               | j                         }t        |j                  j                               }t        ||j                  |j                        }t        d|        y )	N)r	   z--exer   )typedefaultz	--out-dirz
--html-out)r   z/wrote opcode24 runtime enabled block writes -> )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsrD   r   
read_bytesr   r   r   print)parserargsrj   r   s       r   mainr      s    $$9F
dD=4HI
$<
40DDHH//12GWdllDMMBH	;H:
FGr   __main__)r   intreturnr}   )r   bytesr   z
list[dict])r   r   r   dict)rj   r   r   r}   )rj   r   r   r   r   zPath | Noner   r   )r   None)r   
__future__r   r   rw   r   pathlibr   3summarize_save_selector_opcode24_mode1_block_writesr   __file__resolver   r   r   r   r   r   r<   r=   r>   r?   r   r"   rD   ro   r   r   r   __name__r   r   r   <module>r      s    R "     H H~''*
Ul!  %&&8 4
9 5 +ijFWwx1Bqr6 2.-`<~;| 25d 
H zF r   