đ Virtual Compiler Is All You Need For Assembly Code Search
This repository provides models and corresponding evaluation datasets for the ACL 2024 paper "Virtual Compiler Is All You Need For Assembly Code Search", aiming to revolutionize assembly code search through a virtual compiler.
đ Quick Start
This repo contains the models and the corresponding evaluation datasets of ACL 2024 paper "Virtual Compiler Is All You Need For Assembly Code Search".
A virtual compiler is a LLM that is capable of compiling any programming language into underlying assembly code. The virtual compiler model is available at elsagranger/VirtualCompiler, based on 34B CodeLlama.
We evaluate the similarity of the virtual assembly code generated by the virtual compiler and the real assembly code using force execution by script force-exec.py, the corresponding evaluation dataset is available at virtual_assembly_and_ground_truth.
We evaluate the effectiveness of the virtual compiler through a downstream task -- assembly code search, the evaluation dataset is available at elsagranger/AssemblyCodeSearchEval.
⨠Features
- LLM-based Compilation: The virtual compiler, powered by a 34B CodeLlama-based LLM, can compile any programming language into assembly code.
- Similarity Evaluation: Use a script to evaluate the similarity between virtual and real assembly code, with corresponding datasets provided.
- Downstream Task Evaluation: Evaluate the effectiveness of the virtual compiler through assembly code search, with a dedicated evaluation dataset.
đĻ Installation
This project uses FastChat and vllm worker to host the model. Run the following commands in separate terminals, such as tmux
.
LOGDIR="" python3 -m fastchat.serve.openai_api_server \
--host 0.0.0.0 --port 8080 \
--controller-address http://localhost:21000
LOGDIR="" python3 -m fastchat.serve.controller \
--host 0.0.0.0 --port 21000
LOGDIR="" RAY_LOG_TO_STDERR=1 \
python3 -m fastchat.serve.vllm_worker \
--model-path ./VirtualCompiler \
--num-gpus 8 \
--controller http://localhost:21000 \
--max-num-batched-tokens 40960 \
--disable-log-requests \
--host 0.0.0.0 --port 22000 \
--worker-address http://localhost:22000 \
--model-names "VirtualCompiler"
đģ Usage Examples
Basic Usage
After hosting the model, use do_request.py
to make requests to the model.
~/C/VirtualCompiler (main)> python3 do_request.py
test rdx, rdx
setz al
movzx eax, al
neg eax
retn
Advanced Usage
Here is an example of using the assembly code search encoder.
def calc_map_at_k(logits, pos_cnt, ks=[10,]):
_, indices = torch.sort(logits, dim=1, descending=True)
ranks = torch.nonzero(
indices < pos_cnt,
as_tuple=False
)[:, 1].reshape(logits.shape[0], -1)
mrr = torch.mean(1 / (ranks + 1), dim=1)
res = {}
for k in ks:
res[k] = (
torch.sum((ranks < k).float(), dim=1) / min(k, pos_cnt)
).cpu().numpy()
return ranks.cpu().numpy(), res, mrr.cpu().numpy()
pos_asm_cnt = 1
query = ["List all files in a directory"]
anchor_asm = [ {"1": "endbr64", "2": "mov eax, 0" }, ... ]
neg_anchor_asm = [ {"1": "push rbp", "2": "mov rbp, rsp", ... }, ... ]
query_embs = text_encoder(**text_tokenizer(query))
kwargs = dict(padding=True, pad_to_multiple_of=8, return_tensors="pt")
anchor_asm_ids = asm_tokenizer.pad([asm_tokenizer(pos) for pos in anchor_asm], **kwargs)
neg_anchor_asm_ids = asm_tokenizer.pad([asm_tokenizer(neg) for neg in neg_anchor_asm], **kwargs)
asm_embs = asm_encoder(**anchor_asm_ids)
asm_neg_emb = asm_encoder(**neg_anchor_asm_ids)
logits_pos = torch.einsum(
"ic,jc->ij", [query_embs, asm_embs])
logits_neg = torch.einsum(
"ic,jc->ij", [query_embs, asm_neg_emb[pos_asm_cnt:]]
)
logits = torch.cat([logits_pos, logits_neg], dim=1)
ranks, map_at_k, mrr = calc_map_at_k(
logits, pos_asm_cnt, [1, 5, 10, 20, 50, 100])
đ Documentation
As huggingface does not support loading a remote model inside a folder, we host the model trained on the assembly code search dataset augmented by the Virtual Compiler in vic-encoder. You can use the model.py
to test the custom model loading.
Here is an example on text encoder and asm encoder. Please refer to this script on how to extract the assembly code from the binary: process_asm.py.
đ License
This project is licensed under the Apache-2.0 license.