Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view the assembly code generated from my JavaScript (in Chrome)?

is it somehow possible, to view the final machine code (x86 instruction) that a browser generates from my Javascript? E.g.

--- Raw source ---
function add(a, b){
  return a + b;
}
...

--- Code ---
source_position = 0
kind = FUNCTION
Instructions (size = 456)
0x36953100     0  8b4c2404       mov ecx,[esp+0x4]
0x36953104     4  81f991806049   cmp ecx,0x49608091          ;; object: 0x49608091 <undefined>
0x3695310a    10  750a           jnz 22  (0x36953116)
0x3695310c    12  8b4e13         mov ecx,[esi+0x13]
0x3695310f    15  8b4917         mov ecx,[ecx+0x17]
0x36953112    18  894c2404       mov [esp+0x4],ecx
0x36953116    22  55             push ebp

Thanks!

like image 235
Defarine Avatar asked Dec 19 '19 14:12

Defarine


1 Answers

Your script doesn't transform to machine code directly. JavaScript runs on virtual machine V8 (it's true for chrome and classic nodejs) and you can get VM byte code using:

node --print-bytecode script.js

Then V8 executes and optimizes the byte code and calls external C libraries and OS API (system calls) or WEB API. Final machine code may vary even with the same javascript code (for example before and after optimization).

You can also start Chrome from the command line with --js-flags="--print-bytecode"

UPD:

As @PeterCordes noticed nodejs allow to see Turbofan generated machine code using

node --print-opt-code script.js

Chrome:

--js-flags="--print-opt-code"

Also you can use HTML visualizer like https://github.com/v8/v8/tree/master/tools/turbolizer

like image 152
svltmccc Avatar answered Oct 22 '22 16:10

svltmccc