A library for analyzing LLVM IR in Python
Project description
llvm_python
A fairly large proportion of programs are written in C/C++.
Let's imagine that you need to analyze programs in a given language, to search for vulnerabilities, to detect patterns or for optimization purposes. To solve such a problem, it is necessary to have the code in a slightly more convenient form than the source code of the program - an intermediate representation.
You might come up with the idea of building your own compiler, which is quite difficult, or you might decide to use intermediate representations of GCC or LLVM, but in that case you have to deal with the C/C++ API, which is something you don't want when you have elegant solutions in Python.
llvm_python allows you to analyze C/C++ programs in the LLVM IR representation in Python.
Usage example
The following example will build a control flow graph for the following function factorial.c, factorial.ll.
int factorial_req(int n)
{
if (n == 1)
{
return 1;
}
return factorial_req(n - 1) * n;
}
from llvm_python import ir
from llvm_python import parse_assembly
from graphviz import Digraph
with open("../test_files/factorial.ll") as file:
ll = file.read()
mod: ir.Module = parse_assembly(ll)
g = Digraph()
node_attributes = {
"shape": "record",
"style": "filled"
}
def graph_node(node: ir.Value):
text = str(node).replace("\n", "\l")
return node.name[1:], node.name + ":" + text
for block in mod.get_function("factorial_req").blocks:
color = "#f59c7d70" # Default block color
last_instruction = block.instructions[-1]
if last_instruction.op_code_name == "br":
operands = last_instruction.operands
if len(operands) == 3:
g.edge(block.name[1:], operands[1].name[1:], label="True")
g.edge(block.name[1:], operands[2].name[1:], label="False")
color = "#b70d2870" # switch-type block
else:
g.edge(block.name[1:], operands[0].name[1:])
if len(block.pred_blocks) >= 2:
color = "#b70d2870" # merge-type block
g.node(*graph_node(block), **node_attributes, color=color)
g.save("cfg.dot")
Installation
Preferred way
pip install llvm_python
Supported versions
CPython3.7 - CPython3.12
On Windows 64bit and manylinux x86_64 platforms.
Manual installation
- Dependencies
-
For correct build you need CMake >= 3.27
-
C and C++ compilers.
-
The system must also have libraries
libffi
иlibtinfo
.
If the libraries are not found, you can add them manually, example CMakeLists.txt, lines 12-15.
- Preferred build system Ninja
- Cloning llvm_python
git clone git@github.com:Papr1ka/llvm_python.git
cd llvm_python
- LLVM setup
You must have the static library LLVM version >= 16 installed on the system.
This can be done via the distribution's package manager.
Example:
sudo dnf install llvm
sudo dnf install llvm-devel
Or you can build LLVM manually and specify the path to the cmake folder, line 21
LLVM_DIR = "/usr/lib/llvm-18/cmake" # Path to cmake folder of llvm library
Or you can download compiled libraries
- Deb пакеты https://apt.llvm.org/
- Windows https://packages.msys2.org/package/mingw-w64-x86_64-llvm
- Run setup.py
in the directory with setup.py:
python -m pip install .
Contact
For any questions related to the library, you can contact us by mail
``
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for llvm_python-0.0.1b1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e44af6d48f91c30ff9cb59bd3e9f5ff6dde8cf71c2a17370b28a5e1528ba7768 |
|
MD5 | 242aea8ceee78e05726fdfe0cb812f68 |
|
BLAKE2b-256 | 65a991020dd7d846da88135089be50edaa647eb5958f8162852b30ed773cf176 |
Hashes for llvm_python-0.0.1b1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | faf6da1cb8dc6b85521aeb6a3dc4d73461c3b6837ccb8f20631fd41c814466a1 |
|
MD5 | 3dde1330dc55825f61302c41735172a2 |
|
BLAKE2b-256 | efdb1bb7c8840d7163bfbb8764c54aad35fdac8bd685e03ac61d35bf32d31f96 |
Hashes for llvm_python-0.0.1b1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcbec6184492289f39869ce840d015a09f51576fbadd209e007c12a70f5633ab |
|
MD5 | cc27367124d1957674b191f7652fe8c5 |
|
BLAKE2b-256 | 80641d777a81949831e1e3fe95a060745c8f92219c6655f867748e95f491da69 |
Hashes for llvm_python-0.0.1b1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 923faa064883a08644c4e4f1ec34a21183d02109cbb77f173c5180ef57dd26fa |
|
MD5 | 6573e99463f1cc095b565279722c99e7 |
|
BLAKE2b-256 | 1521840c1e1dafdc66188135fa098419e6945a2d8862f1d2b39450c26eb06b91 |
Hashes for llvm_python-0.0.1b1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 857551873ac2fca0da19565cfbaef10a9d47558e1f360b4410e053254143f911 |
|
MD5 | 5fe155cb9ac24fb7fe56feb3a73bde9d |
|
BLAKE2b-256 | 40833ab43785864e6e1bb6e74eccf702d398f9891dd27570834a5be03e71d6e4 |
Hashes for llvm_python-0.0.1b1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4f5a3f22d30dee481694c5017c8d98c20674b59751e1d919f65f00735001707 |
|
MD5 | 3afb7e4585af4b4e9bae3f0e15678031 |
|
BLAKE2b-256 | 1aba98713db6a489920f834dc201aa571cb4d9751e7fe24f1423f052e7486c23 |
Hashes for llvm_python-0.0.1b1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74370c776e89c92d4389d9b402a8da301b0941d48e7526e66fb1fc371f8db6dd |
|
MD5 | ccb9ff43eaefa19339b854bdcdc690f7 |
|
BLAKE2b-256 | 7d90076b032b1e648d0ed2080bcf3fecc82ec7be5e7e705492afa1c87c7cf504 |
Hashes for llvm_python-0.0.1b1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f0d15c03bcc9eff2fab04e5f457bef80d77dc0e3dcc4763374a89a1d9f1bacf |
|
MD5 | f0e2b6e831e33e64f7106928246f8c67 |
|
BLAKE2b-256 | ee9eeaffeeaf186e212db1de9d5192f301ddcdfb495508cfd8a0e85970da83b0 |
Hashes for llvm_python-0.0.1b1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7a7ef4bc87d158fd6c90a7acc21759c6e04710fb1198d6c0d9b37c0d313fd9b |
|
MD5 | fe8b6c613d2c27170607c27269308760 |
|
BLAKE2b-256 | 3f99697840821dcbf63bc1479a1b901656f60cdc5bd450c69ff20d821970b352 |
Hashes for llvm_python-0.0.1b1-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef95a531ddf60b722f6604cb7c0b3f4660c76d12604a529a1918930065403537 |
|
MD5 | b5143611403a912661d3fc73690cb054 |
|
BLAKE2b-256 | 7c1072e15317cacfa082dd387fffa76b8dc380d2c45a4fadb4b89623204d84c3 |
Hashes for llvm_python-0.0.1b1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe70a34912b2ee1486a67118f287af6f3a32a8add1c52e3dd69ba15781a75f51 |
|
MD5 | e5f12af36b687bf4a6dbe42c9ddf1d08 |
|
BLAKE2b-256 | 190a40b148e4f01e46e9a032328232b3420aff8fc831d7014eeb38aa84348627 |
Hashes for llvm_python-0.0.1b1-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57e3971fc045b681997ec64e629d11c76fecf22f4a631f2a243e4fb8e83f0c03 |
|
MD5 | 1ce56d4fda6cd54ab3c3bef59a8e425f |
|
BLAKE2b-256 | 666715e17ca40114bcc75aeedb393fc23a9fdd93a6e8279444edbad49d76b0e2 |