From 63f620972074c91467b52bebe951bdbf8871fd0d Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:18:18 +0100 Subject: [PATCH 01/16] Add getpid option --- src/eval.cpp | 11 ++++++- src/opendevnull.cpp | 80 ++++++++++++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/eval.cpp b/src/eval.cpp index 1400eec..681746a 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -10,6 +10,10 @@ int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode) { return syscall_no_intercept(SYS_openat, dirfd, cpath, flags, mode); } +int hook_getpid() { + return 42; +} + inline int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long *result) { @@ -26,6 +30,9 @@ inline int hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, reinterpret_cast(arg1), static_cast(arg2), static_cast(arg3)); break; + case SYS_getpid: + *result = hook_getpid(); + break; default: // ignore any other syscalls, i.e.: pass them on to the kernel // (syscalls forwarded to the kernel that return are logged in @@ -46,7 +53,9 @@ int hook_guard_wrapper(long syscall_number, long arg0, long arg1, long arg2, if (syscall_number == SYS_openat) was_hooked = hook(syscall_number, arg0, arg1, arg2, arg3, arg4, arg5, syscall_return_value); - + else + if (syscall_number == SYS_getpid) + was_hooked = hook(syscall_number); return was_hooked; } diff --git a/src/opendevnull.cpp b/src/opendevnull.cpp index 2596bec..d7e6aba 100644 --- a/src/opendevnull.cpp +++ b/src/opendevnull.cpp @@ -4,36 +4,64 @@ #include #include -int main() { +int main(int argc, char* argv[]) { + // Check if the correct number of arguments is provided + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " --syscall " << std::endl; + return 1; + } + + + // measure time to run in microseconds - struct timespec start, end; - clock_gettime(CLOCK_MONOTONIC, &start); - // open /dev/null 100000 times + + std::string option = argv[1]; + std::string syscall = argv[2]; + + double reps = 1000.0*100000.0; + // Handle the --syscall option + if (option == "--syscall") { + if (syscall == "open") { + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int j = 0; j < 1000; j++) { + for (int i = 0; i < 100000; i++) { + int fd = openat(AT_FDCWD, "/dev/null", O_RDWR, 0); + + close(fd); + } + } + clock_gettime(CLOCK_MONOTONIC, &end); + double time_taken = + (end.tv_sec - start.tv_sec) * 1e6 + (end.tv_nsec - start.tv_nsec) / 1e3; - for (int j = 0; j < 1000; j++) { - for (int i = 0; i < 100000; i++) { - int fd = openat(AT_FDCWD, "/dev/null", O_RDWR, 0); - close(fd); + std::cout << "Mean time taken: " << time_taken / reps << " microseconds" + << std::endl; } - } - clock_gettime(CLOCK_MONOTONIC, &end); - double time_taken = - (end.tv_sec - start.tv_sec) * 1e6 + (end.tv_nsec - start.tv_nsec) / 1e3; - std::cout << "Time taken: " << time_taken << " microseconds" << std::endl; - // mean of the time for a syscall (10000000) - - std::cout << "Mean time taken: " << time_taken / 100000000.0 << " microseconds" - << std::endl; - // Calculate single syscall () - clock_gettime(CLOCK_MONOTONIC, &start); - int fd = openat(AT_FDCWD, "/dev/null", O_RDWR, 0); - close(fd); - clock_gettime(CLOCK_MONOTONIC, &end); - std::cout << "Time taken for single syscall: " - << (end.tv_sec - start.tv_sec) * 1e6 + - (end.tv_nsec - start.tv_nsec) / 1e3 - << " microseconds" << std::endl; + else if (syscall == "getpid") { + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int j = 0; j < 1000; j++) { + for (int i = 0; i < 100000; i++) { + int ret = getpid(); + } + } + clock_gettime(CLOCK_MONOTONIC, &end); + double time_taken = + (end.tv_sec - start.tv_sec) * 1e6 + (end.tv_nsec - start.tv_nsec) / 1e3; + + + std::cout << "Mean time taken: " << time_taken / reps << " microseconds" + << std::endl; + + + + + } return 0; } -- GitLab From a5812966e6aaee40c31c8e06caf629cac8ac39f3 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:23:54 +0100 Subject: [PATCH 02/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..df784f8 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,56 @@ +# This file is a template, and might need editing before it works on your project. +# This is a sample GitLab CI/CD configuration file that should run without any modifications. +# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts, +# it uses echo commands to simulate the pipeline execution. +# +# A pipeline is composed of independent jobs that run scripts, grouped into stages. +# Stages run in sequential order, but jobs within stages run in parallel. +# +# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages +# +# You can copy and paste this template into a new `.gitlab-ci.yml` file. +# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword. +# +# To contribute improvements to CI/CD templates, please follow the Development guide at: +# https://docs.gitlab.com/ee/development/cicd/templates.html +# This specific template is located at: +# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml + +stages: # List of stages for jobs, and their order of execution + - build + - test + - deploy + +build-job: # This job runs in the build stage, which runs first. + stage: build + image: gekkofs/deps:0.9.4-dev + script: + - echo "Compiling the code..." + - cmake --build ${BUILD_PATH} -j $(nproc) --target install + artifacts: + paths: + - ${BUILD_PATH} + - ${INSTALL_PATH} + expire_in: 1 day + +unit-test-job: # This job runs in the test stage. + stage: test # It only starts when the job in the build stage completes successfully. + image: gekkofs/deps:0.9.4-dev + needs: ['build-job'] + script: + - ${INSTALL_PATH}/bin/opendevnull --syscall getpid + - + +lint-test-job: # This job also runs in the test stage. + stage: test # It can run at the same time as unit-test-job (in parallel). + script: + - echo "Linting code... This will take about 10 seconds." + - sleep 10 + - echo "No lint issues found." + +deploy-job: # This job runs in the deploy stage. + stage: deploy # It only runs when *both* jobs in the test stage complete successfully. + environment: production + script: + - echo "Deploying application..." + - echo "Application successfully deployed." -- GitLab From c57dd71bdf4460944d841d4b08bf461a97f6ab3f Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:24:30 +0100 Subject: [PATCH 03/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index df784f8..a7a4353 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -39,7 +39,6 @@ unit-test-job: # This job runs in the test stage. needs: ['build-job'] script: - ${INSTALL_PATH}/bin/opendevnull --syscall getpid - - lint-test-job: # This job also runs in the test stage. stage: test # It can run at the same time as unit-test-job (in parallel). -- GitLab From 6af67f741d37ad3ce70752d112db4a658a8d36f2 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:26:26 +0100 Subject: [PATCH 04/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a7a4353..e76c21d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,6 +21,11 @@ stages: # List of stages for jobs, and their order of execution - test - deploy +variables: + BUILD_PATH: "${CI_PROJECT_DIR}/syscall/build" + INSTALL_PATH: "${CI_PROJECT_DIR}/syscall/install" + LD_LIBRARY_PATH: "${CI_PROJECT_DIR}/deps/install/lib:${CI_PROJECT_DIR}/deps/install/lib64" + build-job: # This job runs in the build stage, which runs first. stage: build image: gekkofs/deps:0.9.4-dev -- GitLab From 8a856da1a86c261fb4f2c43129761e32ae959b9a Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:27:55 +0100 Subject: [PATCH 05/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e76c21d..456ee9e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,6 +31,7 @@ build-job: # This job runs in the build stage, which runs first. image: gekkofs/deps:0.9.4-dev script: - echo "Compiling the code..." + - mkdir -p ${BUILD_PATH} ${INSTALL_PATH} - cmake --build ${BUILD_PATH} -j $(nproc) --target install artifacts: paths: -- GitLab From 3421e5f14be05726b9991a4543311f161c3c3289 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:30:03 +0100 Subject: [PATCH 06/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 456ee9e..7d4a5ad 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,12 +25,17 @@ variables: BUILD_PATH: "${CI_PROJECT_DIR}/syscall/build" INSTALL_PATH: "${CI_PROJECT_DIR}/syscall/install" LD_LIBRARY_PATH: "${CI_PROJECT_DIR}/deps/install/lib:${CI_PROJECT_DIR}/deps/install/lib64" + CCACHE_DIR: "${CI_PROJECT_DIR}/ccache" + build-job: # This job runs in the build stage, which runs first. stage: build image: gekkofs/deps:0.9.4-dev script: - echo "Compiling the code..." + - ccache --zero-stats + - /usr/sbin/update-ccache-symlinks + - export PATH="/usr/lib/ccache:$PATH" - mkdir -p ${BUILD_PATH} ${INSTALL_PATH} - cmake --build ${BUILD_PATH} -j $(nproc) --target install artifacts: @@ -38,6 +43,10 @@ build-job: # This job runs in the build stage, which runs first. - ${BUILD_PATH} - ${INSTALL_PATH} expire_in: 1 day + cache: + key: ccache-gekko + paths: + - $CCACHE_DIR unit-test-job: # This job runs in the test stage. stage: test # It only starts when the job in the build stage completes successfully. -- GitLab From 9af32c6aaf62523a84ad72007cfe33d6f2b94bc5 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:32:11 +0100 Subject: [PATCH 07/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7d4a5ad..017a166 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -44,7 +44,7 @@ build-job: # This job runs in the build stage, which runs first. - ${INSTALL_PATH} expire_in: 1 day cache: - key: ccache-gekko + key: ccache-syscall paths: - $CCACHE_DIR @@ -54,17 +54,3 @@ unit-test-job: # This job runs in the test stage. needs: ['build-job'] script: - ${INSTALL_PATH}/bin/opendevnull --syscall getpid - -lint-test-job: # This job also runs in the test stage. - stage: test # It can run at the same time as unit-test-job (in parallel). - script: - - echo "Linting code... This will take about 10 seconds." - - sleep 10 - - echo "No lint issues found." - -deploy-job: # This job runs in the deploy stage. - stage: deploy # It only runs when *both* jobs in the test stage complete successfully. - environment: production - script: - - echo "Deploying application..." - - echo "Application successfully deployed." -- GitLab From 9b4e75a50783a632fce49780bd1ad1e2cf3096b6 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:34:38 +0100 Subject: [PATCH 08/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 017a166..015a712 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -27,6 +27,7 @@ variables: LD_LIBRARY_PATH: "${CI_PROJECT_DIR}/deps/install/lib:${CI_PROJECT_DIR}/deps/install/lib64" CCACHE_DIR: "${CI_PROJECT_DIR}/ccache" +image: gekkofs/core:0.9.4-dev build-job: # This job runs in the build stage, which runs first. stage: build -- GitLab From 9d5cd9148eb43509ba851b94bb20819d7a28f287 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:37:31 +0100 Subject: [PATCH 09/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 015a712..f51e408 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,7 +38,8 @@ build-job: # This job runs in the build stage, which runs first. - /usr/sbin/update-ccache-symlinks - export PATH="/usr/lib/ccache:$PATH" - mkdir -p ${BUILD_PATH} ${INSTALL_PATH} - - cmake --build ${BUILD_PATH} -j $(nproc) --target install + - cd ${BUILD_PATH} + - cmake ${CI_PROJECT_DIR} -j $(nproc) --target install artifacts: paths: - ${BUILD_PATH} -- GitLab From 3e51293b1d773b0fa6756e34f5a0833f0bdc195d Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:38:12 +0100 Subject: [PATCH 10/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f51e408..3accb65 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -39,7 +39,8 @@ build-job: # This job runs in the build stage, which runs first. - export PATH="/usr/lib/ccache:$PATH" - mkdir -p ${BUILD_PATH} ${INSTALL_PATH} - cd ${BUILD_PATH} - - cmake ${CI_PROJECT_DIR} -j $(nproc) --target install + - cmake ${CI_PROJECT_DIR} + - make -j $(nproc) install artifacts: paths: - ${BUILD_PATH} -- GitLab From c066cac10573b2fb46ad39858e8f3390600a84b3 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:39:24 +0100 Subject: [PATCH 11/16] hook signature --- src/eval.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/eval.cpp b/src/eval.cpp index 681746a..8e6177e 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -55,7 +55,8 @@ int hook_guard_wrapper(long syscall_number, long arg0, long arg1, long arg2, syscall_return_value); else if (syscall_number == SYS_getpid) - was_hooked = hook(syscall_number); + was_hooked = hook(syscall_number, arg0, arg1, arg2, arg3, arg4, arg5, + syscall_return_value); return was_hooked; } -- GitLab From 5441e5bf1f3ce24a7d3e6c6761952de9f539d540 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:40:31 +0100 Subject: [PATCH 12/16] missing } --- src/opendevnull.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/opendevnull.cpp b/src/opendevnull.cpp index d7e6aba..3198caa 100644 --- a/src/opendevnull.cpp +++ b/src/opendevnull.cpp @@ -57,11 +57,8 @@ int main(int argc, char* argv[]) { std::cout << "Mean time taken: " << time_taken / reps << " microseconds" << std::endl; - - - - } + } return 0; } -- GitLab From 8875660ed485fee66ac6981d0266baa4ea210af4 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:42:32 +0100 Subject: [PATCH 13/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3accb65..3eab227 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -57,3 +57,9 @@ unit-test-job: # This job runs in the test stage. needs: ['build-job'] script: - ${INSTALL_PATH}/bin/opendevnull --syscall getpid + artifacts: + expire_in: 1 week + when: always + paths: + - ${BUILD_PATH} + -- GitLab From 8b20a17c6c19cf16b4da5aff50ee69419e2a1e93 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:44:10 +0100 Subject: [PATCH 14/16] looking for install --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3eab227..d5f0daf 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -56,6 +56,7 @@ unit-test-job: # This job runs in the test stage. image: gekkofs/deps:0.9.4-dev needs: ['build-job'] script: + - ls -ltrh ${INSTALL_PATH} - ${INSTALL_PATH}/bin/opendevnull --syscall getpid artifacts: expire_in: 1 week -- GitLab From ac73d412a4b02f1b69665dc4f57a644516158e5c Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:47:42 +0100 Subject: [PATCH 15/16] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d5f0daf..e5a0361 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -57,7 +57,8 @@ unit-test-job: # This job runs in the test stage. needs: ['build-job'] script: - ls -ltrh ${INSTALL_PATH} - - ${INSTALL_PATH}/bin/opendevnull --syscall getpid + - ${BUILD_PATH}/opendevnull --syscall getpid + - LD_PRELOAD=${BUILD_PATH}/eval_intercept.so ${BUILD_PATH}/opendevnull --syscall getpid artifacts: expire_in: 1 week when: always -- GitLab From 71d67c53756fcb49d67f1403ab2fc4a7e2406981 Mon Sep 17 00:00:00 2001 From: Ramon Nou Date: Mon, 10 Mar 2025 17:50:41 +0100 Subject: [PATCH 16/16] full --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e5a0361..90a0ca0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -58,7 +58,7 @@ unit-test-job: # This job runs in the test stage. script: - ls -ltrh ${INSTALL_PATH} - ${BUILD_PATH}/opendevnull --syscall getpid - - LD_PRELOAD=${BUILD_PATH}/eval_intercept.so ${BUILD_PATH}/opendevnull --syscall getpid + - LD_PRELOAD=${BUILD_PATH}/libeval_intercept.so ${BUILD_PATH}/opendevnull --syscall getpid artifacts: expire_in: 1 week when: always -- GitLab