Skip to content

Instantly share code, notes, and snippets.

@appcypher
Created January 10, 2019 14:52
Show Gist options
  • Save appcypher/155c5d276db443cb0d19a44c5b4ccfb9 to your computer and use it in GitHub Desktop.
Save appcypher/155c5d276db443cb0d19a44c5b4ccfb9 to your computer and use it in GitHub Desktop.
Speculative Example in Python Using Ctypes
from ctypes import cdll, c_int, c_uint, c_void_p, CFUNCTYPE
# Load library and get library
lib = cdll.LoadLibrary('./libwasmer.so')
# Get compile function
compile = lib.compile
compile.restype = c_void_p # vmctx_addr
# NOTE: Ideally we will pass import objects
# Compile and get vmctx
vmctx = compile()
# Get get_func_addr function
get_func_addr = lib.get_func_addr
get_func_addr.argtypes = c_uint # (func_index)
get_func_addr.restype = c_void_p # func_addr
# Get exported function, add at index 2.
add_func_addr = get_func_addr(2)
add_func = CFUNCTYPE(c_int, c_uint, c_uint, c_void_p)(add_func_addr) # c_uint, c_uint, c_void_p -> c_int
# Call exported function.
result = add_func(10, 25, vmctx)
print(f'add(10, 25) = {result}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment