Skip to content

Commit 768c0d6

Browse files
committed
[test] Add some assembly json import tests.
1 parent 93a1f35 commit 768c0d6

File tree

5 files changed

+199
-4
lines changed

5 files changed

+199
-4
lines changed

scripts/AsmJsonImportTest.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
3+
# Bash script to test the asm-json-import input mode of the compiler by
4+
# first exporting a .sol file to JSON that containing assembly json and corresponding bytecode,
5+
# then the compiler is invoked in assembly json import mode `--import-asm-json` and uses the previously
6+
# generated assembly json as input, where the corresponding bytecode output will be stored.
7+
# Finally, the originally generated bytecode will be compared with the one that was generated by using the
8+
# assembly json file as input.
9+
10+
set -eo pipefail
11+
READLINK=readlink
12+
if [[ "$OSTYPE" == "darwin"* ]]; then
13+
READLINK=greadlink
14+
fi
15+
REPO_ROOT=$(${READLINK} -f "$(dirname "$0")"/..)
16+
# shellcheck source=scripts/common_import.sh
17+
source "${REPO_ROOT}/scripts/common_import.sh"
18+
19+
SEMANTICTESTS_DIR="${REPO_ROOT}/test/libsolidity/semanticTests"
20+
NSOURCES="$(find "$SEMANTICTESTS_DIR" -type f | wc -l)"
21+
22+
init_import_tests
23+
24+
# function tests whether importing an assembly json file creates identical bytecode.
25+
# Results are recorded by adding to FAILED or UNCOMPILABLE.
26+
# Also, in case of a mismatch a diff and the respective ASTs are printed
27+
# Expected parameters:
28+
# $1 name of the file to be exported and imported
29+
# $2 any files needed to do so that might be in parent directories
30+
function testImportExportEquivalence {
31+
local nth_input_file="$1"
32+
IFS=" " read -r -a all_input_files <<< "$2"
33+
34+
if $SOLC "$nth_input_file" "${all_input_files[@]}" --combined-json asm,bin > /dev/null 2>&1
35+
then
36+
local types=( "bin" "bin-runtime" "opcodes" "asm" "srcmap" "srcmap-runtime" )
37+
local test_types=( "bin" "bin-runtime" "opcodes" "asm" )
38+
39+
# save exported json as expected result (silently)
40+
$SOLC --combined-json asm,opcodes,bin,srcmap,srcmap-runtime,bin-runtime --pretty-json "$nth_input_file" "${all_input_files[@]}" > expected.json 2> /dev/null
41+
for contract in $(jq '.contracts | keys | .[]' expected.json 2> /dev/null)
42+
do
43+
for type in "${types[@]}"
44+
do
45+
jq --raw-output ".contracts.${contract}.\"${type}\"" expected.json > "expected.${type}"
46+
done
47+
expected_bin=$(cat expected.bin)
48+
if [[ $expected_bin == "" ]]
49+
then
50+
continue
51+
fi
52+
53+
if ! "$SOLC" --import-asm-json expected.asm --combined-json asm,opcodes,bin,srcmap,srcmap-runtime,bin-runtime > imported.json 2> /dev/null
54+
then
55+
# For investigating, use exit 1 here so the script stops at the
56+
# first failing test
57+
# exit 1
58+
echo ""
59+
echo "Failed with contract ${contract}!?"
60+
echo ""
61+
FAILED=$((FAILED + 1))
62+
return 1
63+
fi
64+
65+
for type in "${test_types[@]}"
66+
do
67+
jq --raw-output ".contracts.\"expected.asm\".\"${type}\"" imported.json > "imported.${type}"
68+
if ! diff "expected.${type}" "imported.${type}"
69+
then
70+
echo ""
71+
echo "Failed with contract ${contract} (${type})."
72+
echo ""
73+
if [ "$DIFFVIEW" == "" ]
74+
then
75+
echo "Expected:"
76+
cat "./expected.${type}"
77+
echo "Obtained:"
78+
cat "./imported.${type}"
79+
else
80+
# Use user supplied diff view binary
81+
$DIFFVIEW "expected.${type}" "imported.${type}"
82+
fi
83+
FAILED=$((FAILED + 1))
84+
return 2
85+
fi
86+
done
87+
done
88+
TESTED=$((TESTED + 1))
89+
rm -f expected.json asm.json expected.bin imported.bin
90+
else
91+
# echo "contract $solfile could not be compiled "
92+
UNCOMPILABLE=$((UNCOMPILABLE + 1))
93+
fi
94+
# return 0
95+
}
96+
echo "Looking at $NSOURCES .sol files..."
97+
98+
TEST_FILES=$(find "$SEMANTICTESTS_DIR" -name "*.sol")
99+
run_import_tests "$TEST_FILES" "$SPLITSOURCES" "$NSOURCES" "$PWD"

