PyMemoryEditor 3.2.0__py3-none-any.whl → 3.3.0__py3-none-any.whl

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 (34) hide show
  1. PyMemoryEditor/__init__.py +1 -1
  2. PyMemoryEditor/app/application.py +2 -2
  3. PyMemoryEditor/app/cheat_poll_worker.py +18 -12
  4. PyMemoryEditor/app/cheat_table.py +66 -28
  5. PyMemoryEditor/app/main_window.py +80 -26
  6. PyMemoryEditor/app/pointer_scan_dialog.py +83 -4
  7. PyMemoryEditor/app/results_view.py +71 -8
  8. PyMemoryEditor/app/scan_worker.py +10 -0
  9. PyMemoryEditor/linux/functions.py +30 -8
  10. PyMemoryEditor/linux/process.py +27 -9
  11. PyMemoryEditor/macos/functions.py +120 -39
  12. PyMemoryEditor/macos/process.py +49 -17
  13. PyMemoryEditor/mcp/config.py +34 -8
  14. PyMemoryEditor/mcp/policy.py +185 -18
  15. PyMemoryEditor/mcp/procinfo.py +258 -0
  16. PyMemoryEditor/mcp/server.py +223 -16
  17. PyMemoryEditor/mcp/session.py +59 -5
  18. PyMemoryEditor/mcp/toolset.py +244 -21
  19. PyMemoryEditor/process/_guard.py +42 -0
  20. PyMemoryEditor/process/abstract.py +54 -11
  21. PyMemoryEditor/process/info.py +3 -1
  22. PyMemoryEditor/process/pointer_scan.py +133 -13
  23. PyMemoryEditor/process/scanning.py +51 -22
  24. PyMemoryEditor/util/__init__.py +1 -0
  25. PyMemoryEditor/util/convert.py +13 -3
  26. PyMemoryEditor/util/pattern.py +11 -1
  27. PyMemoryEditor/util/scan.py +52 -0
  28. PyMemoryEditor/win32/functions.py +90 -26
  29. PyMemoryEditor/win32/process.py +34 -10
  30. {pymemoryeditor-3.2.0.dist-info → pymemoryeditor-3.3.0.dist-info}/METADATA +12 -6
  31. {pymemoryeditor-3.2.0.dist-info → pymemoryeditor-3.3.0.dist-info}/RECORD +34 -32
  32. {pymemoryeditor-3.2.0.dist-info → pymemoryeditor-3.3.0.dist-info}/WHEEL +0 -0
  33. {pymemoryeditor-3.2.0.dist-info → pymemoryeditor-3.3.0.dist-info}/entry_points.txt +0 -0
  34. {pymemoryeditor-3.2.0.dist-info → pymemoryeditor-3.3.0.dist-info}/licenses/LICENSE +0 -0
@@ -6,6 +6,8 @@ Built on a Qt model/view so we can stream hundreds of thousands of results
6
6
  into it without freezing the UI. The model keeps an internal address→row
7
7
  index so the scan worker's chunked updates can patch existing rows in O(1).
8
8
  """
9
+ from bisect import bisect_left
10
+ from itertools import compress
9
11
  from typing import Any, Dict, List, Optional, Tuple
10
12
 
11
13
  from PySide6.QtCore import QAbstractTableModel, QModelIndex, Qt, Signal
@@ -61,7 +63,16 @@ class ResultsModel(QAbstractTableModel):
61
63
  self._addresses: List[int] = []
62
64
  self._values: List[Any] = []
63
65
  self._previous: List[Any] = []
66
+ # address → the row it held when the index was last built (its "base"
67
+ # row). Rows dropped since then are listed, ascending, in ``_removed``:
68
+ # an address's current row is its base minus the dropped rows above it
69
+ # (see ``_row_of``). Rebuilding the whole dict after every refine chunk
70
+ # made a refine quadratic in the result count.
64
71
  self._index: Dict[int, int] = {}
72
+ self._removed: List[int] = []
73
+ # Set when an address was appended twice: the base-row bookkeeping
74
+ # assumes one row per address, so _drop_rows falls back to a rebuild.
75
+ self._duplicates = False
65
76
  self._spec: Optional[ValueTypeSpec] = None
66
77
 
67
78
  def rowCount(self, parent=QModelIndex()) -> int:
@@ -132,15 +143,35 @@ class ResultsModel(QAbstractTableModel):
132
143
  self._values.clear()
133
144
  self._previous.clear()
134
145
  self._index.clear()
146
+ self._removed.clear()
147
+ self._duplicates = False
135
148
  self.endResetModel()
136
149
 
150
+ def _row_of(self, address: int) -> Optional[int]:
151
+ """Current row of ``address``, or None if it isn't in the model."""
152
+ base = self._index.get(address)
153
+ if base is None or not self._removed:
154
+ return base
155
+ return base - bisect_left(self._removed, base)
156
+
157
+ def _reindex(self) -> None:
158
+ """Rebuild the address index from the lists (one O(n) pass)."""
159
+ self._index = {addr: idx for idx, addr in enumerate(self._addresses)}
160
+ self._removed = []
161
+ self._duplicates = len(self._index) != len(self._addresses)
162
+
137
163
  def append_chunk(self, chunk: List[Tuple[int, Any]]) -> None:
