| #!/usr/bin/env python3 | |
| """RESTRICTED sources (no redistribution of code): Devign (C-UDA), CleanVul (no license), ReposVul (no license). | |
| Only content hashes, MinHash bands, IDs and labels are kept (StageWriter drops `code` when publishable_code=False). | |
| devign: target -> is_vulnerable; label_source=manual_commit_review (0.80 / 0.75) | |
| cleanvul: func_before -> vulnerable, func_after -> fix; label_source=llm_heuristic, confidence from the CleanVul score | |
| (4:0.80 3:0.70 2:0.45 1:0.30 0:0.15); the score is also used as a confidence signal for overlapping public rows | |
| reposvul: function_before[target=1] -> vulnerable, function_after of changed functions -> fix, target=0 -> unchanged; | |
| label_source=reposvul_untangled (LLM + static-analysis untangling), 0.70 if llm_check&static_check else 0.60""" | |
| import sys, os, json, csv, glob | |
| sys.path.insert(0, os.path.dirname(__file__)); import codecommon as C | |
| import pyarrow.parquet as pq | |
| R = f"{C.ROOT}/raw/code"; csv.field_size_limit(1 << 31) | |
| L_DEV = "RESTRICTED: Devign/CodeXGLUE C-UDA (IDs/hashes/labels only)"; L_CV = "RESTRICTED: CleanVul has no license (IDs/hashes/labels only)" | |
| L_RV = "RESTRICTED: ReposVul has no license (IDs/hashes/labels only)" | |
| SCORE_CONF = {4: 0.80, 3: 0.70, 2: 0.45, 1: 0.30, 0: 0.15} | |
| def devign(wi, path): | |
| w = C.StageWriter("devign", worker=wi, clear=False); split = os.path.basename(path).split("-")[0] | |
| for r in pq.read_table(path).to_pylist(): | |
| vul = bool(r["target"]) | |
| w.add(language="c_cpp", language_detail="c", code=r["func"], project=r["project"], commit_sha=r["commit_id"], is_vulnerable=vul, | |
| pair_role="vuln" if vul else None, label_source="manual_commit_review", | |
| label_confidence=C.confidence("manual_commit_review", "vuln" if vul else None), | |
| origin={"dataset": "devign_codexglue", "original_id": f"{split}:{r['id']}", "original_label": str(int(vul))}, | |
| source_dataset="devign", source_license=L_DEV, publishable_code=False, source_split=split) | |
| return w.close() | |
| def cleanvul(wi, path): | |
| w = C.StageWriter("cleanvul", worker=wi, clear=False); score = int(path.rsplit("_", 1)[1].split(".")[0]) | |
| with open(path, newline="", encoding="utf-8", errors="replace") as f: | |
| for i, r in enumerate(csv.DictReader(f)): | |
| lang = C.lang_from_path(r.get("file_name") or ("x." + (r.get("extension") or ""))) | |
| url = r.get("commit_url") or ""; repo, sha = (url.split("/commit/") + [None])[:2] if "/commit/" in url else (None, None) | |
| pid = C.sha256(f"cleanvul:{score}:{i}")[:32] | |
| common = dict(language=lang[0], language_detail=lang[1], project=C.project_from_repo(repo), repo_url=repo, commit_sha=sha, | |
| commit_date=(r.get("date") or None), file_path=r.get("file_name"), cve_ids=C.cves(r.get("cve_id")), | |
| cwe_ids=C.cwes(r.get("cwe_id")), label_source="llm_heuristic", pair_id=pid, source_dataset="cleanvul", | |
| source_license=L_CV, publishable_code=False, extra=json.dumps({"cleanvul_score": score, "is_test": r.get("is_test")})) | |
| c = SCORE_CONF[score] - (0.2 if str(r.get("is_test")).lower() in ("true", "1") else 0) | |
| w.add(code=r.get("func_before"), is_vulnerable=True, pair_role="vuln", label_confidence=round(max(c, 0.05), 3), | |
| origin={"dataset": "cleanvul", "original_id": f"score{score}:{i}:before", "original_label": f"score={score}"}, **common) | |
| w.add(code=r.get("func_after"), is_vulnerable=False, pair_role="fix", label_confidence=round(max(c - 0.05, 0.05), 3), | |
| origin={"dataset": "cleanvul", "original_id": f"score{score}:{i}:after", "original_label": f"score={score}"}, **common) | |
| return w.close() | |
| def reposvul(wi, args): | |
| start, lines = args | |
| w = C.StageWriter("reposvul", worker=wi, clear=False) | |
| for j, line in enumerate(lines): | |
| r = json.loads(line); repo = (r.get("html_url") or "").split("/commit/")[0] or None | |
| for fi, d in enumerate(r.get("details") or []): | |
| lang = C.lang_from_path(d.get("file_name") or ""); lg = lang[0] if lang[0] != "other" else C.lang_from_name(d.get("file_language")) | |
| good = bool(d.get("llm_check")) and bool(d.get("static_check")) | |
| before_t = {fb.get("function"): fb.get("target") for fb in (d.get("function_before") or [])} | |
| common = dict(language=lg, language_detail=lang[1], project=r.get("project"), repo_url=repo, commit_sha=r.get("commit_id"), | |
| commit_date=r.get("commit_date"), file_path=d.get("file_name"), cve_ids=C.cves(r.get("cve_id")), | |
| cwe_ids=C.cwes(r.get("cwe_id")), label_source="reposvul_untangled", source_dataset="reposvul", | |
| source_license=L_RV, publishable_code=False) | |
| for k, fb in enumerate(d.get("function_before") or []): | |
| vul = str(fb.get("target")) == "1" | |
| w.add(code=fb.get("function"), is_vulnerable=vul, pair_role="vuln" if vul else "unrelated_in_commit", | |
| label_confidence=(0.70 if good else 0.60) if vul else 0.50, | |
| origin={"dataset": "reposvul", "original_id": f"{r.get('index')}:{fi}:before:{k}", "original_label": str(fb.get("target"))}, **common) | |
| for k, fa in enumerate(d.get("function_after") or []): | |
| if str(fa.get("target")) == "1" or fa.get("function") not in before_t: | |
| w.add(code=fa.get("function"), is_vulnerable=False, pair_role="fix", label_confidence=0.60 if good else 0.55, | |
| origin={"dataset": "reposvul", "original_id": f"{r.get('index')}:{fi}:after:{k}", "original_label": str(fa.get("target"))}, **common) | |
| return w.close() | |
| if __name__ == "__main__": | |
| which = sys.argv[1:] or ["devign", "cleanvul", "reposvul"] | |
| if "devign" in which: | |
| C.run_pool("devign", sorted(glob.glob(f"{R}/devign/hf-69bd48c/data/*.parquet")), devign) | |
| if "cleanvul" in which: | |
| C.run_pool("cleanvul", sorted(glob.glob(f"{R}/cleanvul/hf-74b2fda/vulnerability_score_*.csv")), cleanvul) | |
| if "reposvul" in which: | |
| def chunks(): | |
| buf, start = [], 0 | |
| with open(f"{R}/reposvul/2024/ReposVul_all.jsonl") as f: | |
| for i, l in enumerate(f): | |
| buf.append(l) | |
| if len(buf) == 100: yield (start, buf); buf, start = [], i + 1 | |
| if buf: yield (start, buf) | |
| import multiprocessing as mp | |
| C.clear_stage("reposvul") | |
| with mp.Pool(6) as p: print("[reposvul] total rows", sum(p.starmap(reposvul, enumerate(chunks()), chunksize=1))) | |
Xet Storage Details
- Size:
- 6.68 kB
- Xet hash:
- ee310bd28813094a08e6f00a6d0ea3a26cc915a7fc98e0c1c8d8b0541cc2bb37
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.