Skip to content

BinaryBrew

BinaryBrew is a middle-end container for recovered binary information: instructions, functions, CFGs, exception information, and recovery builders.

Sources:

  • B2R2/src/MiddleEnd/API/BinaryBrew.fs
  • B2R2/src/MiddleEnd/ControlFlowAnalysis/FunctionCollection.fs
  • B2R2/src/MiddleEnd/ControlFlowAnalysis/Function.fs
  • B2R2/src/MiddleEnd/ControlFlowGraph/LowUIRCFG.fs
ConstructorUse
BinaryBrew(hdl)Default function identification and CFG recovery.
BinaryBrew(hdl, allowBBLOverlap)Default recovery with optional overlapping basic blocks.
BinaryBrew(hdl, strategies)Use explicit CFG-building strategies.
BinaryBrew(hdl, exnInfo, strategies)Use explicit exception info and strategies.
BinaryBrew<'FnCtx, 'GlCtx>(hdl, strategies, irBlkOptimizer)Generic form with explicit strategies and a LowUIR block optimizer.
BinaryBrew<'FnCtx, 'GlCtx>(...)Generic form for custom function/global recovery contexts.
EVMBinaryBrew(hdl, strategies)EVM-oriented variant using EVM function context.

The default BinaryBrew(hdl) constructs ExceptionInfo, runs FunctionIdentification, then runs CFGRecovery.

APIMeaning
BinHandle: BinHandleOriginal binary handle.
FunctionsRecovered function collection.
ExceptionInfoException-handling metadata used during CFG recovery.
InstructionsLinear-sweep instruction collection.
BuildersCFG builder table used internally by recovery strategies.

brew.Functions is a FunctionCollection. It is the usual entry point for iterating over recovered functions in tutorial scripts.

APIMeaning
Functions.SequenceSequence of recovered Function values.
Functions.AddressesSequence of recovered function entry addresses.
Functions.CountNumber of recovered functions.
Functions[addr] / Functions.Find(addr)Find a function by entry address.
Functions.Find(name)Find all recovered functions with a matching name.
Functions.FindByID(id)Find a function by its address-derived function ID.

Each Function contains address, name, call, and CFG information.

APIMeaning
EntryPointFunction entry address.
IDAddress-derived function ID.
NameRecovered or synthetic function name.
CFGRecovered LowUIR control-flow graph.
NoRetNon-returning-function status.
Callees / CallersCall relationships recovered during CFG construction.
JumpTablesJump tables associated with the function.
IsExternalTrue for external/imported functions.

The tutorial solutions mostly use EntryPoint and CFG.Size.

CFG APIMeaning
CFG.SizeNumber of vertices/basic blocks in the graph.
CFG.IsEmpty()True when the graph has no vertices.
CFG.Vertices / CFG.EdgesAll vertices or edges.
CFG.Roots / CFG.SingleRootRoot vertices, or exactly one root.
CFG.Exits / CFG.UnreachablesExit and unreachable vertices.
CFG.GetPreds v / CFG.GetSuccs vPredecessor or successor vertices.
let hdl = BinHandle("bin/findme")
let brew = BinaryBrew hdl
let text = hdl.File.GetTextSectionPointer()
let isInText (addr: Addr) =
text.IsValid && text.Addr <= addr && addr <= text.MaxAddr
brew.Functions.Sequence
|> Seq.filter (fun fn -> isInText fn.EntryPoint)
|> Seq.sortBy (fun fn -> fn.EntryPoint)
|> Seq.iter (fun fn ->
printfn "0x%016x blocks=%d" fn.EntryPoint fn.CFG.Size)

Use the default constructor first. Reach for explicit strategies when you need to change recovery behavior, disable a default pass, run EVM-specific recovery, or provide a custom function context.

NeedAPI
Default native recoveryBinaryBrew(hdl)
Permit overlapping basic blocksBinaryBrew(hdl, allowBBLOverlap = true)
Custom CFG strategy sequenceBinaryBrew(hdl, strategies)
EVM recovery contextEVMBinaryBrew(hdl, strategies)