| 1 |
from pypy.interpreter.baseobjspace import W_Root |
| 2 |
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror |
| 3 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 4 |
from pypy.interpreter.typedef import TypeDef |
| 5 |
from pypy.module._rawffi.alt.interp_ffitype import W_FFIType |
| 6 |
|
| 7 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 8 |
|
| 9 |
from rpython.rlib import jit |
| 10 |
from rpython.rlib import libffi |
| 11 |
from rpython.rlib.clibffi import get_libc_name, StackCheckError, LibFFIError |
| 12 |
from rpython.rlib.rdynload import DLOpenError |
| 13 |
from rpython.rlib.rarithmetic import r_uint |
| 14 |
from rpython.rlib.objectmodel import we_are_translated |
| 15 |
from pypy.module._rawffi.alt.type_converter import FromAppLevelConverter, ToAppLevelConverter |
| 16 |
from pypy.module._rawffi.interp_rawffi import got_libffi_error, wrap_dlopenerror |
| 17 |
|
| 18 |
import os |
| 19 |
if os.name == 'nt': |
| 20 |
def _getfunc(space, CDLL, w_name, w_argtypes, w_restype, variadic_args=0): |
| 21 |
argtypes_w, argtypes, w_restype, restype = unpack_argtypes( |
| 22 |
space, w_argtypes, w_restype) |
| 23 |
if space.isinstance_w(w_name, space.w_text): |
| 24 |
|
| 25 |
name = space.text_w(w_name) |
| 26 |
try: |
| 27 |
func = CDLL.cdll.getpointer(name, argtypes, restype, |
| 28 |
flags=CDLL.flags, variadic_args=variadic_args) |
| 29 |
except KeyError: |
| 30 |
raise oefmt(space.w_AttributeError, |
| 31 |
"No symbol %s found in library %s", |
| 32 |
name, CDLL.name) |
| 33 |
except LibFFIError: |
| 34 |
raise got_libffi_error(space) |
| 35 |
|
| 36 |
return W_FuncPtr(func, argtypes_w, w_restype) |
| 37 |
elif space.isinstance_w(w_name, space.w_int): |
| 38 |
ordinal = space.int_w(w_name) |
| 39 |
try: |
| 40 |
func = CDLL.cdll.getpointer_by_ordinal( |
| 41 |
ordinal, argtypes, restype, |
| 42 |
flags=CDLL.flags, variadic_args=variadic_args) |
| 43 |
except KeyError: |
| 44 |
raise oefmt(space.w_AttributeError, |
| 45 |
"No ordinal %d found in library %s", |
| 46 |
ordinal, CDLL.name) |
| 47 |
except LibFFIError: |
| 48 |
raise got_libffi_error(space) |
| 49 |
|
| 50 |
return W_FuncPtr(func, argtypes_w, w_restype) |
| 51 |
else: |
| 52 |
raise oefmt(space.w_TypeError, |
| 53 |
"function name must be a string or integer") |
| 54 |
else: |
| 55 |
@unwrap_spec(name='text', variadic_args=int) |
| 56 |
def _getfunc(space, CDLL, w_name, w_argtypes, w_restype, variadic_args=0): |
| 57 |
name = space.text_w(w_name) |
| 58 |
argtypes_w, argtypes, w_restype, restype = unpack_argtypes( |
| 59 |
space, w_argtypes, w_restype) |
| 60 |
try: |
| 61 |
func = CDLL.cdll.getpointer(name, argtypes, restype, |
| 62 |
flags=CDLL.flags, variadic_args=variadic_args) |
| 63 |
except KeyError: |
| 64 |
raise oefmt(space.w_AttributeError, |
| 65 |
"No symbol %s found in library %s", name, CDLL.name) |
| 66 |
except LibFFIError: |
| 67 |
raise got_libffi_error(space) |
| 68 |
|
| 69 |
return W_FuncPtr(func, argtypes_w, w_restype) |
| 70 |
|
| 71 |
def unwrap_ffitype(space, w_argtype, allow_void=False): |
| 72 |
res = w_argtype.get_ffitype() |
| 73 |
if res is libffi.types.void and not allow_void: |
| 74 |
raise oefmt(space.w_TypeError, "void is not a valid argument type") |
| 75 |
return res |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
class W_FuncPtr(W_Root): |
| 81 |
|
| 82 |
_immutable_fields_ = ['func', 'argtypes_w[*]', 'w_restype'] |
| 83 |
|
| 84 |
def __init__(self, func, argtypes_w, w_restype): |
| 85 |
self.func = func |
| 86 |
self.argtypes_w = argtypes_w |
| 87 |
self.w_restype = w_restype |
| 88 |
self.to_free = [] |
| 89 |
|
| 90 |
@jit.unroll_safe |
| 91 |
def build_argchain(self, space, args_w): |
| 92 |
expected = len(self.argtypes_w) |
| 93 |
given = len(args_w) |
| 94 |
if given != expected: |
| 95 |
arg = 'arguments' |
| 96 |
if len(self.argtypes_w) == 1: |
| 97 |
arg = 'argument' |
| 98 |
raise oefmt(space.w_TypeError, |
| 99 |
'%s() takes exactly %d %s (%d given)', |
| 100 |
self.func.name, expected, arg, given) |
| 101 |
|
| 102 |
argchain = libffi.ArgChain() |
| 103 |
argpusher = PushArgumentConverter(space, argchain, self) |
| 104 |
for i in range(expected): |
| 105 |
w_argtype = self.argtypes_w[i] |
| 106 |
w_arg = args_w[i] |
| 107 |
argpusher.unwrap_and_do(w_argtype, w_arg) |
| 108 |
return argchain |
| 109 |
|
| 110 |
def call(self, space, args_w): |
| 111 |
self = jit.promote(self) |
| 112 |
argchain = self.build_argchain(space, args_w) |
| 113 |
func_caller = CallFunctionConverter(space, self.func, argchain) |
| 114 |
try: |
| 115 |
return func_caller.do_and_wrap(self.w_restype) |
| 116 |
except StackCheckError as e: |
| 117 |
raise OperationError(space.w_ValueError, space.newtext(e.message)) |
| 118 |
|
| 119 |
|
| 120 |
def free_temp_buffers(self, space): |
| 121 |
for buf in self.to_free: |
| 122 |
if not we_are_translated(): |
| 123 |
buf[0] = '\00' |
| 124 |
|
| 125 |
lltype.free(buf, flavor='raw') |
| 126 |
self.to_free = [] |
| 127 |
|
| 128 |
def getaddr(self, space): |
| 129 |
""" |
| 130 |
Return the physical address in memory of the function |
| 131 |
""" |
| 132 |
return space.newint(rffi.cast(rffi.SIGNED, self.func.funcsym)) |
| 133 |
|
| 134 |
|
| 135 |
class PushArgumentConverter(FromAppLevelConverter): |
| 136 |
""" |
| 137 |
A converter used by W_FuncPtr to unwrap the app-level objects into |
| 138 |
low-level types and push them to the argchain. |
| 139 |
""" |
| 140 |
|
| 141 |
def __init__(self, space, argchain, w_func): |
| 142 |
FromAppLevelConverter.__init__(self, space) |
| 143 |
self.argchain = argchain |
| 144 |
self.w_func = w_func |
| 145 |
|
| 146 |
def handle_signed(self, w_ffitype, w_obj, intval): |
| 147 |
self.argchain.arg(intval) |
| 148 |
|
| 149 |
def handle_unsigned(self, w_ffitype, w_obj, uintval): |
| 150 |
self.argchain.arg(uintval) |
| 151 |
|
| 152 |
def handle_pointer(self, w_ffitype, w_obj, intval): |
| 153 |
self.argchain.arg(intval) |
| 154 |
|
| 155 |
def handle_char(self, w_ffitype, w_obj, intval): |
| 156 |
self.argchain.arg(intval) |
| 157 |
|
| 158 |
def handle_unichar(self, w_ffitype, w_obj, intval): |
| 159 |
self.argchain.arg(intval) |
| 160 |
|
| 161 |
def handle_longlong(self, w_ffitype, w_obj, longlongval): |
| 162 |
self.argchain.arg(longlongval) |
| 163 |
|
| 164 |
def handle_char_p(self, w_ffitype, w_obj, strval): |
| 165 |
buf = rffi.str2charp(strval) |
| 166 |
self.w_func.to_free.append(rffi.cast(rffi.VOIDP, buf)) |
| 167 |
addr = rffi.cast(lltype.Unsigned, buf) |
| 168 |
self.argchain.arg(addr) |
| 169 |
|
| 170 |
def handle_unichar_p(self, w_ffitype, w_obj, utf8val, utf8len): |
| 171 |
buf = rffi.utf82wcharp(utf8val, utf8len) |
| 172 |
self.w_func.to_free.append(rffi.cast(rffi.VOIDP, buf)) |
| 173 |
addr = rffi.cast(lltype.Unsigned, buf) |
| 174 |
self.argchain.arg(addr) |
| 175 |
|
| 176 |
def handle_float(self, w_ffitype, w_obj, floatval): |
| 177 |
self.argchain.arg(floatval) |
| 178 |
|
| 179 |
def handle_singlefloat(self, w_ffitype, w_obj, singlefloatval): |
| 180 |
self.argchain.arg(singlefloatval) |
| 181 |
|
| 182 |
def handle_struct(self, w_ffitype, w_structinstance): |
| 183 |
|
| 184 |
ptrval = w_structinstance.rawmem |
| 185 |
self.argchain.arg_raw(ptrval) |
| 186 |
|
| 187 |
def handle_struct_rawffi(self, w_ffitype, w_structinstance): |
| 188 |
|
| 189 |
ptrval = w_structinstance.ll_buffer |
| 190 |
self.argchain.arg_raw(ptrval) |
| 191 |
|
| 192 |
|
| 193 |
class CallFunctionConverter(ToAppLevelConverter): |
| 194 |
""" |
| 195 |
A converter used by W_FuncPtr to call the function, expect the result of |
| 196 |
a correct low-level type and wrap it to the corresponding app-level type |
| 197 |
""" |
| 198 |
|
| 199 |
def __init__(self, space, func, argchain): |
| 200 |
ToAppLevelConverter.__init__(self, space) |
| 201 |
self.func = func |
| 202 |
self.argchain = argchain |
| 203 |
|
| 204 |
def get_longlong(self, w_ffitype): |
| 205 |
return self.func.call(self.argchain, rffi.LONGLONG) |
| 206 |
|
| 207 |
def get_ulonglong(self, w_ffitype): |
| 208 |
return self.func.call(self.argchain, rffi.ULONGLONG) |
| 209 |
|
| 210 |
def get_signed(self, w_ffitype): |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
restype = w_ffitype.get_ffitype() |
| 219 |
call = self.func.call |
| 220 |
if restype is libffi.types.slong: |
| 221 |
|
| 222 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.LONG)) |
| 223 |
elif restype is libffi.types.slonglong: |
| 224 |
assert libffi.IS_WIN64 |
| 225 |
|
| 226 |
x = call(self.argchain, rffi.LONGLONG) |
| 227 |
elif restype is libffi.types.sint: |
| 228 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.INT)) |
| 229 |
elif restype is libffi.types.sshort: |
| 230 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.SHORT)) |
| 231 |
elif restype is libffi.types.schar: |
| 232 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.SIGNEDCHAR)) |
| 233 |
else: |
| 234 |
raise self.error(w_ffitype) |
| 235 |
return x |
| 236 |
|
| 237 |
def get_unsigned(self, w_ffitype): |
| 238 |
return self.func.call(self.argchain, lltype.Unsigned) |
| 239 |
|
| 240 |
def get_unsigned_which_fits_into_a_signed(self, w_ffitype): |
| 241 |
|
| 242 |
restype = w_ffitype.get_ffitype() |
| 243 |
call = self.func.call |
| 244 |
if restype is libffi.types.ulong: |
| 245 |
assert libffi.IS_WIN64 |
| 246 |
|
| 247 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.ULONG)) |
| 248 |
elif restype is libffi.types.uint: |
| 249 |
assert not libffi.IS_32_BIT |
| 250 |
|
| 251 |
|
| 252 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.UINT)) |
| 253 |
elif restype is libffi.types.ushort: |
| 254 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.USHORT)) |
| 255 |
elif restype is libffi.types.uchar: |
| 256 |
x = rffi.cast(rffi.SIGNED, call(self.argchain, rffi.UCHAR)) |
| 257 |
else: |
| 258 |
raise self.error(w_ffitype) |
| 259 |
return x |
| 260 |
|
| 261 |
|
| 262 |
def get_pointer(self, w_ffitype): |
| 263 |
ptrres = self.func.call(self.argchain, rffi.VOIDP) |
| 264 |
return rffi.cast(lltype.Unsigned, ptrres) |
| 265 |
|
| 266 |
def get_char(self, w_ffitype): |
| 267 |
return self.func.call(self.argchain, rffi.UCHAR) |
| 268 |
|
| 269 |
def get_unichar(self, w_ffitype): |
| 270 |
return self.func.call(self.argchain, rffi.WCHAR_T) |
| 271 |
|
| 272 |
def get_float(self, w_ffitype): |
| 273 |
return self.func.call(self.argchain, rffi.DOUBLE) |
| 274 |
|
| 275 |
def get_singlefloat(self, w_ffitype): |
| 276 |
return self.func.call(self.argchain, rffi.FLOAT) |
| 277 |
|
| 278 |
def get_struct(self, w_ffitype, w_structdescr): |
| 279 |
addr = self.func.call(self.argchain, rffi.SIGNED, is_struct=True) |
| 280 |
return w_structdescr.fromaddress(self.space, addr) |
| 281 |
|
| 282 |
def get_struct_rawffi(self, w_ffitype, w_structdescr): |
| 283 |
uintval = self.func.call(self.argchain, lltype.Unsigned, is_struct=True) |
| 284 |
return w_structdescr.fromaddress(self.space, uintval) |
| 285 |
|
| 286 |
def get_void(self, w_ffitype): |
| 287 |
return self.func.call(self.argchain, lltype.Void) |
| 288 |
|
| 289 |
|
| 290 |
def unpack_argtypes(space, w_argtypes, w_restype): |
| 291 |
argtypes_w = [space.interp_w(W_FFIType, w_argtype) |
| 292 |
for w_argtype in space.listview(w_argtypes)] |
| 293 |
argtypes = [unwrap_ffitype(space, w_argtype) for w_argtype in |
| 294 |
argtypes_w] |
| 295 |
w_restype = space.interp_w(W_FFIType, w_restype) |
| 296 |
restype = unwrap_ffitype(space, w_restype, allow_void=True) |
| 297 |
return argtypes_w, argtypes, w_restype, restype |
| 298 |
|
| 299 |
@unwrap_spec(addr=r_uint, name='text', flags=int, variadic_args=int) |
| 300 |
def descr_fromaddr(space, w_cls, addr, name, w_argtypes, |
| 301 |
w_restype, flags=libffi.FUNCFLAG_CDECL, variadic_args=0): |
| 302 |
argtypes_w, argtypes, w_restype, restype = unpack_argtypes(space, |
| 303 |
w_argtypes, |
| 304 |
w_restype) |
| 305 |
addr = rffi.cast(rffi.VOIDP, addr) |
| 306 |
try: |
| 307 |
func = libffi.Func(name, argtypes, restype, addr, flags, |
| 308 |
variadic_args=variadic_args) |
| 309 |
return W_FuncPtr(func, argtypes_w, w_restype) |
| 310 |
except LibFFIError: |
| 311 |
raise got_libffi_error(space) |
| 312 |
|
| 313 |
|
| 314 |
W_FuncPtr.typedef = TypeDef( |
| 315 |
'_rawffi.alt.FuncPtr', |
| 316 |
__call__ = interp2app(W_FuncPtr.call), |
| 317 |
getaddr = interp2app(W_FuncPtr.getaddr), |
| 318 |
free_temp_buffers = interp2app(W_FuncPtr.free_temp_buffers), |
| 319 |
fromaddr = interp2app(descr_fromaddr, as_classmethod=True) |
| 320 |
) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
class W_CDLL(W_Root): |
| 327 |
def __init__(self, space, name, mode, handle): |
| 328 |
self.flags = libffi.FUNCFLAG_CDECL |
| 329 |
self.space = space |
| 330 |
if name is None: |
| 331 |
self.name = "<None>" |
| 332 |
else: |
| 333 |
self.name = name |
| 334 |
try: |
| 335 |
self.cdll = libffi.CDLL(name, mode, handle) |
| 336 |
except DLOpenError as e: |
| 337 |
raise wrap_dlopenerror(space, e, self.name) |
| 338 |
except OSError as e: |
| 339 |
raise wrap_oserror(space, e) |
| 340 |
|
| 341 |
@unwrap_spec(variadic_args=int) |
| 342 |
def getfunc(self, space, w_name, w_argtypes, w_restype, variadic_args=0): |
| 343 |
return _getfunc(space, self, w_name, w_argtypes, w_restype, variadic_args=variadic_args) |
| 344 |
|
| 345 |
@unwrap_spec(name='text') |
| 346 |
def getaddressindll(self, space, name): |
| 347 |
try: |
| 348 |
address_as_uint = rffi.cast(lltype.Unsigned, |
| 349 |
self.cdll.getaddressindll(name)) |
| 350 |
except KeyError: |
| 351 |
raise oefmt(space.w_ValueError, |
| 352 |
"No symbol %s found in library %s", name, self.name) |
| 353 |
return space.newint(address_as_uint) |
| 354 |
|
| 355 |
def getidentifier(self, space): |
| 356 |
return space.newint(self.cdll.getidentifier()) |
| 357 |
|
| 358 |
@unwrap_spec(name='fsencode_or_none', mode=int, handle=int) |
| 359 |
def descr_new_cdll(space, w_type, name, mode=-1, handle=0): |
| 360 |
return W_CDLL(space, name, mode, handle) |
| 361 |
|
| 362 |
|
| 363 |
W_CDLL.typedef = TypeDef( |
| 364 |
'_rawffi.alt.CDLL', |
| 365 |
__new__ = interp2app(descr_new_cdll), |
| 366 |
getfunc = interp2app(W_CDLL.getfunc), |
| 367 |
getaddressindll = interp2app(W_CDLL.getaddressindll), |
| 368 |
__int__ = interp2app(W_CDLL.getidentifier), |
| 369 |
__long__ = interp2app(W_CDLL.getidentifier), |
| 370 |
) |
| 371 |
|
| 372 |
class W_WinDLL(W_CDLL): |
| 373 |
def __init__(self, space, name, mode, handle): |
| 374 |
W_CDLL.__init__(self, space, name, mode, handle) |
| 375 |
self.flags = libffi.FUNCFLAG_STDCALL |
| 376 |
|
| 377 |
@unwrap_spec(name='fsencode_or_none', mode=int, handle=int) |
| 378 |
def descr_new_windll(space, w_type, name, mode=-1, handle=0): |
| 379 |
return W_WinDLL(space, name, mode, handle) |
| 380 |
|
| 381 |
|
| 382 |
W_WinDLL.typedef = TypeDef( |
| 383 |
'_rawffi.alt.WinDLL', |
| 384 |
__new__ = interp2app(descr_new_windll), |
| 385 |
getfunc = interp2app(W_WinDLL.getfunc), |
| 386 |
getaddressindll = interp2app(W_WinDLL.getaddressindll), |
| 387 |
__int__ = interp2app(W_CDLL.getidentifier), |
| 388 |
__long__ = interp2app(W_CDLL.getidentifier), |
| 389 |
) |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
def get_libc(space): |
| 394 |
return W_CDLL(space, get_libc_name(), -1, 0) |