Skip to content

Commit 519c298

Browse files
authored
🤖 Merge PR DefinitelyTyped#75102 frida-gum: Add ControlFlowGraph, findFunctionRange by @oleavr
1 parent 8f01318 commit 519c298

3 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎types/frida-gum/frida-gum-tests.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,39 @@ Process.mainModule;
192192
// $ExpectType string | null
193193
Process.mainModule.version;
194194

195+
// $ExpectType MemoryRange | null
196+
Process.findFunctionRange(Process.mainModule.base);
197+
198+
const cfg = new ControlFlowGraph(Process.mainModule.base);
199+
// $ExpectType NativePointer
200+
cfg.entrypoint;
201+
// $ExpectType BasicBlock
202+
const entryBlock = cfg.entryBlock;
203+
// $ExpectType BasicBlock[]
204+
cfg.blocks;
205+
// $ExpectType BasicBlock | null
206+
cfg.findBlockContaining(Process.mainModule.base);
207+
// $ExpectType boolean
208+
cfg.dominates(Process.mainModule.base, Process.mainModule.base);
209+
// $ExpectType DominatingSite[]
210+
const sites = cfg.enumerateDominatingSites(Process.mainModule.base);
211+
// $ExpectType NativePointer
212+
sites[0].address;
213+
// $ExpectType number
214+
sites[0].capacity;
215+
// $ExpectType NativePointer
216+
entryBlock.start;
217+
// $ExpectType NativePointer
218+
entryBlock.end;
219+
// $ExpectType BasicBlock[]
220+
entryBlock.successors;
221+
// $ExpectType BasicBlock[]
222+
entryBlock.predecessors;
223+
// $ExpectType BasicBlock | null
224+
entryBlock.immediateDominator;
225+
// $ExpectType Instruction[]
226+
entryBlock.instructions;
227+
195228
const art = Process.getModuleByName("libart.so");
196229
// $ExpectType NativePointer
197230
art.getSymbolByName("ExecuteNterpImpl");

‎types/frida-gum/index.d.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,17 @@ declare namespace Process {
438438
*/
439439
function getRangeByAddress(address: NativePointerValue): RangeDetails;
440440

441+
/**
442+
* Determines the code range of the function that `address` belongs to,
443+
* derived from the platform's unwind tables. A function whose body is split
444+
* across several ranges (e.g. a cold fragment) is represented by one range
445+
* per fragment; this returns the one covering `address`. Where no unwind
446+
* information is available — e.g. a leaf function, or a target lacking
447+
* unwind tables altogether — the containing symbol's bounds are used as a
448+
* best-effort fallback. Returns null if neither yields a range.
449+
*/
450+
function findFunctionRange(address: NativePointerValue): MemoryRange | null;
451+
441452
/**
442453
* Enumerates memory ranges satisfying `specifier`.
443454
*
@@ -4321,6 +4332,121 @@ declare class Instruction {
43214332
toString(): string;
43224333
}
43234334

4335+
/**
4336+
* Control-flow graph of a single function, with its basic blocks, edges, and
4337+
* dominator relationships.
4338+
*/
4339+
declare class ControlFlowGraph {
4340+
/**
4341+
* Builds the control-flow graph of the function containing `entrypoint`.
4342+
*
4343+
* The function's bounds are resolved the same way as
4344+
* `Process.findFunctionRange()` — from the platform's unwind tables, with
4345+
* the containing symbol's bounds as a best-effort fallback — and its
4346+
* architecture and mode are determined automatically. On 32-bit ARM, a
4347+
* least significant bit set to 1 indicates Thumb.
4348+
*
4349+
* Throws an exception if the bounds of the function cannot be determined.
4350+
*
4351+
* @param entrypoint Address of the function to analyze.
4352+
*/
4353+
constructor(entrypoint: NativePointerValue);
4354+
4355+
/**
4356+
* Address that the graph was built from.
4357+
*/
4358+
entrypoint: NativePointer;
4359+
4360+
/**
4361+
* Basic block that the function begins with.
4362+
*/
4363+
entryBlock: BasicBlock;
4364+
4365+
/**
4366+
* All basic blocks making up the graph.
4367+
*/
4368+
blocks: BasicBlock[];
4369+
4370+
/**
4371+
* Looks up the basic block containing `address`. Returns null if no block
4372+
* covers it.
4373+
*
4374+
* @param address Address to look up.
4375+
*/
4376+
findBlockContaining(address: NativePointerValue): BasicBlock | null;
4377+
4378+
/**
4379+
* Determines whether the block containing `a` dominates the block
4380+
* containing `b`, i.e. whether every path from the entry block to `b`
4381+
* passes through `a`.
4382+
*
4383+
* @param a Address whose block is the potential dominator.
4384+
* @param b Address whose block is potentially dominated.
4385+
*/
4386+
dominates(a: NativePointerValue, b: NativePointerValue): boolean;
4387+
4388+
/**
4389+
* Enumerates the sites that dominate `target`, nearest first.
4390+
*
4391+
* @param target Address to find the dominating sites of.
4392+
*/
4393+
enumerateDominatingSites(target: NativePointerValue): DominatingSite[];
4394+
}
4395+
4396+
/**
4397+
* A basic block within a `ControlFlowGraph`. Not constructable; obtain
4398+
* instances through the graph.
4399+
*/
4400+
declare class BasicBlock {
4401+
/**
4402+
* Address of the first instruction in the block.
4403+
*/
4404+
start: NativePointer;
4405+
4406+
/**
4407+
* Address just past the last instruction in the block.
4408+
*/
4409+
end: NativePointer;
4410+
4411+
/**
4412+
* Blocks that control may flow to from this block.
4413+
*/
4414+
successors: BasicBlock[];
4415+
4416+
/**
4417+
* Blocks that control may flow to this block from.
4418+
*/
4419+
predecessors: BasicBlock[];
4420+
4421+
/**
4422+
* Block that immediately dominates this one, or null for the entry block.
4423+
*/
4424+
immediateDominator: BasicBlock | null;
4425+
4426+
/**
4427+
* Instructions making up this block.
4428+
*/
4429+
instructions: Instruction[];
4430+
}
4431+
4432+
/**
4433+
* A site that dominates a given target, as returned by
4434+
* `ControlFlowGraph#enumerateDominatingSites()`.
4435+
*/
4436+
interface DominatingSite {
4437+
/**
4438+
* Instruction-aligned address that dominates the target.
4439+
*/
4440+
address: NativePointer;
4441+
4442+
/**
4443+
* Number of contiguous bytes at `address`, within a single range and with
4444+
* no incoming branch, that a redirect may overwrite without another
4445+
* control-flow edge landing inside the patched region.
4446+
*/
4447+
capacity: number;
4448+
}
4449+
43244450
declare class X86Instruction extends Instruction {
43254451
/**
43264452
* Array of objects describing each operand.

‎types/frida-gum/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/frida-gum",
4-
"version": "19.5.9999",
4+
"version": "19.6.9999",
55
"nonNpm": true,
66
"nonNpmDescription": "frida-gum",
77
"projects": [

0 commit comments

Comments
 (0)