From 19cfa8020a048251ebe5c22ed8b5980854af7d52 Mon Sep 17 00:00:00 2001 From: Francisco Javier Honduvilla Coto Date: Tue, 23 Apr 2024 16:41:26 +0100 Subject: [PATCH] wip vmtests 2.0 --- ...upload.yml => static-build-and-upload.yml} | 0 .github/workflows/vmtests.yml | 34 ++++++++ .gitignore | 1 + flake.nix | 6 ++ src/profiler.rs | 23 +++--- vm.nix | 78 +++++++++++++++++++ vmtest.toml | 19 +++++ 7 files changed, 152 insertions(+), 9 deletions(-) rename .github/workflows/{upload.yml => static-build-and-upload.yml} (100%) create mode 100644 .github/workflows/vmtests.yml create mode 100644 vm.nix create mode 100644 vmtest.toml diff --git a/.github/workflows/upload.yml b/.github/workflows/static-build-and-upload.yml similarity index 100% rename from .github/workflows/upload.yml rename to .github/workflows/static-build-and-upload.yml diff --git a/.github/workflows/vmtests.yml b/.github/workflows/vmtests.yml new file mode 100644 index 0000000..1094d0c --- /dev/null +++ b/.github/workflows/vmtests.yml @@ -0,0 +1,34 @@ +name: vmtests +on: + pull_request: + push: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + vmtests: + runs-on: ubuntu-22.04 + permissions: + id-token: write + contents: read + + steps: + - uses: actions/checkout@main + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + + - name: Install system dependencies + run: | + export DEBIAN_FRONTEND=noninteractive + sudo apt-get update && sudo apt-get -y install --no-install-recommends qemu-system-x86 qemu-guest-agent + + - name: Set up nix dev env + run: nix develop --command echo 0 + + - name: Build `lightswitch` + run: nix develop --ignore-environment --command bash -c 'cargo build' + + - name: Run kernel tests + run: nix develop --command 'vmtest' \ No newline at end of file diff --git a/.gitignore b/.gitignore index 142cfac..2ec4df3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target flame.svg src/bpf/*_skel.rs +.vmtest.log \ No newline at end of file diff --git a/flake.nix b/flake.nix index c8a4a13..0ab9f7d 100644 --- a/flake.nix +++ b/flake.nix @@ -53,6 +53,12 @@ # snapshot testing plugin binary cargo-insta # ocamlPackages.magic-trace + qemu + (import ./vm.nix { inherit pkgs; }).vmtest + (import ./vm.nix { inherit pkgs; }).kernel_5_15 + (import ./vm.nix { inherit pkgs; }).kernel_6_0 + (import ./vm.nix { inherit pkgs; }).kernel_6_2 + (import ./vm.nix { inherit pkgs; }).kernel_6_6 ]; LIBCLANG_PATH = lib.makeLibraryPath [ llvmPackages_16.libclang ]; diff --git a/src/profiler.rs b/src/profiler.rs index eb1dd51..f42da45 100644 --- a/src/profiler.rs +++ b/src/profiler.rs @@ -588,6 +588,8 @@ impl Profiler<'_> { } } + // Adds a mapping key to the LPM trie. This could either be an executable mapping + // or just a pid with a mapping set to zero and the appropriate prefix. fn add_bpf_mapping( &mut self, key: &exec_mappings_key, @@ -910,17 +912,11 @@ impl Profiler<'_> { if have_unwind_info { self.native_unwind_state.dirty = true; - // Add entry just with the pid to signal processes that we already know about. - let key = exec_mappings_key::new( - pid.try_into().unwrap(), - 0x0, - 32, // pid bits - ); - self.add_bpf_mapping(&key, &mapping_t::default()).unwrap(); - // Add process info for mapping in mappings { - for address_range in summarize_address_range(mapping.begin, mapping.end - 1) { + let lpm_range = summarize_address_range(mapping.begin, mapping.end - 1); + debug!("Adding {} entries to the LPM trie", lpm_range.len()); + for address_range in lpm_range { let key = exec_mappings_key::new( pid.try_into().unwrap(), address_range.addr, @@ -930,6 +926,15 @@ impl Profiler<'_> { self.add_bpf_mapping(&key, &mapping).unwrap(); } } + + // Once all the executable mappings are updated, add entry just with the pid + // to signal that is a known process. + let key = exec_mappings_key::new( + pid.try_into().unwrap(), + 0x0, + 32, // number of bits for pids + ); + self.add_bpf_mapping(&key, &mapping_t::default()).unwrap(); } } diff --git a/vm.nix b/vm.nix new file mode 100644 index 0000000..173f4b2 --- /dev/null +++ b/vm.nix @@ -0,0 +1,78 @@ +{ pkgs }: +{ + kernel_5_15 = pkgs.stdenv.mkDerivation { + name = "download-kernel-5.15"; + src = pkgs.fetchurl { + url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v5.15-fedora38"; + hash = "sha256-nq8W72vuNKCgO1OS6aJtAfg7AjHavRZ7WAkP7X6V610="; + }; + dontUnpack = true; + installPhase = '' + mkdir -p $out + cp -r $src $out/bzImage + ''; + }; + + kernel_6_0 = pkgs.stdenv.mkDerivation { + name = "download-kernel-6.0"; + src = pkgs.fetchurl { + url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.0-fedora38"; + hash = "sha256-ZBBQ0yVUn+Isd2b+a32oMEbNo8T1v46P3rEtZ+1j9Ic="; + }; + dontUnpack = true; + installPhase = '' + mkdir -p $out + cp -r $src $out/bzImage + ''; + }; + + kernel_6_2 = pkgs.stdenv.mkDerivation { + name = "download-kernel-6.2"; + src = pkgs.fetchurl { + url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.2-fedora38"; + hash = "sha256-YO2HEIWTuEEJts9JrW3V7UVR7t4J3+8On+tjdELa2m8="; + }; + dontUnpack = true; + installPhase = '' + mkdir -p $out + cp -r $src $out/bzImage + ''; + }; + + kernel_6_6 = pkgs.stdenv.mkDerivation { + name = "download-kernel-6.6"; + src = pkgs.fetchurl { + url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.6-fedora38"; + hash = "sha256-6Fu16SPBITP0sI3lapkckZna6GKBn2hID038itt82jA="; + }; + dontUnpack = true; + installPhase = '' + mkdir -p $out + cp -r $src $out/bzImage + ''; + }; + + vmtest = pkgs.rustPlatform.buildRustPackage { + name = "vmtest"; + src = pkgs.fetchFromGitHub { + owner = "danobi"; + repo = "vmtest"; + rev = "51f11bf301fea054342996802a16ed21fb5054f4"; + sha256 = "sha256-qtTq0dnDHi1ITfQzKrXz+1dRMymAFBivWpjXntD09+A="; + }; + cargoHash = "sha256-SHjjCWz4FVVk1cczkMltRVEB3GK8jz2tVABNSlSZiUc="; + # nativeCheckInputs = [ pkgs.qemu ]; + + # There are some errors trying to access `/build/source/tests/*`. + doCheck = false; + + meta = with pkgs.lib; { + description = "Helps run tests in virtual machines"; + homepage = "https://github.com/danobi/vmtest/"; + license = licenses.asl20; + mainProgram = ""; + maintainers = with maintainers; [ ]; + platforms = platforms.linux; + }; + }; +} diff --git a/vmtest.toml b/vmtest.toml new file mode 100644 index 0000000..d0ba1bc --- /dev/null +++ b/vmtest.toml @@ -0,0 +1,19 @@ +[[target]] +name = "Fedora 5.15" +kernel = "/nix/store/cg9wq48zz2dbvayh7sigr5smaf4dwcxp-download-kernel-5.15/bzImage" +command = "/mnt/vmtest/target/debug/lightswitch --duration 0" + +[[target]] +name = "Fedora 6.0" +kernel = "/nix/store/hqvx9h3s3nclvjd1wkdg4j1j4z4rxm9g-download-kernel-6.0/bzImage" +command = "/mnt/vmtest/target/debug/lightswitch --duration 0" + +[[target]] +name = "Fedora 6.2" +kernel = "/nix/store/79wzcqy12fncdf6pk1fj627ggh7v0nc7-download-kernel-6.2/bzImage" +command = "/mnt/vmtest/target/debug/lightswitch --duration 0" + +[[target]] +name = "Fedora 6.6" +kernel = "/nix/store/77ixckavs2qidx1pglmlxsj6bfvjqijb-download-kernel-6.6/bzImage" +command = "/mnt/vmtest/target/debug/lightswitch --duration 0" \ No newline at end of file