L'annĂ©e derniĂšre, notre Ă©quipe a dĂ» analyser le bytecode du V8. Ă cette Ă©poque, il n'existait aucun outil prĂȘt Ă l'emploi qui pouvait restaurer ce code et fournir une navigation pratique Ă travers celui-ci. Il a Ă©tĂ© dĂ©cidĂ© d'essayer d'Ă©crire un module processeur pour le framework Ghidra. En raison des particularitĂ©s du langage de description d'instructions utilisĂ©, nous avons non seulement un ensemble d'instructions lisibles, mais Ă©galement un dĂ©compilateur de type C. Cet article est la suite d'une sĂ©rie de documents ( 1 , 2 ) sur notre plugin pour Ghidra.
Plusieurs mois se sont Ă©coulĂ©s entre la rĂ©daction du module processeur et l'article. Pendant ce temps, la spĂ©cification SLEIGH n'a pas changĂ© et le module dĂ©crit fonctionne sur les versions 9.1.2â9.2.2, qui ont Ă©tĂ© publiĂ©es au cours des six derniers mois.
Maintenant sur ghidra.re et dans la documentation jointe Ă Ghidra, il y a une assez bonne description des capacitĂ©s du langage - ces matĂ©riaux valent la peine d'ĂȘtre lus avant d'Ă©crire vos propres modules. Les modules de processeur prĂȘts Ă l'emploi des dĂ©veloppeurs de framework peuvent ĂȘtre d'excellents exemples, surtout si vous connaissez l'architecture qui y est dĂ©crite.
Dans la documentation, vous pouvez lire que les modules de processeur pour Ghidra sont Ă©crits dans le langage SLEIGH, qui provient du langage SLED (Specification Language for Encoding and Decoding) et a Ă©tĂ© dĂ©veloppĂ© Ă dessein pour Ghidra. Il traduit le code machine en p-code (un langage intermĂ©diaire utilisĂ© par Ghidra pour construire du code dĂ©compilĂ©). En tant que langage destinĂ© Ă dĂ©crire les instructions du processeur, il prĂ©sente de nombreuses limitations, qui peuvent cependant ĂȘtre arrĂȘtĂ©es en raison du mĂ©canisme d'injection de p-code dans le code java.
github. , SLEIGH . , p-code, . The Ghidra Book: The Definitive Guide.
Eclipse, , Ghidra: GhidraDev GhidraSleighEditor. Ghidra Module Project v8_bytecode. , .

, , The Ghidra Book: The Definitive Guide. .
*.spec â .
*.ldefs â . . *.sla, .
*.pspec â .
*.opinion â ; , opinion : .
*.slaspec, *.sinc â , SLEIGH.
.sla, slaspec-.
, , , , . . .
V8
Jsc-, , c JavaScript Node.Js 8.16.0 bytenode ( Node.Js, npm). , bytenode Node.js . , jsc js:

Node.js , . , ( bytecode-register.cc, bytecode-register.h). v8 Node.js:

, aX , . .

â <this>, aX â , , rN â , . 1- , 2- Wide- 4- ExtraWide-. Wide- :

, SLEIGH , . , 124 rN 125 aX. , . :

, Node.js - . (X aX) . , , .
, , .
CSPEC
, cspec-, github. :
Ghidra . â , , . Ghidra SLEIGH , Intel x86, . , , . , , .
, :
Compiler Specific P-code Interpretation;
Compiler Datatype Organization ( <data_organization>);
Compiler Scoping and Memory Access ( <global>);
Compiler Special Purpose Registers ( <stackpointer>);
Parameter Passing ( <default_proto>).
, , .
<data_organization> <stackpointer> ; <prototype> <default_proto>, . : <input>
, <output>
, <unaffected>
.
, aX. . , register. . , , , , . (space="register"
) <input>
, , 0x14000 (0x14000 , , *.slaspec aX).
(acc), <output>
. , , . <unaffected>
, , , .
, <global>
register 0x2000.

LDEFS
â .ldefs. : ( le), (*.sla, *.pspec,*.cspec), id , Ghidra. - , Node.js, , <language>, *.ldefs , Ghidra.

, , .


PSPEC
( .pspec). processor_spec.rxg ( Ghidra ). - . , .
, ( <processor_spec> ).

SLASPEC
SLEIGH .slaspec.
. , , .

, ( register ram), define space, â define register. offset , , . size. , *.cspec , .