138
164
  """Append newly-discovered addresses (used by FirstScanWorker)."""
139
165
  if not chunk:
140
166
  return
167
+ if self._removed:
168
+ # New rows get base rows past every old one only on a fresh index.
169
+ self._reindex()
141
170
  first = len(self._addresses)
142
171
  self.beginInsertRows(QModelIndex(), first, first + len(chunk) - 1)
143
172
  for address, value in chunk:
173
+ if address in self._index:
174
+ self._duplicates = True
144
175
  self._index[address] = len(self._addresses)
145
176
  self._addresses.append(address)
146
177
  self._values.append(value)
@@ -158,7 +189,7 @@ class ResultsModel(QAbstractTableModel):
158
189
  rows_to_drop: List[int] = []
159
190
  rows_changed: List[int] = []
160
191
  for address, current, keep in chunk:
161
- row = self._index.get(address)
192
+ row = self._row_of(address)
162
193
  if row is None:
163
194
  continue
164
195
  if not keep:
@@ -198,14 +229,28 @@ class ResultsModel(QAbstractTableModel):
198
229
  return
199
230
 
200
231
  runs = _contiguous_runs(rows)
232
+ # Base rows of the dropped addresses, recorded before the lists shift
233
+ # (rows are ascending, so these come out ascending too).
234
+ dropped_bases: List[int] = []
235
+ if not self._duplicates:
236
+ for row in rows:
237
+ base = self._index.pop(self._addresses[row], None)
238
+ if base is not None:
239
+ dropped_bases.append(base)
240
+
201
241
  if len(runs) > _RESET_RUN_THRESHOLD:
202
- dropped = set(rows)
203
- keep = [i for i in range(count) if i not in dropped]
242
+ # Compact only the window the dropped rows span: a refine walks the
243
+ # rows in order, so that window is about one chunk wide, and the
244
+ # rest of the list is shifted by a single memmove rather than
245
+ # copied element by element.
246
+ lo, hi = rows[0], rows[-1] + 1
247
+ mask = bytearray(b"\x01") * (hi - lo)
248
+ for row in rows:
249
+ mask[row - lo] = 0
204
250
  self.beginResetModel()
205
- self._addresses = [self._addresses[i] for i in keep]
206
- self._values = [self._values[i] for i in keep]
207
- self._previous = [self._previous[i] for i in keep]
208
- self._index = {addr: idx for idx, addr in enumerate(self._addresses)}
251
+ for column in (self._addresses, self._values, self._previous):
252
+ column[lo:hi] = list(compress(column[lo:hi], mask))
253
+ self._forget_dropped(dropped_bases)
209
254
  self.endResetModel()
210
255
  return
211
256
 
@@ -221,7 +266,25 @@ class ResultsModel(QAbstractTableModel):
221
266
  del self._previous[first:last + 1]
222
267
  self.endRemoveRows()
223
268
 
