
    7,JjY                    *   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	  ee
      j                         j                  d   Zedz  ZdZd	Zdd
ZddZddZddZddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZedk(  r e        yy)zHSummarize the remaining global-reset proof gap for secondaryBranchState.    )annotationsN)Path)Any   outmap1_01amap2_02dc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     O/home/exedev/hwanse/tools/summarize_save_selector_secondary_global_reset_gap.py	load_jsonr      s*    ;;=::dnngn677    c                ^    | j                  d      }| j                  d      }||y | d| S )Ngroupslot:get)rootr   r   s      r   selector_for_rootr      s;    HHWE88FD}WAdVr   c                    | j                  d      xs g D ]  }t        |      |k(  s|c S  | j                  d      xs g D ]  }t        |      |k(  s|c S  i S )NrouteOverlapRootsroots)r   r   )
fill_rootsselectorrows      r   find_root_by_selectorr%       sj    ~~128b S!X-J ~~g&," S!X-J Ir   c                    | D cg c]e  }|j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      dg c}S c c}w )NvaHexvalueHexhelperArgumentHexbeforeCurrentFrontierReaderafterCurrentFrontierReaderrouteRelevance)r'   r(   r)   r*   r+   r,   r   )rowsr$   s     r   compact_fill_rowsr.   *   st     
  WWW%
+!$)<!=+.773P+Q*-''2N*O!gg&67	

 
 
s   A*A2c                    | |||dS )Nareastatusevidenceconsequence r0   s       r   	proof_rowr6   8   s    "	 r   c                   | j                  d      xs i }| j                  d      xs i }| j                  d      xs i }	|j                  d      xs d}
|j                  d      xs d}t        ||
      }t        ||      }t        |j                  d      xs g       }t        |j                  d      xs g       }t        |j                  d	      xs g       }|j                  d
