Skip to main content

il - inline assembly in Python 3

Examples

  1. Decorator API - simple
import il
import ctypes

@il.asm
def add_ints(rdi=ctypes.c_int32, rsi=ctypes.c_int32):
    """
    # return sum of two 32-bit integers
    # 64-bit Linux/MacOS call convention
    #
    .intel_syntax noprefix
    mov rax, 0
    mov eax, edi
    add eax, esi
    ret
    """
    return ctypes.c_int32

print(add_ints(43, -1))
  1. Function API - powerful
add_ints = il.def_asm(
     name="add_ints",
     prototype=ctypes.CFUNCTYPE(ctypes.c_int32,  # return value (eax)
                                ctypes.c_int32,  # 1st param (edi)
                                ctypes.c_int32), # 2nd param (esi)
     code="""
     .intel_syntax noprefix
     mov rax, 0
     mov eax, edi
     add eax, esi
     ret
     """)

print(add_ints(43, -1))

Dependencies

  • If object code is available: no dependencies outside Python standard library. ctypes from the standard library is needed for loading and running object code.

  • If object code is not available: as and objcopy (from binutils) are required for compiling assembly.

Install

$ sudo python3 setup.py install

Library API documentation and call conventions

$ python3 -c 'import il; help(il)'

How it works

  • Assume that mylib.py contains inlined assembly. By default, il looks for object code from mylib.py.il. If found, that code will be executed when inlined functions are called.

  • If object code is not found, il uses binutils: as (assembler) and objcopy to compile the assembly on-the-fly and extract object code from the result. Object code is saved to mylib.py.il for later use.

  • Note: il does not link object code before running it.

  • You can view contents of mylib.py.il using il:

    $ python3 -c 'import il; print(il.dump_lib("mylib.py.il", disasm=False))'
    

    (Use disasm=True to disassemble the code in the dump. Requires objdump.)

Debugging inlined assembly

  1. Import the library, print the pid of the Python process and the address of the function that you want to debug:

    >>> import mylib
    >>> import os
    >>> os.getpid()
    12345
    >>> print(mylib.myfunc.il_addr)
    21954560
    
  2. Attach GDB to the Python process, set a breakpoint to the address and let the Python process continue.

    $ gdb -p 12345
    (gdb) layout asm
    (gdb) break *21954560
    (gdb) cont
    
  3. Call the function in Python

    >>> mylib.myfunc()
    
  4. Now you can step assembly instructions and see register values in GDB:

    (gdb) ni
    (gdb) info registers
    

Metadata

Release files for il 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for il 0.3
File Size Uploaded Provenance Actions
il-0.3.tar.gz 7.3 kB Without provenance Details for il-0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for il 0.3
File Interpreter ABI Platform Actions
il-0.3-py3-none-any.whl Python 3 none any Details for il-0.3-py3-none-any.whl

Total release size: 15.5 kB

Release files / il-0.3.tar.gz

Download URL il-0.3.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2d8a1b2777d2b5ca8635766b6e0155420ec4bf3ac9f24c5ab8947936b269629e
BLAKE2b-256 checksum
How to use checksums
8298984ab586c7f77285512bdd907e00508817b85ac9a06c791d221b7d9a15c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

Release files / il-0.3-py3-none-any.whl

Download URL il-0.3-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eaf1a4dca9810bb9b203dd8ecf4235a52dc14d54780176b1395e2d76171689f2
BLAKE2b-256 checksum
How to use checksums
a17cc0b8ea34aa852b3173185b1440a4031722c1cb1e2002bb908a8076d498dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6

Security for il 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page