Skip to content

Commit

Permalink
Lift as constant detected pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
fnhartmann authored and mari-mari committed Apr 11, 2024
1 parent 0cc75f7 commit e6814c2
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions decompiler/frontend/binaryninja/handlers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
from typing import Union

from binaryninja import DataVariable, SymbolType, Type, mediumlevelil
from binaryninja import DataVariable, SymbolType, Type, mediumlevelil, BinaryView
from decompiler.frontend.lifter import Handler
from decompiler.structures.pseudo import (
Constant,
Expand Down Expand Up @@ -35,10 +35,12 @@ def register(self):
}
)

def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs) -> Constant:
def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs):
"""Lift the given constant value."""
if constant.constant in [math.inf, -math.inf, math.nan]:
return NotUseableConstant(str(constant.constant))
if self._addr_in_section(constant.function.view, constant.constant):
return self.lift_constant_pointer(constant)
return Constant(constant.constant, vartype=self._lifter.lift(constant.expr_type))

@staticmethod
Expand Down Expand Up @@ -72,11 +74,25 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, **
if isinstance(res.type, Pointer) and res.type.type == CustomType.void():
return res

if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&'
if isinstance(pointer, mediumlevelil.MediumLevelILImport): # Temp fix for '&'git
return res

return UnaryOperation(
OperationType.address,
[res],
vartype=res.type,
)

def _in_read_only_section(self, addr: int, view: BinaryView) -> bool:
"""Returns True if address is contained in a read only section, False otherwise"""
for _, section in view.sections.items():
if addr >= section.start and addr <= section.end and section.semantics == SectionSemantics.ReadOnlyDataSectionSemantics:
return True
return False

def _addr_in_section(self, view: BinaryView, addr: int) -> bool:
"""Returns True if address is contained in a section, False otherwise"""
for _, section in view.sections.items():
if addr >= section.start and addr <= section.end:
return True
return False

0 comments on commit e6814c2

Please sign in to comment.