scripts/common_import.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
function init_import_tests()
6+
{
7+
export SOLIDITY_BUILD_DIR=${SOLIDITY_BUILD_DIR:-${REPO_ROOT}/build}
8+
export SOLC=${SOLIDITY_BUILD_DIR}/solc/solc
9+
export SPLITSOURCES=${REPO_ROOT}/scripts/splitSources.py
10+
export FAILED=0
11+
export UNCOMPILABLE=0
12+
export TESTED=0
13+
14+
if [[ "$(find . -maxdepth 0 -type d -empty)" == "" ]]; then
15+
echo "Test directory not empty. Skipping!"
16+
exit 1
17+
fi
18+
}
19+
20+
function run_import_tests()
21+
{
22+
local TEST_FILES=$1
23+
local SPLITSOURCES=$2
24+
local NSOURCES=$3
25+
local WORKINGDIR=$4
26+
27+
for solfile in $TEST_FILES; do
28+
echo -n "."
29+
# create a temporary sub-directory
30+
local FILETMP
31+
FILETMP=$(mktemp -d)
32+
cd "$FILETMP"
33+
34+
set +e
35+
local OUTPUT
36+
OUTPUT=$("$SPLITSOURCES" "$solfile")
37+
local SPLITSOURCES_RC=$?
38+
set -e
39+
if [ ${SPLITSOURCES_RC} == 0 ]; then
40+
# echo $OUTPUT
41+
NSOURCES=$((NSOURCES - 1))
42+
for i in $OUTPUT; do
43+
testImportExportEquivalence "$i" "$OUTPUT"
44+
NSOURCES=$((NSOURCES + 1))
45+
done
46+
elif [ ${SPLITSOURCES_RC} == 1 ]; then
47+
testImportExportEquivalence "$solfile"
48+
elif [ ${SPLITSOURCES_RC} == 2 ]; then
49+
# The script will exit with return code 2, if an UnicodeDecodeError occurred.
50+
# This is the case if e.g. some tests are using invalid utf-8 sequences. We will ignore
51+
# these errors, but print the actual output of the script.
52+
echo -e "\n${OUTPUT}\n"
53+
testImportExportEquivalence "$solfile"
54+
else
55+
# All other return codes will be treated as critical errors. The script will exit.
56+
echo -e "\nGot unexpected return code ${SPLITSOURCES_RC} from ${SPLITSOURCES}. Aborting."
57+
echo -e "\n${OUTPUT}\n"
58+
59+
cd "$WORKINGDIR"
60+
# Delete temporary files
61+
rm -rf "$FILETMP"
62+
63+
exit 1
64+
fi
65+
66+
cd "$WORKINGDIR"
67+
# Delete temporary files
68+
rm -rf "$FILETMP"
69+
done
70+
71+
echo ""
72+
73+
if [ "$FAILED" = 0 ]; then
74+
echo "SUCCESS: $TESTED tests passed, $FAILED failed, $UNCOMPILABLE could not be compiled ($NSOURCES sources total)."
75+
else
76+
echo "FAILURE: Out of $NSOURCES sources, $FAILED failed, ($UNCOMPILABLE could not be compiled)."
77+
exit 1
78+
fi
79+
}

solc/CommandLineInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ bool CommandLineInterface::compile()
790790
return false;
791791
}
792792
}
793-
if (m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport)
793+
else if (m_options.input.mode == InputMode::CompilerWithEvmAssemblyJsonImport)
794794
{
795795
solAssert(m_fileReader.sourceCodes().size() == 1, "");
796796
try

test/cmdlineTests.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function ask_expectation_update
154154

155155
# General helper function for testing SOLC behaviour, based on file name, compile opts, exit code, stdout and stderr.
156156
# An failure is expected.
157-
function test_solc_behaviour
157+
function test_solc_behaviour()
158158
{
159159
local filename="${1}"
160160
local solc_args
@@ -288,7 +288,7 @@ EOF
288288
}
289289

290290

291-
function test_solc_assembly_output
291+
function test_solc_assembly_output()
292292
{
293293
local input="${1}"
294294
local expected="${2}"
@@ -572,6 +572,18 @@ SOLTMPDIR=$(mktemp -d)
572572
)
573573
rm -r "$SOLTMPDIR"
574574

575+
printTask "Testing ASM-JSON import..."
576+
SOLTMPDIR=$(mktemp -d)
577+
(
578+
cd "$SOLTMPDIR"
579+
if ! "$REPO_ROOT/scripts/AsmJsonImportTest.sh"
580+
then
581+
rm -rf "$SOLTMPDIR"
582+
exit 1
583+
fi
584+
)
585+
rm -rf "$SOLTMPDIR"
586+
575587
printTask "Testing AST export with stop-after=parsing..."
576588
"$REPO_ROOT/test/stopAfterParseTests.sh"
577589

test/cmdlineTests/asm_json/output

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,5 +1582,10 @@ EVM assembly:
15821582
}
15831583
]
15841584
}
1585-
}
1585+
},
1586+
"sourceList":
1587+
[
1588+
"asm_json/input.sol",
1589+
"#utility.yul"
1590+
]
15861591
}

0 commit comments

Comments
 (0)