From 4d822fd15d6daf79115604710af4e9ce447ff008 Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Thu, 7 Jan 2021 23:27:52 +0100 Subject: [PATCH] Exercises: Add interceptor library --- exercises/README.md | 28 ++++++++++++++++++++++++++++ exercises/task22/CMakeLists.txt | 12 ++++++++++++ exercises/task22/executable.cpp | 9 +++++++++ exercises/task22/libFoo/foo.cpp | 23 +++++++++++++++++++++++ exercises/task22/libFoo/foo.hpp | 12 ++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 exercises/task22/CMakeLists.txt create mode 100644 exercises/task22/executable.cpp create mode 100644 exercises/task22/libFoo/foo.cpp create mode 100644 exercises/task22/libFoo/foo.hpp diff --git a/exercises/README.md b/exercises/README.md index aec633a..217ea87 100644 --- a/exercises/README.md +++ b/exercises/README.md @@ -329,3 +329,31 @@ Explain all parts like it's done in the lecture. Take a look at [Boost Operators](https://www.boost.org/doc/libs/1_74_0/libs/utility/operators.htm). Try to understand *why* the *curiously recurring template pattern (CRTP)* is used. + +## Task 22 + +You are given the code and build instructions for a shared library and an executable which uses the shared library. +The shared library features two functions `random_number` and `just_a_print` inside the `foo` namespace. + +Your task is to create an *interceptor* library: + +- `random_number` should be replaced with a function that always returns `4` for improved determinism +- `just_a_print` should be wrapped so that some text is printed before and after the original function is executed + +Running the executable with and without the interceptor library could look like this: + +``` +$ ./executable +Random Number: 880806932 + +Just a print to stdout, nothing else + +$ LD_PRELOAD=$PWD/interceptor.so ./executable +Random Number: 4 + +some text before +Just a print to stdout, nothing else +some text after +``` + +**Hint:** For Linux, have a look at the related man-pages, like *ld-linux(8)*. diff --git a/exercises/task22/CMakeLists.txt b/exercises/task22/CMakeLists.txt new file mode 100644 index 0000000..fc5961f --- /dev/null +++ b/exercises/task22/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.8) +project(example LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_library(foo SHARED libFoo/foo.cpp) +target_include_directories(foo PUBLIC libFoo) + +add_executable(executable executable.cpp) +target_link_libraries(executable foo) diff --git a/exercises/task22/executable.cpp b/exercises/task22/executable.cpp new file mode 100644 index 0000000..07fc5c4 --- /dev/null +++ b/exercises/task22/executable.cpp @@ -0,0 +1,9 @@ +#include + +#include "foo.hpp" + +int main() +{ + std::cout << "Random Number: " << foo::random_number() << "\n\n"; + foo::just_a_print(); +} diff --git a/exercises/task22/libFoo/foo.cpp b/exercises/task22/libFoo/foo.cpp new file mode 100644 index 0000000..803503b --- /dev/null +++ b/exercises/task22/libFoo/foo.cpp @@ -0,0 +1,23 @@ +#include "foo.hpp" + +#include +#include +#include + +namespace foo { + +int random_number() +{ + static std::random_device device; + static std::mt19937 rng(device()); + static std::uniform_int_distribution dist(0, std::numeric_limits::max()); + + return dist(rng); +} + +void just_a_print() +{ + std::cout << "Just a print to stdout, nothing else\n"; +} + +} // end namespace foo diff --git a/exercises/task22/libFoo/foo.hpp b/exercises/task22/libFoo/foo.hpp new file mode 100644 index 0000000..82ae5ac --- /dev/null +++ b/exercises/task22/libFoo/foo.hpp @@ -0,0 +1,12 @@ +#ifndef FOO_HPP +#define FOO_HPP + +namespace foo { + +int random_number(); + +void just_a_print(); + +} // end namespace foo + +#endif // FOO_HPP