From 22d9ae7c410508bb321025297e90997845973715 Mon Sep 17 00:00:00 2001 From: xlauko Date: Thu, 5 Sep 2024 15:46:51 +0200 Subject: [PATCH] debug --- .github/workflows/ci.yml | 17 +++++++-- scripts/ghidra/decompile-entrypoint.sh | 12 ++++++ scripts/ghidra/decompile-headless.sh | 53 ++++++++++++++++++++------ test/ghidra/argc.c | 2 +- test/lit.cfg.py | 17 +-------- 5 files changed, 70 insertions(+), 31 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6aa3c58..f019b84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,8 +61,19 @@ jobs: - name: Build ${{ matrix.build-type }} with sanitizers set ${{ matrix.sanitizers }} run: cmake --build --preset ci --config ${{ matrix.build-type }} -j $(nproc) + - name: Build the headless docker image + run: bash ./scripts/ghidra/build-headless-docker.sh + - name: Test ${{ matrix.build-type }} with sanitizers set ${{ matrix.sanitizers }} run: | - bash ./scripts/ghidra/build-headless-docker.sh - cmake --preset ci - ctest --preset ci --build-config ${{ matrix.build-type }} + lit ./builds/ci/test -v -DCI_OUTPUT_FOLDER=${{ github.workspace }}/builds/ci/test/ghidra/Output --filter=argc.c + + # mkdir -p ./builds/ci/test/ghidra/Output/ + # touch ./builds/ci/test/ghidra/Output/output.json + # sudo chmod -R 777 ./builds/ci/test/ghidra/Output + # /usr/lib/llvm-18/bin/clang ./test/ghidra/test.c -o ./builds/ci/test/ghidra/Output/test.o + # docker run --rm \ + # -v ${{ github.workspace }}/builds/ci/test/ghidra/Output:/mnt/output:rw \ + # trailofbits/patchestry-decompilation:latest \ + # /mnt/output/test.o test /mnt/output/output.json + # cat ./builds/ci/test/ghidra/Output/output.json diff --git a/scripts/ghidra/decompile-entrypoint.sh b/scripts/ghidra/decompile-entrypoint.sh index 7bf13a7..021d128 100644 --- a/scripts/ghidra/decompile-entrypoint.sh +++ b/scripts/ghidra/decompile-entrypoint.sh @@ -13,8 +13,20 @@ if [ "$#" -lt 3 ]; then fi INPUT_PATH=$1 +if [ ! -f $INPUT_PATH ]; then + echo "Input file does not exist" + exit 1 +fi + FUNCTION_NAME=$2 OUTPUT_PATH=$3 +echo "Decompiling function $FUNCTION_NAME from $INPUT_PATH to $OUTPUT_PATH" +chmod -R 777 $OUTPUT_PATH + +if [ ! -f $OUTPUT_PATH ]; then + echo "Output file does not exist" + exit 1 +fi # Create a new Ghidra project and import the file ${GHIDRA_HEADLESS} ${GHIDRA_PROJECTS} patchestry-decompilation \ diff --git a/scripts/ghidra/decompile-headless.sh b/scripts/ghidra/decompile-headless.sh index 1561ef1..263e5ea 100755 --- a/scripts/ghidra/decompile-headless.sh +++ b/scripts/ghidra/decompile-headless.sh @@ -17,6 +17,7 @@ show_help() { echo " -o, --output Path to the output file where results will be saved" echo " -v, --verbose Enable verbose output" echo " -t, --interactive Start Docker container in interactive mode" + echo " -c, --ci Run in CI mode" echo } @@ -52,6 +53,10 @@ while [[ $# -gt 0 ]]; do INTERACTIVE=true shift ;; + -c|--ci) + CI_OUTPUT_FOLDER="$2" + shift 2 + ;; *) echo "Unknown option: $1" show_help @@ -100,24 +105,50 @@ absolute_path() { fi } -INPUT_ABS_PATH=$(absolute_path "$INPUT_PATH") -OUTPUT_ABS_PATH=$(absolute_path "$OUTPUT_PATH") +INPUT_PATH=$(absolute_path "$INPUT_PATH") +OUTPUT_PATH=$(absolute_path "$OUTPUT_PATH") -RUN="docker run --rm \ - -v \"$INPUT_ABS_PATH:/input.o\" \ - -v \"$OUTPUT_ABS_PATH:/output.json\" \ - trailofbits/patchestry-decompilation:latest" +if [ ! -e "$INPUT_PATH" ]; then + echo "Error: Input file does not exist: $INPUT_PATH" + exit 1 +fi -if file "$INPUT_PATH" | grep -q "Mach-O"; then - FUNCTION_NAME="_$FUNCTION_NAME" +if [ ! -e "$OUTPUT_PATH" ]; then + echo "Error: Output file does not exist: $OUTPUT_PATH" + exit 1 fi -if [ "$INTERACTIVE" = true ]; then - RUN=$(echo "$RUN" | sed 's/docker run --rm/docker run -it --rm --entrypoint \/bin\/bash/') +if [ -n $CI_OUTPUT_FOLDER ]; then + mkdir -p $CI_OUTPUT_FOLDER + + INPUT_PATH=$(basename $INPUT_PATH) + OUTPUT_PATH=$(basename $OUTPUT_PATH) + + touch "$CI_OUTPUT_FOLDER/$OUTPUT_PATH" + chmod -R 777 $CI_OUTPUT_FOLDER + + RUN="docker run --rm \ + -v $CI_OUTPUT_FOLDER:/mnt/output:rw \ + trailofbits/patchestry-decompilation:latest \ + /mnt/output/$INPUT_PATH \"$FUNCTION_NAME\" /mnt/output/$OUTPUT_PATH" else - RUN="$RUN /input.o \"$FUNCTION_NAME\" /output.json" + RUN="docker run --rm \ + -v \"$INPUT_PATH:/input.o\" \ + -v \"$OUTPUT_PATH:/output.json\" \ + trailofbits/patchestry-decompilation:latest" + + if file "$INPUT_PATH" | grep -q "Mach-O"; then + FUNCTION_NAME="_$FUNCTION_NAME" + fi + + if [ "$INTERACTIVE" = true ]; then + RUN=$(echo "$RUN" | sed 's/docker run --rm/docker run -it --rm --entrypoint \/bin\/bash/') + else + RUN="$RUN /input.o \"$FUNCTION_NAME\" /output.json" + fi fi + if [ "$VERBOSE" = true ]; then echo "Running Docker container with the following command:" echo "$RUN" diff --git a/test/ghidra/argc.c b/test/ghidra/argc.c index 7ff3561..9078a63 100644 --- a/test/ghidra/argc.c +++ b/test/ghidra/argc.c @@ -1,6 +1,6 @@ // UNSUPPORTED: system-windows // RUN: %cc %s -g -o %t.o -// RUN: %decompile-headless --input %t.o --function argc --output %t +// RUN: %decompile-headless --input %t.o --function argc --output %t %ci_output_folder // RUN: %file-check -vv %s --input-file %t // CHECK: "name":"{{_?argc}}" diff --git a/test/lit.cfg.py b/test/lit.cfg.py index 5ddbeda..2f995f3 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -59,21 +59,6 @@ for tool in tools: llvm_config.add_tool_substitutions([tool]) -# # Add test directory to substitutions -# config.substitutions.append(('%test_dir', os.path.join(config.test_source_root, 'ghidra'))) - # Add PATH to substitutions config.substitutions.append(('%PATH%', config.environment['PATH'])) - -# config.substitutions.append( -# ('%decompile-headless', config.decompiler_headless_tool -# f"'{config.python_executable}' -c '" -# f"import subprocess;" -# f"import sys;" -# f"args = sys.argv[1:4];" -# f"print(args);" -# f"args[1] = \"{config.function_prefix}\" + args[1]; " -# f"result = subprocess.run([\"{config.decompiler_headless_tool}\"] + args, capture_output=True, text=True); " -# f"print(result.stdout + result.stderr); " -# f"sys.exit(result.returncode)' ") -# ) +config.substitutions.append(('%ci_output_folder', f"--ci {llvm_config.lit_config.params.get('CI_OUTPUT_FOLDER', '')}"))