ida-domain 0.3.5.dev1__tar.gz → 0.3.6.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/PKG-INFO +1 -1
  2. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/__init__.py +1 -1
  3. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/functions.py +239 -25
  4. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/hooks.py +1 -1
  5. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain.egg-info/PKG-INFO +1 -1
  6. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/pyproject.toml +1 -1
  7. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/LICENSE +0 -0
  8. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/README.md +0 -0
  9. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/base.py +0 -0
  10. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/bytes.py +0 -0
  11. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/comments.py +0 -0
  12. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/database.py +0 -0
  13. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/entries.py +0 -0
  14. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/flowchart.py +0 -0
  15. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/heads.py +0 -0
  16. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/instructions.py +0 -0
  17. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/names.py +0 -0
  18. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/operands.py +0 -0
  19. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/segments.py +0 -0
  20. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/signature_files.py +0 -0
  21. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/strings.py +0 -0
  22. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/types.py +0 -0
  23. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain/xrefs.py +0 -0
  24. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain.egg-info/SOURCES.txt +0 -0
  25. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain.egg-info/dependency_links.txt +0 -0
  26. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain.egg-info/requires.txt +0 -0
  27. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/ida_domain.egg-info/top_level.txt +0 -0
  28. {ida_domain-0.3.5.dev1 → ida_domain-0.3.6.dev2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ida-domain
3
- Version: 0.3.5.dev1
3
+ Version: 0.3.6.dev2
4
4
  Summary: IDA Domain API - Python interface for IDA Pro reverse engineering platform
5
5
  Author-email: Hex-Rays SA <support@hex-rays.com>
6
6
  License-Expression: MIT
@@ -25,7 +25,7 @@ def _load_dependencies() -> None:
25
25
  import idapro
26
26
 
27
27
 
28
- __version__ = '0.3.5-dev.1'
28
+ __version__ = '0.3.6-dev.2'
29
29
 
30
30
  # Make sure all dependencies are loaded
31
31
  _load_dependencies()
@@ -186,11 +186,79 @@ class FunctionChunk:
186
186
  """True if is the function main chunk"""
187
187
 
188
188
 
189
- class _LVarRefsVisitor(ida_hexrays.ctree_visitor_t):
190
- """Visitor to find references to a specific local variable in pseudocode."""
189
+ # ============================================================================
190
+ # Ctree Assignment Detection
191
+ # ============================================================================
192
+ #
193
+ # The hexrays ctree represents decompiled code as nested expression trees.
194
+ # Determining write access requires analyzing the tree structure.
195
+ #
196
+ # Direct assignment: v9 = 10
197
+ #
198
+ # cot_asg
199
+ # ├── x: cot_var (v9) ← variable is direct child of assignment
200
+ # └── y: cot_num (10)
201
+ #
202
+ # Nested assignment: HIWORD(v9) = a1 (helper function wrapping variable)
203
+ #
204
+ # cot_asg
205
+ # ├── x: cot_helper (HIWORD)
206
+ # │ └── a[0]: cot_var (v9) ← variable is nested in left subtree
207
+ # └── y: cot_var (a1)
208
+ #
209
+ # Other nested patterns include casts, pointer dereferences, and array indexing:
210
+ # *ptr = x → cot_ptr wraps the variable
211
+ # arr[i] = x → cot_idx wraps the variable
212
+ # (cast)v = x → cot_cast wraps the variable
213
+ #
214
+ # Problem:
215
+ # A direct parent check (parent.op == cot_asg and parent.x == expr) only
216
+ # handles direct assignments. In nested cases, the variable's immediate
217
+ # parent is the wrapper expression (helper/cast/ptr), not the assignment.
218
+ #
219
+ # Solution:
220
+ # Two-direction traversal:
221
+ # 1. UP: Traverse ancestor chain to locate an assignment operator
222
+ # 2. DOWN: Verify the variable resides in the assignment's left subtree
223
+ #
224
+ # Without this logic, `HIWORD(v9) = a1` incorrectly reports `v9` as READ
225
+ # because the immediate parent is cot_helper. This affects data flow analysis
226
+ # and variable access classification.
227
+ #
228
+ # Performance characteristics:
229
+ # - Up-traversal: O(d) where d = ancestor depth, typically 1-5 levels
230
+ # - Down-traversal: O(n) where n = nodes in left subtree, typically 1-10
231
+ # - Invoked once per variable reference during ctree visitor traversal
232
+ #
233
+ # ============================================================================
234
+
235
+ # All assignment operators in IDA hexrays ctree
236
+ _ASSIGNMENT_OPS = (
237
+ ida_hexrays.cot_asg,
238
+ ida_hexrays.cot_asgadd,
239
+ ida_hexrays.cot_asgmul,
240
+ ida_hexrays.cot_asgsub,
241
+ ida_hexrays.cot_asgsdiv,
242
+ ida_hexrays.cot_asgudiv,
243
+ ida_hexrays.cot_asgsmod,
244
+ ida_hexrays.cot_asgumod,
245
+ ida_hexrays.cot_asgbor,
246
+ ida_hexrays.cot_asgxor,
247
+ ida_hexrays.cot_asgband,
248
+ ida_hexrays.cot_asgsshr,
249
+ ida_hexrays.cot_asgushr,
250
+ ida_hexrays.cot_asgshl,
251
+ )
252
+
253
+
254
+ class _LVarRefsVisitor(ida_hexrays.ctree_parentee_t):
255
+ """Visitor to find references to a specific local variable in pseudocode.
256
+
257
+ Uses ctree_parentee_t which properly maintains parent information.
258
+ """
191
259
 
192
260
  def __init__(self, cfunc: Any, lvar_index: int):
193
- super().__init__(ida_hexrays.CV_FAST)
261
+ super().__init__()
194
262
  self.cfunc = cfunc
195
263
  self.lvar_index = lvar_index
196
264
  self.refs: List[LocalVariableReference] = []
@@ -201,8 +269,11 @@ class _LVarRefsVisitor(ida_hexrays.ctree_visitor_t):
201
269
  # Found any variable - check if it's our target
202
270
  if expr.v.idx == self.lvar_index:
203
271
  # Found a reference to our variable
204
- access_type = self._determine_access_type(expr)
205
- context = self._determine_context(expr)
272
+ # Use parent_expr() to get the parent expression
273
+ parent = self.parent_expr()
274
+
275
+ access_type = self._determine_access_type(expr, parent)
276
+ context = self._determine_context(expr, parent)
206
277
  ea = expr.ea if expr.ea != BADADDR else None
207
278
 
208
279
  # Extract line information
@@ -231,45 +302,160 @@ class _LVarRefsVisitor(ida_hexrays.ctree_visitor_t):
231
302
  self.refs.append(ref)
232
303
  return 0
233
304
 
234
- def _determine_access_type(self, expr: Any) -> LocalVariableAccessType:
305
+ def _is_on_left_side_of_assignment(self, asg_expr: Any, child_expr: Any) -> bool:
306
+ """Check if child_expr is in the left subtree of an assignment.
307
+
308
+ Performs a depth-first traversal from the assignment's left operand (.x)
309
+ to determine if child_expr is reachable. If reachable, the variable is
310
+ on the write side of the assignment.
311
+
312
+ This handles nested patterns where the variable is wrapped in expressions:
313
+ HIWORD(v9) = a1 → v9 is inside cot_helper, which is in .x
314
+ *ptr = val → ptr is inside cot_ptr, which is in .x
315
+
316
+ Args:
317
+ asg_expr: An assignment expression (cot_asg or compound assignment).
318
+ child_expr: The expression to search for in the left subtree.
319
+
320
+ Returns:
321
+ True if child_expr is found in the left subtree (write side).
322
+
323
+ Performance:
324
+ O(n) where n = number of nodes in left subtree. Typically 1-10 nodes
325
+ for common patterns like helpers, casts, or pointer dereferences.
326
+ """
327
+ # asg_expr might be a citem_t, we need to access the cexpr_t
328
+ # Check if it's an expression type with x attribute
329
+ if not hasattr(asg_expr, 'x'):
330
+ # Try to get the expression from citem_t
331
+ if hasattr(asg_expr, 'cexpr'):
332
+ asg_expr = asg_expr.cexpr
333
+ else:
334
+ return False
335
+
336
+ # Get the left side of the assignment
337
+ left = asg_expr.x
338
+ if left is None:
339
+ return False
340
+
341
+ # Use a simple BFS/DFS to find if child_expr is in the left subtree
342
+ stack = [left]
343
+ while stack:
344
+ current = stack.pop()
345
+ if current is None:
346
+ continue
347
+ # Check if this is our target expression (by object identity or ea)
348
+ if current == child_expr:
349
+ return True
350
+ if hasattr(current, 'ea') and hasattr(child_expr, 'ea'):
351
+ if current.ea == child_expr.ea and current.op == child_expr.op:
352
+ return True
353
+ # Traverse children based on expression type
354
+ if hasattr(current, 'x') and current.x is not None:
355
+ stack.append(current.x)
356
+ if hasattr(current, 'y') and current.y is not None:
357
+ stack.append(current.y)
358
+ if hasattr(current, 'z') and current.z is not None:
359
+ stack.append(current.z)
360
+ return False
361
+
362
+ def _find_assignment_in_ancestors(self) -> tuple:
363
+ """Find an assignment operator in the ancestor chain.
364
+
365
+ Traverses from the immediate parent upward through the ancestor chain
366
+ to locate an assignment operator. When found, determines whether the
367
+ current expression is on the left (write) or right (read) side.
368
+
369
+ This enables correct access type detection for nested patterns:
370
+ HIWORD(v9) = a1 → parent of v9 is cot_helper, not assignment
371
+ → ancestor traversal finds the cot_asg above
372
+
373
+ Uses self.parents vector maintained by ctree_parentee_t during traversal.
374
+
375
+ Returns:
376
+ Tuple of (assignment_expr, is_on_left_side):
377
+ - (expr, True) if in left subtree (write side)
378
+ - (expr, False) if in right subtree (read side)
379
+ - (None, False) if no assignment found in ancestors
380
+
381
+ Performance:
382
+ O(d) where d = ancestor depth. Typically 1-5 levels for common
383
+ patterns. The parents vector is already maintained by the base
384
+ class during tree traversal.
385
+ """
386
+ num_parents = len(self.parents)
387
+ for i in range(num_parents):
388
+ # Access parents from end (nearest) to beginning (farthest)
389
+ parent_item = self.parents[num_parents - 1 - i]
390
+ if parent_item is None:
391
+ continue
392
+ # Check if this parent is an expression with an assignment op
393
+ if not hasattr(parent_item, 'op'):
394
+ continue
395
+ if parent_item.op in _ASSIGNMENT_OPS:
396
+ # Found an assignment - determine if we're on the left side
397
+ # The item right before this assignment in the parent chain
398
+ # is the child that leads to our expression
399
+ if i == 0:
400
+ # Immediate parent is assignment, use parent_expr()
401
+ child = self.parent_expr()
402
+ else:
403
+ # Get the item that is child of this assignment
404
+ child = self.parents[num_parents - i]
405
+ is_left = self._is_on_left_side_of_assignment(parent_item, child)
406
+ return (parent_item, is_left)
407
+ return (None, False)
408
+
409
+ def _determine_access_type(self, expr: Any, parent: Any) -> LocalVariableAccessType:
235
410
  """Determine how the variable is being accessed."""
236
- parent = expr.cexpr
237
411
  if not parent:
238
412
  return LocalVariableAccessType.READ
239
413
 
240
414
  # Check if this is a write operation (left side of assignment)
241
415
  if parent.op == ida_hexrays.cot_asg and parent.x == expr:
242
416
  return LocalVariableAccessType.WRITE
243
- # Check if address is taken
244
- elif parent.op == ida_hexrays.cot_ref and parent.x == expr:
417
+ # Check compound assignments at immediate parent level
418
+ elif parent.op in _ASSIGNMENT_OPS and parent.x == expr:
419
+ return LocalVariableAccessType.WRITE
420
+ # Check if address is taken - but also check ancestors for assignment
421
+ elif parent.op == ida_hexrays.cot_ref:
422
+ # Address is taken - check if this is part of an assignment
423
+ asg, is_left = self._find_assignment_in_ancestors()
424
+ if asg and is_left:
425
+ return LocalVariableAccessType.WRITE
245
426
  return LocalVariableAccessType.ADDRESS
246
- # Check compound assignments
247
- elif (
248
- parent.op
249
- in (
250
- ida_hexrays.cot_asgadd,
251
- ida_hexrays.cot_asgmul,
252
- ida_hexrays.cot_asgsub,
253
- ida_hexrays.cot_asgsdiv,
254
- ida_hexrays.cot_asgudiv,
255
- ida_hexrays.cot_asgsmod,
256
- ida_hexrays.cot_asgumod,
257
- )
258
- and parent.x == expr
427
+
428
+ # For calls (including helpers like HIWORD), casts, ptr derefs - check ancestors
429
+ if parent.op in (
430
+ ida_hexrays.cot_call,
431
+ ida_hexrays.cot_helper,
432
+ ida_hexrays.cot_cast,
433
+ ida_hexrays.cot_ptr,
434
+ ida_hexrays.cot_memptr,
435
+ ida_hexrays.cot_memref,
436
+ ida_hexrays.cot_add,
437
+ ida_hexrays.cot_idx,
259
438
  ):
260
- return LocalVariableAccessType.WRITE
439
+ asg, is_left = self._find_assignment_in_ancestors()
440
+ if asg and is_left:
441
+ return LocalVariableAccessType.WRITE
261
442
 
262
443
  return LocalVariableAccessType.READ
263
444
 
264
- def _determine_context(self, expr: Any) -> LocalVariableContext:
445
+ def _determine_context(self, expr: Any, parent: Any) -> LocalVariableContext:
265
446
  """Determine the context where the variable is used."""
266
- parent = expr.cexpr
267
447
  if not parent:
268
448
  return LocalVariableContext.OTHER
269
449
 
270
450
  if parent.op == ida_hexrays.cot_asg:
271
451
  return LocalVariableContext.ASSIGNMENT
452
+ elif parent.op in _ASSIGNMENT_OPS:
453
+ return LocalVariableContext.ASSIGNMENT
272
454
  elif parent.op == ida_hexrays.cot_call:
455
+ # Check if there's an assignment ancestor - if so, this is ASSIGNMENT context
456
+ asg, is_left = self._find_assignment_in_ancestors()
457
+ if asg and is_left:
458
+ return LocalVariableContext.ASSIGNMENT
273
459
  return LocalVariableContext.CALL_ARG
274
460
  elif parent.op in (
275
461
  ida_hexrays.cot_eq,
@@ -293,13 +479,41 @@ class _LVarRefsVisitor(ida_hexrays.ctree_visitor_t):
293
479
  ida_hexrays.cot_smod,
294
480
  ida_hexrays.cot_umod,
295
481
  ):
482
+ # Check if there's an assignment ancestor
483
+ asg, is_left = self._find_assignment_in_ancestors()
484
+ if asg and is_left:
485
+ return LocalVariableContext.ASSIGNMENT
296
486
  return LocalVariableContext.ARITHMETIC
297
487
  elif parent.op == ida_hexrays.cot_idx:
488
+ # Check if there's an assignment ancestor
489
+ asg, is_left = self._find_assignment_in_ancestors()
490
+ if asg and is_left:
491
+ return LocalVariableContext.ASSIGNMENT
298
492
  return LocalVariableContext.ARRAY_INDEX
299
493
  elif parent.op in (ida_hexrays.cot_ptr, ida_hexrays.cot_memptr, ida_hexrays.cot_memref):
494
+ # Check if there's an assignment ancestor
495
+ asg, is_left = self._find_assignment_in_ancestors()
496
+ if asg and is_left:
497
+ return LocalVariableContext.ASSIGNMENT
300
498
  return LocalVariableContext.POINTER_DEREF
301
499
  elif parent.op == ida_hexrays.cot_cast:
500
+ # Check if there's an assignment ancestor
501
+ asg, is_left = self._find_assignment_in_ancestors()
502
+ if asg and is_left:
503
+ return LocalVariableContext.ASSIGNMENT
302
504
  return LocalVariableContext.CAST
505
+ elif parent.op == ida_hexrays.cot_ref:
506
+ # Address taken - check for assignment ancestor
507
+ asg, is_left = self._find_assignment_in_ancestors()
508
+ if asg and is_left:
509
+ return LocalVariableContext.ASSIGNMENT
510
+ return LocalVariableContext.OTHER
511
+ elif parent.op == ida_hexrays.cot_helper:
512
+ # Helper functions like HIWORD - check for assignment ancestor
513
+ asg, is_left = self._find_assignment_in_ancestors()
514
+ if asg and is_left:
515
+ return LocalVariableContext.ASSIGNMENT
516
+ return LocalVariableContext.OTHER
303
517
 
304
518
  return LocalVariableContext.OTHER
305
519
 
@@ -2387,7 +2387,7 @@ class ProcessorHooks(_BaseHooks, IDP_Hooks):
2387
2387
  Returns:
2388
2388
  PyObject*: 1 if success, 0 if not implemented or failed.
2389
2389
  """
2390
- return IDP_Hooks.ev_decorate_name(self, name, mangle, cc, optional_type)
2390
+ return 0
2391
2391
 
2392
2392
  def ev_arch_changed(self) -> int:
2393
2393
  """
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ida-domain
3
- Version: 0.3.5.dev1
3
+ Version: 0.3.6.dev2
4
4
  Summary: IDA Domain API - Python interface for IDA Pro reverse engineering platform
5
5
  Author-email: Hex-Rays SA <support@hex-rays.com>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ida-domain"
3
- version = "0.3.5-dev.1"
3
+ version = "0.3.6-dev.2"
4
4
  description = "IDA Domain API - Python interface for IDA Pro reverse engineering platform"
5
5
  requires-python = ">=3.9"
6
6
  license = "MIT"
File without changes