Skip to content

Commit

Permalink
Update Script to the new process API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole André Vadla Ravnås committed Jan 24, 2014
1 parent 651548f commit facb55c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 49 deletions.
61 changes: 43 additions & 18 deletions gum/gumscriptmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ struct _GumScriptMatchContext

static Handle<Value> gum_script_module_on_enumerate_exports (
const Arguments & args);
static gboolean gum_script_module_handle_export_match (const gchar * name,
GumAddress address, gpointer user_data);
static gboolean gum_script_module_handle_export_match (
const GumExportDetails * details, gpointer user_data);
static const gchar * gum_export_type_to_string (GumExportType type);
static Handle<Value> gum_script_module_on_enumerate_ranges (
const Arguments & args);
static gboolean gum_script_module_handle_range_match (
const GumMemoryRange * range, GumPageProtection prot, gpointer user_data);
const GumRangeDetails * details, gpointer user_data);
static Handle<Value> gum_script_module_on_find_base_address (
const Arguments & args);
static Handle<Value> gum_script_module_on_find_export_by_name (
Expand Down Expand Up @@ -129,18 +130,24 @@ gum_script_module_on_enumerate_exports (const Arguments & args)
}

static gboolean
gum_script_module_handle_export_match (const gchar * name,
GumAddress address,
gum_script_module_handle_export_match (const GumExportDetails * details,
gpointer user_data)
{
GumScriptMatchContext * ctx =
static_cast<GumScriptMatchContext *> (user_data);

Local<Object> exp (Object::New ());
exp->Set (String::New ("type"),
String::New (gum_export_type_to_string (details->type)), ReadOnly);
exp->Set (String::New ("name"),
String::New (details->name), ReadOnly);
exp->Set (String::New ("address"), _gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (details->address)), ReadOnly);

Handle<Value> argv[] = {
String::New (name),
_gum_script_pointer_new (ctx->self->core, GSIZE_TO_POINTER (address))
exp
};
Local<Value> result = ctx->on_match->Call (ctx->receiver, 2, argv);
Local<Value> result = ctx->on_match->Call (ctx->receiver, 1, argv);

gboolean proceed = TRUE;
if (!result.IsEmpty () && result->IsString ())
Expand All @@ -152,6 +159,21 @@ gum_script_module_handle_export_match (const gchar * name,
return proceed;
}

static const gchar *
gum_export_type_to_string (GumExportType type)
{
switch (type)
{
case GUM_EXPORT_FUNCTION: return "function";
case GUM_EXPORT_VARIABLE: return "variable";
default:
break;
}

g_assert_not_reached ();
return NULL;
}

static Handle<Value>
gum_script_module_on_enumerate_ranges (const Arguments & args)
{
Expand Down Expand Up @@ -198,28 +220,31 @@ gum_script_module_on_enumerate_ranges (const Arguments & args)
}

static gboolean
gum_script_module_handle_range_match (const GumMemoryRange * range,
GumPageProtection prot,
gum_script_module_handle_range_match (const GumRangeDetails * details,
gpointer user_data)
{
GumScriptMatchContext * ctx =
static_cast<GumScriptMatchContext *> (user_data);

char prot_str[4] = "---";
if ((prot & GUM_PAGE_READ) != 0)
if ((details->prot & GUM_PAGE_READ) != 0)
prot_str[0] = 'r';
if ((prot & GUM_PAGE_WRITE) != 0)
if ((details->prot & GUM_PAGE_WRITE) != 0)
prot_str[1] = 'w';
if ((prot & GUM_PAGE_EXECUTE) != 0)
if ((details->prot & GUM_PAGE_EXECUTE) != 0)
prot_str[2] = 'x';

Local<Object> range (Object::New ());
range->Set (String::New ("base"), _gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (details->range->base_address)), ReadOnly);
range->Set (String::New ("size"),
Integer::NewFromUnsigned (details->range->size), ReadOnly);
range->Set (String::New ("protection"), String::New (prot_str), ReadOnly);

Handle<Value> argv[] = {
_gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (range->base_address)),
Integer::NewFromUnsigned (range->size),
String::New (prot_str)
range
};
Local<Value> result = ctx->on_match->Call (ctx->receiver, 3, argv);
Local<Value> result = ctx->on_match->Call (ctx->receiver, 1, argv);

gboolean proceed = TRUE;
if (!result.IsEmpty () && result->IsString ())
Expand Down
57 changes: 31 additions & 26 deletions gum/gumscriptprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ static Handle<Value> gum_script_process_on_get_current_thread_id (
const Arguments & args);
static Handle<Value> gum_script_process_on_enumerate_threads (
const Arguments & args);
static gboolean gum_script_process_thread_match (GumThreadDetails * details,
gpointer user_data);
static gboolean gum_script_process_thread_match (
const GumThreadDetails * details, gpointer user_data);
static const gchar * gum_script_thread_state_to_string (GumThreadState state);
static Handle<Value> gum_script_process_on_enumerate_modules (
const Arguments & args);
static gboolean gum_script_process_handle_module_match (const gchar * name,
const GumMemoryRange * range, const gchar * path, gpointer user_data);
static gboolean gum_script_process_handle_module_match (
const GumModuleDetails * details, gpointer user_data);
static Handle<Value> gum_script_process_on_enumerate_ranges (
const Arguments & args);
static gboolean gum_script_process_handle_range_match (const GumMemoryRange * range,
GumPageProtection prot, gpointer user_data);
static gboolean gum_script_process_handle_range_match (
const GumRangeDetails * details, gpointer user_data);

void
_gum_script_process_init (GumScriptProcess * self,
Expand Down Expand Up @@ -150,7 +150,7 @@ gum_script_process_on_enumerate_threads (const Arguments & args)
}

static gboolean
gum_script_process_thread_match (GumThreadDetails * details,
gum_script_process_thread_match (const GumThreadDetails * details,
gpointer user_data)
{
GumScriptMatchContext * ctx =
Expand Down Expand Up @@ -227,22 +227,24 @@ gum_script_process_on_enumerate_modules (const Arguments & args)
}

static gboolean
gum_script_process_handle_module_match (const gchar * name,
const GumMemoryRange * range,
const gchar * path,
gum_script_process_handle_module_match (const GumModuleDetails * details,
gpointer user_data)
{
GumScriptMatchContext * ctx =
static_cast<GumScriptMatchContext *> (user_data);

Local<Object> module (Object::New ());
module->Set (String::New ("name"), String::New (details->name), ReadOnly);
module->Set (String::New ("base"), _gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (details->range->base_address)), ReadOnly);
module->Set (String::New ("size"),
Integer::NewFromUnsigned (details->range->size), ReadOnly);
module->Set (String::New ("path"), String::New (details->path), ReadOnly);

Handle<Value> argv[] = {
String::New (name),
_gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (range->base_address)),
Integer::NewFromUnsigned (range->size),
String::New (path)
module
};
Local<Value> result = ctx->on_match->Call (ctx->receiver, 4, argv);
Local<Value> result = ctx->on_match->Call (ctx->receiver, 1, argv);

gboolean proceed = TRUE;
if (!result.IsEmpty () && result->IsString ())
Expand Down Expand Up @@ -290,28 +292,31 @@ gum_script_process_on_enumerate_ranges (const Arguments & args)
}

static gboolean
gum_script_process_handle_range_match (const GumMemoryRange * range,
GumPageProtection prot,
gum_script_process_handle_range_match (const GumRangeDetails * details,
gpointer user_data)
{
GumScriptMatchContext * ctx =
static_cast<GumScriptMatchContext *> (user_data);

char prot_str[4] = "---";
if ((prot & GUM_PAGE_READ) != 0)
if ((details->prot & GUM_PAGE_READ) != 0)
prot_str[0] = 'r';
if ((prot & GUM_PAGE_WRITE) != 0)
if ((details->prot & GUM_PAGE_WRITE) != 0)
prot_str[1] = 'w';
if ((prot & GUM_PAGE_EXECUTE) != 0)
if ((details->prot & GUM_PAGE_EXECUTE) != 0)
prot_str[2] = 'x';

Local<Object> range (Object::New ());
range->Set (String::New ("base"), _gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (details->range->base_address)), ReadOnly);
range->Set (String::New ("size"),
Integer::NewFromUnsigned (details->range->size), ReadOnly);
range->Set (String::New ("protection"), String::New (prot_str), ReadOnly);

Handle<Value> argv[] = {
_gum_script_pointer_new (ctx->self->core,
GSIZE_TO_POINTER (range->base_address)),
Integer::NewFromUnsigned (range->size),
String::New (prot_str)
range
};
Local<Value> result = ctx->on_match->Call (ctx->receiver, 3, argv);
Local<Value> result = ctx->on_match->Call (ctx->receiver, 1, argv);

gboolean proceed = TRUE;
if (!result.IsEmpty () && result->IsString ())
Expand Down
10 changes: 5 additions & 5 deletions tests/core/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ SCRIPT_TESTCASE (process_modules_can_be_enumerated)
{
COMPILE_AND_LOAD_SCRIPT (
"Process.enumerateModules({"
"onMatch: function(name, address, size, path) {"
"onMatch: function(module) {"
" send('onMatch');"
" return 'stop';"
"},"
Expand All @@ -510,7 +510,7 @@ SCRIPT_TESTCASE (process_ranges_can_be_enumerated)
{
COMPILE_AND_LOAD_SCRIPT (
"Process.enumerateRanges('--x', {"
"onMatch: function(address, size, prot) {"
"onMatch: function(range) {"
" send('onMatch');"
" return 'stop';"
"},"
Expand All @@ -526,7 +526,7 @@ SCRIPT_TESTCASE (module_exports_can_be_enumerated)
{
COMPILE_AND_LOAD_SCRIPT (
"Module.enumerateExports(\"%s\", {"
"onMatch: function(name, address) {"
"onMatch: function(export) {"
" send('onMatch');"
" return 'stop';"
"},"
Expand All @@ -546,7 +546,7 @@ SCRIPT_TESTCASE (module_exports_enumeration_performance)
COMPILE_AND_LOAD_SCRIPT (
"var start = new Date();"
"Module.enumerateExports(\"%s\", {"
"onMatch: function(name, address) {"
"onMatch: function(export) {"
"},"
"onComplete: function() {"
"}"
Expand All @@ -563,7 +563,7 @@ SCRIPT_TESTCASE (module_ranges_can_be_enumerated)
{
COMPILE_AND_LOAD_SCRIPT (
"Module.enumerateRanges(\"%s\", '--x', {"
"onMatch: function(address, size, prot) {"
"onMatch: function(range) {"
" send('onMatch');"
" return 'stop';"
"},"
Expand Down

0 comments on commit facb55c

Please sign in to comment.