2

下拉&编译 chromium&v8 代码

 2 years ago
source link: https://kiprey.github.io/2020/11/fetch-chromium/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
  • 由于chromium的多线程、多进程机制较为复杂,因此调试起来较为麻烦,通过源代码层面打log来调试显得十分必要,而且源码级调试可以大幅度降低调试难度。

  • 同时,倘若需要某个特定版本的chromium时,委托他人代为编译也较为不便,

  • 因此,手动编译chromium是十分必要的。在这篇文章中,笔者将自己下拉代码&编译代码的步骤列入其中,仅供参考。

二、前置操作

  • 由于国内神奇的网络环境,我们需要设置一下代理服务

  • 首先在linux端下载shadowsocksr

    git clone [email protected]:shadowsocksrr/shadowsocksr.git

    修改shadowsocksr/user-config.json的内容

    {
    "server": "<server IP>", // 服务器IP
    "server_ipv6": "::",
    "server_port": <server Port>, // 服务器的ssr端口
    "local_address": "127.0.0.1", // 本地地址,这里无需修改
    "local_port": 52001, // 本地用于监听socks5的端口,ssr开启后请求将转发至该端口

    "password": "<server Password>", // 服务器端的ssr密码
    "method": "rc4-md5", // 加密方式
    "protocol": "auth_aes128_md5", // 协议
    "protocol_param": "",
    "obfs": "tls1.2_ticket_auth", // obfs
    "obfs_param": "",
    "speed_limit_per_con": 0,
    "speed_limit_per_user": 0,

    "additional_ports" : {}, // only works under multi-user mode
    "additional_ports_only" : false, // only works under multi-user mode
    "timeout": 120,
    "udp_timeout": 60,
    "dns_ipv6": false,
    "connect_verbose_info": 0,
    "redirect": "",
    "fast_open": false
    }

    然后进入shadowsocksr/shadowsocks/,执行local.py以启动本地socks5

    sudo python local.py

    还有一个server.py文件,主要用于服务器端建立ssr结点。这里我们不涉及这个,因此忽略该文件。

    之后,在本地的local port端口处(按照上面的配置信息,这里应该是52001端口),将会建立一个socks5监听端口。所有发送至该端口的数据将会被转发至远程ssr结点

    使用命令netstat -ntlp可以查看端口信息。

  • socks5建立完成后,我们需要设置http/https代理转发,使得http/https数据可以被转发至socks5中。

    因此我们需要下载privoxy,之后在其配置文件中追加一句指令开启代理转发,最后启动该服务。

    sudo apt-get install privoxy
    echo "forward-socks5 / 127.0.0.1:52001 ." >> /etc/privoxy/config
    sudo service privoxy start
  • 代理服务已经启动完成,现在我们需要设置curl和git使用代理来访问网络。

    export http_proxy=http://127.0.0.1:8118
    export https_proxy=https://127.0.0.1:8118
    git config --global http.proxy http://127.0.0.1:8118
    git config --global https.proxy https://127.0.0.1:8118
  • git代理已经设置完成,现在我们来下载最重要的工具depot_tools,这个工具用于下拉chromium/v8代码

    # clone depot_tools,并且把depot_tools的目录加到PATH环境变量
    # 因为git加了代理所以第一个命令可以成功clone了
    git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
    # /path/to/depot_tools改成depot_tools的目录
    echo 'export PATH=$PATH:"/path/to/depot_tools"' >> ~/.bashrc
    # 重新加载.bashrc配置文件
    source ~/.bashrc

三、chromium代码下拉及编译

  • chromium的代码下拉只要一句命令,非常简便,但必须使用git代理

    fetch chromium

    所有代码差不多有24G左右,下拉代码的过程中,最重要的是网络一定要好!由于git clone不支持断点续传,一旦下拉代码的过程中存在网络波动导致连接中断,那就功亏一篑了。

    下拉chromium的代码只有这一条途径,别想着先下github上的代码再整依赖,这是无用的。

  • 代码下拉好后,安装一下代码编译所需要的依赖

    sudo src/build/install-build-deps.sh

    如果该脚本不适用于当前linux版本,则直接尝试编译代码也可以,只不过有时候会提示某个命令无法执行而中断编译,此时只需要手动安装一下对应软件即可。

  • 之后设置git分支

    # 切换分支,如果编译最新版的话,就不用这行命令
    # 如果是要调洞的话,就要在这里切到有漏洞的那个commit
    git reset --hard [commit hash with vulnerability]
    # 下载依赖
    gclient sync
  • 开始尝试编译

    # 生成配置文件
    # 其中 is_debug = true将会使得在编译chromium时,完整保留调试信息
    # is_component_build = true将会使得编译时产生拆分的众多.so/.dll文件,这样可以降低链接所需要消耗的时间
    # is_asan = true 编译时启动asan
    gn gen out/asan_debug --args="is_debug=true is_component_build=true is_asan = true"
    # 开始编译,预计耗时4个小时
    autoninja -C out/asan_debug chrome
  • 编译完成后,即可启动chromium

    ./out/asan_debug/chrome

    笔者启动chromium时,asan提示odr-violation报错

    # Kiprey @ Kipwn in /usr/class/chromium [14:19:24] C:1
    $ ./src/out/asan_debug/chrome
    =================================================================
    ==189815==ERROR: AddressSanitizer: odr-violation (0x7f44b9504700):
    [1] size=40 'vtable for media::VaapiDmaBufVideoFrameMapper' ../../media/gpu/vaapi/vaapi_dmabuf_video_frame_mapper.cc
    [2] size=40 'vtable for media::VaapiDmaBufVideoFrameMapper' ../../media/gpu/vaapi/vaapi_dmabuf_video_frame_mapper.cc
    These globals were registered at these points:
    [1]:
    #0 0x55f8a95f810d in __asan_register_globals /b/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/lib/asan/asan_globals.cpp:360:3
    #1 0x7f4471d6895b in asan.module_ctor (/usr/class/chromium/src/out/asan_debug/libservice.so+0x2b5595b)

    [2]:
    #0 0x55f8a95f810d in __asan_register_globals /b/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/lib/asan/asan_globals.cpp:360:3
    #1 0x7f44b87abe7b in asan.module_ctor (/usr/class/chromium/src/out/asan_debug/libmedia_gpu.so+0x335e7b)

    ==189815==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0
    SUMMARY: AddressSanitizer: odr-violation: global 'vtable for media::VaapiDmaBufVideoFrameMapper' at ../../media/gpu/vaapi/vaapi_dmabuf_video_frame_mapper.cc
    ==189815==ABORTING

    odr-violation这类错误我们忽略即可,因此我们需要设置一下环境变量ASAN_OPTIONS

    export ASAN_OPTIONS=detect_odr_violation=0

    之后即可正常执行chrome。

四、v8代码下拉及编译

  • v8的代码下拉也很简单,一条命令即可,同样必须使用git代理。

    fetch v8
  • 之后就和编译chromium一样,先设置git分支,再设置编译参数,最后开始编译

    cd v8
    # 如果编译最新版的话,就不用这行命令
    # 如果是要调洞的话,就要在这里切到有漏洞的那个commit
    git reset --hard [commit hash with vulnerability]
    # gclient sync 用来下载一些其他需要的东西,
    # 这个还需要curl的代理,之前也已经在环境变量配置了
    gclient sync
    # 设置编译参数
    tools/dev/v8gen.py x64.debug
    # 编译
    ninja -C out.gn/x64.debug

    如果需要切换特定版本,则使用以下命令

    # 这里的<TagName>指的就是version
    # 查看对应version/tag是否存在
    git tag | grep "<TagName>"
    # 切换至目标version/tag
    git checkout <TagName>
    # 下载对应依赖项
    gclient sync
  • 编译完成后即可执行v8

    ./out.gn/x64.debug/d8
  • v8 还自带了gdb插件,可以让我们更加方便的使用gdb来调试v8。

    ~/.gdbinit内添加以下两行即可使用:

    source /path/to/v8/tools/gdbinit
    source /path/to/v8/tools/gdb-v8-support.py

    有兴趣的话还可以简单阅读一下这两个文件,这样可以更好的了解 v8 插件的使用方式。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK