Skip to content

Commit

Permalink
Add --bytes-only flag and basic CLI sanity test
Browse files Browse the repository at this point in the history
  • Loading branch information
stuxnot committed Sep 24, 2024
1 parent 434a4a5 commit 20d8d9e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ if(NYXSTONE_BUILD_EXAMPLES)

include(CTest)
add_test(NAME TestExample COMMAND $<TARGET_FILE:example>)
add_test(NAME TestCLI COMMAND "${CMAKE_CURRENT_LIST_DIR}/tool/test-cli.sh")
endif()
27 changes: 21 additions & 6 deletions examples/nyxstone-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ constexpr auto USAGE = R"(Usage: nyxstone [-t=<triple>] [-p=<pc>] [-d] <input>
-p, --address=<pc> Initial address to assemble/disassemble relative to
-l, --labels=<list> Label-to-address mappings (used when assembling only)
-d, --disassemble Treat <input> as bytes to disassemble instead of assembly
-b, --bytes-only Only output assembled bytes
-h, --help Show this help and usage message
Notes:
Expand All @@ -56,6 +57,7 @@ struct Options {
std::vector<Nyxstone::LabelDefinition> labels;
bool disassemble = false;
bool show_help = false;
bool bytes_only = false;

std::string input;

Expand All @@ -77,6 +79,8 @@ tl::expected<Options, std::string> Options::parse(int argc, char const** argv)
"--address",
"-l",
"--labels",
"-b"
"--bytes-only"
});
args.parse(argc, argv);

Expand Down Expand Up @@ -117,6 +121,8 @@ tl::expected<Options, std::string> Options::parse(int argc, char const** argv)

options.disassemble = args[{ "-d", "--disassemble" }];

options.bytes_only = args[{ "-b", "--bytes-only" }];

if (args.pos_args().size() < 2) {
return tl::unexpected("Missing input");
}
Expand Down Expand Up @@ -172,12 +178,21 @@ int main(int argc, char const** argv)
.map(print_instructions);
} else {
const auto& assembly = options.input;
nyxstone->assemble_to_instructions(assembly, options.address, options.labels)
.map_error([&assembly](const auto& error) {
std::cerr << "Could not assemble " << assembly << " (" << error << ")\n";
exit(1);
})
.map(print_instructions);
if (!options.bytes_only) {
nyxstone->assemble_to_instructions(assembly, options.address, options.labels)
.map_error([&assembly](const auto& error) {
std::cerr << "Could not assemble " << assembly << " (" << error << ")\n";
exit(1);
})
.map(print_instructions);
} else {
nyxstone->assemble(assembly, options.address, options.labels)
.map_error([&assembly](const auto& error) {
std::cerr << "Could not assemble " << assembly << " (" << error << ")\n";
exit(1);
})
.map(print_bytes);
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions tool/test-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

set -eu

# Sanity check that assembling and disassembling yields the same output
# NOTE: Currently we cannot test using labels, since we keep them when assembling.
assembly="cmp rax, rbx; inc rax; add rsp, 8; ret"
address="0xdeadbeef"

assembled=$(./nyxstone -t "x86_64" -p "$address" "$assembly")
assembled_bytes=$(./nyxstone -t "x86_64" --bytes-only -p "$address" "$assembly")
# assembled_bytes="03 02"
disassembled=$(./nyxstone -t "x86_64" -p "$address" -d "$assembled_bytes")

if [[ "$assembled" = "$disassembled" ]]; then
exit 0
else
echo "Output Mismatch"
echo "---------------"
echo "Assembled:"
echo "$assembled"
echo "Disassembled:"
echo "$disassembled"
exit -1
fi

0 comments on commit 20d8d9e

Please sign in to comment.