Skip to content

Commit

Permalink
Merge pull request #35 from emproof-com/refactor/external-recommendat…
Browse files Browse the repository at this point in the history
…ions

Refactor according to some internal and external recommendations
  • Loading branch information
pkoppe authored Oct 27, 2023
2 parents 290f807 + 09a9b9d commit 78fd158
Show file tree
Hide file tree
Showing 15 changed files with 576 additions and 705 deletions.
69 changes: 3 additions & 66 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,67 +1,4 @@
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: false
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
BreakStringLiterals: false
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
FixNamespaceComments: true
IncludeBlocks: Regroup
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
BasedOnStyle: WebKit
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
TabWidth: 4
UseTab: Never
UseTab: Never
ColumnLimit: 120
68 changes: 24 additions & 44 deletions bindings/python/pynyxstone.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <sstream>
#include <iomanip>
#include <sstream>
#include <unordered_map>

#include <nyxstone.h>

namespace py = pybind11;

std::vector<Nyxstone::LabelDefinition> convert_labels(std::unordered_map<std::string, uint64_t>&& labels) {
using namespace nyxstone;

std::vector<Nyxstone::LabelDefinition> convert_labels(std::unordered_map<std::string, uint64_t>&& labels)
{
std::vector<Nyxstone::LabelDefinition> vlabels {};
vlabels.reserve(labels.size());

for (auto [label, address] : labels) {
vlabels.push_back(Nyxstone::LabelDefinition {std::move(label), address});
vlabels.push_back(Nyxstone::LabelDefinition { std::move(label), address });
}

return vlabels;
}

std::vector<uint8_t> assemble_to_bytes(
Nyxstone& nyxstone,
std::string assembly,
uint64_t address,
std::unordered_map<std::string, uint64_t> labels) {
Nyxstone& nyxstone, std::string assembly, uint64_t address, std::unordered_map<std::string, uint64_t> labels)
{
auto vlabels = convert_labels(std::move(labels));

std::vector<uint8_t> bytes;
Expand All @@ -33,44 +34,41 @@ std::vector<uint8_t> assemble_to_bytes(
}

std::vector<Nyxstone::Instruction> assemble_to_instructions(
Nyxstone& nyxstone,
std::string assembly,
uint64_t address,
std::unordered_map<std::string, uint64_t> labels) {
Nyxstone& nyxstone, std::string assembly, uint64_t address, std::unordered_map<std::string, uint64_t> labels)
{
auto vlabels = convert_labels(std::move(labels));

std::vector<Nyxstone::Instruction> instructions;
nyxstone.assemble_to_instructions(assembly, address, vlabels, instructions);
return instructions;
}

std::string disassemble_to_text(Nyxstone& nyxstone, std::vector<uint8_t> bytes, uint64_t address, uint64_t count) {
std::string disassemble_to_text(Nyxstone& nyxstone, std::vector<uint8_t> bytes, uint64_t address, uint64_t count)
{
std::string assembly;
nyxstone.disassemble_to_text(bytes, address, count, assembly);
return assembly;
}

std::vector<Nyxstone::Instruction>
disassemble_to_instructions(Nyxstone& nyxstone, std::vector<uint8_t> bytes, uint64_t address, uint64_t count) {
std::vector<Nyxstone::Instruction> disassemble_to_instructions(
Nyxstone& nyxstone, std::vector<uint8_t> bytes, uint64_t address, uint64_t count)
{
std::vector<Nyxstone::Instruction> instructions;
nyxstone.disassemble_to_instructions(bytes, address, count, instructions);
return instructions;
}

PYBIND11_MODULE(nyxstone, m) {
PYBIND11_MODULE(nyxstone, m)
{
m.doc() = "pybind11 plugin for nyxstone";

py::class_<Nyxstone::Instruction>(m, "Instruction")
.def(
py::init<uint64_t, std::string, std::vector<uint8_t>>(),
py::arg("address"),
py::arg("assembly"),
.def(py::init<uint64_t, std::string, std::vector<uint8_t>>(), py::arg("address"), py::arg("assembly"),
py::arg("bytes"))
.def_readwrite("address", &Nyxstone::Instruction::address)
.def_readwrite("bytes", &Nyxstone::Instruction::bytes)
.def_readwrite("assembly", &Nyxstone::Instruction::assembly)
.def(
"__eq__",
.def("__eq__",
[](const Nyxstone::Instruction& self, const Nyxstone::Instruction& other) {
return self.address == other.address && self.assembly == other.assembly && self.bytes == other.bytes;
})
Expand All @@ -96,38 +94,20 @@ PYBIND11_MODULE(nyxstone, m) {
.def("with_triple", &NyxstoneBuilder::with_triple, "Specify the llvm target triple")
.def("with_features", &NyxstoneBuilder::with_features, "Specify the llvm features to be en- or disabled")
.def("with_cpu", &NyxstoneBuilder::with_cpu, "Specify the cpu to use")
.def(
"with_immediate_style",
&NyxstoneBuilder::with_immediate_style,
.def("with_immediate_style", &NyxstoneBuilder::with_immediate_style,
"Specify the style in which immediates are printed")
.def("build", &NyxstoneBuilder::build, "Build the Nyxstone instance");

py::class_<Nyxstone>(m, "Nyxstone")
.def(
"assemble_to_bytes",
&assemble_to_bytes,
py::arg("assembly"),
py::arg("address") = 0x0,
.def("assemble_to_bytes", &assemble_to_bytes, py::arg("assembly"), py::arg("address") = 0x0,
py::arg("labels") = py::dict {})
.def(
"assemble_to_instructions",
&assemble_to_instructions,
py::arg("assembly"),
py::arg("address") = 0x0,
.def("assemble_to_instructions", &assemble_to_instructions, py::arg("assembly"), py::arg("address") = 0x0,
py::arg("labels") = py::dict {})
.def(
"disassemble_to_text",
&disassemble_to_text,
py::arg("bytes"),
py::arg("address") = 0x0,
.def("disassemble_to_text", &disassemble_to_text, py::arg("bytes"), py::arg("address") = 0x0,
py::arg("count") = 0,
"Disassemble bytes to assembly text.\n"
"count specifies the number of instructions to disassemble, '0' means all instructions")
.def(
"disassemble_to_instructions",
&disassemble_to_instructions,
py::arg("bytes"),
py::arg("address") = 0x0,
.def("disassemble_to_instructions", &disassemble_to_instructions, py::arg("bytes"), py::arg("address") = 0x0,
py::arg("count") = 0x0,
"Disassemble bytes to instruction information.\n"
"count specifies the number of instructions to disassemble, '0' means all instructions");
Expand Down
50 changes: 25 additions & 25 deletions bindings/rust/src/nyxstone_ffi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "nyxstone_ffi.hpp"

using namespace nyxstone;

struct LabelDefinition final {
rust::str name {};
uint64_t address = 0;
Expand All @@ -12,17 +14,16 @@ struct Instruction final {
};

rust::Vec<uint8_t> NyxstoneFFI::assemble_to_bytes(
const rust::str assembly,
uint64_t address,
const rust::Slice<const LabelDefinition> labels) const {
const rust::str assembly, uint64_t address, const rust::Slice<const LabelDefinition> labels) const
{
std::vector<Nyxstone::LabelDefinition> cpp_labels {};
cpp_labels.reserve(labels.size());
std::transform(std::begin(labels), std::end(labels), std::back_inserter(cpp_labels), [](const auto& label) {
return Nyxstone::LabelDefinition {std::string(label.name), label.address};
return Nyxstone::LabelDefinition { std::string(label.name), label.address };
});
std::vector<uint8_t> cpp_bytes {};

nyxstone->assemble_to_bytes(std::string {assembly}, address, cpp_labels, cpp_bytes);
nyxstone->assemble_to_bytes(std::string { assembly }, address, cpp_labels, cpp_bytes);

rust::Vec<uint8_t> bytes;
bytes.reserve(cpp_bytes.size());
Expand All @@ -32,44 +33,45 @@ rust::Vec<uint8_t> NyxstoneFFI::assemble_to_bytes(
}

rust::Vec<Instruction> NyxstoneFFI::assemble_to_instructions(
const rust::str assembly,
uint64_t address,
const rust::Slice<const LabelDefinition> labels) const {
const rust::str assembly, uint64_t address, const rust::Slice<const LabelDefinition> labels) const
{
std::vector<Nyxstone::LabelDefinition> cpp_labels;
cpp_labels.reserve(labels.size());
std::transform(std::begin(labels), std::end(labels), std::back_inserter(cpp_labels), [](const auto& label) {
return Nyxstone::LabelDefinition {std::string(label.name), label.address};
return Nyxstone::LabelDefinition { std::string(label.name), label.address };
});
std::vector<Nyxstone::Instruction> cpp_instructions {};

nyxstone->assemble_to_instructions(std::string {assembly}, address, cpp_labels, cpp_instructions);
nyxstone->assemble_to_instructions(std::string { assembly }, address, cpp_labels, cpp_instructions);

rust::Vec<Instruction> instructions {};
instructions.reserve(cpp_instructions.size());
for (const auto& cpp_insn : cpp_instructions) {
rust::Vec<uint8_t> bytes;
bytes.reserve(cpp_insn.bytes.size());
std::copy(cpp_insn.bytes.begin(), cpp_insn.bytes.end(), std::back_inserter(bytes));
instructions.push_back({cpp_insn.address, rust::String(cpp_insn.assembly), bytes});
instructions.push_back({ cpp_insn.address, rust::String(cpp_insn.assembly), bytes });
}

return instructions;
}

rust::String
NyxstoneFFI::disassemble_to_text(const rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const {
rust::String NyxstoneFFI::disassemble_to_text(
const rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const
{
std::vector<uint8_t> cpp_bytes;
cpp_bytes.reserve(bytes.size());
std::copy(bytes.begin(), bytes.end(), std::back_inserter(cpp_bytes));
std::string cpp_disassembly;

nyxstone->disassemble_to_text(cpp_bytes, address, count, cpp_disassembly);

return {cpp_disassembly};
return { cpp_disassembly };
}

rust::Vec<Instruction>
NyxstoneFFI::disassemble_to_instructions(const rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const {
rust::Vec<Instruction> NyxstoneFFI::disassemble_to_instructions(
const rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const
{
std::vector<uint8_t> cpp_bytes {};
cpp_bytes.reserve(bytes.size());
std::copy(bytes.begin(), bytes.end(), std::back_inserter(cpp_bytes));
Expand All @@ -83,23 +85,21 @@ NyxstoneFFI::disassemble_to_instructions(const rust::Slice<const uint8_t> bytes,
rust::Vec<uint8_t> insn_bytes;
insn_bytes.reserve(cpp_insn.bytes.size());
std::copy(cpp_insn.bytes.begin(), cpp_insn.bytes.end(), std::back_inserter(insn_bytes));
instructions.push_back({cpp_insn.address, rust::String(cpp_insn.assembly), std::move(insn_bytes)});
instructions.push_back({ cpp_insn.address, rust::String(cpp_insn.assembly), std::move(insn_bytes) });
}

return instructions;
}

std::unique_ptr<NyxstoneFFI> create_nyxstone_ffi( // cppcheck-suppress unusedFunction
const rust::str triple_name,
const rust::str cpu,
const rust::str features,
const IntegerBase imm_style) {
std::unique_ptr<NyxstoneFFI> create_nyxstone_ffi( // cppcheck-suppress unusedFunction
const rust::str triple_name, const rust::str cpu, const rust::str features, const IntegerBase imm_style)
{
NyxstoneBuilder::IntegerBase style = static_cast<NyxstoneBuilder::IntegerBase>(static_cast<uint8_t>(imm_style));

return std::make_unique<NyxstoneFFI>(NyxstoneBuilder()
.with_triple(std::string {triple_name})
.with_cpu(std::string {cpu})
.with_features(std::string {features})
.with_triple(std::string { triple_name })
.with_cpu(std::string { cpu })
.with_features(std::string { features })
.with_immediate_style(style)
.build());
}
31 changes: 17 additions & 14 deletions bindings/rust/src/nyxstone_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ enum class IntegerBase : uint8_t;
// See class and function documentation in Nyxstone.h for further info.
class NyxstoneFFI {
// Internal Nyxstone instance
std::unique_ptr<Nyxstone> nyxstone;
std::unique_ptr<nyxstone::Nyxstone> nyxstone;

public:
public:
/**
* @brief Constructor for NyxstoneFFI.
* @param nyxstone Unique_ptr holding the Nyxstone instance.
*/
explicit NyxstoneFFI(std::unique_ptr<Nyxstone>&& nyxstone) : nyxstone(std::move(nyxstone)) {}
* @brief Constructor for NyxstoneFFI.
* @param nyxstone Unique_ptr holding the Nyxstone instance.
*/
explicit NyxstoneFFI(std::unique_ptr<nyxstone::Nyxstone>&& nyxstone)
: nyxstone(std::move(nyxstone))
{
}
~NyxstoneFFI() = default;

NyxstoneFFI(const NyxstoneFFI& other) = delete;
NyxstoneFFI(NyxstoneFFI&& other) = delete;

rust::Vec<uint8_t>
assemble_to_bytes(rust::str assembly, uint64_t address, rust::Slice<const LabelDefinition> labels) const;
rust::Vec<uint8_t> assemble_to_bytes(
rust::str assembly, uint64_t address, rust::Slice<const LabelDefinition> labels) const;

rust::Vec<Instruction>
assemble_to_instructions(rust::str assembly, uint64_t address, rust::Slice<const LabelDefinition> labels) const;
rust::Vec<Instruction> assemble_to_instructions(
rust::str assembly, uint64_t address, rust::Slice<const LabelDefinition> labels) const;

rust::String disassemble_to_text(rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const;

rust::Vec<Instruction>
disassemble_to_instructions(rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const;
rust::Vec<Instruction> disassemble_to_instructions(
rust::Slice<const uint8_t> bytes, uint64_t address, size_t count) const;
};

/// @brief Creates a NyxstoneFFI instance for the specified triple with the specified CPU and features.
Expand All @@ -48,5 +51,5 @@ class NyxstoneFFI {
/// @param cpu The cpu to be used.
/// @param features Llvm features string.
/// @param imm_style The integer representation for immediates.
std::unique_ptr<NyxstoneFFI>
create_nyxstone_ffi(rust::str triple_name, rust::str cpu, rust::str features, IntegerBase imm_style);
std::unique_ptr<NyxstoneFFI> create_nyxstone_ffi(
rust::str triple_name, rust::str cpu, rust::str features, IntegerBase imm_style);
Loading

0 comments on commit 78fd158

Please sign in to comment.