mirror of
https://github.com/morluto/rea.git
synced 2026-11-10 16:57:19 +00:00
* refactor(analysis): centralize resource budgets and recovery facts * feat(analysis): expose ASAR and Ghidra runtime admission controls * feat(artifacts): expose reader admission and preserve limited recognition * feat(javascript): expose recoverable semantic precision budgets * feat(web): add recoverable source-map budgets and compact retained views * feat(web): make historical capture decoding budgets recoverable * fix(desktop): enforce selected view policies across capture and export * feat(runtime): make Frida and LLDB retention budgets selectable * feat(native): expose diagnostic budgets and preserve exhaustion causes * feat(managed): expose recoverable snapshot and representation budgets * feat(metadata): make Go and Rust inspection budgets recoverable * feat(wasm): expose representation and producer admissions * feat(evm): expose per-call worker resource budgets * feat(android): make resource and string-pool omissions recoverable * feat(producers): expose command and recorded-capture budgets * docs(analysis): record recovery contracts and integrated validation * fix(docs): link calibration oracle from published verification pages * fix(docs): restore desktop task anchors and receipt links * test(analysis): align fixtures with selectable budget contracts * fix(analysis): remove source-location projector dependency cycle * test(analysis): refresh budget recovery and verifier regressions * test(analysis): preserve format and publication recovery expectations * test(windows): align conformance with owned runtime admission * fix(desktop): preserve selected alias kinds in exported state * test(analysis): align provider fixtures with recoverable budgets * test(desktop): validate emitted alias metadata structurally * fix(ghidra): classify bounded Windows endpoint refusals * test(cli): assert selectable artifact admission recovery * fix(cli): preserve capture provenance in environment builds
63 lines
3.5 KiB
Python
63 lines
3.5 KiB
Python
"""Shared bounded reply/limit protocol, independent of the supplied artifact format."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import resource
|
|
import sys
|
|
|
|
def lower_resource_limits(policy):
|
|
limits = {}
|
|
for name, kind, maximum in (
|
|
("address_space_bytes", resource.RLIMIT_AS, policy["address_space_bytes"]),
|
|
("cpu_seconds", resource.RLIMIT_CPU, policy["cpu_seconds"]),
|
|
("file_size_bytes", resource.RLIMIT_FSIZE, policy["file_bytes"]),
|
|
):
|
|
soft, hard = resource.getrlimit(kind)
|
|
finite = [maximum] + [value for value in (soft, hard) if value != resource.RLIM_INFINITY]
|
|
effective = min(finite)
|
|
# Preserve the inherited hard boundary and never raise a caller's soft limit.
|
|
resource.setrlimit(kind, (effective, hard))
|
|
limits[name] = effective
|
|
return limits
|
|
|
|
|
|
def main(request_path, inspect, failure_type, profile):
|
|
request = json.loads(Path(request_path).read_text(encoding="utf-8"))
|
|
policy = request["producer_limits"]
|
|
for key in ("input_bytes", "output_bytes", "diagnostic_bytes", "address_space_bytes", "file_bytes", "cpu_seconds"):
|
|
value = policy[key]
|
|
if type(value) is not int or not 1 <= value <= 9007199254740991:
|
|
raise ValueError("producer_limits." + key + " must be a positive safe integer")
|
|
os.environ["PWNLIB_NOTERM"] = "1"
|
|
os.environ["PWNLIB_CACHE_DIR"] = str(Path(request_path).parent / "cache")
|
|
limits = None
|
|
# Release bounded emergency headroom before constructing a MemoryError reply.
|
|
# The outer exit-status contract also covers allocation failure during serialization.
|
|
memory_reserve = bytearray(1024 * 1024)
|
|
try:
|
|
limits = lower_resource_limits(policy)
|
|
descriptor = os.open(Path(request_path).parent / "limits.json", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "w", encoding="utf-8") as handle:
|
|
json.dump(limits, handle)
|
|
value = inspect(Path(request["snapshot_path"]), Path(request_path).parent / "cache")
|
|
value["limitations"].append("Effective owned Python resource soft limits: " + json.dumps(limits, sort_keys=True) + ". Inherited tighter limits are retained.")
|
|
reply = {"ok": True, "profile": profile, "value": value}
|
|
except failure_type as error:
|
|
reply = {"ok": False, "reason": error.reason, "message": str(error)}
|
|
except MemoryError:
|
|
memory_reserve = None
|
|
reply = {"ok": False, "reason": "resource-limit", "message": "pwntools memory allocation failed under the effective resource limits; the exact allocation cause is unknown.", "reported_limits": limits}
|
|
except Exception as error:
|
|
if isinstance(error, OSError) and error.errno == 12:
|
|
memory_reserve = None
|
|
reply = {"ok": False, "reason": "resource-limit", "message": "pwntools reported OSError(ENOMEM); the exact allocation cause is unknown.", "reported_limits": limits}
|
|
else:
|
|
reply = {"ok": False, "reason": "decoder", "message": type(error).__name__ + ": " + str(error)}
|
|
encoded = json.dumps(reply, ensure_ascii=True, allow_nan=False).encode("utf-8")
|
|
if len(encoded) > policy["output_bytes"]:
|
|
encoded = json.dumps({"ok": False, "reason": "output-limit", "message": "Complete artifact result exceeds producer_limits.output_bytes=" + str(policy["output_bytes"]) + "."}).encode("utf-8")
|
|
descriptor = os.open(request["reply_path"], os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "wb") as handle:
|
|
handle.write(encoded)
|
|
|