JonathanSalwan / Triton

Triton is a dynamic binary analysis library. Build your own program analysis tools, automate your reverse engineering, perform software verification or just emulate code.
https://triton-library.github.io
Apache License 2.0
3.4k stars 525 forks source link

Unable to pass additional arguments to callbacks #1170

Closed archercreat closed 1 year ago

archercreat commented 1 year ago

Hello! I could not find a way to pass additional arguments to instrumentation callbacks (e.g. get/set concrete memory). Usually I do it with bind:

api.addCallback(triton::callbacks::callback_e::GET_CONCRETE_MEMORY_VALUE, 
  std::bind(&my_class::read_memory_cb, this, std::placeholder::_01, std::placeholder::_02);

But bind doesn't work with addCallback api. Is there another way to do this?

JonathanSalwan commented 1 year ago

Hey,

yes this is something that is missing. It's been I while that I think about passing an argument as void*. I will do it as soon as possible (~3 weeks). Meanwhile, an ugly possibility could be to to put your arg in a global variable.

archercreat commented 1 year ago

I'll try to make a pull request on weekends, thanks for confirming the issue!

aris-bb commented 1 year ago

You can use a lambda to capture your custom argument like this:

void my_class::fn()
{
  auto get_memory_callback = [this](triton::Context& ctx, const triton::arch::MemoryAccess& mem)
  {

  };

  ctx->addCallback
  (
      triton::callbacks::GET_CONCRETE_MEMORY_VALUE,
      triton::callbacks::getConcreteMemoryValueCallback{ get_memory_callback, &get_memory_callback }
  );
}
archercreat commented 1 year ago

Hey! Sorry for the long reply, I got sick.. The example that @aris-bb gave is sufficient for me.

JonathanSalwan commented 1 year ago

I hope you recovered well!

he example that @aris-bb gave is sufficient for me.

Let's close this issue then