| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 2 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 3 |
from rpython.rlib.rdynload import DLLHANDLE, dlsym, dlclose |
| 4 |
|
| 5 |
from pypy.interpreter.error import oefmt |
| 6 |
|
| 7 |
from pypy.module._cffi_backend.parse_c_type import ( |
| 8 |
_CFFI_OPCODE_T, GLOBAL_S, CDL_INTCONST_S, STRUCT_UNION_S, FIELD_S, |
| 9 |
ENUM_S, TYPENAME_S, ll_set_cdl_realize_global_int) |
| 10 |
from pypy.module._cffi_backend.realize_c_type import getop |
| 11 |
from pypy.module._cffi_backend.lib_obj import W_LibObject |
| 12 |
from pypy.module._cffi_backend import cffi_opcode, cffi1_module, misc |
| 13 |
|
| 14 |
class W_DlOpenLibObject(W_LibObject): |
| 15 |
|
| 16 |
def __init__(self, ffi, w_filename, flags): |
| 17 |
space = ffi.space |
| 18 |
fname, handle, autoclose = misc.dlopen_w(space, w_filename, flags) |
| 19 |
W_LibObject.__init__(self, ffi, fname) |
| 20 |
self.libhandle = handle |
| 21 |
if autoclose: |
| 22 |
self.register_finalizer(space) |
| 23 |
|
| 24 |
def _finalize_(self): |
| 25 |
h = self.libhandle |
| 26 |
if h != rffi.cast(DLLHANDLE, 0): |
| 27 |
self.libhandle = rffi.cast(DLLHANDLE, 0) |
| 28 |
dlclose(h) |
| 29 |
|
| 30 |
def cdlopen_fetch(self, name): |
| 31 |
if not self.libhandle: |
| 32 |
raise oefmt(self.ffi.w_FFIError, "library '%s' has been closed", |
| 33 |
self.libname) |
| 34 |
try: |
| 35 |
cdata = dlsym(self.libhandle, name) |
| 36 |
found = bool(cdata) |
| 37 |
except KeyError: |
| 38 |
found = False |
| 39 |
if not found: |
| 40 |
raise oefmt(self.ffi.w_FFIError, |
| 41 |
"symbol '%s' not found in library '%s'", |
| 42 |
name, self.libname) |
| 43 |
return rffi.cast(rffi.CCHARP, cdata) |
| 44 |
|
| 45 |
def cdlopen_close(self): |
| 46 |
libhandle = self.libhandle |
| 47 |
self.libhandle = rffi.cast(DLLHANDLE, 0) |
| 48 |
|
| 49 |
if not libhandle: |
| 50 |
return |
| 51 |
self.may_unregister_rpython_finalizer(self.ffi.space) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
self.dict_w.clear() |
| 59 |
|
| 60 |
if dlclose(libhandle) < 0: |
| 61 |
raise oefmt(self.ffi.w_FFIError, "error closing library '%s'", |
| 62 |
self.libname) |
| 63 |
|
| 64 |
|
| 65 |
class StringDecoder: |
| 66 |
def __init__(self, ffi, string): |
| 67 |
self.ffi = ffi |
| 68 |
self.string = string |
| 69 |
self.pos = 0 |
| 70 |
|
| 71 |
def next_4bytes(self): |
| 72 |
pos = self.pos |
| 73 |
src = ord(self.string[pos]) |
| 74 |
if src >= 0x80: |
| 75 |
src -= 0x100 |
| 76 |
src = ((src << 24) | |
| 77 |
(ord(self.string[pos + 1]) << 16) | |
| 78 |
(ord(self.string[pos + 2]) << 8 ) | |
| 79 |
(ord(self.string[pos + 3]) )) |
| 80 |
self.pos = pos + 4 |
| 81 |
return src |
| 82 |
|
| 83 |
def next_opcode(self): |
| 84 |
return rffi.cast(_CFFI_OPCODE_T, self.next_4bytes()) |
| 85 |
|
| 86 |
def next_name(self): |
| 87 |
frm = self.pos |
| 88 |
i = self.string.find('\x00', frm) |
| 89 |
if i < 0: |
| 90 |
i = len(self.string) |
| 91 |
self.pos = i + 1 |
| 92 |
p = rffi.str2charp(self.string[frm : i]) |
| 93 |
self.ffi._finalizer.free_mems.append(p) |
| 94 |
return rffi.cast(rffi.CONST_CCHARP, p) |
| 95 |
|
| 96 |
|
| 97 |
def allocate(ffi, nbytes): |
| 98 |
nbytes = llmemory.raw_malloc_usage(nbytes) |
| 99 |
if not we_are_translated(): |
| 100 |
nbytes *= 2 |
| 101 |
|
| 102 |
p = lltype.malloc(rffi.CCHARP.TO, nbytes, flavor='raw', zero=True) |
| 103 |
ffi._finalizer.free_mems.append(p) |
| 104 |
return p |
| 105 |
|
| 106 |
@specialize.arg(1) |
| 107 |
def allocate_array(ffi, OF, nitems): |
| 108 |
nbytes = llmemory.raw_malloc_usage(rffi.sizeof(OF)) |
| 109 |
p = allocate(ffi, nitems * nbytes) |
| 110 |
return rffi.cast(rffi.CArrayPtr(OF), p) |
| 111 |
|
| 112 |
|
| 113 |
def ffiobj_init(ffi, module_name, version, types, w_globals, |
| 114 |
w_struct_unions, w_enums, w_typenames, w_includes): |
| 115 |
space = ffi.space |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
rffi.cast(lltype.Signed, ffi.ctxobj) |
| 120 |
|
| 121 |
if version == -1 and not types: |
| 122 |
return |
| 123 |
if not (cffi1_module.VERSION_MIN <= version <= cffi1_module.VERSION_MAX): |
| 124 |
raise oefmt(space.w_ImportError, |
| 125 |
"cffi out-of-line Python module '%s' has unknown version %s", |
| 126 |
module_name, hex(version)) |
| 127 |
|
| 128 |
if types: |
| 129 |
|
| 130 |
n = len(types) // 4 |
| 131 |
ntypes = allocate_array(ffi, _CFFI_OPCODE_T, n) |
| 132 |
decoder = StringDecoder(ffi, types) |
| 133 |
for i in range(n): |
| 134 |
ntypes[i] = decoder.next_opcode() |
| 135 |
ffi.ctxobj.ctx.c_types = ntypes |
| 136 |
rffi.setintfield(ffi.ctxobj.ctx, 'c_num_types', n) |
| 137 |
ffi.cached_types = [None] * n |
| 138 |
|
| 139 |
if w_globals is not None: |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
globals_w = space.fixedview(w_globals) |
| 144 |
n = len(globals_w) // 2 |
| 145 |
size = n * rffi.sizeof(GLOBAL_S) + n * llmemory.raw_malloc_usage(rffi.sizeof(CDL_INTCONST_S)) |
| 146 |
p = allocate(ffi, size) |
| 147 |
nglobs = rffi.cast(rffi.CArrayPtr(GLOBAL_S), p) |
| 148 |
p = rffi.ptradd(p, n * rffi.sizeof(GLOBAL_S)) |
| 149 |
nintconsts = rffi.cast(rffi.CArrayPtr(CDL_INTCONST_S), p) |
| 150 |
for i in range(n): |
| 151 |
decoder = StringDecoder(ffi, space.bytes_w(globals_w[i * 2])) |
| 152 |
nglobs[i].c_type_op = decoder.next_opcode() |
| 153 |
nglobs[i].c_name = decoder.next_name() |
| 154 |
op = getop(nglobs[i].c_type_op) |
| 155 |
if op == cffi_opcode.OP_CONSTANT_INT or op == cffi_opcode.OP_ENUM: |
| 156 |
w_integer = globals_w[i * 2 + 1] |
| 157 |
ll_set_cdl_realize_global_int(nglobs[i]) |
| 158 |
bigint = space.bigint_w(w_integer) |
| 159 |
ullvalue = bigint.ulonglongmask() |
| 160 |
rffi.setintfield(nintconsts[i], 'neg', int(bigint.get_sign() <= 0)) |
| 161 |
rffi.setintfield(nintconsts[i], 'value', ullvalue) |
| 162 |
ffi.ctxobj.ctx.c_globals = rffi.cast(lltype.Ptr(GLOBAL_S), nglobs) |
| 163 |
rffi.setintfield(ffi.ctxobj.ctx, 'c_num_globals', n) |
| 164 |
|
| 165 |
if w_struct_unions is not None: |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
struct_unions_w = space.fixedview(w_struct_unions) |
| 170 |
n = len(struct_unions_w) |
| 171 |
nftot = 0 |
| 172 |
for i in range(n): |
| 173 |
nftot += space.len_w(struct_unions_w[i]) - 1 |
| 174 |
nstructs = allocate_array(ffi, STRUCT_UNION_S, n) |
| 175 |
nfields = allocate_array(ffi, FIELD_S, nftot) |
| 176 |
nf = 0 |
| 177 |
for i in range(n): |
| 178 |
|
| 179 |
desc = space.fixedview(struct_unions_w[i]) |
| 180 |
nf1 = len(desc) - 1 |
| 181 |
decoder = StringDecoder(ffi, space.bytes_w(desc[0])) |
| 182 |
rffi.setintfield(nstructs[i], 'c_type_index', decoder.next_4bytes()) |
| 183 |
flags = decoder.next_4bytes() |
| 184 |
rffi.setintfield(nstructs[i], 'c_flags', flags) |
| 185 |
nstructs[i].c_name = decoder.next_name() |
| 186 |
if flags & (cffi_opcode.F_OPAQUE | cffi_opcode.F_EXTERNAL): |
| 187 |
rffi.setintfield(nstructs[i], 'c_size', -1) |
| 188 |
rffi.setintfield(nstructs[i], 'c_alignment', -1) |
| 189 |
rffi.setintfield(nstructs[i], 'c_first_field_index', -1) |
| 190 |
rffi.setintfield(nstructs[i], 'c_num_fields', 0) |
| 191 |
assert nf1 == 0 |
| 192 |
else: |
| 193 |
rffi.setintfield(nstructs[i], 'c_size', -2) |
| 194 |
rffi.setintfield(nstructs[i], 'c_alignment', -2) |
| 195 |
rffi.setintfield(nstructs[i], 'c_first_field_index', nf) |
| 196 |
rffi.setintfield(nstructs[i], 'c_num_fields', nf1) |
| 197 |
for j in range(nf1): |
| 198 |
decoder = StringDecoder(ffi, space.bytes_w(desc[j + 1])) |
| 199 |
|
| 200 |
|
| 201 |
type_op = decoder.next_opcode() |
| 202 |
nfields[nf].c_field_type_op = type_op |
| 203 |
rffi.setintfield(nfields[nf], 'c_field_offset', -1) |
| 204 |
if getop(type_op) != cffi_opcode.OP_NOOP: |
| 205 |
field_size = decoder.next_4bytes() |
| 206 |
else: |
| 207 |
field_size = -1 |
| 208 |
rffi.setintfield(nfields[nf], 'c_field_size', field_size) |
| 209 |
nfields[nf].c_name = decoder.next_name() |
| 210 |
nf += 1 |
| 211 |
assert nf == nftot |
| 212 |
ffi.ctxobj.ctx.c_struct_unions = rffi.cast(lltype.Ptr(STRUCT_UNION_S), nstructs) |
| 213 |
ffi.ctxobj.ctx.c_fields = rffi.cast(lltype.Ptr(FIELD_S), nfields) |
| 214 |
rffi.setintfield(ffi.ctxobj.ctx, 'c_num_struct_unions', n) |
| 215 |
|
| 216 |
if w_enums: |
| 217 |
|
| 218 |
enums_w = space.fixedview(w_enums) |
| 219 |
n = len(enums_w) |
| 220 |
nenums = allocate_array(ffi, ENUM_S, n) |
| 221 |
for i in range(n): |
| 222 |
decoder = StringDecoder(ffi, space.bytes_w(enums_w[i])) |
| 223 |
rffi.setintfield(nenums[i], 'c_type_index', decoder.next_4bytes()) |
| 224 |
rffi.setintfield(nenums[i], 'c_type_prim', decoder.next_4bytes()) |
| 225 |
nenums[i].c_name = decoder.next_name() |
| 226 |
nenums[i].c_enumerators = decoder.next_name() |
| 227 |
ffi.ctxobj.ctx.c_enums = rffi.cast(lltype.Ptr(ENUM_S), nenums) |
| 228 |
rffi.setintfield(ffi.ctxobj.ctx, 'c_num_enums', n) |
| 229 |
|
| 230 |
if w_typenames: |
| 231 |
|
| 232 |
|
| 233 |
typenames_w = space.fixedview(w_typenames) |
| 234 |
n = len(typenames_w) |
| 235 |
ntypenames = allocate_array(ffi, TYPENAME_S, n) |
| 236 |
for i in range(n): |
| 237 |
decoder = StringDecoder(ffi, space.bytes_w(typenames_w[i])) |
| 238 |
rffi.setintfield(ntypenames[i],'c_type_index',decoder.next_4bytes()) |
| 239 |
ntypenames[i].c_name = decoder.next_name() |
| 240 |
ffi.ctxobj.ctx.c_typenames = rffi.cast(lltype.Ptr(TYPENAME_S), ntypenames) |
| 241 |
rffi.setintfield(ffi.ctxobj.ctx, 'c_num_typenames', n) |
| 242 |
|
| 243 |
if w_includes: |
| 244 |
from pypy.module._cffi_backend.ffi_obj import W_FFIObject |
| 245 |
|
| 246 |
for w_parent_ffi in space.fixedview(w_includes): |
| 247 |
parent_ffi = space.interp_w(W_FFIObject, w_parent_ffi) |
| 248 |
ffi.included_ffis_libs.append((parent_ffi, None)) |