LoginSignup
11
6

More than 5 years have passed since last update.

4大 JavaScript エンジンを Linux コマンドラインでビルド&実行して逆アセンブルさせる

Last updated at Posted at 2018-10-15

1. はじめに

メジャーな Web ブラウザで使われている4種類の JavaScript エンジン V8 (Chrome), ChakraCore (IE, Edge), SpiderMonkey (Firefox), JavaScriptCore (Safari) をそれぞれ Linux のコマンドラインでビルドして、JavaScript のコードを実行したり、JIT コンパイルされたバイトコードやマシンコードの逆アセンブリを出力する方法をメモしておきます。パフォーマンスの比較や JIT コンパイルされた結果の確認などにご活用ください。

まず、予め共通で必要になる開発環境のパッケージをインストールしておきます。この記事では Fedora Core 上での実行を想定しています。他のディストリビューションで必要なパッケージは、それぞれ参考リンク先で確認してください。

$ sudo dnf group install -y "Development Tools" "C Development Tools and Libraries"

2. V8 (Chrome)

(12月19日更新)
参考:Building V8 from source

  • 準備
$ sudo dnf install -y git depot_tools
  • レポジトリを取得
$ gclient
$ fetch v8
$ cd v8
  • レポジトリを更新
$ git pull
$ gclient sync
  • リリース版をビルド&実行
$ ./build/install-build-deps.sh # 1度だけ
$ tools/dev/gm.py x64.release
$ out/x64.release/d8 [file.js]
  • デバッグ版をビルド&実行
$ ./build/install-build-deps.sh # 1度だけ
$ tools/dev/gm.py x64.debug
$ out/x64.debug/d8 [file.js]
  • バイトコードを逆アセンブル
$ out/x64.release/d8 --print-bytecode [file.js]
  • マシンコードを逆アセンブル
$ out/x64.debug/d8 --print-code [file.js]
$ out/x64.debug/d8 --print-opt-code [file.js]

3. ChakraCore (Edge, IE)

参考:Building ChakraCore

  • 準備
$ sudo dnf install -y git cmake clang gcc gcc-c++ kernel-devel python llvm
$ sudo dnf install -y libuuid-devel lttng-ust-devel.x86_64 libicu-devel.x86_64
$ sudo dnf install -y libstdc++-static.x86_64
  • レポジトリを取得
$ git clone https://github.com/Microsoft/ChakraCore
$ cd ChakraCore
  • リリース版をビルド&実行
$ ./build.sh -j $(nproc)
$ out/Release/ch [file.js]
  • デバッグ版をビルド&実行
$ ./build.sh --debug -j $(nproc)
$ out/Debug/ch [file.js]
  • バイトコードを逆アセンブル
$ out/Debug/ch --dump:bytecode [file.js]
$ out/Debug/ch --AsmDumpMode [file.js]
  • マシンコードを逆アセンブル
$ out/Debug/ch --dump:assembly [file.js]

4. SpiderMonkey (Firefox)

参考: SpiderMonkey Build Documentation

  • 準備
$ wget -O bootstrap.py https://hg.mozilla.org/mozilla-central/raw-file/default/python/mozboot/bin/bootstrap.py
$ python bootstrap.py
$ sudo dnf install -y autoconf213
  • レポジトリを取得
$ hg clone https://hg.mozilla.org/mozilla-central/
$ cd mozilla-central/js/src
  • リリース版をビルド&実行
$ autoconf-2.13
$ mkdir build_OPT.OBJ
$ cd build_OPT.OBJ
$ ../configure
$ make -j $(nproc)
$ dist/bin/js [file.js]
  • デバッグ版をビルド&実行
$ autoconf-2.13
$ mkdir build_DBG.OBJ
$ cd build_DGB.OBJ
$ ../configure --enable-debug --disable-optimize
$ make -j $(nproc)
$ dist/bin/js [file.js]
  • バイトコード及びマシンコードを逆アセンブル
# use debug build
$ dist/bin/js -D [file.js]

5. JavaScriptCore (Safari)

参考:How to Build JavaScriptCore on Your Machine

  • 準備
$ sudo dnf install -y libicu-dev python ruby bison flex cmake ninja-build git gperf
  • レポジトリを取得
$ git clone git://git.webkit.org/WebKit.git
$ cd WebKit
  • リリース版をビルド&実行
$ Tools/Scripts/build-webkit --jsc-only
$ WebKitBuild/Release/bin/jsc [file.js]
  • デバッグ版をビルド&実行
$ Tools/Scripts/build-webkit --jsc-only --debug
$ WebKitBuild/Debug/bin/jsc [file.js]
  • バイトコードを逆アセンブル
$ WebKitBuild/Debug/bin/jsc -d [file.js]
  • マシンコードを逆アセンブル
$ JSC_dumpDisassembly=true WebKitBuild/Release/bin/jsc [file.js]
11
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6