Skip to main content

A library for analyzing LLVM IR in Python

Project description

PyPI Actions status release

llvm2py

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.

llvm2py allows you to analyze C/C++ programs in the LLVM IR representation in Python.

The library is in beta, so in case of problems, feel free to create issues.

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 llvm2py import ir
from llvm2py 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 llvm2py

Supported versions

CPython3.7 - CPython3.12

On Windows 64bit and manylinux x86_64 platforms.

Manual installation

  1. 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
  1. Cloning llvm2py

git clone git@github.com:Papr1ka/llvm2py.git

cd llvm2py

  1. 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

  1. Run setup.py

in the directory with setup.py:

python -m pip install .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llvm2py-0.0.1b2.tar.gz (17.9 kB view details)

Uploaded Source

Built Distributions

llvm2py-0.0.1b2-cp312-cp312-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.12 Windows x86-64

llvm2py-0.0.1b2-cp312-cp312-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

llvm2py-0.0.1b2-cp311-cp311-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.11 Windows x86-64

llvm2py-0.0.1b2-cp311-cp311-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

llvm2py-0.0.1b2-cp310-cp310-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.10 Windows x86-64

llvm2py-0.0.1b2-cp310-cp310-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

llvm2py-0.0.1b2-cp39-cp39-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.9 Windows x86-64

llvm2py-0.0.1b2-cp39-cp39-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

llvm2py-0.0.1b2-cp38-cp38-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.8 Windows x86-64

llvm2py-0.0.1b2-cp38-cp38-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

llvm2py-0.0.1b2-cp37-cp37m-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.7m Windows x86-64

llvm2py-0.0.1b2-cp37-cp37m-manylinux_2_28_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.28+ x86-64

File details

Details for the file llvm2py-0.0.1b2.tar.gz.

File metadata

  • Download URL: llvm2py-0.0.1b2.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for llvm2py-0.0.1b2.tar.gz
Algorithm Hash digest
SHA256 6725e4bdf1c1b47aa751c20e5961badcbd73267f6e72ea8f37427267a4083f0a
MD5 1984b8a74bec3bcaf3b979f25a336a40
BLAKE2b-256 eed9b21e83e2dd7562deae9213e3c4d2c35562b07d172e9b64d93c2bac0f1537

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cc4d1238291aab1cf895aafaf433c17b0ee251e6520d03bd3d2c777ccba5c969
MD5 b4be6b4a449b4ec81e9dc4d33d646f68
BLAKE2b-256 a7ba5f26522471d06ab8090f6dd16faf2d51c92343421bf654de8ff592cda813

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f9ee11da86c63523c2291a463414abf409da3ebaff4f2bd14c4d7ad59189757e
MD5 3427da6a2713a6550e5cccbbb8ef3a5d
BLAKE2b-256 c406a2e92fe352a12f5ecac6afeb50cabfef17fa7c04e6489ccd53d6a3370f9b

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a76b9ea621b569b05c158dff81a5bc315aae3d07eb527bb4d899de5a2bb11e39
MD5 71a9096d818b735f01c49b13f30403be
BLAKE2b-256 09f7d042fb8ea37823f674ea0b5470ca19093e14b0acb2c4abc511353325f046

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9190a5cf2d32218ce0d9bcc6261d694f8b7e45af0b143ce9a788f219a9801809
MD5 86a0e8e57fe2f5fe046478601677960e
BLAKE2b-256 89fb3bb74ed2019551ea0cff902f72308f94a83890e7592e5e6fbecd30c0a6c4

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3cc3cccc0323a60d4618358b2713f3dab5ee7aa00510db48ceb1b3069fe07d48
MD5 535a45728afb7aa95e4e14f0ad36814c
BLAKE2b-256 84079d067c80889893e888d1be13f6cc0324d49af76f962a6051fd329b49b119

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ef8d75e58c7f2e62b4dbe36232d8f83f29554158cc7c55589c96758bdad85e6c
MD5 051220882a852906a961538c60934775
BLAKE2b-256 5be30cefb5b86eb087854b1a8833515a502ad210536e8d0c962585d0b8638bcd

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.0.1b2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for llvm2py-0.0.1b2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2484922034801f45fdf7d5d444baaf86d9b88646d7cfc19176f61c79729945f9
MD5 4f37f3ba2c3fc09f6e532b6656fac68d
BLAKE2b-256 17dd297203d145ee329ec96cde2659e64fec1c68931c1c9c23fe4502c4dd8916

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3072a103bbaa9e1e5ba12cdf2496bd20b51dbbd0047c21194260ce189329f537
MD5 57e5bd97154ca00dae52c20d4a315e1a
BLAKE2b-256 c059c2901d8307580d3b27607bb38d60221bde54f89a207dcc4377f3c28541f1

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.0.1b2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for llvm2py-0.0.1b2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c5f8832a029adde2e324869652455f18ea4e23c8a05a5224184e307c948d7fa6
MD5 ac7712374603810d170708aef3e4bb69
BLAKE2b-256 6179e989b103b2a2c71a8c4fa42d1d9ef928a5c70bdfd772fa7f9b23cb29cfbb

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp38-cp38-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2098a1a3e89e0efa8438c12ab3b19f5fcba8bbeb182003e7a69696e6aa002b6c
MD5 034483afddf11da731e2ad711eeca108
BLAKE2b-256 ce14165fe3629907e2c52241945b3fb0405b02d9692fbb3ffd5604cc7937fff8

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.0.1b2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for llvm2py-0.0.1b2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 4070925db39adc186395e295c5925b0a46d2d24ac701d525da214dba041e7131
MD5 f9f92b5696d0bfa5abd0ba5aaa0d540b
BLAKE2b-256 b4ee339d417c7235482f37f42f223f08ec539417bb354bf7c73506b0e87475e3

See more details on using hashes here.

File details

Details for the file llvm2py-0.0.1b2-cp37-cp37m-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.0.1b2-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e884864651f05771b3a1728453a2c5df8f32ecd8de9151c5b81312a890c68c19
MD5 36230746a5d9d0fc2b82fe1edd726fd6
BLAKE2b-256 9b890bf8227af673b220ad0727e5513aaf0ec53479a9d588998c73c7fdcf96f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page