Turning a Function Pointer to Callable

Well, the memory address of a compiled function is obtained but how to turn it to Python callable that can be used as an extension. The answer to this the use of ctypes module that can create a Python callable and can wrap arbitrary memory address.

The code below shows how to obtain the raw, low-level address of a C function and how to turn it back into a callable object.

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import ctypes
lib = ctypes.cdll.LoadLibrary(None)
  
# Get the address of sin() from the C math library
addr = ctypes.cast(lib.sin, ctypes.c_void_p).value
print ("addr : ", addr)

chevron_right


Output :

addr : 140735505915760

 
Code #2 : Turn the address into a callable function

filter_none

edit
close

play_arrow

link
brightness_4
code

functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
func = functype(addr)
print ("Function : ", func)

chevron_right


Output :

Function : <CFunctionType object at 0x1006816d0>

 
Code #3 : Call the resulting function

filter_none

edit
close

play_arrow

link
brightness_4
code

print ("func(2) : ", func(2))
  
print ("func(0) : ", func(0))

chevron_right


Output :

func(2) : 0.9092974268256817

func(0) : 0.0

A CFUNCTYPE instance has to be created first to make a callable. The first argument to CFUNCTYPE() is the return type. Next arguments are the types of arguments. After defining the function type, it is wrapped around an integer memory address to create a callable object. The resulting object is used like any normal function accessed through ctypes.
It is becoming increasingly common for programs and libraries to utilize advanced code generation techniques like just-in-time compilation, as found in libraries such as LLVM (LLVM itself is not an acronym; it is the full name of the project.)

The code below uses the llvmpy extension to make an assembly function, obtain a function pointer to it, and turn it into a Python callable.

Code #4 :

filter_none

edit
close

play_arrow

link
brightness_4
code

from llvm.core import Module, Function, Type, Builder
  
mod = Module.new('example')
f = Function.new(mod, Type.function(
                  Type.double(), [Type.double(), Type.double()], False), 'foo')
  
block = f.append_basic_block('entry')
builder = Builder.new(block)
  
x2 = builder.fmul(f.args[0], f.args[0])
y2 = builder.fmul(f.args[1], f.args[1])
  
r = builder.fadd(x2, y2)
builder.ret(r)

chevron_right


Output :

<llvm.core.Instruction object at 0x10078e990>

 
Code #5 :

filter_none

edit
close

play_arrow

link
brightness_4
code

from llvm.ee import ExecutionEngine
  
engine = ExecutionEngine.new(mod)
ptr = engine.get_pointer_to_function(f)
ptr

chevron_right


Output :

4325863440

Code #6 : Call the resulting function

filter_none

edit
close

play_arrow

link
brightness_4
code

foo = ctypes.CFUNCTYPE(ctypes.c_double, 
                       ctypes.c_double, 
                       ctypes.c_double)(ptr)
  
print (foo(2, 3))
  
print ("\n", foo(4, 5))
  
print ("\n", foo(1, 2))

chevron_right


Output :

13.0

41.0

5.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.