Skip to content

Commit

Permalink
Add kernel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javierhonduco committed Apr 23, 2024
1 parent c5b8525 commit 4521399
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 9 deletions.
File renamed without changes.
34 changes: 34 additions & 0 deletions .github/workflows/vmtests.yml
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
flame.svg
src/bpf/*_skel.rs
.vmtest.log
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
# snapshot testing plugin binary
cargo-insta
# ocamlPackages.magic-trace
(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 ];
Expand Down
23 changes: 14 additions & 9 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
}

Expand Down
78 changes: 78 additions & 0 deletions vm.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}
19 changes: 19 additions & 0 deletions vmtest.toml
Original file line number Diff line number Diff line change
@@ -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/p8xc9gqdkpfjpmcb9ja2gx6g6bd9cby7-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"

0 comments on commit 4521399

Please sign in to comment.