Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Sep 6, 2024
1 parent 485b24d commit 22d9ae7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 31 deletions.
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions scripts/ghidra/decompile-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
53 changes: 42 additions & 11 deletions scripts/ghidra/decompile-headless.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/ghidra/argc.c
Original file line number Diff line number Diff line change
@@ -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}}"

Expand Down
17 changes: 1 addition & 16 deletions test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')}"))

0 comments on commit 22d9ae7

Please sign in to comment.