示例#1
0
def signal(n, handler):
    if isinstance(handler, int):
        return signal_i(n, handler)
    import ffi

    cb = ffi.callback("v", handler, "i")
    return signal_p(n, cb)
示例#2
0
def sigaction(signum, handler, flags=0):
    sa = new(sigaction_t)
    sa_old = new(sigaction_t)
    cb = ffi.callback("v", handler, "i", lock=True)
    sa.sa_handler = cb.cfun()
    sa.sa_flags = flags
    r = sigaction_(signum, sa, sa_old)
    if r != 0:
        raise RuntimeError("sigaction_ error: %d (errno = %d)" %
                           (r, os.errno()))
    return cb  # sa_old.sa_handler
示例#3
0
def signal(n, handler):
    if isinstance(handler, int):
        # We don't try to remove callback from _hmap here, as we return old
        # signal handler. If it's based on callback installed earlier, it
        # can be reinstated again via next call to signal(), so we still
        # need to keep the callback around.
        return signal_i(n, handler)
    import ffi
    cb = ffi.callback("v", handler, "i")
    _hmap[n] = cb
    siginterrupt(n, True)
    return signal_p(n, cb)
示例#4
0
def ffi_open(names):
    err = None
    for n in names:
        try:
            mod = ffi.open(n)
            return mod
        except OSError as e:
            err = e
    raise err


libc = ffi_open(("libc.so", "libc.so.0", "libc.so.6", "libc.dylib"))

qsort = libc.func("v", "qsort", "piip")


def cmp(pa, pb):
    a = ffi.as_bytearray(pa, 1)
    b = ffi.as_bytearray(pb, 1)
    # print("cmp:", a, b)
    return a[0] - b[0]


cmp_c = ffi.callback("i", cmp, "pp")

s = bytearray(b"foobar")
print("org string:", s)
qsort(s, len(s), 1, cmp_c)
print("qsort'ed:", s)
示例#5
0
    print("SKIP")
    raise SystemExit


def ffi_open(names):
    err = None
    for n in names:
        try:
            mod = ffi.open(n)
            return mod
        except OSError as e:
            err = e
    raise err

libc = ffi_open(('libc.so', 'libc.so.0', 'libc.so.6', 'libc.dylib'))

qsort = libc.func("v", "qsort", "piip")

def cmp(pa, pb):
    a = ffi.as_bytearray(pa, 1)
    b = ffi.as_bytearray(pb, 1)
    #print("cmp:", a, b)
    return a[0] - b[0]

cmp_c = ffi.callback("i", cmp, "pp")

s = bytearray(b"foobar")
print("org string:", s)
qsort(s, len(s), 1, cmp_c)
print("qsort'ed:", s)
示例#6
0
 def timeit(callback,number=1000000):
     cb = ffi.callback('v',callback,'')
     return _timeit(cb,number)
示例#7
0
target_mach = LLVMCreateTargetMachine(target, def_triple, "", "",
                                      LLVMCodeGenLevelDefault,
                                      LLVMRelocDefault,
                                      LLVMCodeModelJITDefault)

orc = LLVMOrcCreateInstance(target_mach)


def orc_sym_resolver(name, ctx):
    print("orc_sym_resolver")
    addr = by_ref("Q")
    err = LLVMOrcGetSymbolAddress(orc, addr, name)
    return addr[0]


orc_sym_resolver_cb = ffi.callback("Q", orc_sym_resolver, "sp")

shmod = LLVMOrcMakeSharedModule(mod)
#print("mod", mod, "shmod", shmod)

orc_mod_ref = by_ref("P")
LLVMOrcAddLazilyCompiledIR(orc, orc_mod_ref, shmod, orc_sym_resolver_cb, None)

addr_ref = by_ref("Q")
err = LLVMOrcGetSymbolAddress(orc, addr_ref, "sum")
addr = addr_ref[0]
#print(err, addr)

f = ffi.func("i", addr, "ii")
print("result", f(5, 10))
示例#8
0
def signal(n, handler):
    if isinstance(handler, int):
        return signal_i(n, handler)
    import ffi
    cb = ffi.callback("v", handler, "i")
    return signal_p(n, cb)
示例#9
0
time = libc.func("i", "time", "p")
open = libc.func("i", "open", "si")
qsort = libc.func("v", "qsort", "piiC")
# And one variable
errno = libc.var("i", "errno")

print("time:", time)
print("UNIX time is:", time(None))
print()

perror("perror before error")
open("somethingnonexistent__", 0)
print("errno object:", errno)
print("errno value:", errno.get())
perror("perror after error")
print()

def cmp(pa, pb):
    a = uctypes.bytearray_at(pa, 1)
    b = uctypes.bytearray_at(pb, 1)
    print("cmp:", a, b)
    return a[0] - b[0]

cmp_cb = ffi.callback("i", cmp, "PP")
print("callback:", cmp_cb)

s = bytearray(b"foobar")
print("org string:", s)
qsort(s, len(s), 1, cmp_cb)
print("qsort'ed string:", s)
示例#10
0
qsort = libc.func("v", "qsort", "piiC")
# And one variable
errno = libc.var("i", "errno")

print("time:", time)
print("UNIX time is:", time(None))
print()

perror("perror before error")
open("somethingnonexistent__", 0)
print("errno object:", errno)
print("errno value:", errno.get())
perror("perror after error")
print()


def cmp(pa, pb):
    a = uctypes.bytearray_at(pa, 1)
    b = uctypes.bytearray_at(pb, 1)
    print("cmp:", a, b)
    return a[0] - b[0]


cmp_cb = ffi.callback("i", cmp, "PP")
print("callback:", cmp_cb)

s = bytearray(b"foobar")
print("org string:", s)
qsort(s, len(s), 1, cmp_cb)
print("qsort'ed string:", s)