Latest available version: IDA and decompilers v8.4.240320sp1 see all releases
Hex-Rays logo State-of-the-art binary code analysis tools
email icon
hexrays_sample10.cpp
/*
* Hex-Rays Decompiler project
* Copyright (c) 2007-2024 by Hex-Rays, support@hex-rays.com
* ALL RIGHTS RESERVED.
*
* Sample plugin for Hex-Rays Decompiler.
* It installs a custom microcode optimization rule:
* call !DbgRaiseAssertionFailure <fast:>.0
* =>
* call !DbgRaiseAssertionFailure <fast:"char *" "assertion text">.0
*
* To see this plugin in action please use arm64_brk.i64
*/
#include <hexrays.hpp>
//--------------------------------------------------------------------------
struct nt_assert_optimizer_t : public optinsn_t
{
virtual int idaapi func(mblock_t *, minsn_t *ins, int /*optflags*/) override
{
if ( handle_nt_assert(ins) )
return 1;
return 0;
}
//lint -e{818} ins could be made const
bool handle_nt_assert(minsn_t *ins) const
{
// recognize call !DbgRaiseAssertionFailure <fast:>.0
if ( !ins->is_helper("DbgRaiseAssertionFailure") )
return false;
// did we already add an argument?
mcallinfo_t &fi = *ins->d.f;
if ( !fi.args.empty() )
return false;
// use a comment from the disassembly listing as the call argument
qstring cmt;
if ( !get_cmt(&cmt, ins->ea, false) )
return false;
// remove "NT_ASSERT(" to make the listing nicer
if ( cmt.starts_with("NT_ASSERT(\"") )
cmt.remove(0, 11);
if ( cmt.ends_with("\")") )
cmt.remove_last(2);
// all ok, transform the instruction by adding one more call argument
mcallarg_t &fa = fi.args.push_back();
fa.t = mop_str;
fa.cstr = cmt.extract();
fa.type = tinfo_t::get_stock(STI_PCCHAR); // const char *
fa.size = fa.type.get_size();
return true;
}
};
//--------------------------------------------------------------------------
struct plugin_ctx_t : public plugmod_t
{
nt_assert_optimizer_t nt_assert_optimizer;
plugin_ctx_t()
{
install_optinsn_handler(&nt_assert_optimizer);
}
~plugin_ctx_t()
{
remove_optinsn_handler(&nt_assert_optimizer);
}
virtual bool idaapi run(size_t) override;
};
//--------------------------------------------------------------------------
static plugmod_t *idaapi init()
{
return nullptr; // no decompiler
const char *hxver = get_hexrays_version();
msg("Hex-rays version %s has been detected, %s ready to use\n",
hxver, PLUGIN.wanted_name);
return new plugin_ctx_t;
}
//--------------------------------------------------------------------------
bool idaapi plugin_ctx_t::run(size_t arg)
{
if ( arg == 1 )
{
remove_optinsn_handler(&nt_assert_optimizer);
msg("%s disabled\n", PLUGIN.wanted_name);
}
else if ( arg == 2 )
{
install_optinsn_handler(&nt_assert_optimizer);
msg("%s enabled\n", PLUGIN.wanted_name);
}
else
{
msg("The %d arg is unknown (1 disable, 2 enable)\n", int(arg));
}
return false;
}
//--------------------------------------------------------------------------
static const char comment[] = "Sample10 plugin for Hex-Rays decompiler";
//--------------------------------------------------------------------------
//
// PLUGIN DESCRIPTION BLOCK
//
//--------------------------------------------------------------------------
plugin_t PLUGIN =
{
IDP_INTERFACE_VERSION,
PLUGIN_HIDE // Plugin should not appear in the Edit, Plugins menu
| PLUGIN_MULTI, // The plugin can work with multiple idbs in parallel
init, // initialize
nullptr,
nullptr,
comment, // long comment about the plugin
nullptr, // multiline help about the plugin
"Optimize DbgRaiseAssertionFailure", // the preferred short name of the plugin
nullptr, // the preferred hotkey to run the plugin
};
Microcode of one basic block.
Definition: hexrays.hpp:3805
A call argument.
Definition: hexrays.hpp:3011
tinfo_t type
formal argument type
Definition: hexrays.hpp:3015
Information about a call.
Definition: hexrays.hpp:3126
mcallargs_t args
call arguments
Definition: hexrays.hpp:3134
Microinstruction class #insn.
Definition: hexrays.hpp:3465
ea_t ea
instruction address
Definition: hexrays.hpp:3473
mop_t d
destination operand
Definition: hexrays.hpp:3476
bool is_helper(const char *name) const
Is a helper call with the specified name? Helper calls usually have well-known function names (see We...
Definition: hexrays.hpp:10981
mopt_t t
Operand type.
Definition: hexrays.hpp:2445
int size
Operand size.
Definition: hexrays.hpp:2465
HexRays SDK header file.
bool init_hexrays_plugin(int flags=0)
Check that your plugin is compatible with hex-rays decompiler.
Definition: hexrays.hpp:8601
const mopt_t mop_str
immediate string constant (user representation)
Definition: hexrays.hpp:2258
void install_optinsn_handler(optinsn_t *opt)
Install an instruction level custom optimizer.
Definition: hexrays.hpp:10477
void term_hexrays_plugin()
Stop working with hex-rays decompiler.
Definition: hexrays.hpp:8609
bool remove_optinsn_handler(optinsn_t *opt)
Remove an instruction level custom optimizer.
Definition: hexrays.hpp:10483
const char * get_hexrays_version()
Get decompiler version.
Definition: hexrays.hpp:11511
User defined callback to optimize individual microcode instructions.
Definition: hexrays.hpp:2109
virtual int func(mblock_t *blk, minsn_t *ins, int optflags)=0
Optimize an instruction.