Skip to content

Commit

Permalink
Add decoder ctor and dtor
Browse files Browse the repository at this point in the history
There is one usage of Capstone per decoder. This should be threadsafe
as it doesn't make sense to use a decoder with multiple threads
simultaneously. See comments in ctor for why there are two Capstone
handles per decoder.
  • Loading branch information
hainest committed Dec 14, 2023
1 parent 7745234 commit 433cb1d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
36 changes: 33 additions & 3 deletions instructionAPI/src/x86/decoder.C
Expand Up @@ -7,11 +7,41 @@

namespace Dyninst { namespace InstructionAPI {

x86_decoder::x86_decoder(Dyninst::Architecture a): InstructionDecoderImpl(a),
mode{a == Dyninst::Arch_x86_64 ? CS_MODE_64 : CS_MODE_32} {}
x86_decoder::x86_decoder(Dyninst::Architecture a): InstructionDecoderImpl(a) {

x86_decoder::~x86_decoder() {}
mode = (a == Dyninst::Arch_x86_64) ? CS_MODE_64 : CS_MODE_32;

auto create = [this](disassem &d, cs_opt_value v) {
cs_open(CS_ARCH_X86, this->mode, &d.handle);
cs_option(d.handle, CS_OPT_DETAIL, v);
d.insn = cs_malloc(d.handle);
};

/*
* With details enabled, a Capstone instruction object has complete information.
*
* This is used in 'decodeOperands' because all of the details are needed.
*/
create(dis_with_detail, CS_OPT_ON);

/*
* Without details, a Capstone instruction object has fewer populated fields
* (e.g., no operand details) so takes up less space and time. Capstone instruction
* objects _always_ populate the mnemonic and a string representation of the operands.
*
* This is used in 'decodeOpcode' to quickly create an Instruction object.
*/
create(dis_without_detail, CS_OPT_OFF);
}

x86_decoder::~x86_decoder() {
cs_free(dis_with_detail.insn, 1);
cs_close(&dis_with_detail.handle);

cs_free(dis_without_detail.insn, 1);
cs_close(&dis_without_detail.handle);
}

void x86_decoder::doDelayedDecode(Instruction const*) {}
void x86_decoder::decodeOpcode(InstructionDecoder::buffer&) {}
bool x86_decoder::decodeOperands(Instruction const*) { return true; }
Expand Down
8 changes: 7 additions & 1 deletion instructionAPI/src/x86/decoder.h
Expand Up @@ -40,7 +40,13 @@ namespace Dyninst { namespace InstructionAPI {

class x86_decoder final : public InstructionDecoderImpl {
cs_mode mode{};
csh handle{};

struct disassem final {
csh handle{};
cs_insn *insn{};
};
disassem dis_with_detail{};
disassem dis_without_detail{};

public:

Expand Down

0 comments on commit 433cb1d

Please sign in to comment.