<a href="https://arxiv.org/abs/2403.18624"><img src="https://img.shields.io/badge/arXiv-2403.18624-b31b1b.svg?style=for-the-badge">
<a href="https://opensource.org/license/mit/"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge">
📜 <a href="#-overview">Overview</a>
| 📚 <a href="#-primevul-dataset">Dataset</a>
| 💻 <a href="#-experiments">Experiments</a>
| 📝 <a href="#-citation">Citation</a>
PrimeVul is a new dataset for vulnerability detection, aiming to train and evaluate code language models in the realistic vulnerability detection settings.
To facilitate the future research and evaluation using PrimeVul, we retrieve more metadata, so that users could customize their training and evaluation (e.g., including more contexts) according to thier needs. Specifically, we have retrieve these key attributes:
How to Use: While commit and vulnerability metadata are directly saved as part of the json object, file-level metadata is saved separately in file_info.json and file_contents/. Using the func_hash in the dataset, the file information can be found in file_info.json, which also provides the path to the local copy of the whole file.
[!NOTE]
PrimeVul is a dataset that combines and reconstructs existing vulnerability detection datasets with more accurate labels and thorough evaluation. However, not all datasets provide sufficient resources to retrieve the metadata (e.g., some samples do not originally have CWE type or CVE numbers). Therefore, we could not retrieve the same metadata for every sample in PrimeVul. In PrimeVul-v0.1, we only include vulnerabilities that we successfully retrieved their metadata. For the full set of samples that we orignally used in the paper, please refer to the original release below.
conda env create -f environment.yml
cd os_expr;
PROJECT="primevul_cls"
TYPE=<MODEL_TYPE>
MODEL=<HUGGINGFACE_MODEL>
TOKENIZER=<HUGGINGFACE_TOKENIZER>
OUTPUT_DIR=../output/
python run_ft.py \
--project ${PROJECT} \
--model_dir ${MODEL} \
--output_dir=${OUTPUT_DIR} \
--model_type=${TYPE} \
--tokenizer_name=${TOKENIZER} \
--model_name_or_path=${MODEL} \
--do_train \
--do_test \
--train_data_file=<PATH_TO_primevul_train.jsonl> \
--eval_data_file=<PATH_TO_primevul_valid.jsonl> \
--test_data_file=<PATH_TO_primevul_test.jsonl> \
--epoch 10 \
--block_size 512 \
--train_batch_size 64 \
--eval_batch_size 128 \
--learning_rate 2e-5 \
--warmup_steps 1000 \
--max_grad_norm 1.0 \
--evaluate_during_training \
--seed 123456
cd ..;
cd os_expr;
WEIGHT=30
PROJECT="primevul_cls_weights_${WEIGHT}"
TYPE=<MODEL_TYPE>
MODEL=<HUGGINGFACE_MODEL>
TOKENIZER=<HUGGINGFACE_TOKENIZER>
OUTPUT_DIR=../output/
python run_ft.py \
--project ${PROJECT} \
--model_dir ${MODEL} \
--output_dir=${OUTPUT_DIR} \
--model_type=${TYPE} \
--tokenizer_name=${TOKENIZER} \
--model_name_or_path=${MODEL} \
--do_train \
--do_test \
--train_data_file=<PATH_TO_primevul_train.jsonl> \
--eval_data_file=<PATH_TO_primevul_valid.jsonl> \
--test_data_file=<PATH_TO_primevul_test.jsonl> \
--epoch 10 \
--block_size 512 \
--train_batch_size 64 \
--eval_batch_size 128 \
--learning_rate 2e-5 \
--class_weight \
--vul_weight $WEIGHT \
--warmup_steps 1000 \
--max_grad_norm 1.0 \
--evaluate_during_training \
--seed 123456
cd ..;
cd os_expr;
PROJECT="primevul_cls_clr"
TYPE=<MODEL_TYPE>
MODEL=<HUGGINGFACE_MODEL>
TOKENIZER=<HUGGINGFACE_TOKENIZER>
OUTPUT_DIR=../output/
python run_ft.py \
--project ${PROJECT} \
--model_dir ${MODEL} \
<--clr_mask> \ # Add this parameter to enable CA-CLR
--clr_temp 1.0 \
--output_dir=${OUTPUT_DIR} \
--model_type=${TYPE} \
--tokenizer_name=${TOKENIZER} \
--model_name_or_path=${MODEL} \
--do_train \
--do_test \
--train_data_file=<PATH_TO_primevul_train.jsonl> \
--eval_data_file=<PATH_TO_primevul_valid.jsonl> \
--test_data_file=<PATH_TO_primevul_test.jsonl> \
--epoch 10 \
--block_size 512 \
--group_size 32 \
--train_batch_size 1 \
--eval_batch_size 1 \
--learning_rate 2e-5 \
--warmup_steps 1000 \
--max_grad_norm 1.0 \
--evaluate_during_training \
--seed 123456
cd ..;
For 7B models, since it requires more expensive computation, we implement the training differently fromm < 7B models
accelerate config and choose the configuration based on your need. The configuration we used is shown in os_expr/default_config.yaml.PROJECT="parallel_primevul_cls"
TYPE=<MODEL_TYPE>
MODEL=<HUGGINGFACE_MODEL>
TOKENIZER=<HUGGINGFACE_TOKENIZER>
OUTPUT_DIR=../output/
accelerate launch run_with_accelerator.py \
--project ${PROJECT} \
--model_dir ${MODEL} \
--output_dir=${OUTPUT_DIR} \
--model_type=${TYPE} \
--tokenizer_name=${TOKENIZER} \
--model_name_or_path=${MODEL} \
--do_train \
--do_test \
--train_data_file=<PATH_TO_primevul_train.jsonl> \
--eval_data_file=<PATH_TO_primevul_valid.jsonl> \
--test_data_file=<PATH_TO_primevul_test.jsonl> \
--gradient_accumulation_steps 4 \
--epoch 10 \
--block_size 512 \
--train_batch_size 8 \
--eval_batch_size 16 \
--learning_rate 2e-5 \
--warmup_steps 1000 \
--max_grad_norm 1.0 \
--evaluate_during_training \
--seed 123456
cd openai_expr;
MODEL=gpt-3.5-turbo-0125 # gpt-4-0125-preview
PROMPT_STRATEGY="std_cls";
python run_prompting.py \
--model $MODEL \
--prompt_strategy $PROMPT_STRATEGY \
--data_path <PATH_TO_primevul_test_paired.jsonl> \
--output_folder ../output_dir \
--temperature 0.0 \
--max_gen_length 1024 \
--seed 1337 \
--logprobs \
--fewshot_eg
cd ..;
cd openai_expr;
MODEL=gpt-3.5-turbo-0125 # gpt-4-0125-preview
PROMPT_STRATEGY=cot;
python run_prompting.py \
--model $MODEL \
--prompt_strategy $PROMPT_STRATEGY \
--data_path <PATH_TO_primevul_test_paired.jsonl> \
--output_folder ../output_dir \
--temperature 0.0 \
--max_gen_length 1024 \
--seed 1337
cd ..;
python calc_vd_score.py \
--pred_file <OUTPUT_DIR>/predictions.txt
--test_file <PATH_TO_primevul_test.jsonl>
@article{ding2024primevul,
title={Vulnerability Detection with Code Language Models: How Far Are We?},
author={Yangruibo Ding and Yanjun Fu and Omniyyah Ibrahim and Chawin Sitawarin and Xinyun Chen and Basel Alomair and David Wagner and Baishakhi Ray and Yizheng Chen},
journal={arXiv preprint arXiv:2403.18624},
year={2024}
}
$ claude mcp add PrimeVul \
-- python -m otcore.mcp_server <graph>