Skip to content

Commit

Permalink
[WIP] Wire up some more
Browse files Browse the repository at this point in the history
Co-authored-by: Håvard Sørbø <[email protected]>
  • Loading branch information
oleavr and hsorbo committed Sep 14, 2023
1 parent d73a8dc commit 0dc291b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
64 changes: 25 additions & 39 deletions gum/gumswiftapiresolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "gumprocess.h"

#include <string.h>
#include <unistd.h> /* temporary */

#define GUM_DESCRIPTOR_FLAGS_KIND(flags) \
(flags & 0x1f)
Expand Down Expand Up @@ -86,6 +85,7 @@ struct _GumModuleMetadata
const gchar * name;
const gchar * path;

GumAddress base_address;
GArray * functions;
GumSwiftApiResolver * resolver;
};
Expand Down Expand Up @@ -279,6 +279,7 @@ gum_swift_api_resolver_init (GumSwiftApiResolver * self)
module->ref_count = 2;
module->name = d->name;
module->path = d->path;
module->base_address = d->range->base_address;
module->functions = NULL;
module->resolver = self;

Expand Down Expand Up @@ -460,8 +461,6 @@ gum_module_metadata_unref (GumModuleMetadata * module)
}
}

static GumAddress the_base;

static GArray *
gum_module_metadata_get_functions (GumModuleMetadata * self)
{
Expand All @@ -470,19 +469,10 @@ gum_module_metadata_get_functions (GumModuleMetadata * self)
self->functions = g_array_new (FALSE, FALSE, sizeof (GumFunctionMetadata));
g_array_set_clear_func (self->functions,
(GDestroyNotify) gum_function_metadata_free);
g_printerr ("sizeof (GumContextDescriptor) == %zu\n", sizeof (GumContextDescriptor));
g_printerr ("sizeof (GumTypeContextDescriptor) == %zu\n", sizeof (GumTypeContextDescriptor));
g_printerr ("sizeof (GumClassDescriptor) == %zu\n", sizeof (GumClassDescriptor));
g_printerr ("offsetof (GumClassDescriptor, target_type) == %zu\n", G_STRUCT_OFFSET (GumClassDescriptor, target_type));
g_printerr ("offsetof (GumClassDescriptor, superclass_type) == %zu\n", G_STRUCT_OFFSET (GumClassDescriptor, superclass_type));
g_printerr ("collecting exports of %s\n", self->path);
the_base = gum_module_find_base_address (self->path);
gum_module_enumerate_sections (self->path,
gum_module_metadata_collect_section, self);
g_printerr (">>>\n");
gum_module_enumerate_exports (self->path,
gum_module_metadata_collect_export, self);
g_printerr ("<<<\n");
}

return self->functions;
Expand Down Expand Up @@ -513,8 +503,6 @@ gum_module_metadata_collect_section (const GumSectionDetails * details,
descriptor_flags = type->target_ctx.flags;

name = gum_resolve_relative_direct_ptr (&type->name);
g_printerr ("\n=== %s\n", name);
g_printerr ("\tflags=0x%08x\n", descriptor_flags);

switch (GUM_DESCRIPTOR_FLAGS_KIND (descriptor_flags))
{
Expand Down Expand Up @@ -579,7 +567,6 @@ gum_module_metadata_collect_section (const GumSectionDetails * details,
}
}

g_printerr ("\tclass offset=0x%x\n", (int) (GUM_ADDRESS (cd) - the_base));
if (GUM_TYPE_FLAGS_CLASS_HAS_VTABLE (type_flags))
{
const GumVTableDescriptorHeader * vth;
Expand All @@ -590,17 +577,22 @@ gum_module_metadata_collect_section (const GumSectionDetails * details,
methods = GUM_ALIGN ((const GumMethodDescriptor *) (vth + 1),
GumMethodDescriptor);

g_printerr ("\tvtable offset: 0x%x\n", vth->vtable_offset);
g_printerr ("\tvtable size: 0x%x\n", vth->vtable_size);

for (i = 0; i != vth->vtable_size; i++)
{
const GumMethodDescriptor * method = &methods[i];
gconstpointer impl;
GumFunctionMetadata func;

impl = gum_resolve_relative_direct_ptr (&method->impl);

g_printerr ("\t\tmethods[%u]: 0x%08x, %p\n",
func.name = g_strdup_printf (
"%s.vtable[%u](%s+0x%" G_GINT64_MODIFIER "x)",
name,
i,
method->flags,
gum_resolve_relative_direct_ptr (&method->impl));
module->name,
GUM_ADDRESS (impl) - module->base_address);
func.address = GUM_ADDRESS (impl);
g_array_append_val (module->functions, func);
}

trailer = methods + vth->vtable_size;
Expand All @@ -616,38 +608,32 @@ gum_module_metadata_collect_section (const GumSectionDetails * details,
methods = GUM_ALIGN ((const GumMethodOverrideDescriptor *) (oth + 1),
GumMethodOverrideDescriptor);

g_printerr ("\toverride_table size: 0x%x\n", oth->num_entries);

for (i = 0; i != oth->num_entries; i++)
{
const GumMethodOverrideDescriptor * method = &methods[i];
gconstpointer impl;
GumFunctionMetadata func;

impl = gum_resolve_relative_direct_ptr (&method->impl);

g_printerr ("\t\tmethods[%u]: %p\n",
func.name = g_strdup_printf (
"%s.overrides[%u](%s+0x%" G_GINT64_MODIFIER "x)",
name,
i,
gum_resolve_relative_direct_ptr (&method->impl));
module->name,
GUM_ADDRESS (impl) - module->base_address);
func.address = GUM_ADDRESS (impl);
g_array_append_val (module->functions, func);
}

trailer = methods + oth->num_entries;
}

#if 0
static int count = 0;
count++;
if (count == 3)
{
g_printerr ("Waiting for debugger in PID %u...\n", getpid ());
while (!gum_process_is_debugger_attached ())
{
g_usleep (G_USEC_PER_SEC);
}
}
#endif

break;
}
case GUM_CONTEXT_DESCRIPTOR_STRUCT:
{
g_printerr ("\t[struct] %s\n", name);
/* TODO */
break;
}
default:
Expand Down
14 changes: 13 additions & 1 deletion tests/core/apiresolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ TESTCASE (objc_method_can_be_resolved_from_instance_method_address)
g_free (method);
}

#include <unistd.h>

TESTCASE (swift_method_can_be_resolved)
{
GumAddress address;
Expand All @@ -259,9 +261,19 @@ TESTCASE (swift_method_can_be_resolved)

g_printerr (">>>\n");
gum_api_resolver_enumerate_matches (fixture->resolver,
"*CoreDevice!*RSDDeviceInfo*", resolve_method_impl, &address, &error);
"*CoreDevice!*RemoteDevice*", resolve_method_impl, &address, &error);
//"*CoreDevice!*RSDDeviceInfo*", resolve_method_impl, &address, &error);
//"*hello*!*", resolve_method_impl, &address, &error);
g_printerr ("<<<\n");
g_assert_no_error (error);

#if 0
g_printerr ("Waiting for debugger in PID %u...\n", getpid ());
while (!gum_process_is_debugger_attached ())
{
g_usleep (G_USEC_PER_SEC);
}
#endif
}

static gboolean
Expand Down

0 comments on commit 0dc291b

Please sign in to comment.