### Install GEF with GDB Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/gef_tool.md This command installs GEF by downloading and executing a setup script. Ensure GDB is installed beforehand. ```bash bash -c "$(curl -fsSL http://gef.blah.cat/sh)" ``` -------------------------------- ### Setup and Environment Activation Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/time_planner_app.md Installs dependencies and activates the virtual environment for the project. Ensure Python, PySide6, Pillow, and PyInstaller are installed. ```powershell .\setup.ps1 .\venv\Scripts\Activate.ps1 pip install -r requirements.txt ``` -------------------------------- ### Main Function with Thread and Channel Setup Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/example_app.md Sets up multi-producer, single-consumer channels and spawns a thread for a temperature sensor. This is the entry point for the example application. ```rust use std::sync::mpsc::{self, Receiver}; use std::thread; use std::time::{Duration, Instant}; fn main() { // Simulate a sensor that continuously sends data for 10 seconds. let (tx_temp, rx_temp): (mpsc::Sender, Receiver) = mpsc::channel(); let (tx_speed, rx_speed): (mpsc::Sender, Receiver) = mpsc::channel(); // Spawn a thread for the temp_sensor let tx_temp_clone = tx_temp.clone(); ``` -------------------------------- ### Install and Build Project Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/README.md Use this command to install dependencies and build the project. ```sh ./run.sh -b ``` -------------------------------- ### Install Specific Package Versions using apt Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Example of using apt within a Dockerfile to install specific versions of packages, ensuring reproducibility. ```Dockerfile # Example of installing specific versions of packages using apt RUN apt-get update \ && apt-get install -y \ = \ = \ ... = \ && rm -rf /var/lib/apt/lists/* ``` -------------------------------- ### Install Third-Party Library in Docker Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Installs a third-party library by downloading, extracting, configuring, building, and installing it within the Docker image. Ensure to replace placeholders with actual library details. ```Dockerfile RUN curl -L -o .tar.gz \ && tar -xzvf .tar.gz \ && cd \ && ./configure \ && make \ && make install \ && cd .. \ && rm -rf .tar.gz ``` -------------------------------- ### Install mdBook Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/README.md Installs the mdBook toolchain. This is a prerequisite for building and serving the book. ```sh cargo install mdbook ``` -------------------------------- ### GEF Debugging Workflow Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/gef_tool.md A step-by-step example demonstrating how to use GEF to debug a program, including setting breakpoints, stepping through code, and inspecting memory. ```bash gdb -q ./buggy_program ``` ```bash break main ``` ```bash run ``` ```bash next ``` ```bash x/gx ``` ```bash heap chunks ``` ```bash search-pattern ``` -------------------------------- ### Install and Initialize afl.rs Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/cybersecurity_utils/fuzzing.md Instructions for installing cargo-afl, creating a new binary project for fuzzing, and setting up dependencies. This method uses AFL for fuzzing. ```sh cd fuzzing # Install cargo-afl cargo install cargo-afl cargo new --bin wc-tool-fuzz-afl-target cd wc-tool-fuzz-afl-target # Modify `fuzzing/wc-tool-fuzz-afl-target/Cargo.toml` by adding: # [dependencies] # afl = "*" # url = "*" # Modify `fuzzing/wc-tool-fuzz-afl-target/src/main.rs` # Build project cargo afl build # Start fuzzing cargo afl fuzz -i in -o out target/debug/wc-tool-fuzz-afl-target ``` -------------------------------- ### Install Static Analysis Tools for x86_64 Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Conditionally installs Node.js, CodeChecker, cpplint, and Infer for x86_64 architecture. Includes downloading, building, and linking. ```dockerfile RUN if [ "$ARCH" = "-amd64" ]; then \ curl -sL https://deb.nodesource.com/setup_16.x | bash - \ && apt-get install -y nodejs \ && git clone --depth 1 https://github.com/Ericsson/CodeChecker.git ${CODE_CHECKER_PATH} \ && cd ${CODE_CHECKER_PATH} \ && make package \ && chmod +x ${CODE_CHECKER_PATH}/build/CodeChecker/bin/report-converter \ && pip install --no-cache-dir cpplint \ && VERSION=1.1.0; \ curl -sSL "https://github.com/facebook/infer/releases/download/v$VERSION/infer-linux64-v$VERSION.tar.xz" \ | tar -xJ -C /opt \ && ln -s "/opt/infer-linux64-v$VERSION/bin/infer" /usr/local/bin/infer \ ; fi ``` -------------------------------- ### Setup Python Virtual Environment Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Creates a Python virtual environment at /opt/venv and adds its bin directory to the PATH. ```dockerfile ENV VIRTUAL_ENV=/opt/venv RUN python3 -m venv $VIRTUAL_ENV ENV PATH="$VIRTUAL_ENV/bin:$PATH" ``` -------------------------------- ### Docker Run Commands for Multi-Container C/C++ Application Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Manually starts individual containers for a C/C++ application, including a database and a web server. This approach is suitable for simpler setups or ad-hoc deployments. ```bash # Start the database container docker run --name database -e MYSQL_ROOT_PASSWORD=root_password -e MYSQL_DATABASE=my_database -d mysql:latest # Start the web server container docker run --name web_server -p 8080:80 --link database my_project_web_server:latest ``` -------------------------------- ### Serve the mdBook Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/README.md Starts a local web server to preview the book. Navigate to your book directory before running this command. ```sh mdbook serve ``` -------------------------------- ### Install Rustfmt Formatter Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Install the Rustfmt component for code formatting using 'rustup component add rustfmt'. ```sh rustup component add rustfmt ``` -------------------------------- ### Install Rustup and Default Toolchain Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/rustup.md Installs rustup and the default Rust toolchain, including the compiler (rustc) and package manager (cargo). ```sh curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### Install Specific Rust Toolchains Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/rustup.md Installs the stable and nightly versions of the Rust toolchain. ```sh rustup toolchain install stable ``` ```sh rustup toolchain install nightly ``` -------------------------------- ### Install Additional Project Dependencies Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Installs extra libraries required for the project, such as graphics and audio libraries. Includes cleanup. ```dockerfile RUN apt-get update \ && apt-get install --no-install-recommends -qy \ libxrandr-dev \ libxcursor-dev \ libudev-dev \ libx11-dev \ libfreetype-dev \ libopenal-dev \ libflac-dev \ libvorbis-dev \ libgl1-mesa-dev \ libegl1-mesa-dev \ # Cleanup && apt-get autoremove -y \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* ``` -------------------------------- ### Deploy Docker Container in Production Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Example of running a Docker container in a production environment. Ensure the image is available in your registry. ```bash # Example of deploying a Docker container in production docker run -d --name my_cpp_app my_registry/my_cpp_image:latest ``` -------------------------------- ### List Installed Rust Toolchains Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/rustup.md Displays all Rust toolchains currently installed on your system. ```sh rustup toolchain list ``` -------------------------------- ### Install and Initialize cargo-fuzz Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/cybersecurity_utils/fuzzing.md Steps to install cargo-fuzz, initialize a fuzzing target, and add a new fuzz test. Requires the nightly Rust compiler. ```sh cd fuzzing # Set up rust nightly version rustup default nightly # Install cargo-fuzz cargo install cargo-fuzz cargo fuzz init cargo fuzz add fuzz_wc_tool # Modify `fuzzing/fuzz/fuzz_targets/fuzz_wc_tool.rs` # Start fuzzing cargo fuzz run fuzz_wc_tool # Set back to stable version rustup default stable ``` -------------------------------- ### Install Mandatory Build Tools and Libraries Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Installs essential build tools, compilers, libraries, and analysis tools for C++ development. Includes cleanup steps to reduce image size. ```dockerfile RUN apt-get update && apt-get install --no-install-recommends -qy \ # Build tools make \ autoconf \ automake \ ninja-build \ libtool \ m4 \ cmake \ ccache \ # GNU Toolchain gcc \ g++ \ # LLVM Toolchain clang-15 \ clang-tools \ clangd-15 \ libclang-15-dev \ lld \ lldb \ # C/C++ libraries libgtest-dev \ libgmock-dev \ # Libraries gnupg \ unzip \ #gcc-multilib \ build-essential \ software-properties-common \ # Python python3 \ python3-pip \ python3-venv \ python3-dev \ python3-setuptools \ # Networking curl \ # Code analysis cppcheck \ iwyu \ clang-tidy \ clang-format \ # Debugging/tracing gdb \ gdbserver \ valgrind \ strace \ # Code coverage lcov \ gcovr \ # Documentation doxygen \ graphviz \ doxygen-latex \ doxygen-doxyparse \ # Version control git \ # Other tools lsb-release \ jq \ gawk \ # Cleanup && apt-get autoremove -y \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* ``` -------------------------------- ### Run Docker Image Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Command to run a Docker container from a previously built image. This example assumes the image is named 'hello-world'. ```bash docker run hello-world ``` -------------------------------- ### C++ Hello World Source File Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md A basic C++ program that prints 'Hello, World!' to the console. This file serves as the source code for the multi-architecture Docker build example. ```cpp #include int main() { std::cout << "Hello, World!" << std::endl; return 0; } ``` -------------------------------- ### mdBook Chapter Content Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/md_book.md Write your chapter content in Markdown files, like this example for the introduction chapter. ```md # Intro Hey It's my book written in `markdown`. ## Background and Motivation I was very motivated to create markdown stuff. ## Conclussion All is fine in my **book**. ``` -------------------------------- ### Install GEF Debugger Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Installs the GEF (GDB Enhanced Features) debugger using a curl script. Ensure the URL is correct and accessible. ```dockerfile RUN bash -c "$(curl -fsSL https://gef.blah.cat/sh)" ``` -------------------------------- ### Install Clippy Linter Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Install the Clippy linter component for Rust using 'rustup component add clippy'. ```sh rustup component add clippy ``` -------------------------------- ### Install and Run cargo-audit Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/security_features.md Install the cargo-audit tool and use it to check your project's dependencies for known vulnerabilities. The `fix` feature attempts to automatically resolve issues. ```sh # Install cargo install cargo-audit --features=fix # Run cargo audit cargo audit fix ``` -------------------------------- ### Runtime Stage Base Image and Dependencies Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Defines the runtime stage using a slim Debian image and installs runtime-specific libraries. Includes cleanup. ```dockerfile FROM debian:bookworm-slim as project-runtime RUN apt-get update \ && apt-get install --no-install-recommends -qy \ libxrandr2 \ libxcursor1 \ libudev1 \ libfreetype6 \ libopenal1 \ libflac12 \ libvorbisfile3 \ libgl1 \ libegl1 \ gdbserver \ && apt-get autoremove -y \ && apt-get clean -y \ && rm -rf /var/lib/apt/lists/* ``` -------------------------------- ### Profiling Application Performance Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Builds the application in debug mode, runs it, and then starts the profiler client to connect to the running application on port 28077. ```shell # Build the app in Debug mode ./run.sh -ad -amd64 # Run the app ./run.sh -ri -amd64 # Run the profiler client app and connect to port 28077 ~/Projects/cpp-project-template/app/thirdparty/EasyProfiler/client_tools/easy_profiler-v2.1.0-linux/run_easy_profiler.sh ``` -------------------------------- ### CMakeLists.txt for Format String Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/examples/memory_safety/format_string/CMakeLists.txt Configures the CMake build system for a C++ program named 'format_string'. It sets up separate executables for release and debug builds with different compiler optimization flags. ```cmake cmake_minimum_required(VERSION 3.10) # Specify the minimum version of CMake set(name format_string) project(${name}) # Release build configuration add_executable(${name}_release ${name}.cpp) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") # Debug build configuration add_executable(${name}_debug ${name}.cpp) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0") ``` -------------------------------- ### Install and Use cargo-auditable Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/security_features.md Install cargo-auditable to embed dependency information into binaries for post-build auditing. This allows scanning binaries without source code or lock files. ```sh # Install cargo install cargo-auditable # Build your project with dependency lists embedded in the binaries cargo auditable build --release # Scan the binary for vulnerabilities cargo audit bin target/release/car_project # Check for the `dep-v0` section readelf -S target/release/car_project readelf -p .dep-v0 target/release/car_project # Decompress zlib section content objdump -s -j .dep-v0 target/release/car_project | grep '^ ' | cut -c7-42 | xxd -r -p | python3 -c "import sys, zlib; sys.stdout.buffer.write(zlib.decompress(sys.stdin.buffer.read()))" ``` -------------------------------- ### Start GDB with GEF Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/gef_tool.md Launches GDB in quiet mode with the GEF enhancements enabled for debugging a specified program. ```bash gdb -q ./your_program ``` -------------------------------- ### Integer Overflow Example in C++ Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/type_safety/variable_overflow.md Illustrates default integer overflow behavior in C++ where uint8_t addition wraps around. ```cpp #include #include int main() { uint8_t a = 200; uint8_t b = 100; uint8_t c = a + b; std::cout << "c: " << static_cast(c) << std::endl; return 0; } ``` -------------------------------- ### C++ Example: Zeroing Memory with `memset` Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/memory_safety/zeroing_memory.md Illustrates secure memory zeroing in C++ using `memset`. This approach is compared to the Rust implementation, highlighting potential differences in how compiler optimizations might affect manual memory clearing. ```cpp #include #include #include #include #include static uint8_t* ptr = nullptr; void print_ptr_memory_address(uint8_t label) { printf("%d) Pointer memory address: %p\n", label, ptr); printf("%d) SensitiveDataMemory: ["); for (size_t i = 0; i < 32; ++i) { printf("%02x ", ptr[i]); } printf("]\n"); } struct SensitiveData { char password[32]; // Sensitive data SensitiveData(const char* pwd) { strncpy(password, pwd, sizeof(password) - 1); password[sizeof(password) - 1] = '\0'; } ~SensitiveData() { std::cout << "Zeroing memory" << std::endl; memset(password, 0, sizeof(password)); //% volatile char *p = password; //% for (size_t i = 0; i < sizeof(password); i++) { //% *(p + i) = 0; //% } } }; void process_password(const char* pwd) { SensitiveData data(pwd); ptr = reinterpret_cast(data.password); // Simulate operations on the sensitive data std::cout << "Processing sensitive data..." << std::endl; print_ptr_memory_address(1); } void do_other_work() { std::cout << "Doing other work..." << std::endl; print_ptr_memory_address(3); } int main() { process_password("abcdefghijklmnopqrstuvwxyz123456"); print_ptr_memory_address(2); do_other_work(); return 0; } ``` -------------------------------- ### Run the Program in GEF Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/gef_tool.md Start the execution of the program within the GEF debugger. The program will run until it hits the first breakpoint. ```bash r ``` -------------------------------- ### Rust Borrowing Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/safety_features_overview.md Shows how to borrow a String immutably for a function. The original String can still be used after the function call. ```rust fn calculate_length(s: &String) -> usize { s.len() } fn main() { let a = String::from("Hello"); let len = calculate_length(&a); // a is borrowed println!("The length of '{}' is {}.", a, len); // a can still be used here } ``` -------------------------------- ### CMakeLists.txt for Memory Leaks Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/examples/memory_safety/memory_leaks/CMakeLists.txt Configures a C++ project for memory leak demonstration. It defines release and debug build targets with distinct compiler flags. ```cmake cmake_minimum_required(VERSION 3.10) # Specify the minimum version of CMake set(name memory_leaks) project(${name}) # Release build configuration add_executable(${name}_release ${name}.cpp) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") # Debug build configuration add_executable(${name}_debug ${name}.cpp) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0") ``` -------------------------------- ### Docker Compose Configuration for Services Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Example Docker Compose file defining services for a C/C++ project, including image, command, network mode, volumes, ports, and restart policies. ```yaml version: "3" services: hub-frpc: image: energy-hub-manager-amd64:0.1 command: /hub/frpc_amd64 -c /var/hub/frpc.ini network_mode: "host" volumes: - /var/hub:/var/hub ports: - 7400:7400 restart: unless-stopped hub-manager: image: energy-hub-manager-amd64:0.1 command: python3 -u /hub/main.py volumes: - /var/run/docker.sock:/var/run/docker.sock - /tmp/logs:/tmp/logs - /var/hub:/var/hub network_mode: "host" restart: on-failure ``` -------------------------------- ### Dockerfile for C++ Application Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md A Dockerfile to build a C++ application. It installs g++, copies the source code, compiles it, and sets the entrypoint to run the compiled binary. ```dockerfile FROM alpine:latest # Install build dependencies RUN apk update && apk add --no-cache \ g++ \ && rm -rf /var/cache/apk/* # Copy the source code COPY main.cpp /app/ # Set the working directory WORKDIR /app/ # Compile the C++ code RUN g++ -o hello main.cpp # Run the compiled binary CMD ["./hello"] ``` -------------------------------- ### Run GDB Server in Docker Container Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Execute a Docker container and start the GDB server, ensuring it listens on the specified port for remote connections. ```bash # Start Docker container with GDB server docker run --rm -d -p 1234:1234 my_image gdbserver :1234 my_executable ``` -------------------------------- ### Run RustScan with Docker Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/cybersecurity_utils/scanners.md Pull the RustScan Docker image and run a scan against a specified IP address with a given batch size. Ensure Docker is installed and running. ```sh docker pull rustscan/rustscan:2.1.1 docker run -it --rm --name rustscan rustscan/rustscan:2.1.1 -b 10000 -a 127.0.0.1 ``` -------------------------------- ### Run Debug/Release App under GDB Server Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Starts the application within the container, exposing it for remote debugging via GDB server on port 2345. Includes instructions on how to connect and interact with the debugger. ```shell # Debug app build ./run.sh -rig -amd64 ./run.sh -rig -arm64 # To connect to the server please run: gdb app/build/bin/my_project (gdb) target remote localhost:2345 (gdb) c # Demo: # Stop execution CTRL+C # Set breakpoint (gdb) break Game::updateStatusTextView (gdb) set var m_score = 9999999 ``` -------------------------------- ### Rust Example: Zeroing Memory with `zeroize` Crate Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/memory_safety/zeroing_memory.md Demonstrates secure memory zeroing for sensitive data in Rust using the `zeroize` crate. This is crucial for preventing data remnants after deallocation, especially when compiler optimizations might otherwise remove manual zeroing attempts. ```rust use std::ptr::write_volatile; use zeroize::Zeroize; static mut PTR: *const u8 = std::ptr::null(); fn print_ptr_memory_address(label: u8) { unsafe { println!("{label}) Pointer memory address: {:p}", PTR); println!("{label}) SensitiveDataMemory: {{:x?}}", core::slice::from_raw_parts(PTR, 32)); } } struct SensitiveData { password: [u8; 32], // Sensitive data } impl SensitiveData { pub fn new(password: &[u8]) -> Self { let mut data = SensitiveData { password: [0; 32] }; let len = password.len().min(data.password.len()); data.password[..len].copy_from_slice(&password[..len]); data } } impl Drop for SensitiveData { fn drop(&mut self) { println!("Zeroing memory"); // unsafe { // write_volatile(&mut self.password, [0u8; 32]); // } self.password.zeroize() } } fn process_password(pwd: &[u8]) { let data = SensitiveData::new(pwd); unsafe { PTR = data.password.as_ptr(); } // Simulate operations on the sensitive data println!("Processing sensitive data..."); print_ptr_memory_address(1); } fn do_other_work() { println!("Doing other work..."); print_ptr_memory_address(3); } pub fn main() { process_password(b"abcdefghijklmnopqrstuvwxyz123456"); print_ptr_memory_address(2); do_other_work(); } ``` -------------------------------- ### Serve Project Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/README.md Use this command to serve the project locally. The website will be available at http://localhost:8080. ```sh ./run.sh -s ``` -------------------------------- ### Build and Open Project Documentation Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Generate and view documentation for your project's dependencies by running 'cargo doc --open'. ```sh cargo doc --open ``` -------------------------------- ### Docker Client Commands: Build, Run, List, Inspect Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Common Docker client commands for building images, running containers, listing containers, and inspecting container details. ```bash docker build -t my_image . docker run --name my_container my_image docker ps -a docker inspect my_container ``` -------------------------------- ### Docker Client Commands: Attach, Start/Stop, Kill, Remove All Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Commands for attaching to containers, starting/stopping/killing containers, and removing all containers. ```bash docker exec -it CONTAINER_ID_OR_NAME /bin/bash docker start/stop CONTAINER_ID_OR_NAME docker kill $(docker ps -q) docker rm $(docker ps -q -a) ``` -------------------------------- ### Start GEF Debugger Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/gef_tool.md Launch the GDB debugger with GEF enabled for the compiled program. This command starts the debugger in quiet mode. ```bash gdb -q ./build/credentials_demo ``` -------------------------------- ### Build Release App for Different Architectures Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Use these commands to build the release version of the application for amd64 and arm64 architectures. ```shell ./run.sh -a -amd64 ./run.sh -a -arm64 ``` -------------------------------- ### Copy Application Binary and Set CMD Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Copies the built application binary to the runtime image and sets the default command to run the application, optionally with gdbserver. ```dockerfile COPY ../app/build/bin /app #CMD ["/app/my_project"] CMD ["gdbserver", ":2345", "/app/my_project"] ``` -------------------------------- ### Create Project Directory and Dockerfile Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Commands to create a new directory for your C/C++ project and an empty Dockerfile within it. ```bash mkdir my_cpp_project cd my_cpp_project touch Dockerfile ``` -------------------------------- ### Build Multi-Architecture Docker Image Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Commands to set up and use Docker buildx to create a Docker image that supports multiple architectures (amd64 and arm64). ```bash # Create the builder (if not already created) docker buildx create --name my_builder # Use the builder for subsequent builds docker buildx use my_builder # Build the multi-architecture image and load it into local Docker daemon docker buildx build --platform linux/amd64,linux/arm64 -t hello-world:latest . docker buildx build --load -t hello-world:latest . ``` -------------------------------- ### Basic Dockerfile for C/C++ Development Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md A foundational Dockerfile that sets up an Ubuntu-based environment with essential build tools for C/C++ development. ```Dockerfile # Use an official Ubuntu as a base image FROM ubuntu:latest # Set environment variables ENV DEBIAN_FRONTEND noninteractive # Install build essentials and other necessary tools RUN apt-get update && apt-get install -y \ build-essential \ cmake \ gcc \ g++ \ gdb \ && rm -rf /var/lib/apt/lists/* # Set the working directory in the container WORKDIR /usr/src/my_cpp_project # Copy the local project files into the container COPY . . ``` -------------------------------- ### Update Rust Toolchains Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/rustup.md Updates all installed Rust toolchains to their latest available versions. ```sh rustup update ``` -------------------------------- ### Build Production Image for Different Architectures Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Use these commands to build the production Docker image for amd64 and arm64 architectures. ```shell # Debug app build ./run.sh -pb -amd64 ./run.sh -pb -arm64 ``` -------------------------------- ### Use Multi-Stage Builds in Dockerfile Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Demonstrates a multi-stage Docker build, separating build dependencies from runtime dependencies to create smaller, more efficient images. ```Dockerfile # Single-stage build FROM base_image AS builder RUN build_commands FROM base_image COPY --from=builder /app /app ``` -------------------------------- ### Perform Memcheck (Valgrind) and View Report Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Builds the application in debug mode, enters the container, runs Valgrind for memcheck on the project, and provides the path to the generated HTML report. ```shell # Build the app ./run.sh -ad -amd64 # Enter the container ./run.sh -s -amd64 # Inside the container cmake --build build -t memcheck-my_project # Memcheck Report firefox ~/Projects/cpp-project-template/app/build/memcheck_report/index.html ``` -------------------------------- ### Rust Mutable Borrowing Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/safety_features_overview.md Demonstrates how to borrow a String mutably to modify it within a function. The original String can be used after modification. ```rust fn append_world(s: &mut String) { s.push_str(" world"); // s is now a mutable reference, allowing us to modify the original String } fn main() { let mut a = String::from("Hello"); append_world(&mut a); // a is mutably borrowed println!("The new value of 'a' is {}.", a); // a can still be used here because the mutable borrow ends at the end of the `append_world` scope } ``` -------------------------------- ### Debug Flags for C Executable (Commented Out) Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/code/gef_tool/CMakeLists.txt An example of how to configure C compiler flags for the debug build, commented out in the provided CMakeLists.txt. ```cmake # add_executable(${name} ${name}.c) # set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -fno-stack-protector -no-pie -fno-PIE -z execstack") ``` -------------------------------- ### Format Rust Code with Rustfmt Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Format all .rs files in your project according to the Rust style guide using 'cargo fmt'. ```sh cargo fmt ``` -------------------------------- ### Build the mdBook Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/README.md Compiles the book into static HTML files. Navigate to your book directory before running this command. ```sh mdbook build ``` -------------------------------- ### Enter the Development Container Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Use these commands to enter the development container for amd64 and arm64 architectures, providing an interactive shell. ```shell ./run.sh -s -amd64 ./run.sh -s -arm64 ``` -------------------------------- ### Docker Client Commands: System Prune, Images, Search Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Commands for cleaning up unused Docker resources, listing local images, and searching for images in a registry. ```bash docker system prune docker images docker search $IMAGE_NAME ``` -------------------------------- ### Build and Share Docker Image for C/C++ Development Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Builds a Docker image for a C/C++ development environment, tags it for a registry, and pushes it. This facilitates sharing consistent development environments across teams. ```bash # Example of building and sharing a Docker image docker build -t my_cpp_image . docker tag my_cpp_image my_registry/my_cpp_image:latest docker push my_registry/my_cpp_image:latest ``` -------------------------------- ### Add Dependencies to Cargo.toml Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Add libraries to your project by listing them under the [dependencies] section in Cargo.toml. Run 'cargo build' to download and compile them. ```toml [dependencies] serde = "1.0" ``` -------------------------------- ### Cache Docker Dependencies Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Optimizes Docker build caching by copying and installing dependencies before copying the rest of the application code. This speeds up rebuilds when only application code changes. ```Dockerfile # Good practice: using caching for dependencies COPY package.json /app RUN npm install COPY . /app RUN npm build ``` -------------------------------- ### Dangling Pointer Example in C Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/memory_safety/pointers.md Demonstrates a dangling pointer scenario in C where a pointer references memory that has been deallocated after the scope ends. This leads to undefined behavior when dereferenced. ```c #include "stdio.h" int main() { int* a = NULL; { int b = 5; a = &b; } int c = 10; // At this point, b goes out of scope, but the memory allocated to it does not printf("a: %d\n", *a); return 0; } ``` -------------------------------- ### Create a New Rust Project Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Use 'cargo new' to create a new Rust project directory with a Cargo.toml file and a src directory. Navigate into the project directory using 'cd'. ```sh cargo new my_project cd my_project ``` -------------------------------- ### mdBook Configuration File Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/md_book.md Define your book's metadata, author, and language in the `book.toml` file. ```toml [book] title = "Your mdBook name" authors = ["Your Name"] language = "en" [build] build-dir = "book" ``` -------------------------------- ### Rust Ownership Move Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/safety_features_overview.md Demonstrates how ownership of a String is moved from one variable to another. Attempting to use the original variable after the move results in a compile-time error. ```rust fn main() { let a: String = String::from("Hello"); let b = a; // a's ownership is moved to b println!("{}", b); // println!("{}", a); // This line would cause a compile-time error } ``` ```cpp #include #include int main() { std::string a = "hello"; std::string b = a; // Duplicate the data in a. std::cout << b << std::endl; std::cout << a << std::endl; return 0; } ``` -------------------------------- ### C++ Classic Double Free Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/memory_safety/double_free.md Demonstrates a classic double free error in C++ by manually allocating and deallocating memory. This code will lead to undefined behavior. ```cpp #include void cause_double_free() { int* ptr = new int(10); // Allocate memory on the heap delete ptr; // Correctly free memory *ptr = 11; std:: cout << "ptr = " << *ptr << std::endl; delete ptr; // Double free error: undefined behavior *ptr = 12; std:: cout << "ptr = " << *ptr << std::endl; } int main() { cause_double_free(); return 0; } ``` -------------------------------- ### Build Debug App for Different Architectures Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/posts/cpp_proj_template.md Use these commands to build the debug version of the application for amd64 and arm64 architectures. ```shell # Debug app build ./run.sh -ad -amd64 ./run.sh -ad -arm64 ``` -------------------------------- ### Adjust Brightness Closure Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/example_app.md Demonstrates using closures to modify data within a generic image processor. The `adjust_brightness` function takes a closure that transforms the image data. ```rust // Demonstrates the use of closures to modify data, showcasing Rust's closure capabilities. fn adjust_brightness(adjustment_closure: F, processor: &mut RearCameraImageProcessor) where F: Fn(T) -> T, T: Display + Clone, { let current_data = processor.read().clone(); println!("Current image brightness: {}", current_data); let adjusted_data = adjustment_closure(current_data); processor.write(adjusted_data); } ``` -------------------------------- ### Rust Borrow Checker Lifetime Example Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/intro/safety_features_overview.md Illustrates a compile-time error due to a reference lifetime mismatch. The outer 'result' reference attempts to outlive the 'tmp' variable it points to. ```rust fn main() { let result; { let tmp = 42; result = &tmp; } println!("result: {}", result); } ``` -------------------------------- ### Build a Rust Project Source: https://github.com/luk6xff/luk6xff.github.io/blob/master/content/other/safe_secure_rust_book/src/rust_ecosystem/cargo.md Compile your Rust project using 'cargo build'. ```sh cargo build ```