Skip to content

Commit 60d3d71

Browse files
committed
bindgen: Fix dropped constructors and out params
Upstream 01fd680. A static function that makes the class it sits on was not read at all, so every named constructor in the API was missing. One that throws was then missing twice over: skipped for __init__ because it throws, and skipped as a factory for being the primary, leaving nothing to make the class with. An out parameter was dropped along with the value it carried, so a gboolean method that answers through one now binds as Optional, and any other out parameter is refused rather than bound as a result the caller cannot read. Also vendored the matching frida-bindgen submodule bump (df17879..eace049) into frida/frida_bindgen_core/model.py: named constructors are now found among class functions, and methods expose their out parameters.
1 parent 75f1993 commit 60d3d71

3 files changed

Lines changed: 107 additions & 11 deletions

File tree

‎frida/frida_bindgen/codegen.py‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def generate_aio_async_method(method: Method, model: Model) -> Optional[str]:
454454
if method.return_value is not None:
455455
call = wrap_result(call, method.return_value.type, model)
456456

457-
ret = "None" if method.return_value is None else pyi_type(method.return_value.type, model)
457+
ret = facade_return_type(method, model)
458458

459459
return f""" async def {method.name}({signature}) -> {ret}:
460460
return {call}"""
@@ -478,7 +478,7 @@ def generate_custom_facade_method(
478478
body = textwrap.indent(logic.strip(), " " * 8)
479479
keyword = "async def" if awaitable else "def"
480480

481-
ret = "None" if method.return_value is None else pyi_type(method.return_value.type, model)
481+
ret = facade_return_type(method, model)
482482

483483
return f""" {keyword} {method.name}({signature}) -> {ret}:
484484
{body}
@@ -685,7 +685,7 @@ def generate_py_sync_method(method: Method, model: Model) -> Optional[str]:
685685
if method.return_value is not None:
686686
call = wrap_result(call, method.return_value.type, model)
687687

688-
ret = "None" if method.return_value is None else pyi_type(method.return_value.type, model)
688+
ret = facade_return_type(method, model)
689689

690690
return f""" def {method.name}({facade_signature(method, params)}) -> {ret}:
691691
return {call}"""
@@ -721,7 +721,7 @@ def generate_py_async_method(method: Method, model: Model) -> Optional[str]:
721721
if method.return_value is not None:
722722
call = wrap_result(call, method.return_value.type, model)
723723

724-
ret = "None" if method.return_value is None else pyi_type(method.return_value.type, model)
724+
ret = facade_return_type(method, model)
725725

726726
return f""" def {method.name}({signature}) -> {ret}:
727727
return {call}"""
@@ -811,8 +811,8 @@ def generate_pyi_class(otype: ObjectType, model: Model) -> List[str]:
811811

812812
body = []
813813

814-
primary = primary_constructor(otype)
815-
if primary is not None and not primary.throws:
814+
primary = inline_constructor(otype)
815+
if primary is not None:
816816
params = pyi_parameters(primary.input_parameters, model)
817817
if params is not None:
818818
signature = ", ".join(["self"] + params)
@@ -887,7 +887,13 @@ def pyi_method(method: Method, model: Model) -> Optional[str]:
887887
if params is None:
888888
return None
889889
signature = ", ".join(["self"] + params)
890-
ret = "None" if method.return_value is None else pyi_type(method.return_value.type, model)
890+
out = method.optional_out_parameter
891+
if out is not None:
892+
ret = f"Optional[{pyi_type(out.type, model)}]"
893+
elif method.return_value is None:
894+
ret = "None"
895+
else:
896+
ret = pyi_type(method.return_value.type, model)
891897
return f" def {method.name}({signature}) -> {ret}: ..."
892898

893899

@@ -1394,6 +1400,11 @@ def generate_method(otype: ObjectType, method: Method) -> Optional[Tuple[str, st
13941400
marshal = synchronous_method_return_marshal(method)
13951401
if marshal is None:
13961402
return None
1403+
out = method.optional_out_parameter
1404+
if out is not None:
1405+
marshal = build_return_marshal(out.type, method.object_type.model)
1406+
if marshal is None:
1407+
return None
13971408
params = []
13981409
for param in method.input_parameters:
13991410
sync_param = build_sync_param(param)
@@ -1737,14 +1748,17 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17371748
indent = " " * (len(otype.c_symbol_prefix) + len(method.name) + 3)
17381749
handle = f"({otype.c_type} *) PY_GOBJECT_HANDLE (self)"
17391750
returns_strv = method.return_value is not None and method.return_value.type.name == "utf8[]"
1751+
out = method.optional_out_parameter
17401752
call_arg_list = [handle] + [p.call_arg for p in params]
17411753
if returns_strv:
17421754
call_arg_list.append("&retval_length")
1755+
if out is not None:
1756+
call_arg_list.append(f"&{out.name}")
17431757
if method.throws:
17441758
call_arg_list.append("&error")
17451759
call = f"{method.c_identifier} ({', '.join(call_arg_list)})"
17461760

1747-
if not params and not method.throws and not returns_strv:
1761+
if not params and not method.throws and not returns_strv and out is None:
17481762
if method.return_value is None:
17491763
body = f" {call};\n\n PyFrida_RETURN_NONE;"
17501764
else:
@@ -1763,6 +1777,8 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17631777
decls.append(" GError * error = NULL;")
17641778
if returns_strv:
17651779
decls.append(" gint retval_length;")
1780+
if out is not None:
1781+
decls.append(f" {out_parameter_storage_type(out)} {out.name};")
17661782
for p in params:
17671783
decls += [f" {line}" for line in p.decl.splitlines()]
17681784

@@ -1777,7 +1793,15 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
17771793

17781794
pre = "".join(f"\n {p.pre}\n" for p in params if p.pre)
17791795

1780-
if method.return_value is None:
1796+
if out is not None:
1797+
invocation = f" gboolean found = {call};"
1798+
success = (
1799+
" if (found)\n"
1800+
f" result = {marshal.format(value=out.name)};\n"
1801+
" else\n"
1802+
" {\n Py_IncRef (Py_None);\n result = Py_None;\n }"
1803+
)
1804+
elif method.return_value is None:
17811805
invocation = f" {call};"
17821806
success = " Py_IncRef (Py_None);\n result = Py_None;"
17831807
elif returns_strv:
@@ -1822,6 +1846,22 @@ def generate_synchronous_method(otype: ObjectType, method: Method, marshal: str,
18221846
"""
18231847

18241848

1849+
def facade_return_type(method: Method, model: Model) -> str:
1850+
out = method.optional_out_parameter
1851+
if out is not None:
1852+
return f"Optional[{pyi_type(out.type, model)}]"
1853+
if method.return_value is None:
1854+
return "None"
1855+
return pyi_type(method.return_value.type, model)
1856+
1857+
1858+
def out_parameter_storage_type(param: Parameter) -> str:
1859+
"""What the caller declares: the out parameter's own type, less the pointer."""
1860+
c = param.type.c
1861+
assert c.endswith("*"), c
1862+
return c[:-1].strip()
1863+
1864+
18251865
def synchronous_method_return_marshal(method: Method) -> Optional[str]:
18261866
if method.is_async or method.is_property_accessor:
18271867
return None
@@ -2351,8 +2391,16 @@ def primary_constructor(otype: ObjectType) -> Optional[Procedure]:
23512391
return next((c for c in otype.constructors if c.name == "new"), otype.constructors[0])
23522392

23532393

2394+
def inline_constructor(otype: ObjectType) -> Optional[Procedure]:
2395+
"""The constructor __init__ takes; one that throws is reached as a factory instead."""
2396+
ctor = primary_constructor(otype)
2397+
if ctor is None or ctor.throws:
2398+
return None
2399+
return ctor
2400+
2401+
23542402
def factory_constructors(otype: ObjectType) -> List[Procedure]:
2355-
primary = primary_constructor(otype)
2403+
primary = inline_constructor(otype)
23562404
return [
23572405
ctor
23582406
for ctor in otype.constructors

‎frida/frida_bindgen/model.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def regular_object_types(self) -> List[ObjectType]:
1717

1818

1919
class ObjectType(core.ObjectType):
20+
@cached_property
21+
def methods(self) -> List["Method"]:
22+
return [m for m in super().methods if not m.out_parameters or m.optional_out_parameter is not None]
23+
2024
@cached_property
2125
def py_name(self) -> str:
2226
custom = self.customizations
@@ -276,6 +280,7 @@ def _make_class(
276280
type_struct,
277281
parent,
278282
constructors,
283+
functions,
279284
methods,
280285
properties,
281286
signals,
@@ -290,6 +295,7 @@ def _make_class(
290295
type_struct,
291296
parent,
292297
constructors,
298+
functions,
293299
methods,
294300
properties,
295301
signals,
@@ -306,6 +312,7 @@ def _make_interface(
306312
type_struct,
307313
parent,
308314
constructors,
315+
functions,
309316
methods,
310317
properties,
311318
signals,
@@ -319,6 +326,7 @@ def _make_interface(
319326
type_struct,
320327
parent,
321328
constructors,
329+
functions,
322330
methods,
323331
properties,
324332
signals,

‎frida/frida_bindgen_core/model.py‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class ObjectType:
116116
type_struct: str
117117
_parent: Optional[str]
118118
_constructors: List[ET.Element]
119+
_functions: List[ET.Element]
119120
_methods: List[ET.Element]
120121
_properties: List[ET.Element]
121122
_signals: List[ET.Element]
@@ -141,12 +142,29 @@ def is_frida_list(self) -> bool:
141142
def customizations(self):
142143
return self.model.customizations.type_customizations.get(self.name)
143144

145+
@cached_property
146+
def _named_constructors(self) -> List[ET.Element]:
147+
result = []
148+
for element in self._functions:
149+
retval = element.find("./return-value", GIR_NAMESPACES)
150+
if retval is None:
151+
continue
152+
type_element = retval.find("./type", GIR_NAMESPACES)
153+
if type_element is None:
154+
continue
155+
name = type_element.get("name")
156+
if name is None or "." not in name:
157+
continue
158+
if name.split(".")[-1] == self.name:
159+
result.append(element)
160+
return result
161+
144162
@cached_property
145163
def constructors(self) -> List[Constructor]:
146164
factory = self.model.factory
147165
constructors = []
148166
custom = self.customizations
149-
for element in self._constructors:
167+
for element in self._constructors + self._named_constructors:
150168
if element.get("introspectable") == "0" or element.get("deprecated") == "1":
151169
continue
152170

@@ -395,6 +413,24 @@ class Method(Procedure):
395413

396414
object_type: ObjectType
397415

416+
@cached_property
417+
def out_parameters(self) -> List[Parameter]:
418+
"""What the method hands back through a pointer, less a string vector's length."""
419+
if self.return_value is not None and self.return_value.type.name == "utf8[]":
420+
return []
421+
return [p for p in self.parameters if p.direction == Direction.OUT]
422+
423+
@cached_property
424+
def optional_out_parameter(self) -> Optional[Parameter]:
425+
"""The one value a `gboolean` method hands back, or nothing when it answers no."""
426+
outs = self.out_parameters
427+
if len(outs) != 1:
428+
return None
429+
rv = self.return_value
430+
if rv is None or rv.type.name != "gboolean":
431+
return None
432+
return outs[0]
433+
398434

399435
@dataclass
400436
class Property:
@@ -571,6 +607,7 @@ def resolve_type(name: str) -> Tuple[str, ET.Element]:
571607
if parent is not None:
572608
parent, _ = resolve_type(parent)
573609
constructors = element.findall(".//constructor", GIR_NAMESPACES)
610+
functions = element.findall("./function", GIR_NAMESPACES)
574611
methods = element.findall(".//method", GIR_NAMESPACES)
575612
properties = element.findall(".//property", GIR_NAMESPACES)
576613
signals = element.findall(".//glib:signal", GIR_NAMESPACES)
@@ -585,6 +622,7 @@ def resolve_type(name: str) -> Tuple[str, ET.Element]:
585622
type_struct=type_struct,
586623
parent=parent,
587624
constructors=constructors,
625+
functions=functions,
588626
methods=methods,
589627
properties=properties,
590628
signals=signals,
@@ -607,6 +645,7 @@ def resolve_type(name: str) -> Tuple[str, ET.Element]:
607645
if parent is not None:
608646
parent, _ = resolve_type(parent)
609647
constructors = []
648+
functions = element.findall("./function", GIR_NAMESPACES)
610649
methods = element.findall(".//method", GIR_NAMESPACES)
611650
properties = element.findall(".//property", GIR_NAMESPACES)
612651
signals = element.findall(".//glib:signal", GIR_NAMESPACES)
@@ -618,6 +657,7 @@ def resolve_type(name: str) -> Tuple[str, ET.Element]:
618657
type_struct=type_struct,
619658
parent=parent,
620659
constructors=constructors,
660+
functions=functions,
621661
methods=methods,
622662
properties=properties,
623663
signals=signals,

0 commit comments

Comments
 (0)