224
- self._index = {addr: idx for idx, addr in enumerate(self._addresses)}
269
+ self._forget_dropped(dropped_bases)
270
+
271
+ def _forget_dropped(self, dropped_bases: List[int]) -> None:
272
+ """Fold a drop's base rows into the index bookkeeping.
273
+
274
+ Merging two ascending lists is linear and runs in C, unlike rebuilding
275
+ the whole dict; the dict itself is rebuilt only once more rows have
276
+ been dropped than remain, so its cost is amortised over those drops.
277
+ """
278
+ if self._duplicates:
279
+ self._reindex()
280
+ return
281
+ if dropped_bases:
282
+ if not self._removed or dropped_bases[0] > self._removed[-1]:
283
+ self._removed.extend(dropped_bases)
284
+ else:
285
+ self._removed = sorted(self._removed + dropped_bases)
286
+ if len(self._removed) > len(self._addresses):
287
+ self._reindex()
225
288
 
226
289
  def address_at(self, row: int) -> Optional[int]:
227
290
  if 0 <= row < len(self._addresses):
@@ -208,10 +208,18 @@ class _BaseWorker(QThread):
208
208
  super().__init__(parent)
209
209
  self._process = process
210
210
  self._cancelled = False
211
+ # Set by run() once the scan went through to the end: not cancelled,
212
+ # no error. ``finished_ok`` alone can't say so — a cancelled scan
213
+ # still emits it with the partial count.
214
+ self._completed = False
211
215
 
212
216
  def cancel(self) -> None:
213
217
  self._cancelled = True
214
218
 
219
+ def completed(self) -> bool:
220
+ """Whether the last run finished without being cancelled or failing."""
221
+ return self._completed
222
+
215
223
 
216
224
  class FirstScanWorker(_BaseWorker):
217
225
  """Performs the very first scan, finding every address that matches."""
@@ -312,6 +320,7 @@ class FirstScanWorker(_BaseWorker):
312
320
  self.chunk_ready.emit(chunk)
313
321
 
314
322
  self.progress.emit(100.0)
323
+ self._completed = not self._cancelled
315
324
  self.finished_ok.emit(count)
316
325
  except Exception as exc: # noqa: BLE001 — surface every backend error to the UI
317
326
  _LOG.warning("First scan failed: %s: %s", type(exc).__name__, exc)
@@ -442,6 +451,7 @@ class RefineScanWorker(_BaseWorker):
442
451
  if not self._cancelled:
443
452
  self.status.emit(f"Checked {seen:,}/{total:,}, kept {kept:,}…")
444
453
  self.progress.emit(100.0)
454
+ self._completed = not self._cancelled
445
455
  self.finished_ok.emit(kept)
446
456
  except Exception as exc: # noqa: BLE001 — surface every backend error to the UI
447
457
  _LOG.warning("Refine scan failed: %s: %s", type(exc).__name__, exc)
@@ -215,11 +215,17 @@ def get_memory_regions(pid: int) -> Generator["MemoryRegion", None, None]:
215
215
  Translates the typical I/O failures of ``/proc/<pid>/maps`` into the
216
216
  library's own exception hierarchy so callers don't have to special-case
217
217
  raw ``FileNotFoundError`` / ``PermissionError`` from the kernel pseudo-fs.
