Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/cli commands and options #15

Merged
merged 14 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
name: Run zombienet tests
name: Zombienet

on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
pull_request:
branches: [ develop ]
push:
paths:
- 'tests/**'
- '.github/workflows/main.yaml'

jobs:
check:
Expand Down Expand Up @@ -91,12 +93,10 @@ jobs:
git pull --rebase origin feat/cicd
git push origin feat/cicd



run-tests:
runs-on: ubuntu-latest
needs: check
if: ${{ needs.check.outputs.new_release == 'true' }}
if: needs.check.outputs.new_release == 'true' || github.event_name == 'push'
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -189,15 +189,18 @@ jobs:

- name: Run wasm_injector
run: |
./target/debug/wasm_injector nothing ./target/debug/test.wasm ./wasm/orig.wasm.hex --hexified
./target/debug/wasm_injector stack-overflow ./target/debug/test.wasm ./wasm/stack-overflow.wasm.hex --hexified
./target/debug/wasm_injector heap-overflow ./target/debug/test.wasm ./wasm/heap-overflow.wasm.hex --hexified
./target/debug/wasm_injector infinite-loop ./target/debug/test.wasm ./wasm/infinite-loop.wasm.hex --hexified
./target/debug/wasm_injector convert ./target/debug/test.wasm ./wasm/orig.wasm.hex --hexified
./target/debug/wasm_injector inject stack-overflow ./target/debug/test.wasm ./wasm/stack-overflow.wasm.hex --hexified
./target/debug/wasm_injector inject heap-overflow ./target/debug/test.wasm ./wasm/heap-overflow.wasm.hex --hexified
./target/debug/wasm_injector inject infinite-loop ./target/debug/test.wasm ./wasm/infinite-loop.wasm.hex --hexified

- name: Set up Julia
uses: julia-actions/setup-julia@v1
with:
version: '1.9.1'

- name: Run Julia script
run: julia scripts/runTests.jl
- name: Run Polkadot Tests
run: julia scripts/runTests.jl polkadot

- name: Run Kagome Tests
run: julia scripts/runTests.jl kagome
61 changes: 61 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Rust

on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop

env:
CARGO_TERM_COLOR: always

jobs:
test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.3.10", features = [ "derive" ] }
clap = { version = "4.3.19", features = [ "derive" ] }
itertools = "0.11.0"
sp-maybe-compressed-blob = "5.0.0"
thiserror = "1.0.40"
Expand Down
140 changes: 97 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,120 @@
# wasm-injector
# wasm_injector

`wasm-injector` is a Rust-based command line utility to manipulate WebAssembly (wasm) modules. It allows you to inject code, compress, and hexify a given wasm module. This utility is especially useful when you need to modify wasm files for testing, optimization, or debugging.
`wasm_injector` is a Rust-based command line utility to manipulate WebAssembly (wasm) modules. It allows you to inject code, compress, and hexify a given wasm module. This utility is especially useful when you need to modify wasm files for testing, optimization, or debugging.

## Prerequisites
## Prerequisites

To use this utility, you need to have Rust installed on your system. If you don't have Rust installed, you can follow the official instructions [here](https://www.rust-lang.org/tools/install).
To use this utility, you need to have Rust installed on your system. If you don't have Rust installed, you can follow the official instructions [here](https://www.rust-lang.org/tools/install).

## Installation
## Installation

Clone the repository and build the project with `cargo`, Rust's package manager:
Clone the repository and build the project with `cargo`, Rust's package manager:

```sh
git clone https://github.com/your_github_username/wasm-injector.git
cd wasm-injector
cargo build --release
```
```sh
git clone https://github.com/LimeChain/parachain-conformance-dev.git
cd parachain-conformance-dev
cargo build --release
```

This will create an executable in the `target/release` directory.
This will create an executable in the `target/release` directory.

