def getFFI(options=None): if options is None: options = {} if platform.system() == "Darwin": options["sysroot"] = subprocess.check_output( ["xcrun", "--show-sdk-path"]).strip() return pydffi.FFI(**options)
def test_recursive_struct(self): FFI = pydffi.FFI() CU = FFI.cdef(''' typedef struct { unsigned char a; int b; int c; short d; } A; typedef struct _Node Node; struct _Node { A v; struct _Node* next; }; ''') A = CU.types.A Node = CU.types.Node A0 = A(a=0, b=1, c=10, d=20) A1 = A(a=1, b=2, c=-10, d=-20) N1 = Node(v=A1, next=pydffi.ptr(Node)()) N0 = Node(v=A0, next=pydffi.ptr(N1)) for T in self.generated_types(Node, "_Node"): V = self.purectypes.unpack(T, bytes(pydffi.view_as_bytes(N0))) for attr in ("a", "b", "c", "d"): self.assertEqual(getattr(V.v, attr), getattr(N0.v, attr)) self.assertEqual(V.next, int(N0.next))
def test_structsformat(self): FFI = pydffi.FFI() CU = FFI.cdef(''' #include <stdbool.h> typedef struct { bool a; unsigned int b; unsigned short c; } A; typedef struct { unsigned char buf[9]; unsigned short v0; A a; unsigned short v1; } B; ''') vA = CU.types.A(a=1,b=0xAAAAAAAA,c=0x4444) buf = pydffi.view_as_bytes(vA) vAup = struct.unpack(CU.types.A.format, buf) self.assertEqual(vAup, (1,0xAAAAAAAA,0x4444)) buf_ref = bytearray(b"012345678") vB = CU.types.B(v0=1,v1=2,a=vA,buf=pydffi.view_as(CU.types.B.buf.type, buf_ref)) buf = pydffi.view_as_bytes(vB) vBup = struct.unpack(CU.types.B.format, buf) self.assertEqual(bytearray(vBup[:9]), buf_ref) self.assertEqual(vBup[9:], (1,1,0xAAAAAAAA,0x4444,2))
def test_recursive_union(self): FFI = pydffi.FFI() CU = FFI.cdef(''' typedef struct { unsigned char a; int b; int c; short d; } A; typedef union _Node Node; union _Node { A v; Node* next; }; ''') A = CU.types.A Node = CU.types.Node A0 = A(a=0, b=1, c=10, d=20) A1 = A(a=1, b=2, c=-10, d=-20) N1 = Node() N1.next = pydffi.ptr(Node)() N0 = Node() N0.next = pydffi.ptr(N1) for T in self.generated_types(Node, "_Node"): V = self.purectypes.unpack(T, bytes(pydffi.view_as_bytes(N0))) self.assertEqual(V.next, int(N0.next))
def test_it(self): pydffi.dlopen(_HERE + '/library.so') FFI = pydffi.FFI() CU = FFI.cdef('#include "{}/library.h"'.format(_HERE)) foo = CU.types.Foo(bar=1) CU.funcs.by_ptr(FFI.ptr(foo)) self.assertEqual(foo.bar, 2);
def get_CU(): J = pydffi.FFI() return J.compile(''' struct A { short a; int b; }; short get_a(struct A a) { return a.a; }; int get_b(struct A a) { return a.b; }; ''')
def get_CU(self): FFI = pydffi.FFI() return FFI.compile(''' struct A { short a; int b; }; short get_a(struct A a) { return a.a; }; int get_b(struct A a) { return a.b; }; ''')
def get_A(): J = pydffi.FFI() CU = J.cdef(''' struct A { short a; int b; }; // TODO: force declaration of struct A... short __foo(struct A a) { return a.a; } ''') SA = CU.getStructType("A") return pydffi.CStructObj(SA)
def get_A(): FFI = pydffi.FFI() CU = FFI.cdef(''' struct A { short a; int b; }; // TODO: force declaration of struct A... short __foo(struct A a) { return a.a; } ''') SA = CU.types.A return pydffi.CStructObj(SA)
def test_union(self): FFI = pydffi.FFI() CU = FFI.cdef(''' #include <stdint.h> typedef union { struct { uint8_t v8[4]; }; uint32_t v32; } IP; ''') Obj = CU.types.IP() Obj.v32 = 0xAABBCCDD for IP in self.generated_types(CU.types.IP, "IP"): V = self.purectypes.unpack(IP, bytes(pydffi.view_as_bytes(Obj))) self.assertEqual(V.v32, 0xAABBCCDD) for i in range(4): self.assertEqual(V.v8[i], Obj.v8[i]) V = self.purectypes.pack(IP, V) V = pydffi.view_as(pydffi.const(CU.types.IP), V) self.assertEqual(V.v32, 0xAABBCCDD)
def test_struct(self): FFI = pydffi.FFI() CU = FFI.cdef(''' typedef struct { unsigned char a; int b; int c; short d; } A; ''') Obj = CU.types.A(a=1, b=2, c=10, d=20) for A in self.generated_types(CU.types.A, "A"): V = self.purectypes.unpack(A, bytes(pydffi.view_as_bytes(Obj))) self.assertEqual(V.a, 1) self.assertEqual(V.b, 2) self.assertEqual(V.c, 10) self.assertEqual(V.d, 20) V = self.purectypes.pack(A, V) V = pydffi.view_as(CU.types.A, V) self.assertEqual(Obj.a, V.a) self.assertEqual(Obj.b, V.b) self.assertEqual(Obj.c, V.c) self.assertEqual(Obj.d, V.d)
def __init__(self, llvm_root): pydffi.dlopen(os.path.join(llvm_root, "lib", "libLLVM.so")) self.FFI=pydffi.FFI(includeDirs=[os.path.join(llvm_root, "include")]) self.CU=self.FFI.cdef(''' #include <llvm-c/Target.h> #include <llvm-c/Disassembler.h> size_t disasm(LLVMDisasmContextRef Disasm, uint8_t const* In, size_t InLen, uint64_t Addr, char* Out, size_t OutLen) { return LLVMDisasmInstruction(Disasm, (uint8_t*)In, InLen, Addr, Out, OutLen); } ''') self.CU.funcs.LLVMInitializeX86Disassembler() self.CU.funcs.LLVMInitializeX86Target() self.CU.funcs.LLVMInitializeX86TargetInfo() self.CU.funcs.LLVMInitializeX86TargetMC() self.llvm_disasm = self.CU.funcs.LLVMCreateDisasm("x86_64-pc-linux-gnu", (self.FFI.VoidPtrTy)(), self.FFI.Int(0), (self.FFI.VoidPtrTy)(), (self.FFI.VoidPtrTy)()) if int(self.llvm_disasm) == 0: raise RuntimeError("unable to create an LLVM disassembler engine") # Set Intel syntax self.CU.funcs.LLVMSetDisasmOptions(self.llvm_disasm, 4) self.LLVMDisasmInstruction = self.CU.funcs.disasm self.tmpbuf = self.FFI.arrayType(self.FFI.CharTy, 256)()
A.b = 5 assert (A.a == 1) assert (A.b == 5) def get_buf_view(): J = pydffi.FFI() buf = bytearray(b"hello") return J, J.view(buf) def run_buf_view(): J, buf = get_buf_view() CU = J.compile(''' #include <stdint.h> void print(uint8_t* msg) { puts(msg); } ''') # CHECK: hello CU.getFunction("print").call(buf) run_CU() run_A() run_buf_view() assert ( pydffi.FFI().compile("int foo(int a, int b) { return a+b; }").funcs.foo( 1, 4) == 5)
def test_memrefs(self): self.run_CU() self.run_A() self.run_buf_view() self.assertEqual(pydffi.FFI().compile("int foo(int a, int b) { return a+b; }").funcs.foo(1,4), 5)
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # RUN: "%python" "%s" "%S/../../../tests/includes" # import pydffi import sys F = pydffi.FFI(includeDirs=[sys.argv[1]]) CU = F.compile(''' #include "add.h" ''') assert(CU.funcs.add(4,5) == 9) try: F = pydffi.FFI() CU = F.compile(''' #include "add.h" ''') except pydffi.CompileError: sys.exit(0) print("Last compilatin should have failed!") sys.exit(1)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # RUN: "%python" "%s" | "%FileCheck" "%s" # import pydffi import sys import struct FFI=pydffi.FFI() CU=FFI.compile(''' #include <stdio.h> #include <string.h> struct A { char buf[128]; }; struct A init() { struct A a; strcpy(a.buf, "hello"); return a; }
def get_buf_view(): FFI = pydffi.FFI() buf = bytearray(b"hello\x00") return FFI, pydffi.view_as(FFI.arrayType(FFI.UInt8Ty, len(buf)), buf)
def get_buf_view(): J = pydffi.FFI() buf = bytearray(b"hello") return J, J.view(buf)
def test_basic(self): FFI = pydffi.FFI() Obj = FFI.UIntTy(10) for T in self.generated_types(FFI.UIntTy, "__UIntTy"): V = self.purectypes.unpack(T, bytes(pydffi.view_as_bytes(Obj))) self.assertEqual(V, 10)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # TODO: figure out why this fails on travis/osx # REQUIRES: !darwin # RUN: "%python" "%s" | "%FileCheck" "%s" # import pydffi D = pydffi.FFI() CU = D.compile(''' enum A { V0 = 0, V10 = 10 }; struct S { enum A a; }; int get(enum A a) { return a; } enum A get_a(struct S s) { return s.a; } ''') A = CU.types.A
def setUp(self): self.FFI = pydffi.FFI()