218
+
219
+ The file is parsed as bytes: a mapped path is whatever bytes the file
220
+ system holds, not necessarily UTF-8, and decoding the file as text made
221
+ one such path abort every listing and scan of that process with
222
+ ``UnicodeDecodeError``. The raw path bytes go into the struct;
223
+ ``MemoryRegion.path`` decodes them as UTF-8 with replacement characters.
218
224
  """
219
225
  mapping_filename = "/proc/{}/maps".format(pid)
220
226
 
221
227
  try:
222
- mapping_file = open(mapping_filename, "r")
228
+ mapping_file = open(mapping_filename, "rb")
223
229
  except FileNotFoundError:
224
230
  # Target died between OpenProcess()'s pid_exists() check and now. The
225
231
  # caller already accepts ProcessIDNotExistsError from the open path,
@@ -246,12 +252,12 @@ def get_memory_regions(pid: int) -> Generator["MemoryRegion", None, None]:
246
252
  addressing_range, privileges, offset, device, inode = (
247
253
  region_information[0:5]
248
254
  )
249
- path = region_information[5].strip() if len(region_information) >= 6 else ""
255
+ path = region_information[5].strip() if len(region_information) >= 6 else b""
250
256
 
251
257
  start_address, end_address = [
252
- int(addr, 16) for addr in addressing_range.split("-")
258
+ int(addr, 16) for addr in addressing_range.split(b"-")
253
259
  ]
254
- major_id, minor_id = [int(_id, 16) for _id in device.split(":")]
260
+ major_id, minor_id = [int(_id, 16) for _id in device.split(b":")]
255
261
 
256
262
  offset = int(offset, 16)
257
263
  inode = int(inode) # /proc/<pid>/maps formats the inode as decimal.
@@ -270,8 +276,8 @@ def get_memory_regions(pid: int) -> Generator["MemoryRegion", None, None]:
270
276
 
271
277
  # Truncate to fit the fixed-size inline byte arrays in the struct.
272
278
  # Leave room for a null so attribute reads always terminate cleanly.
273
- privileges_bytes = privileges.encode()[: PRIVILEGES_SIZE - 1]
274
- path_bytes = path.encode()[: PATH_SIZE - 1]
279
+ privileges_bytes = privileges[: PRIVILEGES_SIZE - 1]
280
+ path_bytes = path[: PATH_SIZE - 1]
275
281
 
276
282
  region = MEMORY_BASIC_INFORMATION(
277
283
  start_address,
@@ -645,7 +651,15 @@ def get_threads(pid: int) -> Generator[ThreadInfo, None, None]:
645
651
  state: Optional[str] = None
646
652
  priority: Optional[int] = None
647
653
  try:
648
- with open("{}/{}/stat".format(task_dir, entry), "r") as fh:
654
+ # `comm` inside the stat line is whatever bytes the thread named
655
+ # itself (prctl PR_SET_NAME), not necessarily UTF-8; only the
656
+ # ASCII fields after it are parsed, so decode leniently.
657
+ with open(
658
+ "{}/{}/stat".format(task_dir, entry),
659
+ "r",
660
+ encoding="utf-8",
661
+ errors="replace",
662
+ ) as fh:
649
663
  raw_stat = fh.read()
650
664
  # /proc/<pid>/task/<tid>/stat layout (man 5 proc):
651
665
  # pid (comm) state ppid pgrp session tty_nr tpgid flags minflt
@@ -686,6 +700,12 @@ def get_processes() -> Generator[Tuple[int, str], None, None]:
686
700
  ``name`` comes from ``/proc/<pid>/comm`` (the kernel truncates it to 15
687
701
  characters via ``TASK_COMM_LEN``). Processes that vanish mid-scan are
688
702
  skipped silently (logged at DEBUG).
703
+
704
+ ``comm`` is raw bytes a process can set to anything (``prctl(PR_SET_NAME)``),
705
+ so it is decoded as UTF-8 with replacement characters, independent of the
706
+ locale: one process with a non-UTF-8 name used to raise
707
+ ``UnicodeDecodeError`` here and break ``OpenProcess(name=...)`` for every
708
+ other process.
689
709
  """
690
710
  try:
691
711
  entries = os.listdir("/proc")
@@ -699,7 +719,9 @@ def get_processes() -> Generator[Tuple[int, str], None, None]:
699
719
  pid = int(entry)
700
720
 
701
721
  try:
702
- with open("/proc/{}/comm".format(entry), "r") as fh:
722
+ with open(
723
+ "/proc/{}/comm".format(entry), "r", encoding="utf-8", errors="replace"
724
+ ) as fh:
703
725
  name = fh.readline().rstrip("\n")
704
726
  except OSError as exc:
705
727
  # Race (process exited) or permission issue — skip it.
@@ -6,12 +6,15 @@ import warnings
6
6
  from typing import Generator, Optional, Sequence, Tuple, Type, TypeVar, Union
7
7
 
8
8
  from ..enums import ScanTypesEnum
9
+ from ..util.pattern import compile_pattern
9
10
  from ..process import AbstractProcess
11
+ from ..process._guard import guard_generator
10
12
  from ..process.errors import ClosedProcess, ProcessIDNotExistsError
11
13
  from ..util import (
12
14
  UNSET,
13
15
  prepare_write,
14
16
  resolve_bufflength,
17
+ prepare_search_target,
15
18
  resolve_bufflength_for_value,
16
19
  )
17
20
  from ..process.module_info import ModuleInfo
@@ -181,15 +184,15 @@ class LinuxProcess(AbstractProcess):
181
184
 
182
185
  def get_memory_regions(self) -> Generator[MemoryRegion, None, None]:
183
186
  self.__require_target()
184
- return get_memory_regions(self.pid)
187
+ return guard_generator(get_memory_regions(self.pid), self.__require_open)
185
188
 
