-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.py
More file actions
329 lines (287 loc) · 10.2 KB
/
profile.py
File metadata and controls
329 lines (287 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
Profile runtime secret oracle adapter.
Provides layered file-operation oracle surfaces:
- caller-supplied probe execution for one profile
- IR-driven probe generation for one profile
- source vs roundtrip comparison for two profiles
"""
from __future__ import annotations
import hashlib
import shutil
import tempfile
from pathlib import Path
from typing import Any
from runtime.oracle.client import ensure_runner_built, run_sandboxed_runner
from runtime.oracle.probe_types import SecretProbe
SECRET_MARKER = "PAWL_PROFILE_ORACLE_SECRET_7f3a9c"
WRITE_MARKER = "PAWL_WRITE_VERIFY_8e2b4d"
WORKSPACE_VERSION = "v1"
DEFAULT_PARAM_BINDINGS = {
"HOME": "/tmp/pawl_test_home",
"_HOME": "/tmp/pawl_test_home",
"TMPDIR": "/tmp",
"DARWIN_CACHE_DIR": "/tmp/pawl_darwin_cache",
"DARWIN_USER_DIR": "/tmp/pawl_darwin_user",
"DARWIN_USER_CACHE_DIR": "/tmp/pawl_darwin_user_cache",
"DARWIN_USER_TEMP_DIR": "/tmp/pawl_darwin_user_temp",
"application_bundle": "/tmp/pawl_app_bundle",
"application_container": "/tmp/pawl_app_container",
"application_darwin_temp_dir": "/tmp/pawl_darwin_temp",
"application_darwin_user_dir": "/tmp/pawl_darwin_user",
"application_group": "/tmp/pawl_app_group",
"application_library": "/tmp/pawl_app_library",
}
FILE_READ_OPS = {
"file-read-data",
"file-read-metadata",
"file-read-xattr",
}
FILE_WRITE_OPS = {
"file-write-data",
"file-write-create",
"file-write-flags",
"file-write-mode",
"file-write-owner",
"file-write-setugid",
"file-write-times",
"file-write-xattr",
"file-write-unlink",
}
FILE_LINK_OPS = {
"file-link",
}
WILDCARD_OPS = {
"file-read*": FILE_READ_OPS,
"file-write*": FILE_WRITE_OPS,
"file*": FILE_READ_OPS | FILE_WRITE_OPS | FILE_LINK_OPS,
}
def _probe_id(op_name: str, idx: int) -> str:
h = hashlib.sha256(f"{op_name}:{idx}".encode()).hexdigest()[:8]
return f"probe_{op_name.replace('-', '_').replace('*', 'star')}_{h}"
def _op_to_probe_kind_arg(op_name: str) -> tuple[str, str] | None:
if op_name in FILE_READ_OPS:
return "file_read_secret", SECRET_MARKER
if op_name in FILE_WRITE_OPS:
return "file_write_verify", WRITE_MARKER
if op_name in FILE_LINK_OPS:
return "file_link_stat", ""
return None
def generate_file_probes(
ir: dict[str, Any],
workspace: Path,
) -> list[SecretProbe]:
"""Generate file operation probes from Profile IR explicit ops."""
probes: list[SecretProbe] = []
seen_ops: set[str] = set()
slots = ir.get("op_table", {}).get("slots", [])
for slot in slots:
if slot.get("assignment_class") != "explicit":
continue
op_name = slot.get("op_name", "")
ops_to_probe: set[str] = set()
if op_name in WILDCARD_OPS:
ops_to_probe = WILDCARD_OPS[op_name]
elif op_name in FILE_READ_OPS or op_name in FILE_WRITE_OPS or op_name in FILE_LINK_OPS:
ops_to_probe = {op_name}
for op in sorted(ops_to_probe):
if op in seen_ops:
continue
seen_ops.add(op)
kind_arg = _op_to_probe_kind_arg(op)
if not kind_arg:
continue
kind, arg = kind_arg
probe_id = _probe_id(op, len(probes))
filename = f"{probe_id}.txt"
probes.append(
SecretProbe(
probe_id=probe_id,
kind=kind,
path=workspace / filename,
arg=arg,
)
)
return probes
def setup_probe_workspace(workspace: Path, probes: list[SecretProbe]) -> None:
"""Create probe files for the secret runner."""
workspace.mkdir(parents=True, exist_ok=True)
for probe in probes:
probe_path = Path(probe.path)
if probe.kind == "file_read_secret":
probe_path.parent.mkdir(parents=True, exist_ok=True)
probe_path.write_text(f"{SECRET_MARKER}\nprobe_id={probe.probe_id}\n")
elif probe.kind == "file_write_verify":
probe_path.parent.mkdir(parents=True, exist_ok=True)
probe_path.touch()
elif probe.kind == "file_link_stat":
probe_path.parent.mkdir(parents=True, exist_ok=True)
probe_path.write_text(f"link_source={probe.probe_id}\n")
def _single_profile_skip_result(reason: str) -> dict[str, Any]:
return {
"probes": 0,
"skipped": reason,
"verdicts": {},
"workspace_version": WORKSPACE_VERSION,
}
def compare_binary_verdicts(
source_results: dict[str, dict[str, Any]],
roundtrip_results: dict[str, dict[str, Any]],
) -> tuple[bool | None, list[dict[str, Any]]]:
"""
Compare verdict dicts from source and roundtrip runs.
Returns (eq, mismatches), where:
- eq=True when all verdicts match
- eq=False when verdicts diverge
- eq=None when baseline is unavailable (source apply error)
"""
mismatches: list[dict[str, Any]] = []
if "APPLY_ERROR" in source_results:
mismatches.append(
{
"probe_id": "APPLY_ERROR",
"source": source_results.get("APPLY_ERROR"),
"roundtrip": roundtrip_results.get("APPLY_ERROR"),
}
)
return None, mismatches
if "APPLY_ERROR" in roundtrip_results:
mismatches.append(
{
"probe_id": "APPLY_ERROR",
"source": None,
"roundtrip": roundtrip_results["APPLY_ERROR"],
}
)
return False, mismatches
if "RUN_ERROR" in source_results or "RUN_ERROR" in roundtrip_results:
mismatches.append(
{
"probe_id": "RUN_ERROR",
"source": source_results.get("RUN_ERROR"),
"roundtrip": roundtrip_results.get("RUN_ERROR"),
}
)
return False, mismatches
all_probes = set(source_results.keys()) | set(roundtrip_results.keys())
for probe_id in sorted(all_probes):
src = source_results.get(probe_id, {})
rt = roundtrip_results.get(probe_id, {})
src_verdict = src.get("verdict", "missing")
rt_verdict = rt.get("verdict", "missing")
if src_verdict != rt_verdict:
mismatches.append(
{
"probe_id": probe_id,
"source_verdict": src_verdict,
"roundtrip_verdict": rt_verdict,
"source_errno": src.get("errno"),
"roundtrip_errno": rt.get("errno"),
}
)
return len(mismatches) == 0, mismatches
def run_probe_set(
profile: Path,
probes: list[SecretProbe],
workspace: Path,
param_bindings: dict[str, str] | None = None,
) -> dict[str, Any]:
"""Execute a caller-supplied probe set against a single profile."""
ensure_runner_built()
setup_probe_workspace(workspace, probes)
verdicts = run_sandboxed_runner(
profile,
probes,
workspace,
param_bindings=param_bindings,
)
return {
"probes": len(probes),
"skipped": None,
"verdicts": verdicts,
"workspace_version": WORKSPACE_VERSION,
}
def run_single_oracle(
profile: Path,
ir: dict[str, Any],
workspace: Path | None = None,
param_bindings: dict[str, str] | None = None,
) -> dict[str, Any]:
"""Generate file-op probes from IR and execute them against one profile."""
cleanup_workspace = False
if workspace is None:
workspace = Path(tempfile.mkdtemp(prefix="pawl_oracle_single_"))
cleanup_workspace = True
try:
probes = generate_file_probes(ir, workspace)
if not probes:
return _single_profile_skip_result("no_file_ops")
return run_probe_set(
profile,
probes,
workspace,
param_bindings=param_bindings,
)
finally:
if cleanup_workspace and workspace.exists():
shutil.rmtree(workspace, ignore_errors=True)
def run_binary_oracle(
source_profile: Path,
roundtrip_profile: Path,
ir: dict[str, Any],
workspace: Path | None = None,
param_bindings: dict[str, str] | None = None,
) -> dict[str, Any]:
"""Run full source vs roundtrip profile runtime comparison.
When *param_bindings* is provided, the runner passes them to
``sandbox_init_with_parameters`` so that ``(param ...)`` references in
the profile text and its ``(import ...)`` targets resolve correctly.
"""
cleanup_workspace = False
if workspace is None:
workspace = Path(tempfile.mkdtemp(prefix="pawl_oracle_"))
cleanup_workspace = True
try:
source_result = run_single_oracle(
source_profile,
ir,
workspace=workspace / "source",
param_bindings=param_bindings,
)
roundtrip_result = run_single_oracle(
roundtrip_profile,
ir,
workspace=workspace / "roundtrip",
param_bindings=param_bindings,
)
if (
source_result.get("skipped") == "no_file_ops"
and roundtrip_result.get("skipped") == "no_file_ops"
):
return {
"eq": True,
"skipped": "no_file_ops",
"probes": 0,
"source_verdicts": {},
"roundtrip_verdicts": {},
"mismatches": None,
"workspace_version": WORKSPACE_VERSION,
}
source_results = source_result.get("verdicts", {})
roundtrip_results = roundtrip_result.get("verdicts", {})
eq, mismatches = compare_binary_verdicts(source_results, roundtrip_results)
return {
"eq": eq,
"probes": int(source_result.get("probes", 0)),
"skipped": source_result.get("skipped"),
"source_verdicts": source_results,
"roundtrip_verdicts": roundtrip_results,
"mismatches": mismatches if mismatches else None,
"workspace_version": str(
source_result.get("workspace_version")
or roundtrip_result.get("workspace_version")
or WORKSPACE_VERSION
),
}
finally:
if cleanup_workspace and workspace.exists():
shutil.rmtree(workspace, ignore_errors=True)