From 25b4fdac6f761918cf5713ad16e3e90ab3a47ae7 Mon Sep 17 00:00:00 2001 From: Artem Khramov Date: Sun, 8 Aug 2021 18:59:13 +0300 Subject: [PATCH] #431 Handle offsetted Elf segment case + FreeBSD CI An Elf segment may have a non-zero offset and that may result in subtraction overflow when we calculating the symbol addresses. Apart from that, CI workflows are run within GH Actions with FreeBSD being notable exception. While FreeBSD is not supported on actions, the conventional workaround is to run builds on macos runner using a vagrant box. This change - Handles the subtraction overflow case by defaulting to zero. - removes cirrus config and sets up a FreeBSD GH action job. - Also bumps proc-maps version in attempt to resolve #431 --- .cirrus.yml | 20 ---- .github/workflows/build.yml | 25 +++++ Cargo.lock | 184 ++++++++++++++++++++++++++++-------- Cargo.toml | 2 +- Vagrantfile | 35 +++++++ src/binary_parser.rs | 4 +- src/python_spy.rs | 1 + 7 files changed, 209 insertions(+), 62 deletions(-) delete mode 100644 .cirrus.yml create mode 100644 Vagrantfile diff --git a/.cirrus.yml b/.cirrus.yml deleted file mode 100644 index 6a4dd733..00000000 --- a/.cirrus.yml +++ /dev/null @@ -1,20 +0,0 @@ -freebsd_instance: - image_family: freebsd-12-1 - -freebsd_task: - name: $TOOLCHAIN x86_64-unknown-freebsd - env: - matrix: - # we seem to be broken on rust 1.53 https://github.com/benfred/py-spy/pull/407 - - TOOLCHAIN: 1.52.1 - GITHUB_TOKEN: ENCRYPTED[67283c0b5c9880ac3b7d8fb0335c4b24f95c62dab30b5379dca192600801c380a41f7436c7daaebfaa8f1381237a8412] - setup_script: - - pkg install -y curl bash python - - curl https://sh.rustup.rs -sSf --output rustup.sh - - sh rustup.sh -y --default-toolchain $TOOLCHAIN - build_script: - - $HOME/.cargo/bin/rustup run $TOOLCHAIN cargo build --verbose --release - test_script: - - $HOME/.cargo/bin/rustup run $TOOLCHAIN cargo test --verbose --release - publish_script: - - bash ./ci/publish_freebsd.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9c7c74db..58f56781 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,6 +13,31 @@ env: CARGO_TERM_COLOR: always jobs: + build-freebsd-amd64: + runs-on: macos-latest + + strategy: + matrix: + box: + - fbsd_13_0 + - fbsd_12_2 + + steps: + - name: Cache vagrant boxes + id: cache-vagrant-boxes + uses: actions/cache@v2 + with: + path: ~/.vagrant.d/ + key: ${{ matrix.box }}-vm-cache + - uses: actions/checkout@v2 + - name: Set up vagrant + run: vagrant up ${{ matrix.box }} + - name: Build + run: vagrant ssh ${{ matrix.box }} -- "cd /vagrant; cargo build --verbose" + - name: Test + run: vagrant ssh ${{ matrix.box }} -- "cd /vagrant; cargo test" + + build-linux-armv7: runs-on: [self-hosted, linux, arm] steps: diff --git a/Cargo.lock b/Cargo.lock index b5c0f6de..165c1ea4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.14.1" @@ -73,6 +75,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "anyhow" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" + [[package]] name = "arrayvec" version = "0.4.12" @@ -122,10 +130,34 @@ dependencies = [ "kernel32-sys", "libc", "log", - "mach 0.3.2", + "mach", "winapi 0.2.8", ] +[[package]] +name = "bindgen" +version = "0.53.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c72a978d268b1d70b0e963217e60fdabd9523a941457a6c42a7315d15c7e89e5" +dependencies = [ + "bitflags", + "cexpr", + "cfg-if 0.1.10", + "clang-sys", + "clap", + "env_logger 0.7.1", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "which", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -144,6 +176,15 @@ version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" +[[package]] +name = "cexpr" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -169,6 +210,17 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "clang-sys" +version = "0.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6837df1d5cba2397b835c8530f51723267e16abbf83892e9e5af4f0e5dd10a" +dependencies = [ + "glob", + "libc", + "libloading", +] + [[package]] name = "clap" version = "2.33.3" @@ -295,6 +347,19 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime 1.3.0", + "log", + "regex", + "termcolor", +] + [[package]] name = "env_logger" version = "0.8.2" @@ -311,23 +376,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" dependencies = [ "atty", - "humantime", + "humantime 2.0.1", "log", "regex", "termcolor", ] -[[package]] -name = "errno" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e2b2decb0484e15560df3210cf0d78654bb0864b2c138977c07e377a1bae0e2" -dependencies = [ - "kernel32-sys", - "libc", - "winapi 0.2.8", -] - [[package]] name = "errno" version = "0.2.7" @@ -477,6 +531,15 @@ dependencies = [ "libc", ] +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "humantime" version = "2.0.1" @@ -551,6 +614,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.98" @@ -558,20 +627,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" [[package]] -name = "libm" -version = "0.2.1" +name = "libloading" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" +checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" +dependencies = [ + "cc", + "winapi 0.3.9", +] [[package]] -name = "libproc" -version = "0.3.2" +name = "libm" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58d23e79d5a9fc3f86a75bcd31a5418f27104518aca33615802b0373380beab4" -dependencies = [ - "errno 0.1.8", - "libc", -] +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" [[package]] name = "libproc" @@ -579,7 +648,7 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15fb50befee2d3be15b38c93ef79ba22ecbd667874bf692309ffdff179282b8d" dependencies = [ - "errno 0.2.7", + "errno", "libc", ] @@ -601,15 +670,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "mach" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd13ee2dd61cc82833ba05ade5a30bb3d63f7ced605ef827063c63078302de9" -dependencies = [ - "libc", -] - [[package]] name = "mach" version = "0.3.2" @@ -691,6 +751,16 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "nom" +version = "5.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +dependencies = [ + "memchr", + "version_check", +] + [[package]] name = "num-format" version = "0.4.0" @@ -753,6 +823,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "plain" version = "0.2.3" @@ -806,14 +882,15 @@ dependencies = [ [[package]] name = "proc-maps" -version = "0.1.7" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "405a565c252559e556e2539c8300d01a468053cc6bc12356d2d38441f5db5379" +checksum = "54abe6d3c62735694c58def54d333b0023864993f9fc496dcf58efeefb7c2fa6" dependencies = [ - "failure", + "anyhow", + "bindgen", "libc", - "libproc 0.3.2", - "mach 0.1.2", + "libproc", + "mach", "winapi 0.3.9", ] @@ -849,6 +926,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quick-xml" version = "0.20.0" @@ -993,9 +1076,9 @@ dependencies = [ "goblin", "lazy_static", "libc", - "libproc 0.9.1", + "libproc", "log", - "mach 0.3.2", + "mach", "mach_o_sys", "memmap", "nix 0.22.0", @@ -1029,6 +1112,12 @@ version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "ryu" version = "1.0.5" @@ -1083,6 +1172,12 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" + [[package]] name = "smallvec" version = "1.6.1" @@ -1287,6 +1382,15 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "which" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" +dependencies = [ + "libc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index da6bdd70..c07bacb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ log = "0.4" lru = "0.6" regex = "1" tempfile = "3.0.3" -proc-maps = "0.1.7" +proc-maps = "0.1.9" memmap = "0.7.0" cpp_demangle = "0.3" serde = {version="1.0", features=["rc"]} diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 00000000..27f6cd9d --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,35 @@ +# This code is taken from the Vagrantfile from libjail-rs +# https://github.com/fubarnetes/libjail-rs/blob/727353bd6565c5e7a9be2664258d0197a1c8bb35/Vagrantfile +# licensed under BSD-3 Clause License: +# BSD 3-Clause License + +# Copyright (c) 2018, Fabian Freyer All rights reserved. + +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +# * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +# * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Vagrant.configure("2") do |config| + config.vm.define "fbsd_13_0" do |fbsd_13_0| + fbsd_13_0.vm.box = "freebsd/FreeBSD-13.0-RELEASE" + end + + config.vm.define "fbsd_12_2" do |fbsd_12_2| + fbsd_12_2.vm.box = "freebsd/FreeBSD-12.2-STABLE" + end + + config.vm.synced_folder ".", "/vagrant", type: "rsync", + rsync__exclude: ".git/", + rsync__auto: true + + config.vm.provision "shell", inline: <<-SHELL + pkg bootstrap + pkg install -y rust python llvm + SHELL +end diff --git a/src/binary_parser.rs b/src/binary_parser.rs index 512f19f8..357101f4 100644 --- a/src/binary_parser.rs +++ b/src/binary_parser.rs @@ -101,7 +101,9 @@ pub fn parse_binary(_pid: remoteprocess::Pid, filename: &str, addr: u64, size: u header.p_flags & goblin::elf::program_header::PF_X != 0) .ok_or_else(|| format_err!("Failed to find executable PT_LOAD program header in {}", filename))?; - let offset = offset - program_header.p_vaddr; + // p_vaddr may be larger than the map address in case when the header has an offset and + // the map address is relatively small. In this case we can default to 0. + let offset = offset.checked_sub(program_header.p_vaddr).unwrap_or(0); for sym in elf.syms.iter() { let name = elf.strtab[sym.st_name].to_string(); diff --git a/src/python_spy.rs b/src/python_spy.rs index e8af5749..b08dbd9f 100644 --- a/src/python_spy.rs +++ b/src/python_spy.rs @@ -724,6 +724,7 @@ impl PythonProcessInfo { let filename = process.exe() .context("Failed to get process executable name. Check that the process is running.")?; + println!("{:?}", filename); #[cfg(windows)] let filename = filename.to_lowercase(); #[cfg(windows)]