
    Lj+                    \   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	 ddl
mZ ddlmZmZmZ  e	e      j#                         j$                  d   Zed	z  Zed
z  Zedz  Zedz  Zedz  ZdZdZdZ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&e'dk(  r e&        yy))a  Ground the generic VM opcode 0x14 branch shape.

Opcode 0x14 is easy to misread because the opcode byte overlaps the low byte of
the branch target dword.  A plausible command therefore starts with a
file-backed target VA whose low byte is 0x14, but raw VA-shaped hits are still
only candidates until a known stream boundary reaches them.  This report records
that rule and explicitly checks the HUD top-menu post-selectedRoot table, where
a byte also looks like 0x14 but the overlapped dword is not a VA.
    )annotationsN)Counter)Path)Any)offset_to_varead_sectionsva_to_offset   zHwanse2.exeoutwebz&generic_vm_opcode14_branch_review.jsonz&generic_vm_opcode14_branch_review.htmli8D i8@ ipG c                    | dS d| d| dS )N-0x0x )valuewidths     0tools/build_generic_vm_opcode14_branch_review.pyhxr   $   s#    -3;r%%z):%;;    c                V    t        j                  | dd      S t        |       d      S )N T)quote)htmlescapestr)r   s    r   hr   (   s%    ;;U]r$GGE
$GGr   c                    |y | D ]?  }t        |d         }|t        |d         z   }||cxk  r|k  s.n 1t        |d         c S  y )Nvaraw_sizename)intr   )sectionsr    sectionstartends        r   section_name_forr(   ,   s\    	z (GDM"c'*-..Bwv''	(
 r   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   <Ir   )r	   lenstructunpack_from)exer$   r    offs       r   dword_atr1   7   s?    
x
$C
{cAgC(dC-a00r   c                    | dz  }|dk(  rd}n|dk(  rd}n
|dk(  rd}nd}| dz  }|d	k(  rd
}n|dk(  rd}n|dk(  rd}n
|dk(  rd}nd}| d| S )N   @   z#left=global-u32[0x59db60 + byte2*4]   zleft=object-word[byte2]z*left=linked/base-word[object+0xa8 + byte2]zleft=implicit/default0   r   z,right=inline-u16(stream+4); false advance +8   z6right=global-u32[0x59db60 + byte3*4]; false advance +4    z*right=object-word[byte3]; false advance +4z=right=linked/base-word[object+0xa8 + byte3]; false advance +4zright=unknownz; r   )byte1left	left_textright
right_texts        r   source_moder>   >   s    4<Dt|9		-		@	+	DLE}C
	$M
	$A
	$T
$
[:,''r   c                    | dz  dk(  rdS dS )Nr6   r      r*   r   )r9   s    r   expected_lengthrA   W   s    $&1-A-r   c                   g }g }|D ]]  }|j                  d      dvrt        |d         }|t        |d         z   }| j                  d||      }|dk7  sPt        ||      }||dz   t	        |       k  rt        j                  d| |      d	   }	| |d
z      }
|t        |      | ||dz    j                  d      |	t        |	      t        ||	      d ut        ||	      d|
dd| |dz      dd| |dz      dt        |
      t        |
      d}|d   r|	dz  dk(  rd|d<   |j                  |       nd|d<   |j                  |       | j                  d|d
z   |      }|dk7  r` ||fS )Nr"   >   .data.rdatarawr!      r@   r+   r   r
    r   02x      )r    vaHexrawFirst8HextargettargetVaHex
targetIsVatargetSectionbyte1Hexbyte2Hexbyte3HexexpectedLength
sourceModerP         z%va-shaped-overlapped-target-candidateclassificationzinvalid-or-data-byte)getr#   findr   r,   r-   r.   r   hexr	   r(   rA   r>   append)r/   r$   validinvalidr%   r&   r'   posr    rN   r9   rows               r   scan_opcode14_candidatesrb   [   s   "$E$&G 2;;v&99GEN#c'*-..hhws+Rih,B~#'SX"5++D#s;A>C!GV$'cAg$6$:$:3$?$#%f:".x"@"L%5h%G"$U3K 0"$Sq\#$6 7"$Sq\#$6 7&5e&<"-e"4 |$&4-D)@,SC()LL%,BC()NN3'((7C!GS1C3 Ri2@ '>r   c                     t         j                         } t        |       }t        | |t        dz         }t        | |      \  }}t        | |t              }t        d |D        d       }t        d |D              }t        d |D              }t        d |D              }	t        |      |t        k(  t        |      t        |      t        t        |j                                     t        t        |j                                     t        t        |	j                                     t        t              t        |      t        |      dd}
dd	d
|
t        t        dz         t        |      ddddd||d d |d d ddgd	S )NP   c              3  :   K   | ]  }|d    t         k(  s|  yw)r    N)HUD_POST_SELECTED_ROOT_TABLE_VA.0ra   s     r   	<genexpr>zbuild_report.<locals>.<genexpr>   s     [Cc$i;Z.ZC[s   c              3  &   K   | ]	  }|d      yw)rR   Nr   rg   s     r   ri   zbuild_report.<locals>.<genexpr>   s     ;c#j/;   c              3  &   K   | ]	  }|d      yw)rQ   Nr   rg   s     r   ri   zbuild_report.<locals>.<genexpr>   s     CcS1Crk   c              3  .   K   | ]  }|d    sdnd  yw)rP   ztarget-not-vaztarget-low-byte-not-0x14Nr   rg   s     r   ri   zbuild_report.<locals>.<genexpr>   s%      $  #<06PP$s   a  Opcode 0x14 handler semantics are grounded as an overlapped-target conditional branch shape: dword[stream] is the true target and must be file-backed with low byte 0x14 for a plausible command.  Raw VA-shaped hits are not promoted by themselves; a known stream boundary must reach them.  The false path advance is +8 only for inline-u16 right operands, otherwise +4.  The HUD 0x0047e670 byte is not a valid 0x14 command because dword[stream] is 0x0002c614, not a file-backed VA.)opcode14HandlerVaHexopcode14HandlerGrounded&vaShapedOverlappedTargetCandidateCountinvalidOrDataByteCountvaShapedModeByteCountsvaShapedTargetSectionCountsinvalidReasonCountshudPostSelectedRootVaHex hudPostSelectedRootFirstDwordHexhudRejectedAsOpcode14decisionz(hwanse-generic-vm-opcode14-branch-reviewz0tools/build_generic_vm_opcode14_branch_review.pyz7opcode14-branch-shape-grounded-boundary-filter-requiredzDtarget is dword[stream], so opcode 0x14 overlaps the target low bytezobject+0x40 = dword[stream]z=advance +8 for inline-u16 right operand, otherwise advance +4
0x0040370a)handlerTableEntryVaHexhandlerVaHexbranchTargetEncodingtruePath	falsePathcompareHelperVaHexzeDo not classify a raw byte 0x14 as a command unless dword[stream] is a file-backed VA ending in 0x14.z^0x0047e670 is explicitly rejected by that rule and remains a post-selectedRoot table/data row.)	kindsourcestatussummary	semanticshudFalsePositiveRowvaShapedCandidateSamplesinvalidSamplesnegativeEvidence)EXE
read_bytesr   r1   GENERAL_VM_TABLE_VArb   rf   nextr   r   OPCODE14_HANDLER_VAr,   dictsorteditemsbool)r/   r$   handlerr^   r_   	hud_firsthud_rowmode_countssection_countsinvalid_reason_countsr   s              r   build_reportr      s}   
..
CS!HsH&9H&DEG-c8<NE7h(GHI[7[]abG;U;;KCUCCN# $$  !#7#*.A#A25e*"%g,"&vk.?.?.A'B"C'+F>3G3G3I,J'K#F+@+F+F+H$IJ$&'F$G,.yM!%gbG* ;DK&()<x)G&HwK$j5X".
  '$)#2J!#2,sl
! r   c                   g }| 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             dt        |j                  d             d        ddj                  |      z   dz   S )Nz<tr><td><code>rL   z</code></td><td><code>rO   z</code><br>rQ   z	</td><td>rR   z<br>rV   rU   z</td><td><code>rM   z</code></td></tr>ze<table><thead><tr><th>VA</th><th>target</th><th>mode</th><th>len</th><th>raw</th></tr></thead><tbody>r   z</tbody></table>)r]   r   rZ   join)rowsbodyra   s      r   tabler      s   D 	
3777+,- .377=123;qAY?Z>[ \SWWZ()*$q1F/G.H ISWW-./0 1377>234 5	
	
 suwu|u|  ~B  vC  C  FX  X  Xr   c                `   | d   }d| d   fd|d   fd|d   fd|d   fd	|d
   fg}dj                  d |D              }t        j                  | d      }d| dt        |d          dt        t        j                  | d   dd             dt	        | d          dt	        | d          d| dS )Nr   r   r   rn   z	VA-shapedrp   r_   rq   zHUD rejectedrw   r   c              3  X   K   | ]"  \  }}d t        |       dt        |       d $ yw)z<div class='card'><b>z
