diff --git a/language/rust/rust-binaries-all-versions/Readme.md b/language/rust/rust-binaries-all-versions/Readme.md new file mode 100644 index 0000000..d39dbc8 --- /dev/null +++ b/language/rust/rust-binaries-all-versions/Readme.md @@ -0,0 +1,20 @@ +# Test Files for Rust Binary Versions + +This repository contains two ZIP archives, each containing compiled Rust binaries for minor versions 1.56.0 to 1.70.0, specifically for the 386 and amd64 architectures, along with their source code. + +## Contents + +Each ZIP archive corresponds to a specific architecture and contains the following files: + +- `bin/versions_32.zip`: Zip file containing the compiled Rust binaries for the 386 architecture. +- `bin/versions_64.zip`: Zip file containing the compiled Rust binaries for the amd64 architecture. +- `src/main.rs`: Source code for the binaries. + +## Generating Rust Binaries using Docker + +To automate the process of generating Rust binaries for all versions from 1.56.0 to 1.70.0 using Docker, you can use the provided Python script. Follow the steps below: + +- Make sure Docker is installed and properly configured on your system. +- Run the script using the following command: `python3 rust_binary_generator.py`. + +The script will handle the setup, compilation, and generation of Rust binaries for the specified minor versions. The resulting binaries will be saved in the `bin/` directory. \ No newline at end of file diff --git a/language/rust/rust-binaries-all-versions/rust_binary_generator.py b/language/rust/rust-binaries-all-versions/rust_binary_generator.py new file mode 100644 index 0000000..e1f3b18 --- /dev/null +++ b/language/rust/rust-binaries-all-versions/rust_binary_generator.py @@ -0,0 +1,85 @@ +# Copyright (C) 2023 Mandiant, Inc. All Rights Reserved. +""" +This Python script automates the process of generating Rust binaries for versions 1.56.0 to 1.70.0 using Docker. By leveraging Docker containers, it ensures a consistent and reproducible environment for compiling the Rust source code across different platforms. + +Please note that Docker must be installed and properly configured on your system for the script to execute successfully. + +Usage: + python rust_binary_generator.py + +The resulting binaries will be saved in the `bin/` directory. +""" + +import subprocess + +rust_versions = """ +1.70.0 +1.68.0 +1.66.0 +1.64.0 +1.62.0 +1.60.0 +1.58.0 +1.56.0 +""" + +f = open("docker-compose.yml", "w") + +f.write( + """services: + app: + build: .""" +) + +f.close() + +subprocess.call(["docker", "compose", "up"]) +print("Compose is UP") + +architecture = {"386": "i686-pc-windows-gnu", "amd64": "x86_64-pc-windows-gnu"} + +for arch in architecture.keys(): + for version in rust_versions.split(): + print("version: ", version) + + f = open("Dockerfile", "w") + + target = architecture[arch] + + f.write( + """# syntax=docker/dockerfile:1 +FROM rust:{} +ENV USER root +RUN cargo new known_binary +WORKDIR known_binary +COPY ./src/main.rs ./src/main.rs +RUN apt update && apt install mingw-w64 -y +RUN rustup target add {} +RUN rustup update +RUN cargo build --release --target {} + +""".format( + version, target, target + ) + ) + f.close() + + subprocess.call(["docker", "build", "--tag", "rust-test", "."]) + subprocess.call( + [ + "docker", + "run", + "-v", + ".:/var/opt", + "rust-test", + "mv", + "./target/{}/release/known_binary.exe".format(target), + "/var/opt/bin/rust_{}_{}.exe".format(arch, version), + ] + ) + +subprocess.call(["docker", "compose", "down"]) +print("Compose is DOWN") + +# Free up disk space +subprocess.call(["docker", "system", "prune", "-a", "-f"]) diff --git a/language/rust/rust-binaries-all-versions/src/main.rs b/language/rust/rust-binaries-all-versions/src/main.rs new file mode 100644 index 0000000..8b90d27 --- /dev/null +++ b/language/rust/rust-binaries-all-versions/src/main.rs @@ -0,0 +1,40 @@ +fn main() { + // (all the type annotations are superfluous) + // A reference to a string allocated in read only memory + let pangram: &'static str = "the quick brown fox jumps over the lazy dog"; + println!("Pangram: {}", pangram); + + // Iterate over words in reverse, no new string is allocated + println!("Words in reverse"); + for word in pangram.split_whitespace().rev() { + println!("> {}", word); + } + + // Copy chars into a vector, sort and remove duplicates + let mut chars: Vec = pangram.chars().collect(); + chars.sort(); + chars.dedup(); + + // Create an empty and growable `String` + let mut string = String::new(); + for c in chars { + // Insert a char at the end of string + string.push(c); + // Insert a string at the end of string + string.push_str(", "); + } + + // The trimmed string is a slice to the original string, hence no new + // allocation is performed + let chars_to_trim: &[char] = &[' ', ',']; + let trimmed_str: &str = string.trim_matches(chars_to_trim); + println!("Used characters: {}", trimmed_str); + + // Heap allocate a string + let alice = String::from("I like dogs"); + // Allocate new memory and store the modified string there + let bob: String = alice.replace("dog", "cat"); + + println!("Alice says: {}", alice); + println!("Bob says: {}", bob); +} \ No newline at end of file