Skip to content

Commit

Permalink
api: Avoid exposing internal types
Browse files Browse the repository at this point in the history
  • Loading branch information
oleavr committed Nov 23, 2024
1 parent 20cfd56 commit 2790745
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 133 deletions.
28 changes: 3 additions & 25 deletions src/api/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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_\")]")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
42 changes: 21 additions & 21 deletions src/control-service.vala
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +11,8 @@ namespace Frida {
construct;
}

private HostSession host_session;

private State state = STOPPED;

private WebService service;
Expand All @@ -41,57 +38,60 @@ 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);
host_session.process_crashed.connect (notify_process_crashed);
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 {
Expand Down
Loading

0 comments on commit 2790745

Please sign in to comment.