Binary code clone search is a core primitive in vulnerability triage, malware lineage, license-compliance auditing and large-scale refactoring. Given a query function extracted from a stripped binary, the goal is to retrieve semantically equivalent or very similar functions from a large repository, even when they have been compiled with different opti- mization levels or toolchains. Traditional static approaches based on control-flow graphs, IR normalization or token sequences work well when structure is preserved, but tend to be fragile under aggressive optimization and do not explicitly learn a continuous repre- sentation of functions. This thesis adopts a representation-learning approach inspired by Asm2Vec and re-implements it in PyTorch as a complete, reproducible pipeline. Starting from compiled x86-64 binaries, the system uses angr to recover functions and control-flow graphs, generates edge-coverage and random-walk sequences with selective callee expan- sion, and applies a custom, normalized tokenization scheme to assembly instructions. A PV-DM-style model is then trained with negative sampling to jointly learn token embed- dings and a dense vector for each function, which is later used for clone search via cosine similarity. The model is evaluated on 26,000 training functions from binutils, coreutils, zip and BusyBox, and on 4,000 unseen functions from findutils, diffutils and ToyBox, across multiple optimization-level pairs (O0, O2, O3, Os). The experiments show high Recall@15 and Mean Reciprocal Rank for pairs among optimized levels (O2/O3/Os), and surprisingly strong performance when inferring embeddings for unseen functions, while O0-related pairs remain the hardest case. The thesis also analyzes the limitations of the hand-crafted tokenization, the restricted same-source ground truth, and the dependence on angr, outlining several directions for improving robustness and generalization.
La ricerca di cloni di codice binario è una primitiva fondamentale per l’analisi di vul- nerabilità, lo studio della lineage del malware, le verifiche di conformità alle licenze e le attività di refactoring su larga scala. Dato in input una funzione di query estratta da un binario privo di simboli, l’obiettivo è recuperare, all’interno di un ampio repository, funzioni semanticamente equivalenti o molto simili, anche quando sono state compilate con livelli di ottimizzazione o toolchain differenti. Gli approcci statici tradizionali basati su grafi di flusso di controllo, normalizzazione a livello di IR o sequenze di token fun- zionano bene quando la struttura del codice è relativamente preservata, ma tendono a essere fragili sotto ottimizzazioni aggressive e non apprendono in modo esplicito una rap- presentazione continua delle funzioni. Questa tesi adotta un approccio di representation learning ispirato ad Asm2Vec e lo reimplementa in PyTorch come pipeline completa e riproducibile. A partire da binari x86-64 compilati, il sistema utilizza angr per ricostruire funzioni e grafi di flusso di controllo, genera sequenze basate su edge-coverage e random walk con selective callee expansion e applica uno schema di tokenizzazione normalizzata alle istruzioni assembly. Un modello in stile PV-DM viene quindi addestrato con nega- tive sampling per apprendere congiuntamente gli embedding dei token e un vettore denso per ciascuna funzione, successivamente utilizzato per la ricerca di cloni tramite similarità coseno. Il modello viene valutato su 26,000 funzioni di training provenienti da binu- tils, coreutils, zip e BusyBox, e su 4,000 funzioni non viste tratte da findutils, diffutils e ToyBox, considerando diverse coppie di livelli di ottimizzazione (O0, O2, O3, Os). Gli es- perimenti mostrano valori elevati di Recall@15 e Mean Reciprocal Rank per le coppie tra livelli ottimizzati (O2/O3/Os) e prestazioni sorprendentemente robuste nella stima degli embedding per funzioni non viste, mentre le coppie che coinvolgono O0 si confermano il caso più difficile. La tesi analizza inoltre i limiti della tokenizzazione progettata a mano, della ground truth ristretta a cloni con la stessa origine sorgente e della dipendenza da angr, delineando diverse direzioni per migliorare robustezza e capacità di generalizzazione.
Learning-based binary clone search: implementing Asm2Vec with angr and PyTorch
Ghidoli, Simone
2024/2025
Abstract
Binary code clone search is a core primitive in vulnerability triage, malware lineage, license-compliance auditing and large-scale refactoring. Given a query function extracted from a stripped binary, the goal is to retrieve semantically equivalent or very similar functions from a large repository, even when they have been compiled with different opti- mization levels or toolchains. Traditional static approaches based on control-flow graphs, IR normalization or token sequences work well when structure is preserved, but tend to be fragile under aggressive optimization and do not explicitly learn a continuous repre- sentation of functions. This thesis adopts a representation-learning approach inspired by Asm2Vec and re-implements it in PyTorch as a complete, reproducible pipeline. Starting from compiled x86-64 binaries, the system uses angr to recover functions and control-flow graphs, generates edge-coverage and random-walk sequences with selective callee expan- sion, and applies a custom, normalized tokenization scheme to assembly instructions. A PV-DM-style model is then trained with negative sampling to jointly learn token embed- dings and a dense vector for each function, which is later used for clone search via cosine similarity. The model is evaluated on 26,000 training functions from binutils, coreutils, zip and BusyBox, and on 4,000 unseen functions from findutils, diffutils and ToyBox, across multiple optimization-level pairs (O0, O2, O3, Os). The experiments show high Recall@15 and Mean Reciprocal Rank for pairs among optimized levels (O2/O3/Os), and surprisingly strong performance when inferring embeddings for unseen functions, while O0-related pairs remain the hardest case. The thesis also analyzes the limitations of the hand-crafted tokenization, the restricted same-source ground truth, and the dependence on angr, outlining several directions for improving robustness and generalization.| File | Dimensione | Formato | |
|---|---|---|---|
|
995042_Simone_Ghidoli_Thesis.pdf
accessibile in internet per tutti
Dimensione
519.42 kB
Formato
Adobe PDF
|
519.42 kB | Adobe PDF | Visualizza/Apri |
I documenti in POLITesi sono protetti da copyright e tutti i diritti sono riservati, salvo diversa indicazione.
https://hdl.handle.net/10589/247817