d      }|j                  dd      }|j                  d      du }|	j                  d|j                  dd            }|	j                  d|j                  dd            }|	j                  d|j                  dd            }|	j                  d|j                  d            }| j                  d      du }| j                  d      du }|xr |dk(  xr |xr |dk(  xr |dk(  xr |du }|xs i j                  dd      }|xs i j                  dd      }|xs i j                  dd      }t        |      xr |dk(  xr |dk(  xr |dk(  }|j                  d      du }|j                  d      du } |j                  d      du }!|j                  d       du }"|j                  d!      }#|j                  d"      du }$|xr |"xr	 |#dk(  xr |$}%|xr | xr |! }&|xr |xr |%}'|&s|'sd#nd$}(|rd%nd&})t	        d'|dk(  r|dk(  rd$nd(d)| d*| d+      t	        d,|rd$nd(d-|j                  d.       d/|j                  d0       d1| d2      t	        d3|rd$nd(d4|xs i j                  d5       d6|xs i j                  d7       d8| d9| d:| 
d;      t	        d<|dk(  rd$nd(d=|j                  d>       d?| d@      t	        dA|rdBndCdD| dEdFj                  dG |xs |D              xs dH dI      t	        dJ|dk(  r|du rd$nd(dK|j                  dL       dM| dN| dO      t	        dP|%rd$nd(dQ|" dR|# dS|$ dT| dU      t	        dV|'rd$nd(dW| dX| dY|% dZ      t	        d[|&s| j                  d\      durd(nd$d]|j                  d       d^|j                  d       d_| j                  d\       d`|( da      t	        db|!sd(nd$dc|j                  d       dd      g
}*g de}+df},i dgt        dht        di|j                  dj      dk|)dl|dm|d.|j                  d.      dn|j                  d0      do|dp|xs i j                  d5      dq|xs i j                  d7      dr|ds|dt|du|d|dv|j                  dv      xs |	j                  dv      i dw|j                  d>      dx|j                  dx      xs |	j                  dx      d|d|dy|xs |D -cg c]%  }-|-j                  dz      s|-j                  dz      ' c}-d|
d{|j                  d{      xs |j                  d{      d||j                  d>      xs |j                  d|      d}|j                  d~      d|D -cg c]%  }-|-j                  dz      s|-j                  dz      ' c}-d|j                  dL      d|d|d|j                  d      d"|j                  d"      d |j                  d       d!|#i d|%d|j                  d      d|j                  d      d|j                  d      d|j                  d      d|xs i j                  d      d|d|d|d|'d|&d|(d\| j                  d\      ddd|*d|+d|,S c c}-w c c}-w )NsecondaryBranchStatehelper
routeScopepredecessorSelectorz1:0currentSelectorz2:0fillsvalidAfterFrontierdirectIndexedWriterCountr   unresolvedRefCount#onlyDirectCallInsideOpcode10HandlerT'currentRootValidBeforeFrontierFillCountvalidBeforeFrontierCount&currentRootValidAfterFrontierFillCountvalidAfterFrontierCount&predecessorTailValidSecondaryFillCounttailValidSecondaryFillCountpredecessorLocalTailResetFoundlocalTailResetFoundnoDirectGlobalSecondaryWriter'noKnownPreFrontierResetInScopedEvidenceFtouchBlockWriteCandidateCount!fullCoverBlockWriteCandidateCountdirectOverlapWriteCountrouteOrderProvenpersistenceProvenstrictHotspotFoundselectorAdjacentintermediateSelectorCount,currentRootHasNoKnownBeforeFrontierOverwritez'runtime-order-or-untraced-bytecode-pathclosedz&static-scope-closed-runtime-order-openzstatic-reset-scope-openzdirect secondary table writesopenzdirectIndexedWriterCount=z; unresolvedRefCount=z?No direct table writer remains in the scanned .text references.zhelper entry scopezhelper=helperVaHexz; directCallCount=directCallCountz; onlyOpcode10=z?Secondary changes are still modeled as opcode 0x10 helper rows.zbroad block write shapez
scanRange=scanRangeHexz; addressLike=addressLikeTouchingBaseCountz; directOverlap=z; touchBlock=z; fullCoverBlock=z]No obvious memset/memcpy-style secondaryBranchState reset remains in the current static scan.zcurrent root before frontierzroot=rootRangeHexz; beforeFrontierValidFills=zNThe current selector has no valid secondary fill before the 0x00542b0c reader.zcurrent root after frontierzpost-frontier-onlynonezafterFrontierValidFills=z
; fillVas=,c              3  F   K   | ]  }|j                  d       xs d  yw)r'   -Nr   .0r$   s     r   	<genexpr>z build_summary.<locals>.<genexpr>   s!     #lCGGG$4$;$;#ls   !r_   zAPost-frontier fills cannot initialize the earlier blocker branch.zpredecessor local tailz
tailRange=tailRangeHexz; tailValidSecondaryFills=z; localTailResetFound=zIThe known predecessor 1:0 tail does not locally clear the secondary fill.zselector-order reset windowzselectorAdjacent=z; intermediateSelectors=z; currentNoKnownOverwrite=z; closedStaticResetScope=zuWithin the selector-table adjacency model, no reset-capable selector/root remains between 1:0 and the current reader.zstatic reset shapezclosedStaticResetScope=z; blockWriteShapeClosed=z; selectorOrderResetGapClosed=z_The remaining secondary reset uncertainty is not an obvious static writer or block reset shape.zruntime order and global resetglobalResetRuledOutzrouteOrderProven=z; persistenceProven=z; globalResetRuledOut=z; runtimeResetRiskClass=zTThe remaining reset risk is dynamic execution order or an untraced VM bytecode path.zstrict source hotspotzstrictHotspotFound=zJA strict map1_01a source trigger is still required before route promotion.)zJprove selector 1:0 executes before selector 2:0 in the normal runtime pathzprove the selector-order model corresponds to the real VM execution path before the current reader, not an untraced opcode 0x10/helper pathz1find strict map1_01a source coordinate or hotspota  Static secondaryBranchState reset evidence is narrowed: no direct global writer was found, helper 0x00410c90 is only reached through opcode 0x10, the broad block-write scan found no overlapping memset/memcpy-style reset candidate, the current root has no valid before-frontier fill, and the predecessor tail has no local reset. The selector-order reset window between adjacent selectors 1:0 and 2:0 is also closed by the current static evidence. The proof still remains blocked because selector-table adjacency is not runtime execution order, the VM path before the reader is not captured, and the strict map1_01a source hotspot is not proven.sourcetargetsecondaryRangeHexrangeHexglobalResetCandidateClass directGlobalSecondaryWriterCount!unresolvedGlobalSecondaryRefCounthelperDirectCallCount%helperOnlyCalledInsideOpcode10HandlersecondaryBlockWriteScanRangeHex*secondaryBlockAddressLikeTouchingBaseCount%secondaryBlockDirectOverlapWriteCount&secondaryTouchBlockWriteCandidateCount*secondaryFullCoverBlockWriteCandidateCountsecondaryBlockWriteShapeClosedcurrentRootHexcurrentRootRangeHexfrontierReaderHexcurrentRootAfterFrontierFillVasr'   predecessorRootHexpredecessorRootRangeHexpredecessorFillCount	fillCountpredecessorFillVaspredecessorTailRangeHex%predecessorFillWouldPassCurrentReaderselectorOrderResetGapClosedselectorMergeGapOpen$secondaryRouteOverlapPromotionStatuspromotionStatusclosedStaticResetScopestaticResetShapeRuledOutopenRuntimeOrderOrBytecodeGapruntimeResetRiskClassblocked	proofRowsremainingProofs
conclusion)r   r%   r.   boolr6   joinSOURCETARGET).reset_scopesecondary_sourcespredecessor_tail_resetpersistence_gapsecondary_fill_roots"secondary_route_overlap_candidatessecondary_block_writes	secondaryr9   route_scopepredecessor_selectorcurrent_selectorpredecessor_rootcurrent_rootpredecessor_fillscurrent_fillsafter_frontier_rowsdirect_writer_countunresolved_ref_counthelper_only_opcode10current_before_countcurrent_after_counttail_valid_countlocal_tail_reset_foundno_direct_global_writerno_known_pre_frontier_resetclosed_static_scopeblock_write_touch_countblock_write_full_cover_count block_write_direct_overlap_countblock_write_shape_closedroute_order_provenpersistence_provenstrict_hotspot_foundselector_adjacentintermediate_selector_countcurrent_no_known_overwriteselector_order_reset_gap_closedopen_runtime_gapstatic_reset_shape_ruled_outruntime_reset_risk_classcandidate_class
proof_rows	remainingr   r$   s.                                                 r   build_summaryr   A   s;     67=2I__X&,"F//,/52K*../DEN&**+<=F,-ACWX()=?OPL)*:*>*>w*G*M2N%l&6&6w&?&E2FM+,=,A,ABV,W,][]^#--(BAF$==)=qA!::&KLPTT&??18!< &//07; #0""#@!D )__(""#89 *oo.MNRVV"-//2["\`d"d 	, A%	, 	, !A%	, !		,
 #e+   6;@@A`bcd$:$@b#E#EFikl#m (>(D"'I'IJcef'g$#$ 	2#q(	2(A-	2 -1	  ),,-?@DH(,,-@ATI*../CDL'++,>?4G"1"5"56Q"RJKtS  	 	'	''1,	' '	 $ /^3E^J^_ 	,$	,+ ! #? 	2   	1&  	++q05IQ5NHTZ'(;'<<QRfQghM		
 	 ,H&&**]344FvzzRcGdFe f  457 N	
 	%0Hf4:??OP Q 6 <"AAB`ab c!!A B C566GHdGeg
 l
	
 	*,1Hv%)).9::UVjUkl\		
 	)$7 V*+>*? @88#lGZGk^k#llspstv P	
 	$(A-2HE2QHW]377GH I++;*<<RSiRjl X	
 	)7HV#$5#66NOjNk l++E*FF_`s_tv D	
 	 4H&)*=)> ?))A(B C//N.OQ n		
 	,&+//:O*PX\*\Fbj#O$7$78J$K#L M%%4%8%89L%M$N O''27L'M&N O))A(BD
 c
	
 	#.FH!/"5"56J"K!LMX		
cWJpI	@ 6&6&6 	Y]]:66 	$_	6
 	+,?6 	,-A6 	vzz-06 	 ,=!>6 	01E6 	*,B,Hb+M+Mn+]6 	57M7SQS6X6XYw6x6 	01Q6 	12I6 	56R6 	)*B6  	+!6" 	/--.>?d;??ScCd#6$ 	044^D%6& 	.223FGo;??[nKo'6( 	23G)6* 	12E+6, 	*(;(L},
