diff --git a/src/api/generate.py b/src/api/generate.py index 454e8d872..4152e37be 100644 --- a/src/api/generate.py +++ b/src/api/generate.py @@ -196,25 +196,13 @@ def emit_gir(api: ApiSpec, core_gir: str, base_gir: str, output_dir: Path) -> st enum_type_names = {enum.name for enum in api.enum_types} error_type_names = {error.name for error in api.error_types} - internal_type_names = { - "Frida.HostSessionProvider", - "FridaBase.HostSession", - "FridaBase.AgentSession", - "FridaBase.AgentSessionId", - "FridaBase.AgentScriptId", - } - def merge_and_transform_elements(tag_name: str, spec_set: Set[str]): core_elements = filter_elements(core_root.findall(f".//{tag_name}", GIR_NAMESPACES), spec_set) base_elements = filter_elements(base_root.findall(f".//{tag_name}", GIR_NAMESPACES), spec_set) for elem in core_elements + base_elements: if tag_name == "class": - class_name = elem.get("name") for child in list(elem): - if (child.get("name").startswith("_") - or child.tag == CORE_TAG_FIELD - or class_name == "Device" and child.tag == CORE_TAG_METHOD and child.get("name") == "get_host_session" - or has_type_child_with_name_matching(child, internal_type_names)): + if child.tag == CORE_TAG_FIELD or child.get("name").startswith("_"): elem.remove(child) merged_namespace.append(elem) @@ -231,12 +219,6 @@ def merge_and_transform_elements(tag_name: str, spec_set: Set[str]): def filter_elements(elements: List[ET.Element], spec_set: Set[str]): return [elem for elem in elements if elem.get("name") in spec_set] -def has_type_child_with_name_matching(element: ET.Element, names: Set[str]) -> bool: - for type_elem in element.findall(".//type", GIR_NAMESPACES): - if type_elem.get("name") in names: - return True - return False - def emit_vapi(api, output_dir): with (output_dir / f"frida-core-{api.version}.vapi").open("w", encoding='utf-8') as output_vapi_file: output_vapi_file.write("[CCode (cheader_filename = \"frida-core.h\", cprefix = \"Frida\", lower_case_cprefix = \"frida_\")]") @@ -374,10 +356,7 @@ def parse_api(api_version, toplevel_code, core_header, core_vapi, base_header, b method_cname_lc = object_type.c_name_lc + '_' + method_name if method_cname_lc not in seen_cfunctions: seen_cfunctions.add(method_cname_lc) - if method_name not in ('construct', 'construct_with_host_session', 'get_main_context', 'get_provider', 'get_session') \ - and not "FridaHostSession *" in method_cprototype \ - and not (object_type.name == "Device" and method_name == 'get_host_session') \ - and not (object_type.name in ("Session", "Script") and method_name == 'get_id'): + if method_name != 'construct': if (object_type.c_name + '*') in m.group(0): if method_name == 'new' or method_name.startswith('new_'): object_type.c_constructors.append(method_cprototype) @@ -451,8 +430,7 @@ def parse_api(api_version, toplevel_code, core_header, core_vapi, base_header, b current_object_type.vapi_signals.append(stripped_line) elif "{ get" in stripped_line: name = re.match(r".+?(\w+)\s+{", stripped_line).group(1) - if name not in ('main_context', 'provider', 'session'): - current_object_type.vapi_properties.append(stripped_line) + current_object_type.vapi_properties.append(stripped_line) else: m = re.match(r".+?(\w+)\s+\(", stripped_line) if m is not None: diff --git a/src/control-service.vala b/src/control-service.vala index fe8855b7f..375ad7762 100644 --- a/src/control-service.vala +++ b/src/control-service.vala @@ -1,11 +1,6 @@ namespace Frida { #if HAVE_LOCAL_BACKEND public class ControlService : Object { - public HostSession host_session { - get; - construct; - } - public EndpointParameters endpoint_params { get; construct; @@ -16,6 +11,8 @@ namespace Frida { construct; } + private HostSession host_session; + private State state = STOPPED; private WebService service; @@ -41,43 +38,53 @@ namespace Frida { public ControlService (EndpointParameters endpoint_params, ControlServiceOptions? options = null) { ControlServiceOptions opts = (options != null) ? options : new ControlServiceOptions (); - HostSession host_session; + HostSession session; #if WINDOWS var tempdir = new TemporaryDirectory (); - host_session = new WindowsHostSession (new WindowsHelperProcess (tempdir), tempdir); + session = new WindowsHostSession (new WindowsHelperProcess (tempdir), tempdir); #endif #if DARWIN - host_session = new DarwinHostSession (new DarwinHelperBackend (), new TemporaryDirectory (), + session = new DarwinHostSession (new DarwinHelperBackend (), new TemporaryDirectory (), opts.sysroot, opts.report_crashes); #endif #if LINUX var tempdir = new TemporaryDirectory (); - host_session = new LinuxHostSession (new LinuxHelperProcess (tempdir), tempdir, opts.report_crashes); + session = new LinuxHostSession (new LinuxHelperProcess (tempdir), tempdir, opts.report_crashes); #endif #if FREEBSD - host_session = new FreebsdHostSession (); + session = new FreebsdHostSession (); #endif #if QNX - host_session = new QnxHostSession (); + session = new QnxHostSession (); #endif Object ( - host_session: host_session, endpoint_params: endpoint_params, options: opts ); + link (session); } - public ControlService.with_host_session (HostSession host_session, EndpointParameters endpoint_params, + internal ControlService.with_host_session (HostSession host_session, EndpointParameters endpoint_params, ControlServiceOptions? options = null) { Object ( - host_session: host_session, endpoint_params: endpoint_params, options: (options != null) ? options : new ControlServiceOptions () ); + link (host_session); } construct { + var iface_observer = new TunnelInterfaceObserver (); + iface_observer.interface_detached.connect (on_interface_detached); + + service = new WebService (endpoint_params, WebServiceFlavor.CONTROL, PortConflictBehavior.FAIL, iface_observer); + + main_handler = new ConnectionHandler (this, null); + } + + private void link (HostSession session) { + host_session = session; host_session.spawn_added.connect (notify_spawn_added); host_session.child_added.connect (notify_child_added); host_session.child_removed.connect (notify_child_removed); @@ -85,13 +92,6 @@ namespace Frida { host_session.output.connect (notify_output); host_session.agent_session_detached.connect (on_agent_session_detached); host_session.uninjected.connect (notify_uninjected); - - var iface_observer = new TunnelInterfaceObserver (); - iface_observer.interface_detached.connect (on_interface_detached); - - service = new WebService (endpoint_params, WebServiceFlavor.CONTROL, PortConflictBehavior.FAIL, iface_observer); - - main_handler = new ConnectionHandler (this, null); } public async void start (Cancellable? cancellable = null) throws Error, IOError { diff --git a/src/frida.vala b/src/frida.vala index cc065e0cf..5e41d9c1a 100644 --- a/src/frida.vala +++ b/src/frida.vala @@ -303,7 +303,7 @@ namespace Frida { } } - public void _release_device (Device device) { + internal void _release_device (Device device) { var device_did_exist = devices.remove (device); assert (device_did_exist); } @@ -524,18 +524,10 @@ namespace Frida { } } - public HostSessionProvider provider { - get; - construct; - } - - public weak DeviceManager? manager { - get; - construct; - } - private string? _id; private string? _name; + internal HostSessionProvider provider; + private unowned DeviceManager? manager; private HostSessionOptions? host_session_options; private Promise? host_session_request; @@ -552,16 +544,14 @@ namespace Frida { public delegate bool ProcessPredicate (Process process); - internal Device (DeviceManager? manager, HostSessionProvider provider, string? id = null, string? name = null, + internal Device (DeviceManager? mgr, HostSessionProvider prov, string? id = null, string? name = null, HostSessionOptions? options = null) { - Object ( - manager: manager, - provider: provider, - icon: provider.icon - ); + Object (icon: provider.icon); _id = id; _name = name; + provider = prov; + manager = mgr; host_session_options = options; } @@ -1310,7 +1300,7 @@ namespace Frida { throw new Error.INVALID_OPERATION ("Device is gone"); } - public async HostSession get_host_session (Cancellable? cancellable) throws Error, IOError { + internal async HostSession get_host_session (Cancellable? cancellable) throws Error, IOError { while (host_session_request != null) { try { return yield host_session_request.future.wait_async (cancellable); @@ -1338,16 +1328,6 @@ namespace Frida { } } - public HostSession get_host_session_sync (Cancellable? cancellable = null) throws Error, IOError { - return create ().execute (cancellable); - } - - private class GetHostSessionTask : DeviceTask { - protected override async HostSession perform_operation () throws Error, IOError { - return yield parent.get_host_session (cancellable); - } - } - private void on_host_session_detached (HostSession session) { if (session != current_host_session) return; @@ -1462,7 +1442,7 @@ namespace Frida { } } - public async void _release_session (Session session, bool may_block, Cancellable? cancellable) throws IOError { + internal async void _release_session (Session session, bool may_block, Cancellable? cancellable) throws IOError { AgentSessionId? session_id = null; foreach (var entry in agent_sessions.entries) { if (entry.value == session) { @@ -2074,26 +2054,13 @@ namespace Frida { construct; } - public AgentSessionId id { - get; - construct; - } - - public AgentSession session { - get { - return active_session; - } - } - public uint persist_timeout { get; construct; } - public weak Device device { - get; - construct; - } + private AgentSessionId id; + private unowned Device device; private State state = ATTACHED; private Promise close_request; @@ -2131,12 +2098,10 @@ namespace Frida { } internal Session (Device device, uint pid, AgentSessionId id, SessionOptions options) { - Object ( - pid: pid, - id: id, - persist_timeout: options.persist_timeout, - device: device - ); + Object (pid: pid, persist_timeout: options.persist_timeout); + + this.id = id; + this.device = device; } public bool is_detached () { @@ -2192,7 +2157,7 @@ namespace Frida { uint last_tx_batch_id; try { - yield session.resume (last_rx_batch_id, cancellable, out last_tx_batch_id); + yield active_session.resume (last_rx_batch_id, cancellable, out last_tx_batch_id); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2224,7 +2189,7 @@ namespace Frida { check_open (); try { - yield session.enable_child_gating (cancellable); + yield active_session.enable_child_gating (cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2244,7 +2209,7 @@ namespace Frida { check_open (); try { - yield session.disable_child_gating (cancellable); + yield active_session.disable_child_gating (cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2268,7 +2233,7 @@ namespace Frida { AgentScriptId script_id; try { - script_id = yield session.create_script (source, raw_options, cancellable); + script_id = yield active_session.create_script (source, raw_options, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2306,7 +2271,7 @@ namespace Frida { AgentScriptId script_id; try { - script_id = yield session.create_script_from_bytes (bytes.get_data (), raw_options, + script_id = yield active_session.create_script_from_bytes (bytes.get_data (), raw_options, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); @@ -2345,7 +2310,7 @@ namespace Frida { uint8[] data; try { - data = yield session.compile_script (source, raw_options, cancellable); + data = yield active_session.compile_script (source, raw_options, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2378,7 +2343,7 @@ namespace Frida { uint8[] data; try { - data = yield session.snapshot_script (embed_script, raw_options, cancellable); + data = yield active_session.snapshot_script (embed_script, raw_options, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2782,7 +2747,7 @@ namespace Frida { PortalMembershipId membership_id; try { - membership_id = yield session.join_portal (address, raw_options, cancellable); + membership_id = yield active_session.join_portal (address, raw_options, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -2944,7 +2909,7 @@ namespace Frida { } } - public void _release_script (AgentScriptId script_id) { + internal void _release_script (AgentScriptId script_id) { var script_did_exist = scripts.unset (script_id); assert (script_did_exist); } @@ -3010,7 +2975,7 @@ namespace Frida { private async void close_session_and_peer_connection (Cancellable? cancellable) throws IOError { try { - yield session.close (cancellable); + yield active_session.close (cancellable); } catch (GLib.Error e) { if (e is IOError.CANCELLED) { discard_peer_connection (); @@ -3049,22 +3014,17 @@ namespace Frida { public signal void destroyed (); public signal void message (string json, Bytes? data); - public AgentScriptId id { - get; - construct; - } - - public weak Session session { - get; - construct; - } + private AgentScriptId id; + private unowned Session session; private Promise close_request; private Gum.InspectorServer? inspector_server; internal Script (Session session, AgentScriptId script_id) { - Object (id: script_id, session: session); + Object (); + this.id = script_id; + this.session = session; } public bool is_destroyed () { @@ -3075,7 +3035,7 @@ namespace Frida { check_open (); try { - yield session.session.load_script (id, cancellable); + yield session.active_session.load_script (id, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -3111,7 +3071,7 @@ namespace Frida { check_open (); try { - yield session.session.eternalize_script (id, cancellable); + yield session.active_session.eternalize_script (id, cancellable); yield _do_close (false, cancellable); } catch (GLib.Error e) { @@ -3162,7 +3122,7 @@ namespace Frida { inspector_server.message.connect (on_debugger_message_from_frontend); try { - yield session.session.enable_debugger (id, cancellable); + yield session.active_session.enable_debugger (id, cancellable); } catch (GLib.Error e) { inspector_server = null; @@ -3176,7 +3136,7 @@ namespace Frida { inspector_server = null; try { - yield session.session.disable_debugger (id, cancellable); + yield session.active_session.disable_debugger (id, cancellable); } catch (GLib.Error e) { } @@ -3210,7 +3170,7 @@ namespace Frida { inspector_server = null; try { - yield session.session.disable_debugger (id, cancellable); + yield session.active_session.disable_debugger (id, cancellable); } catch (GLib.Error e) { throw_dbus_error (e); } @@ -3264,7 +3224,7 @@ namespace Frida { if (may_block) { try { - yield parent.session.destroy_script (id, cancellable); + yield parent.active_session.destroy_script (id, cancellable); } catch (GLib.Error e) { } } @@ -3287,23 +3247,18 @@ namespace Frida { } public class PortalMembership : Object { - public uint id { - get; - construct; - } - - public Session session { - get; - construct; - } + private uint id; + private Session session; internal PortalMembership (Session session, PortalMembershipId membership_id) { - Object (id: membership_id.handle, session: session); + Object (); + this.id = membership_id.handle; + this.session = session; } public async void terminate (Cancellable? cancellable = null) throws Error, IOError { try { - yield session.session.leave_portal (PortalMembershipId (id), cancellable); + yield session.active_session.leave_portal (PortalMembershipId (id), cancellable); } catch (GLib.Error e) { throw_dbus_error (e); }