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.

.. note::

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.

.. code-block:: cpp

int factorial_req(int n)
{
    if (n == 1)
    {
        return 1;
    }
    return factorial_req(n - 1) * n;
}

.. code-block:: python

from llvm2py import parse_assembly
from llvm2py.ir import *
from graphviz import Digraph


with open("../test_files/factorial.ll") as file:
    source = file.read()

mod = parse_assembly(source)

g = Digraph()

node_attributes = {"shape": "record", "style": "filled"}


def name_label(label):
    return label.replace("%", "\\%")


def name_block(block):
    name = block.value.val
    return name_label(name)


for block in mod.funcs["factorial_req"].blocks.values():
    color = "#f59c7d70"  # Default block color
    last_instruction = block.instrs[-1]

    match last_instruction:
        case Br(None, Value(label)):
            g.edge(name_block(block), name_label(label))
        case Br(_, Value(label_false), Value(label_true)):
            g.edge(name_block(block), name_label(label_true), label="True")
            g.edge(name_block(block), name_label(label_false), label="False")

    if len(block.pred_blocks) >= 2:
        color = "#b70d2870"  # merge-type block

    g.node(name_block(block), **node_attributes, color=color)

g.save("cfg.dot")

.. |PyPI| image:: https://img.shields.io/pypi/v/llvm2py.svg :target: https://pypi.python.org/pypi/llvm2py

.. |Actions status| image:: https://github.com/Papr1ka/llvm2py/actions/workflows/main.yml/badge.svg?branch=main :target: https://github.com/Papr1ka/llvm2py/actions/workflows/main.yml

.. |Release| image:: https://img.shields.io/github/v/release/Papr1ka/llvm2py.svg?label=release :target: https://github.com/Papr1ka/llvm2py/releases

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.2.0b0.tar.gz (36.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

llvm2py-0.2.0b0-cp313-cp313-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.13Windows x86-64

llvm2py-0.2.0b0-cp313-cp313-manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

llvm2py-0.2.0b0-cp312-cp312-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.12Windows x86-64

llvm2py-0.2.0b0-cp312-cp312-manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

llvm2py-0.2.0b0-cp311-cp311-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.11Windows x86-64

llvm2py-0.2.0b0-cp311-cp311-manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

llvm2py-0.2.0b0-cp310-cp310-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.10Windows x86-64

llvm2py-0.2.0b0-cp310-cp310-manylinux_2_28_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

File details

Details for the file llvm2py-0.2.0b0.tar.gz.

File metadata

  • Download URL: llvm2py-0.2.0b0.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llvm2py-0.2.0b0.tar.gz
Algorithm Hash digest
SHA256 9ef07f53a664cd66c00b70db0f075b1ed01ecce9c577748cba052384ea07af66
MD5 13a5f79a3b7776d23cf8e25daf00d34a
BLAKE2b-256 d9a8c5113f562bcc6aa68f0bed05a303f18cf8f03779cdbb8901c21247d4a0ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0.tar.gz:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.2.0b0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llvm2py-0.2.0b0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c672220133b33cef60e459268da72b68e15e2f610b9be5911bd95a06f61dd87c
MD5 26ae1d927deee219e74861e8cf59cf3b
BLAKE2b-256 f2382d149b21360968bc48a7226ed2c3b286db8fc9987bd4621cd81e0401654d

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp313-cp313-win_amd64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.2.0b0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ca8595389184251aacefb4505a2019864a5c92197d160feeff75929a944f370d
MD5 74aec38fc2e9a5e643eecab32ba1e027
BLAKE2b-256 07a8d803872bf7b315a96843da7558b98aecec85f8b470dccd594a9059c706fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp313-cp313-manylinux_2_28_x86_64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.2.0b0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llvm2py-0.2.0b0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 504cc87a4b4604742a59537d7ed7907f70c02c77a96785612aaf3a3bcc1222f8
MD5 dff86ec23648e0942c0cd70db394bd33
BLAKE2b-256 21662d3790d7b06c221873626c385ccbc077edb876191c4a8722430246416b52

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp312-cp312-win_amd64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.2.0b0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 45fa6a1ec1d40592c4c3a3d26cc858c627c6583cab170ecdb3207fb1a8c0c659
MD5 8153ac71db6dccec4ad396f36783fe97
BLAKE2b-256 41f1c8bb90dfa9c300cb4339130183854385a48ac5880ee027a538575ece8959

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp312-cp312-manylinux_2_28_x86_64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.2.0b0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llvm2py-0.2.0b0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a42036f37db369261c90b724a262d6526a6e13f982a00a144de744e4f0aa0628
MD5 01152d05373a0d0ac9e7db7ac49b9be2
BLAKE2b-256 5b3c79639725a6c5ae4eddf0f4a9727dc4159ee76ebff440642facb353d61da8

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp311-cp311-win_amd64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.2.0b0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 83a6e17149a62c7019681560e9f6edcb28a449da1c2ea664c7fb9aa411fec607
MD5 ea7f95891bde4ab9fcc96682698ef919
BLAKE2b-256 5d918706770400e6809aafe53d31e5df63554c860c95bf01981e1753149fbd6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp311-cp311-manylinux_2_28_x86_64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: llvm2py-0.2.0b0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llvm2py-0.2.0b0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9a6c9d6bc9ad337fc005abe3b9cdf9bd04d22c37734aed0f0d462c0b5ad6184a
MD5 56abd6ad0046be7d29c9cdbd014547f9
BLAKE2b-256 70b6034759dcaf65d725b847744059bc4776efbf2b7c353deafd8e28cb72947a

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp310-cp310-win_amd64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llvm2py-0.2.0b0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for llvm2py-0.2.0b0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b800f5ce5bbc8fbf015a0df664226b55fef0ee4e7dae7fd59c9b88c1aad75fcb
MD5 7543b903526c79e05a9e7209bbf47a92
BLAKE2b-256 d733330297a918d0f0a5696c8eefb567da9a6ab91369c92b3bcc15f0b454ac95

See more details on using hashes here.

Provenance

The following attestation bundles were made for llvm2py-0.2.0b0-cp310-cp310-manylinux_2_28_x86_64.whl:

Publisher: main.yml on Papr1ka/llvm2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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