!$PSPWPWX_P`CGGG,
-62 	3364 	o112FG{KaKeKefzK{566 	"#3#7#7#G  $AKaKeKef  LA768 	 0 4 4[ A96: 	;LaCPSPWPWX_P`swww/a;6< 	"#9#=#=n#M=6> 	12B?6@ 	)*@A6B 	01D1DEl1mC6D 	78K8KLz8{E6F 	O//0BCG6H 	$%@I6J 	&'FK6L 	O//0BCM6N 	_001DEO6P 	o112FGQ6R 	 3 34J KS6T 	/1S1YWY0^0^_p0qU6V 	()@W6X 	23NY6Z 	!"5[6\ 	#$@]6^ 	()9_6` 	 !9a6b 	{/DEc6d 	9e6f 	Zg6h 	9i6j 	jk6 6,,
 bs   	]-
 ]-
]2
1]2
c                   g ddd| d    d| d    dd| j                  d	       dd
| j                  d       dd| j                  d       d| j                  d       d| j                  d       d| j                  d       dd| j                  d       d| j                  d       d| j                  d       d| j                  d       d| j                  d       d| j                  d       d | j                  d!       d"d#j                  | j                  d$      xs g       xs d% d&d'| j                  d(       d"d#j                  | j                  d)      xs g       xs d% d&d*| j                  d+       d,| j                  d-       d.| j                  d/       d0| j                  d1       d2| j                  d3       dd4| j                  d5       d6| j                  d7       d| j                  d8      xs ddd9dd:d;}| j                  d<      xs g D ]\  }|j                  d=|j                  d>       d?|j                  d@       d?|j                  dA       d?|j                  dB       dC	       ^ |j                  g dD       |j                  dE | j                  dF      xs g D               |j                  d       dGj                  |      S )HNz*# Save Selector Secondary Global Reset Gap z
- route: `re   z -> rf   `z- secondaryBranchState: `rg   z- candidate class: `ri   z#- direct global secondary writers: rj   z$- unresolved global secondary refs: rk   z1- helper only called inside opcode 0x10 handler: rm   z%- secondary block-write scan range: `rn   z&- secondary block address-like bases: ro   z#- secondary direct overlap writes: rp   z$- secondary touch block candidates: rq   z)- secondary full-cover block candidates: rr   z&- secondary block-write shape closed: rs   z'- current before-frontier valid fills: rB   z&- current after-frontier valid fills: rD    (, rw   r_   )z- predecessor fills: rz   r|   z*- predecessor tail valid secondary fills: rF   z- closed static reset scope: r   z - static reset shape ruled out: r   z#- selector-order reset gap closed: r   z- runtime reset risk class: `r   z- global reset ruled out: rd   z- promotion status: r   r   z## Proof Rowsz*| area | status | evidence | consequence |z| --- | --- | --- | --- |r   z| r1   z | r2   r3   r4   z |)r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   yw)z- Nr5   ra   items     r   rb   zmarkdown.<locals>.<genexpr>_  s     N2dVNs   r   
)r   r   appendextend)summarylinesr$   s      r   markdownr   8  s>    4 
  WX&'tGH,=+>a@  $GKK0C$D#EQG	 
 w{{+FGHJ  .gkk:\.]-^_  /w{{;^/_.`a  <GKKHo<p;qr  0<]0^/__`a  1=i1j0kl  .gkk:a.b-cd  /w{{;c/d.ef  4GKK@l4m3no  1=]1^0_`  2'++>g2h1ij   1=e1f0ggijnjsjst{tt  Ab  uc  ui  gi  kj  kq  nq  jr  rs  	t! "  ,B CDBtyyQXQ\Q\]qQrQxvxGy  HA  ~A  GB  BC  	D# $ 5W[[Ai5j4kl% & (4L(M'NO' ( +7;;7Q+R*ST) * .gkk:W.X-YZ+ , (4K(L'MQO- . %W[[1F%G$HI/ 0 w{{+<=>?1 2 	3 4 	L!'R5 6 	7 8 	9 : 	; < 	5= > 	$? EB {{;'-2 
!SWWX%6$7s377:;N:OsSVSZSZ[hSiRjjlm	

 
LL01	LLN5F)G)M2NN	LL99Ur   c                   dj                  d | j                  d      xs g D              }dj                  d | j                  d      xs g D              }dj                  | j                  d      xs g       xs d	}dj                  | j                  d
      xs g       xs d	}dj                  ddddt        j                  | d          dt        j                  | d          dt        j                  | j                  d      xs d       dt        j                  | j                  d      xs d       d	d| j                  d       d| j                  d       d| j                  d       d| j                  d       d| j                  d        d!t        j                  |       d"| j                  d#       d!t        j                  |       d$| j                  d%       d&d't        j                  t	        | j                  d(                   d)| j                  d*       d+| j                  d,       d-| j                  d.       d/| j                  d0       d1| j                  d2       d&d3| j                  d4       d5| j                  d6       d7| j                  d8       d9t        j                  | j                  d:      xs d       d;| j                  d<       d=t        j                  | j                  d>      xs d       dd?t        j                  | j                  d@      xs d       dAdB|dCdDdE| dFg      S )GNr   c              3  j  K   | ]  }d t        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       d	  yw)	z<tr><td>r1   r   z	</td><td>r2   r3   r4   z
</td></tr>N)htmlescaper   r`   s     r   rb   zhtml_page.<locals>.<genexpr>e  s       	{{3776?0b12 3{{3778,234 5{{377:.4"56 7{{377=17R89 :		s   B1B3r   r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li>z</li>N)r   r   r   s     r   rb   zhtml_page.<locals>.<genexpr>n  s#     ftDKK-.e4fs   #%r   r   rw   r_   r|   z\<!doctype html><meta charset="utf-8"><title>Save Selector Secondary Global Reset Gap</title>z<style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1120px;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>z1<h1>Save Selector Secondary Global Reset Gap</h1>z<p>Route <code>re   z -&gt; rf   z$</code>; secondaryBranchState <code>rg   z</code>; class <code>ri   z</code>.</p>z$<p>direct global secondary writers: rj   z$; unresolved global secondary refs: rk   z1; helper only called inside opcode 0x10 handler: rm   z'; current before-frontier valid fills: rB   z&; current after-frontier valid fills: rD   r   z); predecessor fills: rz   z+); predecessor tail valid secondary fills: rF   z.</p>z+<p>secondary block-write scan range: <code>rn   z</code>; address-like bases: ro   z; direct overlap writes: rp   z; touch block candidates: rq   z; full-cover block candidates: rr   z&; secondary block-write shape closed: rs   z<p>closed static reset scope: r   z ; static reset shape ruled out: r   z#; selector-order reset gap closed: r   z"; runtime reset risk class: <code>r   z!</code>; global reset ruled out: rd   z; promotion status: <code>r   z<p>r   z</p>zz<h2>Proof Rows</h2><table><thead><tr><th>area</th><th>status</th><th>evidence</th><th>consequence</th></tr></thead><tbody>z</tbody></table>z<h2>Remaining Proofs</h2>z<ul>z</ul>)r   r   r   r   str)r   r-   proofsafterpredecessors        r   	html_pager   d  s   99  ;;{+1r D WWfM^A_AeceffFIIgkk"CDJKRsE))GKK(<=CDKK99h 	G;
$++gh&789WU]M^A_@`  aE  FJ  FQ  FQ  RY  R]  R]  ^q  Rr  Rx  vx  Fy  Ez  zO  PT  P[  P[  \c  \g  \g  hC  \D  \J  HJ  PK  OL  LX  	Y
.w{{;]/^._  `D  EL  EP  EP  Qt  Eu  Dv  vg  ho  hs  hs  t[  h\  g]  ]D  EL  EP  EP  Qz  E{  D|  |b  cj  cn  cn  oW  cX  bY  Y[  \`  \g  \g  hm  \n  [o  oE  FM  FQ  FQ  Rh  Fi  Ej  jl  mq  mx  mx  yD	  mE	  lF	  F	q	  r	y	  r	}	  r	}	  ~	f
  r	g
  q	h
  h
m
  	n

5dkk#gkkRsFtBu6v5w  xU  V]  Va  Va  bN  VO  UP  Pi  jq  ju  ju  v]  j^  i_  _y  zA  zE  zE  Fn  zo  yp  pO  PW  P[  P[  \H  PI  OJ  Jp  qx  q|  q|  }]  q^  p_  _d  	e
(5M)N(OOopwp{p{  }W  qX  pY  Y|  }D  }H  }H  If  }g  |h  hJ  KO  KV  KV  W^  Wb  Wb  cz  W{  WA  A  KB  JC  Cd  el  ep  ep  qF  eG  dH  Hb  cg  cn  cn  ov  oz  oz  {L  oM  oS  QS  cT  bU  Ua  	b
dkk'++l39r:;4@ 	E#
vhe  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_global_reset_gap.jsonF   )ensure_asciiindentr   r   r   -save_selector_secondary_global_reset_gap.html)mkdir
write_textr   dumpsr   )r   out_dirs     r   write_outputsr     sf    MM$M.>>JJ

7q9D@ K  >>JJ9U\K]hoJpr   c                 V   t        j                  t              } | j                  dt        t
               | j                         }t        t        |j                  dz  i       t        |j                  dz  i       t        |j                  dz  i       t        |j                  dz  i       t        |j                  dz  i       t        |j                  d	z  i       t        |j                  d
z  i             }t        ||j                         t        d|j                  dz          y )N)descriptionz	--out-dir)typedefaultz(save_selector_secondary_reset_scope.jsonz*save_selector_secondary_state_sources.jsonz)save_selector_predecessor_tail_reset.jsonz.save_selector_predecessor_persistence_gap.jsonz'save_selector_secondary_fill_roots.jsonz5save_selector_secondary_route_overlap_candidates.jsonz)save_selector_secondary_block_writes.jsonz$wrote secondary global reset gap -> r   )argparseArgumentParser__doc__add_argumentr   OUT
parse_argsr   r   r   r   print)parserargsr   s      r   mainr     s    $$9F
$<D$,,!KKRP$,,!MMrR$,,!LLbQ$,,!QQSUV$,,!JJBO$,,!XXZ\]$,,!LLbQG '4<<(	0@o1o0p
qrr   __main__)r   r   r   r   returnr   )r   dictr   z
str | None)r"   r   r#   r   r   r   )r-   
list[dict]r   r   )
r1   r   r2   r   r3   r   r4   r   r   r   )NN)r   r   r   r   r   r   r   r   r   r   r   dict | Noner   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )r   
__future__r   r   r   r   pathlibr   typingr   __file__resolver   ROOTr   r   r   r   r   r%   r.   r6   r   r   r   r   r   __name__r5   r   r   <module>r      s    N "      H~''*
Ul		8 7;*.ttt !t 	t
 t )4t (t 
tn)X<qs" zF r   