Skip to content

Commit 36a72c5

Browse files
committed
AST: improve on memory variable resolution
1 parent 1053d3e commit 36a72c5

File tree

8 files changed

+70
-15
lines changed

8 files changed

+70
-15
lines changed

chb/app/CHVersion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
chbversion: str = "0.3.0-20250204"
1+
chbversion: str = "0.3.0-20250210"

chb/arm/opcodes/ARMAdd.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ def pointer_arithmetic_expr() -> AST.ASTExpr:
407407
else:
408408
if rhs3.is_constant_expression:
409409
astree.set_ssa_value(str(hl_lhs), hl_rhs)
410-
411410
else:
412411
hl_rhs = XU.xxpr_to_ast_def_expr(rhs3, xdata, iaddr, astree)
413412

chb/arm/opcodes/ARMStoreRegister.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ def vmem(self) -> "XVariable":
6969
def is_vmem_unknown(self) -> bool:
7070
return self.xdata.vars_r[0] is None
7171

72+
@property
73+
def lhsvar(self) -> "XVariable":
74+
return self.var(1, "lhsvar")
75+
76+
@property
77+
def is_lhsvar_unknown(self) -> bool:
78+
return self.xdata.vars_r[1] is None
79+
80+
@property
81+
def is_lhsvar_known(self) -> bool:
82+
return self.xdata.vars_r[1] is not None
83+
7284
@property
7385
def vrn(self) -> "XVariable":
7486
return self.var(1, "vrn")
@@ -234,6 +246,12 @@ def ast_prov(
234246
hl_lhs = XU.xvariable_to_ast_lval(
235247
lhs, xdata, iaddr, astree, memaddr=memaddr)
236248

249+
elif xd.is_vmem_unknown and xd.is_lhsvar_known and xd.is_address_known:
250+
memaddr = xd.xaddr
251+
lhsvar = xd.lhsvar
252+
hl_lhs = XU.xvariable_to_ast_lval(
253+
lhsvar, xdata, iaddr, astree, memaddr=memaddr)
254+
237255
elif xd.is_vmem_unknown and xd.is_address_known:
238256
memaddr = xd.xaddr
239257
hl_lhs = XU.xmemory_dereference_lval(memaddr, xdata, iaddr, astree)
@@ -245,6 +263,11 @@ def ast_prov(
245263
iaddr)
246264
return ([], [])
247265

266+
if xd.is_vmem_unknown:
267+
vmem = "unknown"
268+
else:
269+
vmem = str(xd.vmem)
270+
248271
rhs = xd.xxrt
249272
rdefs = xdata.reachingdefs
250273
defuses = xdata.defuses

chb/arm/opcodes/ARMStoreRegisterByte.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def vmem(self) -> "XVariable":
6464
def is_vmem_unknown(self) -> bool:
6565
return self.xdata.vars_r[0] is None
6666

67+
@property
68+
def lhsvar(self) -> "XVariable":
69+
return self.var(1, "lhsvar")
70+
71+
@property
72+
def is_lhsvar_unknown(self) -> bool:
73+
return self.xdata.vars_r[1] is None
74+
75+
@property
76+
def is_lhsvar_known(self) -> bool:
77+
return self.xdata.vars_r[1] is not None
78+
6779
@property
6880
def xrn(self) -> "XXpr":
6981
return self.xpr(0, "xrn")
@@ -188,11 +200,17 @@ def ast_prov(
188200
xd = ARMStoreRegisterByteXData(xdata)
189201

190202
if xd.is_ok:
191-
lhs = xd.vmem
203+
lhs = xd.lhsvar
192204
memaddr = xd.xaddr
193205
hl_lhs = XU.xvariable_to_ast_lval(
194206
lhs, xdata, iaddr, astree, memaddr=memaddr)
195207

208+
elif xd.is_vmem_unknown and xd.is_lhsvar_known and xd.is_address_known:
209+
memaddr = xd.xaddr
210+
lhsvar = xd.lhsvar
211+
hl_lhs = XU.xvariable_to_ast_lval(
212+
lhsvar, xdata, iaddr, astree, memaddr=memaddr)
213+
196214
elif xd.is_vmem_unknown and xd.is_address_known:
197215
memaddr = xd.xaddr
198216
hl_lhs = XU.xmemory_dereference_lval(memaddr, xdata, iaddr, astree)

chb/ast/ASTCPrettyPrinter.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2022-2024 Aarno Labs LLC
7+
# Copyright (c) 2022-2025 Aarno Labs LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -463,7 +463,7 @@ def visit_asm_instr(self, instr: AST.ASTAsm) -> None:
463463
def visit_lval(self, lval: AST.ASTLval) -> None:
464464
if lval.lhost.is_memref:
465465
memexp = cast(AST.ASTMemRef, lval.lhost).memexp
466-
if lval.offset.is_field_offset:
466+
if lval.offset.is_field_offset and not memexp.is_ast_addressof:
467467
fieldname = cast(AST.ASTFieldOffset, lval.offset).fieldname
468468
suboffset = cast(AST.ASTFieldOffset, lval.offset).offset
469469
memexp.accept(self)
@@ -488,9 +488,13 @@ def visit_variable(self, var: AST.ASTVariable) -> None:
488488
self.ccode.write(var.vname)
489489

490490
def visit_memref(self, memref: AST.ASTMemRef) -> None:
491-
self.ccode.write("(*(")
492-
memref.memexp.accept(self)
493-
self.ccode.write("))")
491+
if memref.memexp.is_ast_addressof:
492+
memexp = cast(AST.ASTAddressOf, memref.memexp)
493+
memexp.lval.accept(self)
494+
else:
495+
self.ccode.write("(*(")
496+
memref.memexp.accept(self)
497+
self.ccode.write("))")
494498

495499
def visit_no_offset(self, offset: AST.ASTNoOffset) -> None:
496500
pass

chb/cmdline/PatchResults.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------------------------------------------------
55
# The MIT License (MIT)
66
#
7-
# Copyright (c) 2023-2024 Aarno Labs, LLC
7+
# Copyright (c) 2023-2025 Aarno Labs, LLC
88
#
99
# Permission is hereby granted, free of charge, to any person obtaining a copy
1010
# of this software and associated documentation files (the "Software"), to deal
@@ -409,6 +409,8 @@ def events(self) -> List[PatchEvent]:
409409
if self._events is None:
410410
self._events = []
411411
for de in self._d["events"]:
412+
if de["category"] == "failure":
413+
continue
412414
self._events.append(PatchEvent(de))
413415
return self._events
414416

chb/invariants/XXpr.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2016-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2021 Henny B. Sipma
9-
# Copyright (c) 2021-2024 Aarno Labs LLC
9+
# Copyright (c) 2021-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -766,19 +766,28 @@ def is_stack_address(self) -> bool:
766766
args = self.operands
767767
if len(args) == 2:
768768
return (args[0].is_stack_base_address and args[1].is_constant)
769+
elif self.is_addressof_var:
770+
xvar = self.get_addressof_var
771+
return xvar is not None and xvar.is_local_stack_variable
769772
else:
770773
return False
771774

772775
def stack_address_offset(self) -> int:
773-
if self.is_stack_address:
776+
if self.is_stack_address and len(self.operands) == 2:
777+
# explicit stack address
774778
stackoffset = self.operands[1]
775779
if self.operator == 'minus':
776780
return stackoffset.negated_value()
777781
else:
778782
return stackoffset.intvalue
779-
else:
780-
raise UF.CHBError(
781-
"Expression is not a stack address: " + str(self))
783+
784+
elif self.is_stack_address and self.is_addressof_var:
785+
xvar = self.get_addressof_var
786+
if xvar is not None:
787+
return xvar.denotation.offset.offsetvalue()
788+
789+
raise UF.CHBError(
790+
"Expression is not a stack address: " + str(self))
782791

783792
@property
784793
def is_heap_address(self) -> bool:

chb/invariants/XXprUtil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ def default() -> AST.ASTExpr:
10351035

10361036
cst2 = cast(AST.ASTIntegerConstant, axpr2).cvalue
10371037

1038-
if not axpr1.is_ast_lval_expr:
1038+
if not (axpr1.is_ast_lval_expr or axpr1.is_ast_addressof):
10391039
chklogger.logger.warning(
10401040
"AST def conversion of pointer expression encountered unexpected "
10411041
+ " base expression %s at address %s",

0 commit comments

Comments
 (0)