</b><span>z</span></div>N)r   )rh   kvs      r   ri   zrender_html.<locals>.<genexpr>   s/     fX\XY[\/!vZ!v]Sfs   (*F)ensure_asciiuf  <!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <title>Generic VM Opcode 0x14 Branch Review</title>
  <style>
    body { margin: 0; padding: 24px; font-family: system-ui, sans-serif; background: #f6f1e8; color: #1e2430; }
    h1 { margin: 0 0 8px; }
    h2 { margin: 22px 0 8px; }
    .sub { color: #606875; margin-top: 0; }
    .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin: 18px 0; }
    .card { background: #fffaf2; border: 1px solid #d8c8aa; border-radius: 6px; padding: 10px 12px; }
    .card b { display: block; color: #6a421b; font-size: 12px; }
    .card span { font-weight: 700; }
    section { background: white; border: 1px solid #decfb2; border-radius: 6px; padding: 14px; margin: 14px 0; overflow: auto; }
    table { width: 100%; border-collapse: collapse; font-size: 13px; }
    th, td { border-bottom: 1px solid #eadcc5; text-align: left; vertical-align: top; padding: 7px 8px; }
    th { background: #f3e6d0; }
    code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
    pre { white-space: pre-wrap; background: #272822; color: #f8f8f2; padding: 12px; border-radius: 6px; }
  </style>
</head>
<body>
  <h1>Generic VM Opcode 0x14 Branch Review</h1>
  <p class="sub">opcode 0x14의 target-overlap branch 형태와 HUD false positive 배제 규칙.</p>
  <div class="cards">z&</div>
  <section><h2>Decision</h2><p>rx   z:</p></section>
  <section><h2>HUD False Positive</h2><pre>r   rJ   r   indentz@</pre></section>
  <section><h2>VA-shaped Candidate Samples</h2>r   z.</section>
  <section><h2>Invalid Samples</h2>r   zi</section>
  <section><h2>Raw JSON</h2><pre id="payload"></pre></section>
  <script>
    const payload = zu;
    document.getElementById('payload').textContent = JSON.stringify(payload, null, 2);
  </script>
</body>
</html>
)r   jsondumpsr   r   )reportr   cards	card_htmlpayloads        r   render_htmlr      s   YG	6(#$	G234	gFGH	G456	!89:E f`effIjje4G2  [ !  !'*"5 67 8,,-djj@U9Vejst.u,v+w x005f=W6X0Y/Z [$$)&1A*B$C#D E I A% %r   c                     t               } t        j                  d       t        j                  d       t        j                  t        j                  | dd      d       t        j                  t        |       d       t        d       y )	NT)exist_okFrJ   r   zutf-8)encodingz$generic_vm_opcode14_branch_review ok)r   OUTmkdirWEBJSON_OUT
write_textr   r   HTML_OUTr   print)r   s    r   mainr      sg    ^FIItIIItI

6aHSZ[F+g>	
01r   __main__)r@   )r   
int | Noner   r#   returnr   )r   r   r   r   )r$   list[dict[str, Any]]r    r   r   z
str | None)r/   bytesr$   r   r    r#   r   r   )r9   r#   r   r   )r9   r#   r   r#   )r/   r   r$   r   r   z1tuple[list[dict[str, Any]], list[dict[str, Any]]])r   dict[str, Any])r   r   r   r   )r   r   r   r   )r   None)(__doc__
__future__r   r   r   r-   collectionsr   pathlibr   typingr   probe_exe_scene_tablesr   r   r	   __file__resolveparentsROOTr   r   r   r   r   r   r   rf   r   r   r(   r1   r>   rA   rb   r   r   r   r   __name__r   r   r   <module>r      s    #       L L H~''*
]
Ul
Ul9999    ", <H1(2.#L6rX0f2 zF r   