(https://ghidra.re/courses/languages/html/sleigh_constructors.html) , , â . SLEIGH , , , « ». 5 .
Table Header ( )
Display Section ( )
Bit Pattern Sections ( )
Disassembly Actions Section ( )
Semantics Actions Section ( )

, .
Table Header , , ( ).
Display Section â , Ghidra.
Bit Pattern Section â - , «» c ( ).
Disassembly Actions Section - , .
Semantics Actions Section , , .
( instruction), , .
, , . , , . . , ( ), .
, :
^ â / , ;
ââ â , , ;
, ;
( - , , #, ).


. , . , . . , :

tokenMaxSize 8. , - . , , , . : start- endBitNumX 0 tokenMaxSize-1 startBitNumX <= endBitNumX.
v8 , . , , «&» «|».
: , , , «» , .
, . , . v8, ( Wide- ExtraWide- , , ). :

, op , Illegal Nop, :

«0xa7» Ghidra Illegal, . unimpl. , , . Nop , , . Nop Node.js , SwitchOnSmiNoFeedback, .

: LdaSmi, (acc ), AddSmi, c .
bytecodes.h Node.js, operand, . , (. AddSmi).

- LdaSmi [-02]. , , disassembly action ( , ).
AddSmi , op, , «;» operand. . , . , , (, , ).

«;» , , , ( ), .
PCode « » Ghidra. - , p-code.


v8 , lda, . acc . , acc, , .


return, , , :

, , . Mul, , .
« » , , , «» . kReg 8 . attach variables 0b 11111111b ( kReg) . , , , 0xfb (11111011b), kReg r0.

, kReg , :


interpreter-generator.cc Node.js. kReg , Table Header src. â export. p-code, export , «» src. Ghidra .

, :

goto
. SLEIGH goto. , kUImm, . disassembly action rel. inst_start SLEIGH .

SLEIGH . , ( ), , ( , p-code ), .

«» dest. *[ram]:4 rel , 4 rel. rel ram. «*» SLEIGH , ( Dynamic References).
[ram] ( ), . p-code, ram.


JumpIfFalse - . SLEIGH goto. js False , , pspec , , . , .


inst_start . TestGreaterThan, goto (<true> ) inst_next. : , , . .
goto inst_next . , «s>», . .


. .
, (. ), . v8, . , 4 CallProperty2 , . :CallProperty2 kReg, kReg, kReg, kReg, [kIdx] Sleigh . - :

, , . , callable, receiver, arg1 arg2 - attach :

kReg . â .
CallProperty2 , call [callable];, . v8 aX ( cspec). , , (, , sinc-, x86). , . Ghidra, . , - , . :

( : sp , ) CallUndefinedReceiver1:

, , java-. , , , SLEIGH. p-code .
, , . , acc , , . , , ( CallVariadicCallOther « » ). define pcodeop OperationName , .

p-code- : callotherfixup
cspec- .
java- , :

. bytenode jsc- js:

jsc- Ghidra. - , Ghidra , eclipse , . : sleigh .
, . .
, . 010. D F, . :

( SLEIGH), . , ( SLEIGH cpool) LdaGlobal. ( ):

, , JavaScript, , .slaspec ( .sinc). , p-code, , p-code. p-code .
v8 , / . . , , .

, , : ForInPrepare r9, r10!3. , , , , , .
,
. . , ARM: ( , - ).

, . , . . , , , , .
, CallProperty , «» , , . , : rangeSrc rangeDst. rangeSrc â , , rangeDst «» . rangeDst , : aX rX .
. «=», , disassembly action. - . , , , aX, rX, . : , , , .
. . , (contextreg ).
, , ( ), . counter offStart , .
, .

, , , - disassembly action. rangeSrc, , disassembly action offStart, â counter. «{».
, v8 range_size: , . rangeSrc .

rangeDst 5 .
a0 counter 0 ( ).

r0 counter 0 ( ).

offStart a0, disassembly action counter , offStart , rangedst1.

offStart r0, disassembly action counter offStart , rangedst1.

, rangedst1( , , , ).

. rangeDstN, N â , , aN/rN.
. rangeSrc , , rangeDst - , . epsilon, .
rangeDst, rangeDst1, rangeDst2, . , github. , rangeDst rangeDstX, , â , .

, . «&» «|».

CallProperty :

:

, , CallVariadicCallOther. github java- p-code. p-code call ( Node.js, , â ). slaspec, , , :

, :

rangeDst ( r7 ) , console.log(1,2,3,4,5,6). bytenode . 0x167, â 0x18b.
, , , , - ( , , , ).
, rangeDst , ( , 2 4 ):

, : , . , , . , , , . , , . , SLEIGH.
Node.js , , .
:
:
https://ghidra.re/courses/languages/html/sleigh.html â SLEIGH.
https://github.com/NationalSecurityAgency/ghidra/tree/master/Ghidra/Framework/SoftwareModeling/data/languages â *.cspec, *.pspec, *.opinion, *.ldefs.
https://spinsel.dev/2020/06/17/ghidra-brainfuck-processor-1.html â brainfuck Ghidra.
https://github.com/PositiveTechnologies/ghidra_nodejs â Ghidra .