## Usage
- This project is developed to inject instructions in WASM modules or polkadot conformance testing. You can download a sample WASM file from [here](https://github.com/paritytech/cumulus/releases/tag/parachains-v9430).
- The script will automatically unhexify and decompress WASM modules passed in as input
- The general format to run the `wasm-injector` is as follows:
## Usage
- This project is developed to inject instructions in WASM modules or polkadot conformance testing. You can download a sample WASM file from [here](https://github.com/paritytech/cumulus/releases/tag/parachains-v9430).
- The script will automatically unhexify and decompress WASM modules passed in as input
- The general format to run the `wasm_injector` is as follows:

```sh
./wasm-injector [INJECTION] [SOURCE] [DESTINATION] [--compressed] [--hexified]
```
```sh
Usage: wasm_injector <COMMAND>

- `[INJECTION]` (required): The type of injection to apply to the wasm module. Possible values ```possible values: [nothing, infinite-loop, jibberish-return-value, stack-overflow, noops, heap-overflow]```
Commands:
inject Inject invalid instructions into a wasm module
convert Convert from `hexified` and/or `compressed` to `raw` wasm module and vice versa
help Print this message or the help of the given subcommand(s)

- `[SOURCE]` (required): A path to the wasm source file. If the wasm module is compressed or hexified, indicate this in the path name as "(hexified)" and/or "(compressed)".
Options:
-h, --help Print help
-V, --version Print version
```

- `[DESTINATION]` (optional): A path where you want the output wasm file to be saved. If this argument is not provided, the modified wasm file will be saved alongside the source file with a predefined file name.
### Inject:
```sh
Usage: wasm_injector inject [OPTIONS] <injection> <SOURCE> [DESTINATION]

- `--compressed` (optional): If this flag is provided, the output file will be compressed.
Arguments:
<injection> [possible values: infinite-loop, bad-return-value, stack-overflow, noops, heap-overflow]
<SOURCE> Wasm source file path. Can be compressed and/or hexified.
[DESTINATION] Destination file path (optional). If not specified, the output file will be a prefixed source file name.

- `--hexified` (optional): If this flag is provided, the output file will be hexified (converted to a hexadecimal representation).
Options:
--compressed Compresses the wasm. Can be used with `--hexified`
--hexified Hexifies the wasm. Can be used with `--compressed`
-h, --help Print help
```

### Convert:
```sh
Usage: wasm_injector convert [OPTIONS] <SOURCE> [DESTINATION]

Arguments:
<SOURCE> Wasm source file path. Can be compressed and/or hexified.
[DESTINATION] Destination file path (optional). If not specified, the output file will be a prefixed source file name.

Options:
--raw Saves the file as raw wasm (default). Can not be used with `--compressed` or `--hexified`.
--compressed Compresses the wasm (zstd compression). Can be used with `--hexified`.
--hexified Hexifies the wasm. Can be used with `--compressed`
-h, --help Print help
```

## Examples

To inject code into a wasm file, compress and hexify it, you can run:
### Inject:
To inject code into a wasm file, compress and hexify it, you can run:

```sh
./wasm_injector inject noops my_wasm_file.wasm --compressed --hexified
```

To specify a custom destination path, you can run:

```sh
./wasm_injector inject noops my_wasm_file.wasm my_destination_directory/injected_new_file.wasm
```

### Convert:

#### From Compressed and/or Hexified Wasm to Raw (default):
```sh
./wasm_injector convert --raw compressed_and_hexified_wasm_file.wasm.hex raw_wasm_file.wasm
```

or

```sh
./wasm_injector convert compressed_and_hexified_wasm_file.wasm.hex raw_wasm_file.wasm
```

#### From Raw Wasm to Compressed and/or Hexified:

```sh
./wasm_injector convert --hexified raw_wasm_file.wasm heified_wasm_file.wasm.hex
```

```sh
./wasm_injector convert --compressed --hexified raw_wasm_file.wasm compressed_and_hexified_wasm_file.wasm.hex
```

```sh
./wasm-injector noops my_wasm_file.wasm --compressed --hexified
```
To run the WASM you need Linux environment with [Zombienet](https://github.com/paritytech/zombienet)
For each test you need to specify the path to the WASM module in the corresponding `.toml` file before running the test

To specify a custom destination path, you can run:
```sh
zombienet -p native test ./tests/0001-parachains-pvf-compilation-time-bad.zndsl
```

```sh
./wasm-injector noops my_wasm_file.wasm my_destination_directory/my_new_file.wasm
```
To run the WASM you need Linux environment with [Zombienet](https://github.com/paritytech/zombienet)
For each test you need to specify the path to the WASM module in the corresponding `.toml` file before running the test

```sh
zombienet -p native test ./tests/0001-parachains-pvf-compilation-time-bad.zndsl
```

## Contributing
## Contributing

Please feel free to contribute to the project. For major changes, please open an issue first to discuss what you would like to change.
Please feel free to contribute to the project. For major changes, please open an issue first to discuss what you would like to change.

## License
## License

This project is licensed under [TODO]. See the LICENSE file for details.
This project is licensed under [TODO]. See the LICENSE file for details.
79 changes: 46 additions & 33 deletions scripts/runTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,64 @@ ENV["PATH"] *= ":$(pwd())/bin"
# Tests directory
tests_dir::String = get(ENV ,"ZN_TESTS", "./tests")

# Test output path
tests_output::String = get(ENV, "ZN_TEST_OUTPUT", "./tests_output")

# Arrays to accumulate passed and failed test names
passed_tests::Vector{String} = []
failed_tests::Vector{String} = []

# For each host ...
for host::String in hosts
withenv("ZOMBIENET_DEFAULT_START_COMMAND" => host) do
# Get host tests directory
host_tests_dir::String = joinpath(tests_dir, host)
# Filter out the non-`.zndsl` files for each host
tests::Vector{String} = filter(file -> endswith(file, ".zndsl"), readdir(host_tests_dir))

# ... run each test
for test::String in tests
if endswith(test, ".zndsl")
# Extract the index and test name from the test path
match_captures = match(r"(\d+)-(.*?)\.zndsl", basename(test)).captures
index::Int64 = parse(Int64, match_captures[1])
test_name::String = match_captures[2]
function run_host_tests(host::String)
println("Running tests for host $(host)")
# Get host tests directory
host_tests_dir::String = joinpath(tests_dir, host)
# Filter out the non-`.zndsl` files for each host
tests::Vector{String} = filter(file -> endswith(file, ".zndsl"), readdir(host_tests_dir))

# ... run each test
for test::String in tests
if endswith(test, ".zndsl")
# Extract the index and test name from the test path
match_captures = match(r"(\d+)-(.*?)\.zndsl", basename(test)).captures
index::Int64 = parse(Int64, match_captures[1])
test_name::String = match_captures[2]

# Prepare the `zombienet test` command
command::Cmd = `zombienet -p native test $(joinpath(host_tests_dir, test))`
full_test_name::String = "[$(host)] $(test_name)"
println("Running test $(full_test_name)")
# Prepare the `zombienet test` command
command::Cmd = `zombienet -p native test $(joinpath(host_tests_dir, test))`
full_test_name::String = "[$(host)] $(test_name)"
println("Running test $(full_test_name)")

# Try to run the test
try
# Try to run the command
run(command)
# Try to run the test
try
# Try to run the command
run(command)

# Add the test name to the passed tests array
push!(passed_tests, full_test_name)
catch e
showerror(stdout, e)
# Add the test name to the failed tests array
push!(failed_tests, full_test_name)
end
# Add the test name to the passed tests array
push!(passed_tests, full_test_name)
catch e
println(e)
# Add the test name to the failed tests array
push!(failed_tests, full_test_name)
end
end
end
end

if isempty(ARGS)
# For each host ...
for host::String in hosts
withenv("ZOMBIENET_DEFAULT_START_COMMAND" => host) do
run_host_tests(host)
end
end
else
# Get the host from the command line arguments
host::String = getindex(ARGS, 1)
if (length(findall( x -> x == host, hosts )) == 0)
println("Host `$(host)` not supported.")
exit(1)
end
# Run the tests for the specified host
run_host_tests(host)
end

# Report the passed and failed tests
println("Passed tests:")
println(passed_tests)
Expand Down
Loading
Loading