hacnho commited on
Commit
2b56111
·
verified ·
1 Parent(s): ec8c92b

Upload reproduce.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. reproduce.py +80 -0
reproduce.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import argparse
5
+ import json
6
+ import time
7
+ from pathlib import Path
8
+
9
+ from google.protobuf import descriptor_pb2
10
+
11
+
12
+ ROOT = Path(__file__).resolve().parent
13
+ ARTIFACTS = ROOT / "artifacts"
14
+
15
+
16
+ def vmhwm_kb() -> int | None:
17
+ with open("/proc/self/status", "r", encoding="utf-8", errors="ignore") as fh:
18
+ for line in fh:
19
+ if line.startswith("VmHWM:"):
20
+ return int(line.split()[1])
21
+ return None
22
+
23
+
24
+ def parse_one(path: Path) -> dict:
25
+ start = time.time()
26
+ before = vmhwm_kb()
27
+ msg = descriptor_pb2.FileDescriptorProto()
28
+ msg.ParseFromString(path.read_bytes())
29
+ after = vmhwm_kb()
30
+ first_dep_len = len(msg.dependency[0]) if msg.dependency else 0
31
+ return {
32
+ "path": str(path),
33
+ "elapsed": time.time() - start,
34
+ "vmhwm_before_kb": before,
35
+ "vmhwm_after_kb": after,
36
+ "dependency_len": len(msg.dependency),
37
+ "option_dependency_len": len(msg.option_dependency),
38
+ "first_dependency_len": first_dep_len,
39
+ }
40
+
41
+
42
+ def main() -> int:
43
+ parser = argparse.ArgumentParser()
44
+ parser.add_argument("--control", type=Path, default=ARTIFACTS / "control_one_dependency.pb")
45
+ parser.add_argument(
46
+ "--malicious",
47
+ type=Path,
48
+ default=ARTIFACTS / "malicious_option_dependency_5000000.pb",
49
+ )
50
+ parser.add_argument("--json-out", type=Path)
51
+ args = parser.parse_args()
52
+
53
+ control = parse_one(args.control)
54
+ malicious = parse_one(args.malicious)
55
+ summary = {
56
+ "format": "Protocol Buffers",
57
+ "protobuf_version": __import__("google.protobuf").protobuf.__version__,
58
+ "entrypoint": "descriptor_pb2.FileDescriptorProto().ParseFromString(...)",
59
+ "control": control,
60
+ "malicious": malicious,
61
+ "delta": {
62
+ "elapsed_ratio": malicious["elapsed"] / max(control["elapsed"], 1e-9),
63
+ "vmhwm_delta_kb": malicious["vmhwm_after_kb"] - control["vmhwm_after_kb"],
64
+ "option_dependencies_added": malicious["option_dependency_len"] - control["option_dependency_len"],
65
+ "repro_ok": (
66
+ control["dependency_len"] == 1
67
+ and control["option_dependency_len"] == 0
68
+ and malicious["option_dependency_len"] == 5_000_000
69
+ and malicious["vmhwm_after_kb"] - control["vmhwm_after_kb"] >= 150_000
70
+ ),
71
+ },
72
+ }
73
+ if args.json_out:
74
+ args.json_out.write_text(json.dumps(summary, indent=2) + "\n")
75
+ print(json.dumps(summary, indent=2))
76
+ return 0
77
+
78
+
79
+ if __name__ == "__main__":
80
+ raise SystemExit(main())