mvd001/cyberset / scripts /code /ingest_primevul.py
mvd001's picture
download
raw
2.67 kB
#!/usr/bin/env python3
"""PrimeVul paper release (MIT; Drive folder 19iLaNDS0z99N8kB_jBRTmDLehwZBolMY) -> staged rows.
train/valid/test splits are kept verbatim in source_split (the test split becomes the protected external benchmark).
target=1 -> vulnerable, label_source=prime_vul, conf 0.90; patched halves from *_paired.jsonl -> pair_role=fix, 0.85;
other target=0 -> unrelated_in_commit, 0.70. The release carries no CVE / date / repo URL: those are linked later via the
hub (fix_commits sha lookup) in build_code_tier.py."""
import sys, os, json
sys.path.insert(0, os.path.dirname(__file__)); import codecommon as C
D = f"{C.ROOT}/raw/code/primevul/paper-release"; LIC = "MIT (PrimeVul, Ding et al. ICSE'25)"
def work(wi, args):
split, lines, pair_of, fix_idx = args
w = C.StageWriter("primevul", worker=wi, clear=False)
for kind, line in lines:
r = json.loads(line); idx = str(r["idx"]); vul = str(r["target"]) == "1"
role = "vuln" if vul else ("fix" if (kind == "paired" or idx in fix_idx) else "unrelated_in_commit")
w.add(language="c_cpp", language_detail="c_or_cpp", code=r["func"], project=r.get("project"), commit_sha=r.get("commit_id"),
is_vulnerable=vul, pair_id=pair_of.get(idx), pair_role=role, cwe_ids=C.cwes(r.get("cwe")),
label_source="prime_vul", label_confidence=C.confidence("prime_vul", role),
origin={"dataset": "primevul_paper", "original_id": f"{split}:{idx}", "original_label": str(r["target"])},
source_dataset="primevul", source_license=LIC, publishable_code=True, source_split=split,
extra=json.dumps({"primevul_hash": r.get("hash"), "big_vul_idx": r.get("big_vul_idx")}))
return w.close()
if __name__ == "__main__":
tasks = []
for split in ("train", "valid", "test"):
P = open(f"{D}/primevul_{split}_paired.jsonl").read().splitlines()
pair_of, fix_idx, paired_rows = {}, set(), []
for i in range(0, len(P) - 1, 2):
a, b = json.loads(P[i]), json.loads(P[i + 1])
pid = C.sha256(f"primevul:{split}:{a['commit_id']}:{a['idx']}")[:32]
pair_of[str(a["idx"])] = pid; pair_of[str(b["idx"])] = pid; fix_idx.add(str(b["idx"]))
paired_rows.append((str(b["idx"]), P[i + 1]))
main = open(f"{D}/primevul_{split}.jsonl").read().splitlines()
main_idx = {str(json.loads(l)["idx"]) for l in main}
lines = [("main", l) for l in main] + [("paired", l) for i, l in paired_rows if i not in main_idx]
for k in range(0, len(lines), 20000): tasks.append((split, lines[k:k + 20000], pair_of, fix_idx))
C.run_pool("primevul", tasks, work)

Xet Storage Details

Size:
2.67 kB
·
Xet hash:
6955fb6ff9697328a2b83d0a95a4ed4480bdf9eb3710a520170c3afe9dd93412

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.