186
189
  def get_threads(self) -> Generator[ThreadInfo, None, None]:
187
190
  self.__require_target()
188
- return get_threads(self.pid)
191
+ return guard_generator(get_threads(self.pid), self.__require_open)
189
192
 
190
193
  def get_modules(self) -> Generator[ModuleInfo, None, None]:
191
194
  self.__require_target()
192
- return get_modules(self.pid)
195
+ return guard_generator(get_modules(self.pid), self.__require_open)
193
196
 
194
197
  def read_process_memory(
195
198
  self,
@@ -218,7 +221,7 @@ class LinuxProcess(AbstractProcess):
218
221
  self.__require_target()
219
222
  if addresses is UNSET:
220
223
  raise TypeError("addresses is required.")
221
- return search_values_by_addresses(
224
+ scan = search_values_by_addresses(
222
225
  self.pid,
223
226
  pytype,
224
227
  resolve_bufflength(pytype, bufflength),
@@ -226,6 +229,7 @@ class LinuxProcess(AbstractProcess):
226
229
  memory_regions=memory_regions,
227
230
  raise_error=raise_error,
228
231
  )
232
+ return guard_generator(scan, self.__require_open)
229
233
 
230
234
  def search_by_value(
231
235
  self,
@@ -246,10 +250,14 @@ class LinuxProcess(AbstractProcess):
246
250
  "Use the method search_by_value_between(...) to search within a range of values."
247
251
  )
248
252
 
249
- return search_addresses_by_value(
253
+ width = resolve_bufflength_for_value(pytype, bufflength, value)
254
+ # Validate now: the scan is a generator, so its own checks would
255
+ # only raise on the first next(), far from this call.
256
+ prepare_search_target(pytype, width, value)
257
+ scan = search_addresses_by_value(
250
258
  self.pid,
251
259
  pytype,
252
- resolve_bufflength_for_value(pytype, bufflength, value),
260
+ width,
253
261
  value,
254
262
  scan_type,
255
263
  progress_information,
@@ -257,6 +265,7 @@ class LinuxProcess(AbstractProcess):
257
265
  memory_regions=memory_regions,
258
266
  aligned=aligned,
259
267
  )
268
+ return guard_generator(scan, self.__require_open)
260
269
 
261
270
  def search_by_pattern(
262
271
  self,
@@ -267,13 +276,17 @@ class LinuxProcess(AbstractProcess):
267
276
  memory_regions: Optional[Sequence[MemoryRegion]] = None,
268
277
  ) -> Generator[Union[int, Tuple[int, dict]], None, None]:
269
278
  self.__require_target()
270
- return search_addresses_by_pattern(
279
+ # Compile now so a malformed pattern raises at the call, not on the
280
+ # first next() of the returned generator.
281
+ compile_pattern(pattern, byte_length=byte_length)
282
+ scan = search_addresses_by_pattern(
271
283
  self.pid,
272
284
  pattern,
273
285
  byte_length=byte_length,
274
286
  progress_information=progress_information,
275
287
  memory_regions=memory_regions,
276
288
  )
289
+ return guard_generator(scan, self.__require_open)
277
290
 
278
291
  def search_by_value_between(
279
292
  self,
@@ -295,10 +308,14 @@ class LinuxProcess(AbstractProcess):
295
308
  if not_between
296
309
  else ScanTypesEnum.VALUE_BETWEEN
297
310
  )
298
- return search_addresses_by_value(
311
+ width = resolve_bufflength_for_value(pytype, bufflength, start, end)
312
+ # Validate now: the scan is a generator, so its own checks would
313
+ # only raise on the first next(), far from this call.
314
+ prepare_search_target(pytype, width, (start, end))
315
+ scan = search_addresses_by_value(
299
316
  self.pid,
300
317
  pytype,
301
- resolve_bufflength_for_value(pytype, bufflength, start, end),
318
+ width,
302
319
  (start, end),
303
320
  scan_type,
304
321
  progress_information,
@@ -306,6 +323,7 @@ class LinuxProcess(AbstractProcess):
306
323
  memory_regions=memory_regions,
307
324
  aligned=aligned,
308
325
  )
326
+ return guard_generator(scan, self.__require_open)
309
327
 
310
328
  def write_process_memory(
311
329
  self,