diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..135475d8d4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,9 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: local + hooks: + - id: fmt + name: fmt + entry: scripts/fmt_all.sh check + language: script \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..9dd4eb1800 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,18 @@ +# How to Contribute to LibAFL + +For bugs, feel free to open issues or contact us directly. Thank you for your support. <3 + +## Pull Request guideline + +Even though we will gladly assist you in finishing up your PR, try to +- keep all the crates compiling with *stable* rust (hide the eventual non-stable code under [`cfg`s](https://github.com/AFLplusplus/LibAFL/blob/main/libafl/build.rs#L26)) +- run `cargo +nightly fmt` on your code before pushing +- check the output of `cargo clippy --all` or `./clippy.sh` +- run `cargo build --no-default-features` to check for `no_std` compatibility (and possibly add `#[cfg(feature = "std")]`) to hide parts of your code. + +Some of the parts in this list may be hard, don't be afraid to open a PR if you cannot fix them by yourself, so we can help. + +### Pre-commit hooks + +Some of these checks can be performed automatically during commit using [pre-commit](https://pre-commit.com/). +Once the package is installed, simply run `pre-commit install` to enable the hooks, the checks will run automatically before the commit becomes effective. \ No newline at end of file diff --git a/README.md b/README.md index 4e38b68408..39c6f5e2df 100644 --- a/README.md +++ b/README.md @@ -116,15 +116,7 @@ The best-tested fuzzer is [`./fuzzers/libfuzzer_libpng`](./fuzzers/libfuzzer_lib ## Contributing -For bugs, feel free to open issues or contact us directly. Thank you for your support. <3 - -Even though we will gladly assist you in finishing up your PR, try to -- keep all the crates compiling with *stable* rust (hide the eventual non-stable code under [`cfg`s](https://github.com/AFLplusplus/LibAFL/blob/main/libafl/build.rs#L26)) -- run `cargo +nightly fmt` on your code before pushing -- check the output of `cargo clippy --all` or `./clippy.sh` -- run `cargo build --no-default-features` to check for `no_std` compatibility (and possibly add `#[cfg(feature = "std")]`) to hide parts of your code. - -Some of the parts in this list may be hard, don't be afraid to open a PR if you cannot fix them by yourself, so we can help. +Please check out [CONTRIBUTING.md](CONTRIBUTING.md) for the contributing guideline. ## Cite diff --git a/scripts/fmt_all.sh b/scripts/fmt_all.sh index 30f7d5c7e6..b1bae3670d 100755 --- a/scripts/fmt_all.sh +++ b/scripts/fmt_all.sh @@ -10,6 +10,10 @@ if ! command -v parallel > /dev/null; then exit 1 fi +if [ "$1" = "check" ]; then + CHECK=1 +fi + # Find main rust crates CRATES_TO_FMT=$(find "$LIBAFL_DIR" -type d \( -path "*/fuzzers/*" -o -path "*/target/*" -o -path "*/utils/noaslr" -o -path "*/utils/gdb_qemu" -o -path "*/docs/listings/baby_fuzzer/listing-*" \) -prune \ -o -name "Cargo.toml" -print \ @@ -21,15 +25,30 @@ CRATES_TO_FMT+=$(find "$LIBAFL_DIR/fuzzers" "$LIBAFL_DIR/fuzzers/backtrace_baby_ echo "Welcome to the happy fmt script. :)" if [ "$CHECK" ]; then + echo "Running fmt in check mode." CARGO_FLAGS="--check" CLANG_FLAGS="--dry-run" fi echo "[*] Formatting Rust crates..." -echo "$CRATES_TO_FMT" | parallel "echo '[*] Running fmt for {}'; cargo +nightly fmt $CARGO_FLAGS --manifest-path {}" +if ! echo "$CRATES_TO_FMT" | parallel --halt-on-error 1 "echo '[*] Running fmt for {}'; cargo +nightly fmt $CARGO_FLAGS --manifest-path {}" +then + echo "Rust format failed." + exit 1 +fi -echo "[*] Formatting C(pp) files" -# shellcheck disable=SC2046 -clang-format-18 "$CLANG_FLAGS" -i --style=file $(find . -type f \( -name '*.cpp' -o -iname '*.hpp' -o -name '*.cc' -o -name '*.cxx' -o -name '*.cc' -o -name '*.c' -o -name '*.h' \) | grep -v '/target/' | grep -v 'libpng-1\.6\.37' | grep -v 'stb_image\.h' | grep -v 'dlmalloc\.c' | grep -v 'QEMU-Nyx') +if command -v clang-format-18 > /dev/null; then + echo "[*] Formatting C(pp) files" + + C_FILES=$(find "$LIBAFL_DIR" -type f \( -name '*.cpp' -o -iname '*.hpp' -o -name '*.cc' -o -name '*.cxx' -o -name '*.cc' -o -name '*.c' -o -name '*.h' \) | grep -v '/target/' | grep -v 'libpng-1\.6\.37' | grep -v 'stb_image\.h' | grep -v 'dlmalloc\.c' | grep -v 'QEMU-Nyx') + if ! clang-format-18 "$CLANG_FLAGS" -i --style=file "$C_FILES" + then + echo "C(pp) format failed." + exit 1 + fi + +else + echo "Warning: clang-format-18 not found. C(pp) files formatting skipped." +fi echo "[*] Done :)"