Skip to content

Commit

Permalink
Add Memory.writeVolatile()
Browse files Browse the repository at this point in the history
  • Loading branch information
DoranekoSystems committed Jan 28, 2025
1 parent 752bf40 commit 8e706f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
31 changes: 31 additions & 0 deletions bindings/gumjs/gumquickmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static JSValue gum_quick_memory_read (JSContext * ctx, GumMemoryValueType type,
static JSValue gum_quick_memory_write (JSContext * ctx, GumMemoryValueType type,
GumQuickArgs * args, GumQuickCore * core);
GUMJS_DECLARE_FUNCTION (gum_quick_memory_read_volatile)
GUMJS_DECLARE_FUNCTION (gum_quick_memory_write_volatile)

static void gum_quick_memory_on_access (GumMemoryAccessMonitor * monitor,
const GumMemoryAccessDetails * details, GumQuickMemory * self);
Expand Down Expand Up @@ -197,6 +198,7 @@ static const JSCFunctionListEntry gumjs_memory_entries[] =
GUMJS_EXPORT_MEMORY_READ_WRITE ("Utf16String", UTF16_STRING),
GUMJS_EXPORT_MEMORY_READ_WRITE ("AnsiString", ANSI_STRING),
JS_CFUNC_DEF ("readVolatile", 0, gum_quick_memory_read_volatile),
JS_CFUNC_DEF ("writeVolatile", 0, gum_quick_memory_write_volatile),

JS_CFUNC_DEF ("allocAnsiString", 0, gumjs_memory_alloc_ansi_string),
JS_CFUNC_DEF ("allocUtf8String", 0, gumjs_memory_alloc_utf8_string),
Expand Down Expand Up @@ -880,6 +882,35 @@ GUMJS_DEFINE_FUNCTION (gum_quick_memory_read_volatile)
_gum_quick_array_buffer_free, data, FALSE);
}

GUMJS_DEFINE_FUNCTION (gum_quick_memory_write_volatile)
{
gpointer address;
GBytes * bytes;
gconstpointer data;
gsize size;

if (!_gum_quick_args_parse (args, "pB", &address, &bytes))
return JS_EXCEPTION;

data = g_bytes_get_data (bytes, &size);

if (size == 0)
{
g_bytes_unref (bytes);
return JS_TRUE;
}

if (!gum_memory_write (address, (guint8 *) data, size))
{
g_bytes_unref (bytes);
return _gum_quick_throw_literal (ctx, "memory write failed");
}

g_bytes_unref (bytes);

return JS_TRUE;
}

#ifdef HAVE_WINDOWS

static gchar *
Expand Down
33 changes: 33 additions & 0 deletions bindings/gumjs/gumv8memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static void gum_v8_memory_read (GumMemoryValueType type,
static void gum_v8_memory_write (GumMemoryValueType type,
const GumV8Args * args);
GUMJS_DECLARE_FUNCTION (gum_v8_memory_read_volatile)
GUMJS_DECLARE_FUNCTION (gum_v8_memory_write_volatile)

#ifdef HAVE_WINDOWS
static gchar * gum_ansi_string_to_utf8 (const gchar * str_ansi, gint length);
Expand Down Expand Up @@ -184,6 +185,7 @@ static const GumV8Function gumjs_memory_functions[] =
GUMJS_EXPORT_MEMORY_READ_WRITE ("Utf16String", UTF16_STRING),
GUMJS_EXPORT_MEMORY_READ_WRITE ("AnsiString", ANSI_STRING),
{ "readVolatile", gum_v8_memory_read_volatile },
{ "writeVolatile", gum_v8_memory_write_volatile },

{ "allocAnsiString", gumjs_memory_alloc_ansi_string },
{ "allocUtf8String", gumjs_memory_alloc_utf8_string },
Expand Down Expand Up @@ -697,6 +699,37 @@ GUMJS_DEFINE_FUNCTION (gum_v8_memory_read_volatile)
g_free (data);
}

GUMJS_DEFINE_FUNCTION (gum_v8_memory_write_volatile)
{
gpointer address;
GBytes * bytes;
gconstpointer data;
gsize size;

if (!_gum_v8_args_parse (args, "pB", &address, &bytes))
return;

data = g_bytes_get_data (bytes, &size);

if (size == 0)
{
g_bytes_unref (bytes);
info.GetReturnValue ().Set (TRUE);
return;
}

if (!gum_memory_write (address, (guint8 *) data, size))
{
g_bytes_unref (bytes);
_gum_v8_throw_ascii_literal (isolate, "memory write failed");
return;
}

g_bytes_unref (bytes);

info.GetReturnValue ().Set (TRUE);
}

static void
gum_v8_memory_write (GumMemoryValueType type,
const GumV8Args * args)
Expand Down

0 comments on commit 8e706f9

Please sign in to comment.