Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fmagin committed Dec 20, 2021
1 parent b6f0664 commit b79d2b4
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 59 deletions.
10 changes: 5 additions & 5 deletions angr/analyses/bindiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class UnmatchedStatementsException(Exception):


# statement difference classes
class Difference(object):
class Difference:
def __init__(self, diff_type, value_a, value_b):
self.type = diff_type
self.value_a = value_a
self.value_b = value_b


class ConstantChange(object):
class ConstantChange:
def __init__(self, offset, value_a, value_b):
self.offset = offset
self.value_a = value_a
Expand Down Expand Up @@ -249,7 +249,7 @@ def compare_statement_dict(statement_1, statement_2):
return differences


class NormalizedBlock(object):
class NormalizedBlock:
# block may span multiple calls
def __init__(self, block, function):
addresses = [block.addr]
Expand Down Expand Up @@ -288,7 +288,7 @@ def __repr__(self):
return '<Normalized Block for %#x, %d bytes>' % (self.addr, size)


class NormalizedFunction(object):
class NormalizedFunction:
# a more normalized function
def __init__(self, function: "Function"):
# start by copying the graph
Expand Down Expand Up @@ -344,7 +344,7 @@ def __init__(self, function: "Function"):
self.call_sites[n] = call_targets


class FunctionDiff(object):
class FunctionDiff:
"""
This class computes the a diff between two functions.
"""
Expand Down
62 changes: 39 additions & 23 deletions angr/analyses/cfg/cfg_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ def __init__(self, sort, context_sensitivity_level, normalize=False, binary=None
:param cle.backends.Backend binary: The binary to recover CFG on. By default the main binary is used.
:param bool force_segment: Force CFGFast to rely on binary segments instead of sections.
:param angr.SimState base_state: A state to use as a backer for all memory loads.
:param bool resolve_indirect_jumps: Whether to try to resolve indirect jumps. This is necessary to resolve jump
targets from jump tables, etc.
:param list indirect_jump_resolvers: A custom list of indirect jump resolvers. If this list is None or empty,
default indirect jump resolvers specific to this architecture and binary
types will be loaded.
:param bool resolve_indirect_jumps: Whether to try to resolve indirect jumps.
This is necessary to resolve jump targets from jump tables, etc.
:param list indirect_jump_resolvers: A custom list of indirect jump resolvers.
If this list is None or empty, default indirect jump resolvers
specific to this architecture and binary types will be loaded.
:param int indirect_jump_target_limit: Maximum indirect jump targets to be recovered.
:param bool detect_tail_calls: Aggressive tail-call optimization detection. This option is only
respected in make_functions().
:param bool sp_tracking_track_memory: Whether or not to track memory writes when tracking the stack pointer. This
increases the accuracy of stack pointer tracking, especially for architectures
without a base pointer. Only used if detect_tail_calls is enabled.
:param bool sp_tracking_track_memory: Whether or not to track memory writes if tracking the stack pointer.
This increases the accuracy of stack pointer tracking,
especially for architectures without a base pointer.
Only used if detect_tail_calls is enabled.
:param None or CFGModel model: The CFGModel instance to write to. A new CFGModel instance will be
created and registered with the knowledge base if `model` is None.
Expand Down Expand Up @@ -1036,7 +1037,12 @@ def normalize(self):

self._normalized = True

def _normalize_core(self, graph: networkx.DiGraph, callstack_key, smallest_node, other_nodes, smallest_nodes, end_addresses_to_nodes):
def _normalize_core(self, graph: networkx.DiGraph,
callstack_key,
smallest_node,
other_nodes,
smallest_nodes,
end_addresses_to_nodes):

# Break other nodes
for n in other_nodes:
Expand Down Expand Up @@ -1350,7 +1356,7 @@ def make_functions(self):
self._release_gil(i, 20)

if self._show_progressbar or self._progress_callback:
progress = min_stage_2_progress + (max_stage_2_progress - min_stage_2_progress) * (i * 1.0 / nodes_count)
progress = min_stage_2_progress + (max_stage_2_progress - min_stage_2_progress) * (i * 1. / nodes_count)
self._update_progress(progress)

self._graph_bfs_custom(self.graph, [ fn ], self._graph_traversal_handler, blockaddr_to_function,
Expand Down Expand Up @@ -1382,7 +1388,7 @@ def make_functions(self):
for i, fn in enumerate(sorted(secondary_function_nodes, key=lambda n: n.addr)):

if self._show_progressbar or self._progress_callback:
progress = min_stage_3_progress + (max_stage_3_progress - min_stage_3_progress) * (i * 1.0 / nodes_count)
progress = min_stage_3_progress + (max_stage_3_progress - min_stage_3_progress) * (i * 1. / nodes_count)
self._update_progress(progress)

self._graph_bfs_custom(self.graph, [fn], self._graph_traversal_handler, blockaddr_to_function,
Expand Down Expand Up @@ -1784,7 +1790,8 @@ def _addr_to_function(self, addr, blockaddr_to_function, known_functions):
is_syscall = self.project.simos.is_syscall_addr(addr)

n = self.model.get_any_node(addr, is_syscall=is_syscall)
if n is None: node = addr
if n is None:
node = addr
else: node = self._to_snippet(n)

if isinstance(addr, SootAddressDescriptor):
Expand Down Expand Up @@ -1939,8 +1946,10 @@ def _graph_traversal_handler(self, g, src, dst, data, blockaddr_to_function, kno

if src_addr not in src_function.block_addrs_set:
n = self.model.get_any_node(src_addr)
if n is None: node = src_addr
else: node = self._to_snippet(n)
if n is None:
node = src_addr
else:
node = self._to_snippet(n)
self.kb.functions._add_node(src_function.addr, node)

if data is None:
Expand All @@ -1951,8 +1960,10 @@ def _graph_traversal_handler(self, g, src, dst, data, blockaddr_to_function, kno

if jumpkind == 'Ijk_Ret':
n = self.model.get_any_node(src_addr)
if n is None: from_node = src_addr
else: from_node = self._to_snippet(n)
if n is None:
from_node = src_addr
else:
from_node = self._to_snippet(n)
self.kb.functions._add_return_from(src_function.addr, from_node, None)

if dst is None:
Expand All @@ -1972,7 +1983,8 @@ def _graph_traversal_handler(self, g, src, dst, data, blockaddr_to_function, kno
dst_function = self._addr_to_function(dst_addr, blockaddr_to_function, known_functions)

n = self.model.get_any_node(src_addr)
if n is None: src_snippet = self._to_snippet(addr=src_addr, base_state=self._base_state)
if n is None:
src_snippet = self._to_snippet(addr=src_addr, base_state=self._base_state)
else:
src_snippet = self._to_snippet(cfg_node=n)

Expand All @@ -1993,8 +2005,8 @@ def _graph_traversal_handler(self, g, src, dst, data, blockaddr_to_function, kno
if isinstance(dst_addr, SootAddressDescriptor):
dst_addr = dst_addr.method

self.kb.functions._add_call_to(src_function.addr, src_snippet, dst_addr, fakeret_snippet, syscall=is_syscall,
ins_addr=ins_addr, stmt_idx=stmt_idx)
self.kb.functions._add_call_to(src_function.addr, src_snippet, dst_addr, fakeret_snippet,
syscall=is_syscall, ins_addr=ins_addr, stmt_idx=stmt_idx)

if dst_function.returning:
returning_target = src.addr + src.size
Expand Down Expand Up @@ -2025,12 +2037,16 @@ def _graph_traversal_handler(self, g, src, dst, data, blockaddr_to_function, kno

# convert src_addr and dst_addr to CodeNodes
n = self.model.get_any_node(src_addr)
if n is None: src_node = src_addr
else: src_node = self._to_snippet(cfg_node=n)
if n is None:
src_node = src_addr
else:
src_node = self._to_snippet(cfg_node=n)

n = self.model.get_any_node(dst_addr)
if n is None: dst_node = dst_addr
else: dst_node = self._to_snippet(cfg_node=n)
if n is None:
dst_node = dst_addr
else:
dst_node = self._to_snippet(cfg_node=n)

# pre-check: if source and destination do not belong to the same section, it must be jumping to another
# function
Expand Down
10 changes: 8 additions & 2 deletions angr/analyses/decompiler/region_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,12 @@ def _compute_region(graph, node, frontier, include_frontier=False, dummy_endnode
else:
return None

def _abstract_acyclic_region(self, graph: networkx.DiGraph, region, frontier, dummy_endnode=None, secondary_graph=None):
def _abstract_acyclic_region(self,
graph: networkx.DiGraph,
region,
frontier,
dummy_endnode=None,
secondary_graph=None):

in_edges = self._region_in_edges(graph, region, data=True)
out_edges = self._region_out_edges(graph, region, data=True)
Expand Down Expand Up @@ -617,7 +622,8 @@ def _abstract_acyclic_region(self, graph: networkx.DiGraph, region, frontier, du
self._abstract_acyclic_region(secondary_graph, region, { })

@staticmethod
def _abstract_cyclic_region(graph: networkx.DiGraph, loop_nodes, head, normal_entries, abnormal_entries, normal_exit_node,
def _abstract_cyclic_region(graph: networkx.DiGraph, loop_nodes, head, normal_entries, abnormal_entries,
normal_exit_node,
abnormal_exit_nodes):
region = GraphRegion(head, None, None, None, True)

Expand Down
8 changes: 6 additions & 2 deletions angr/analyses/forward_analysis/forward_analysis.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from collections import defaultdict
from typing import Dict, Any, List, Callable, Optional, Generic, TypeVar, Tuple, Set
from typing import Dict, List, Callable, Optional, Generic, TypeVar, Tuple, Set, TYPE_CHECKING

import networkx

from .visitors.graph import GraphVisitor, NodeType
from .visitors.graph import NodeType
from ...errors import AngrForwardAnalysisError
from ...errors import AngrSkipJobNotice, AngrDelayJobNotice, AngrJobMergingFailureNotice, AngrJobWideningFailureNotice
from ...utils.algo import binary_insert
from .job_info import JobInfo

if TYPE_CHECKING:
from .visitors.graph import GraphVisitor

AnalysisState = TypeVar("AnalysisState")
NodeKey = TypeVar("NodeKey")
JobType = TypeVar("JobType")
JobKey = TypeVar("JobKey")


class ForwardAnalysis(Generic[AnalysisState]):
"""
This is my very first attempt to build a static forward analysis framework that can serve as the base of multiple
Expand Down
2 changes: 0 additions & 2 deletions angr/analyses/forward_analysis/job_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,3 @@ def add_job(self, job, merged=False, widened=False):
elif widened:
job_type = 'widened'
self.jobs.append((job, job_type))


4 changes: 2 additions & 2 deletions angr/analyses/loop_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VariableTypes:

class AnnotatedVariable:

__slots__ = [ 'variable', 'type' ]
__slots__ = ['variable', 'type']

def __init__(self, variable, type_):
self.variable = variable
Expand Down Expand Up @@ -71,7 +71,7 @@ def process(self):
raise AngrLoopAnalysisError("Got an unexpected type of block %s." % type(self.block))

if not self.block.stmts:
return
return None

for stmt in self.block.stmts:

Expand Down
22 changes: 15 additions & 7 deletions angr/analyses/reaching_definitions/engine_ail.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,15 @@ def _ail_handle_Shr(self, expr: ailment.Expr.BinaryOp) -> MultiValues:
# each value in expr0 >> expr1_v
if len(expr0.values) == 1 and 0 in expr0.values:
if all(v.concrete for v in expr0.values[0]):
vs = {(claripy.LShR(v, expr1_v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr0.values[0]}
vs = {(claripy.LShR(v, expr1_v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr0.values[0]}
r = MultiValues(offset_to_values={0: vs})
elif expr0_v is not None and expr1_v is None:
# expr0_v >> each value in expr1
if len(expr1.values) == 1 and 0 in expr1.values:
if all(v.concrete for v in expr1.values[0]):
vs = {(claripy.LShR(expr0_v, v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr1.values[0]}
vs = {(claripy.LShR(expr0_v, v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr1.values[0]}
r = MultiValues(offset_to_values={0: vs})
else:
if expr0_v.concrete and expr1_v.concrete:
Expand All @@ -615,13 +617,15 @@ def _ail_handle_Sar(self, expr: ailment.Expr.BinaryOp) -> MultiValues:
# each value in expr0 >> expr1_v
if len(expr0.values) == 1 and 0 in expr0.values:
if all(v.concrete for v in expr0.values[0]):
vs = {(claripy.LShR(v, expr1_v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr0.values[0]}
vs = {(claripy.LShR(v, expr1_v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr0.values[0]}
r = MultiValues(offset_to_values={0: vs})
elif expr0_v is not None and expr1_v is None:
# expr0_v >> each value in expr1
if len(expr1.values) == 1 and 0 in expr1.values:
if all(v.concrete for v in expr1.values[0]):
vs = {(claripy.LShR(expr0_v, v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr1.values[0]}
vs = {(claripy.LShR(expr0_v, v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr1.values[0]}
r = MultiValues(offset_to_values={0: vs})
else:
if expr0_v.concrete and expr1_v.concrete:
Expand All @@ -647,13 +651,15 @@ def _ail_handle_Shl(self, expr: ailment.Expr.BinaryOp) -> MultiValues:
# each value in expr0 << expr1_v
if len(expr0.values) == 1 and 0 in expr0.values:
if all(v.concrete for v in expr0.values[0]):
vs = {((v << expr1_v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr0.values[0]}
vs = {((v << expr1_v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr0.values[0]}
r = MultiValues(offset_to_values={0: vs})
elif expr0_v is not None and expr1_v is None:
# expr0_v >> each value in expr1
if len(expr1.values) == 1 and 0 in expr1.values:
if all(v.concrete for v in expr1.values[0]):
vs = {((expr0_v << v._model_concrete.value) if v.concrete else self.state.top(bits)) for v in expr1.values[0]}
vs = {((expr0_v << v._model_concrete.value) if v.concrete else self.state.top(bits))
for v in expr1.values[0]}
r = MultiValues(offset_to_values={0: vs})
else:
if expr0_v.concrete and expr1_v.concrete:
Expand Down Expand Up @@ -836,7 +842,9 @@ def _ail_handle_StackBaseOffset(self, expr: ailment.Expr.StackBaseOffset) -> Mul
stack_addr = self.state.stack_address(expr.offset)
return MultiValues(offset_to_values={0: {stack_addr}})

def _ail_handle_DirtyExpression(self, expr: ailment.Expr.DirtyExpression) -> MultiValues: # pylint:disable=no-self-use
def _ail_handle_DirtyExpression(self,
expr: ailment.Expr.DirtyExpression
) -> MultiValues: # pylint:disable=no-self-use
# FIXME: DirtyExpression needs .bits
top = self.state.top(expr.bits)
return MultiValues(offset_to_values={0: {top}})
Expand Down
3 changes: 2 additions & 1 deletion angr/analyses/reaching_definitions/engine_vex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,8 @@ def _handle_function_cc(self, func_addr: Optional[MultiValues]):
atom = Register(reg_offset, reg_size)
self.state.kill_and_add_definition(atom,
self._codeloc(),
MultiValues(offset_to_values={0: {self.state.top(reg_size * self.arch.byte_width)}}),
MultiValues(offset_to_values=
{0: {self.state.top(reg_size * self.arch.byte_width)}}),
)

if self.arch.call_pushes_ret is True:
Expand Down
6 changes: 2 additions & 4 deletions angr/analyses/reaching_definitions/function_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import TYPE_CHECKING, List, Set, Optional, Tuple, Union
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, List, Set, Optional, Tuple
import logging

from cle import Symbol
Expand All @@ -12,6 +11,7 @@
from angr.analyses.reaching_definitions.rd_state import ReachingDefinitionsState


# pylint: disable=unused-argument, no-self-use
class FunctionHandler:
"""
An abstract base class for function handlers.
Expand Down Expand Up @@ -110,10 +110,8 @@ def handle_external_function_symbol(self,
:return:
"""
if symbol.name:
self.handle_external_function_name(state, symbol.name, src_codeloc)
return self.handle_external_function_name(state, symbol.name, src_codeloc)
else:
l.warning('Symbol for external function has no name, falling back to generic handler',
l.warning('Symbol %s for external function has no name, falling back to generic handler',
symbol)
return self.handle_external_function_fallback(state, src_codeloc)
Expand Down
Loading

0 comments on commit b79d2b4

Please sign in to comment.