| 1 |
|
| 2 |
""" Tests of libffi wrapper |
| 3 |
""" |
| 4 |
|
| 5 |
from rpython.translator.c.test.test_genc import compile |
| 6 |
from rpython.translator import cdir |
| 7 |
from rpython.rlib.clibffi import * |
| 8 |
from rpython.rlib.objectmodel import keepalive_until_here |
| 9 |
from rpython.rtyper.lltypesystem.ll2ctypes import ALLOCATED |
| 10 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 11 |
import gc |
| 12 |
import py |
| 13 |
import sys |
| 14 |
import time |
| 15 |
|
| 16 |
def get_libm_name(platform): |
| 17 |
if platform == 'win32': |
| 18 |
return 'msvcrt.dll' |
| 19 |
elif platform == "darwin": |
| 20 |
return 'libm.dylib' |
| 21 |
else: |
| 22 |
return 'libm.so' |
| 23 |
|
| 24 |
class BaseFfiTest(object): |
| 25 |
|
| 26 |
CDLL = None |
| 27 |
|
| 28 |
@classmethod |
| 29 |
def setup_class(cls): |
| 30 |
for name in type_names: |
| 31 |
|
| 32 |
|
| 33 |
ffistruct = globals()[name] |
| 34 |
rffi.cast(rffi.VOIDP, ffistruct) |
| 35 |
|
| 36 |
def setup_method(self, meth): |
| 37 |
ALLOCATED.clear() |
| 38 |
|
| 39 |
def get_libc(self): |
| 40 |
return self.CDLL(get_libc_name()) |
| 41 |
|
| 42 |
def get_libm(self): |
| 43 |
return self.CDLL(get_libm_name(sys.platform)) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class TestCLibffi(BaseFfiTest): |
| 48 |
|
| 49 |
CDLL = CDLL |
| 50 |
|
| 51 |
def test_library_open(self): |
| 52 |
lib = self.get_libc() |
| 53 |
del lib |
| 54 |
gc.collect() |
| 55 |
assert not ALLOCATED |
| 56 |
|
| 57 |
def test_library_get_func(self): |
| 58 |
lib = self.get_libc() |
| 59 |
ptr = lib.getpointer('fopen', [], ffi_type_void) |
| 60 |
py.test.raises(KeyError, lib.getpointer, 'xxxxxxxxxxxxxxx', [], ffi_type_void) |
| 61 |
del ptr |
| 62 |
del lib |
| 63 |
gc.collect() |
| 64 |
assert not ALLOCATED |
| 65 |
|
| 66 |
def test_library_func_call(self): |
| 67 |
lib = self.get_libc() |
| 68 |
ptr = lib.getpointer('rand', [], ffi_type_sint) |
| 69 |
zeroes = 0 |
| 70 |
first = ptr.call(rffi.INT) |
| 71 |
for i in range(100): |
| 72 |
res = ptr.call(rffi.INT) |
| 73 |
if res == first: |
| 74 |
zeroes += 1 |
| 75 |
assert zeroes < 90 |
| 76 |
|
| 77 |
del ptr |
| 78 |
del lib |
| 79 |
gc.collect() |
| 80 |
assert not ALLOCATED |
| 81 |
|
| 82 |
def test_call_args(self): |
| 83 |
libm = self.get_libm() |
| 84 |
pow = libm.getpointer('pow', [ffi_type_double, ffi_type_double], |
| 85 |
ffi_type_double) |
| 86 |
pow.push_arg(2.0) |
| 87 |
pow.push_arg(2.0) |
| 88 |
res = pow.call(rffi.DOUBLE) |
| 89 |
assert res == 4.0 |
| 90 |
pow.push_arg(3.0) |
| 91 |
pow.push_arg(3.0) |
| 92 |
res = pow.call(rffi.DOUBLE) |
| 93 |
assert res == 27.0 |
| 94 |
del pow |
| 95 |
del libm |
| 96 |
gc.collect() |
| 97 |
assert not ALLOCATED |
| 98 |
|
| 99 |
def test_wrong_args(self): |
| 100 |
libc = self.get_libc() |
| 101 |
|
| 102 |
ulong = cast_type_to_ffitype(rffi.ULONG) |
| 103 |
ctime = libc.getpointer('fopen', [ffi_type_pointer], ulong) |
| 104 |
x = lltype.malloc(lltype.GcStruct('xxx')) |
| 105 |
y = lltype.malloc(lltype.GcArray(rffi.LONG), 3) |
| 106 |
z = lltype.malloc(lltype.Array(rffi.LONG), 4, flavor='raw') |
| 107 |
py.test.raises(ValueError, "ctime.push_arg(x)") |
| 108 |
py.test.raises(ValueError, "ctime.push_arg(y)") |
| 109 |
py.test.raises(ValueError, "ctime.push_arg(z)") |
| 110 |
del ctime |
| 111 |
del libc |
| 112 |
gc.collect() |
| 113 |
lltype.free(z, flavor='raw') |
| 114 |
|
| 115 |
|
| 116 |
def test_unichar(self): |
| 117 |
from rpython.rlib.runicode import MAXUNICODE |
| 118 |
wchar = cast_type_to_ffitype(lltype.UniChar) |
| 119 |
if MAXUNICODE > 65535: |
| 120 |
assert wchar is ffi_type_uint32 |
| 121 |
else: |
| 122 |
assert wchar is ffi_type_uint16 |
| 123 |
|
| 124 |
def test_call_time(self): |
| 125 |
libc = self.get_libc() |
| 126 |
|
| 127 |
ulong = cast_type_to_ffitype(rffi.ULONG) |
| 128 |
try: |
| 129 |
ctime = libc.getpointer('time', [ffi_type_pointer], ulong) |
| 130 |
except KeyError: |
| 131 |
|
| 132 |
ctime = libc.getpointer('_time32', [ffi_type_pointer], ulong) |
| 133 |
ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) |
| 134 |
t0 = ctime.call(rffi.LONG) |
| 135 |
time.sleep(2) |
| 136 |
ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) |
| 137 |
t1 = ctime.call(rffi.LONG) |
| 138 |
assert t1 > t0 |
| 139 |
l_t = lltype.malloc(rffi.CArray(rffi.LONG), 1, flavor='raw') |
| 140 |
ctime.push_arg(l_t) |
| 141 |
t1 = ctime.call(rffi.LONG) |
| 142 |
assert l_t[0] == t1 |
| 143 |
lltype.free(l_t, flavor='raw') |
| 144 |
del ctime |
| 145 |
del libc |
| 146 |
gc.collect() |
| 147 |
assert not ALLOCATED |
| 148 |
|
| 149 |
def test_callback(self): |
| 150 |
size_t = cast_type_to_ffitype(rffi.SIZE_T) |
| 151 |
libc = self.get_libc() |
| 152 |
qsort = libc.getpointer('qsort', [ffi_type_pointer, size_t, |
| 153 |
size_t, ffi_type_pointer], |
| 154 |
ffi_type_void) |
| 155 |
|
| 156 |
def callback(ll_args, ll_res, stuff): |
| 157 |
p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0] |
| 158 |
p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0] |
| 159 |
a1 = rffi.cast(rffi.INTP, p_a1)[0] |
| 160 |
a2 = rffi.cast(rffi.INTP, p_a2)[0] |
| 161 |
res = rffi.cast(rffi.SIGNEDP, ll_res) |
| 162 |
|
| 163 |
if a1 > a2: |
| 164 |
res[0] = 1 |
| 165 |
else: |
| 166 |
res[0] = -1 |
| 167 |
|
| 168 |
ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], |
| 169 |
ffi_type_sint, callback) |
| 170 |
|
| 171 |
TP = rffi.CArray(rffi.INT) |
| 172 |
to_sort = lltype.malloc(TP, 4, flavor='raw') |
| 173 |
to_sort[0] = rffi.cast(rffi.INT, 4) |
| 174 |
to_sort[1] = rffi.cast(rffi.INT, 3) |
| 175 |
to_sort[2] = rffi.cast(rffi.INT, 1) |
| 176 |
to_sort[3] = rffi.cast(rffi.INT, 2) |
| 177 |
qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) |
| 178 |
qsort.push_arg(rffi.sizeof(rffi.INT)) |
| 179 |
qsort.push_arg(4) |
| 180 |
qsort.push_arg(ptr.get_closure()) |
| 181 |
qsort.call(lltype.Void) |
| 182 |
assert ([rffi.cast(lltype.Signed, to_sort[i]) for i in range(4)] == |
| 183 |
[1,2,3,4]) |
| 184 |
lltype.free(to_sort, flavor='raw') |
| 185 |
keepalive_until_here(ptr) |
| 186 |
|
| 187 |
|
| 188 |
def test_compile(self): |
| 189 |
import py |
| 190 |
py.test.skip("Segfaulting test, skip") |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
def f(x, y): |
| 195 |
libm = self.get_libm() |
| 196 |
c_pow = libm.getpointer('pow', [ffi_type_double, ffi_type_double], ffi_type_double) |
| 197 |
c_pow.push_arg(x) |
| 198 |
c_pow.push_arg(y) |
| 199 |
res = c_pow.call(rffi.DOUBLE) |
| 200 |
return res |
| 201 |
|
| 202 |
fn = compile(f, [float, float]) |
| 203 |
res = fn(2.0, 4.0) |
| 204 |
assert res == 16.0 |
| 205 |
|
| 206 |
def test_rawfuncptr(self): |
| 207 |
libm = self.get_libm() |
| 208 |
pow = libm.getrawpointer('pow', [ffi_type_double, ffi_type_double], |
| 209 |
ffi_type_double) |
| 210 |
buffer = lltype.malloc(rffi.DOUBLEP.TO, 3, flavor='raw') |
| 211 |
buffer[0] = 2.0 |
| 212 |
buffer[1] = 3.0 |
| 213 |
buffer[2] = 43.5 |
| 214 |
pow.call([rffi.cast(rffi.VOIDP, buffer), |
| 215 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1))], |
| 216 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) |
| 217 |
assert buffer[2] == 8.0 |
| 218 |
lltype.free(buffer, flavor='raw') |
| 219 |
del pow |
| 220 |
del libm |
| 221 |
gc.collect() |
| 222 |
assert not ALLOCATED |
| 223 |
|
| 224 |
def test_make_struct_ffitype_e(self): |
| 225 |
tpe = make_struct_ffitype_e(16, 4, [ffi_type_pointer, ffi_type_uchar]) |
| 226 |
assert tpe.ffistruct.c_type == FFI_TYPE_STRUCT |
| 227 |
assert tpe.ffistruct.c_size == 16 |
| 228 |
assert tpe.ffistruct.c_alignment == 4 |
| 229 |
assert tpe.ffistruct.c_elements[0] == ffi_type_pointer |
| 230 |
assert tpe.ffistruct.c_elements[1] == ffi_type_uchar |
| 231 |
assert not tpe.ffistruct.c_elements[2] |
| 232 |
lltype.free(tpe, flavor='raw') |
| 233 |
|
| 234 |
def test_nested_struct_elements(self): |
| 235 |
tpe2 = make_struct_ffitype_e(16, 4, [ffi_type_pointer, ffi_type_uchar]) |
| 236 |
tp2 = tpe2.ffistruct |
| 237 |
tpe = make_struct_ffitype_e(32, 4, [tp2, ffi_type_schar]) |
| 238 |
assert tpe.ffistruct.c_elements[0] == tp2 |
| 239 |
assert tpe.ffistruct.c_elements[1] == ffi_type_schar |
| 240 |
assert not tpe.ffistruct.c_elements[2] |
| 241 |
lltype.free(tpe, flavor='raw') |
| 242 |
lltype.free(tpe2, flavor='raw') |
| 243 |
|
| 244 |
def test_struct_by_val(self): |
| 245 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 246 |
from rpython.translator.platform import platform |
| 247 |
from rpython.tool.udir import udir |
| 248 |
|
| 249 |
c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") |
| 250 |
c_file.write(py.code.Source(''' |
| 251 |
#include "src/precommondefs.h" |
| 252 |
#include <stdlib.h> |
| 253 |
#include <stdio.h> |
| 254 |
|
| 255 |
struct x_y { |
| 256 |
Signed x; |
| 257 |
Signed y; |
| 258 |
}; |
| 259 |
|
| 260 |
RPY_EXPORTED |
| 261 |
Signed sum_x_y(struct x_y s) { |
| 262 |
return s.x + s.y; |
| 263 |
} |
| 264 |
|
| 265 |
Signed sum_x_y_p(struct x_y *p) { |
| 266 |
return p->x + p->y; |
| 267 |
} |
| 268 |
|
| 269 |
''')) |
| 270 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 271 |
lib_name = str(platform.compile([c_file], eci, 'x1', standalone=False)) |
| 272 |
|
| 273 |
lib = CDLL(lib_name) |
| 274 |
|
| 275 |
signed = cast_type_to_ffitype(rffi.SIGNED) |
| 276 |
size = signed.c_size*2 |
| 277 |
alignment = signed.c_alignment |
| 278 |
tpe = make_struct_ffitype_e(size, alignment, [signed, signed]) |
| 279 |
|
| 280 |
sum_x_y = lib.getrawpointer('sum_x_y', [tpe.ffistruct], signed) |
| 281 |
|
| 282 |
buffer = lltype.malloc(rffi.SIGNEDP.TO, 3, flavor='raw') |
| 283 |
buffer[0] = 200 |
| 284 |
buffer[1] = 220 |
| 285 |
buffer[2] = 666 |
| 286 |
sum_x_y.call([rffi.cast(rffi.VOIDP, buffer)], |
| 287 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))) |
| 288 |
assert buffer[2] == 420 |
| 289 |
|
| 290 |
lltype.free(buffer, flavor='raw') |
| 291 |
del sum_x_y |
| 292 |
lltype.free(tpe, flavor='raw') |
| 293 |
del lib |
| 294 |
|
| 295 |
gc.collect() |
| 296 |
assert not ALLOCATED |
| 297 |
|
| 298 |
def test_ret_struct_val(self): |
| 299 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 300 |
from rpython.translator.platform import platform |
| 301 |
from rpython.tool.udir import udir |
| 302 |
|
| 303 |
c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") |
| 304 |
c_file.write(py.code.Source(''' |
| 305 |
#include "src/precommondefs.h" |
| 306 |
#include <stdlib.h> |
| 307 |
#include <stdio.h> |
| 308 |
|
| 309 |
struct s2h { |
| 310 |
short x; |
| 311 |
short y; |
| 312 |
}; |
| 313 |
|
| 314 |
RPY_EXPORTED |
| 315 |
struct s2h give(short x, short y) { |
| 316 |
struct s2h out; |
| 317 |
out.x = x; |
| 318 |
out.y = y; |
| 319 |
return out; |
| 320 |
} |
| 321 |
|
| 322 |
RPY_EXPORTED |
| 323 |
struct s2h perturb(struct s2h inp) { |
| 324 |
inp.x *= 2; |
| 325 |
inp.y *= 3; |
| 326 |
return inp; |
| 327 |
} |
| 328 |
|
| 329 |
''')) |
| 330 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 331 |
lib_name = str(platform.compile([c_file], eci, 'x2', standalone=False)) |
| 332 |
|
| 333 |
lib = CDLL(lib_name) |
| 334 |
|
| 335 |
size = ffi_type_sshort.c_size*2 |
| 336 |
alignment = ffi_type_sshort.c_alignment |
| 337 |
tpe = make_struct_ffitype_e(size, alignment, [ffi_type_sshort]*2) |
| 338 |
|
| 339 |
give = lib.getrawpointer('give', [ffi_type_sshort, ffi_type_sshort], |
| 340 |
tpe.ffistruct) |
| 341 |
inbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw') |
| 342 |
inbuffer[0] = rffi.cast(rffi.SHORT, 40) |
| 343 |
inbuffer[1] = rffi.cast(rffi.SHORT, 72) |
| 344 |
|
| 345 |
outbuffer = lltype.malloc(rffi.SHORTP.TO, 2, flavor='raw') |
| 346 |
|
| 347 |
give.call([rffi.cast(rffi.VOIDP, inbuffer), |
| 348 |
rffi.cast(rffi.VOIDP, rffi.ptradd(inbuffer, 1))], |
| 349 |
rffi.cast(rffi.VOIDP, outbuffer)) |
| 350 |
|
| 351 |
assert outbuffer[0] == 40 |
| 352 |
assert outbuffer[1] == 72 |
| 353 |
|
| 354 |
perturb = lib.getrawpointer('perturb', [tpe.ffistruct], tpe.ffistruct) |
| 355 |
|
| 356 |
inbuffer[0] = rffi.cast(rffi.SHORT, 7) |
| 357 |
inbuffer[1] = rffi.cast(rffi.SHORT, 11) |
| 358 |
|
| 359 |
perturb.call([rffi.cast(rffi.VOIDP, inbuffer)], |
| 360 |
rffi.cast(rffi.VOIDP, outbuffer)) |
| 361 |
|
| 362 |
assert inbuffer[0] == 7 |
| 363 |
assert inbuffer[1] == 11 |
| 364 |
|
| 365 |
assert outbuffer[0] == 14 |
| 366 |
assert outbuffer[1] == 33 |
| 367 |
|
| 368 |
lltype.free(outbuffer, flavor='raw') |
| 369 |
lltype.free(inbuffer, flavor='raw') |
| 370 |
del give |
| 371 |
del perturb |
| 372 |
lltype.free(tpe, flavor='raw') |
| 373 |
gc.collect() |
| 374 |
del lib |
| 375 |
|
| 376 |
assert not ALLOCATED |
| 377 |
|
| 378 |
def test_cdll_life_time(self): |
| 379 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 380 |
from rpython.translator.platform import platform |
| 381 |
from rpython.tool.udir import udir |
| 382 |
|
| 383 |
c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") |
| 384 |
c_file.write(py.code.Source(''' |
| 385 |
#include "src/precommondefs.h" |
| 386 |
RPY_EXPORTED |
| 387 |
Signed fun(Signed i) { |
| 388 |
return i + 42; |
| 389 |
} |
| 390 |
''')) |
| 391 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 392 |
lib_name = str(platform.compile([c_file], eci, 'x3', standalone=False)) |
| 393 |
|
| 394 |
lib = CDLL(lib_name) |
| 395 |
signed = cast_type_to_ffitype(rffi.SIGNED) |
| 396 |
fun = lib.getrawpointer('fun', [signed], signed) |
| 397 |
del lib |
| 398 |
|
| 399 |
buffer = lltype.malloc(rffi.SIGNEDP.TO, 2, flavor='raw') |
| 400 |
buffer[0] = 200 |
| 401 |
buffer[1] = -1 |
| 402 |
fun.call([rffi.cast(rffi.VOIDP, buffer)], |
| 403 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1))) |
| 404 |
assert buffer[1] == 242 |
| 405 |
|
| 406 |
lltype.free(buffer, flavor='raw') |
| 407 |
del fun |
| 408 |
|
| 409 |
gc.collect() |
| 410 |
assert not ALLOCATED |
| 411 |
|
| 412 |
def test_variadic_args(self): |
| 413 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 414 |
from rpython.translator.platform import platform |
| 415 |
from rpython.tool.udir import udir |
| 416 |
|
| 417 |
c_file = udir.ensure("test_libffi", dir=1).join("xlib.c") |
| 418 |
c_file.write(py.code.Source(''' |
| 419 |
#include "src/precommondefs.h" |
| 420 |
#include <stdarg.h> |
| 421 |
#include <stdio.h> |
| 422 |
RPY_EXPORTED |
| 423 |
Signed fun(Signed n, ...) { |
| 424 |
va_list ptr; |
| 425 |
int sum = 0; |
| 426 |
printf("n: %ld\\n", n); |
| 427 |
|
| 428 |
va_start(ptr, n); |
| 429 |
for (int i = 0; i < n; i++) { |
| 430 |
Signed foo = va_arg(ptr, Signed); |
| 431 |
sum += foo; |
| 432 |
printf("Arg %d: %ld\\n", i, foo); |
| 433 |
} |
| 434 |
va_end(ptr); |
| 435 |
return sum; |
| 436 |
} |
| 437 |
''')) |
| 438 |
|
| 439 |
eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 440 |
lib_name = str(platform.compile([c_file], eci, 'x4', standalone=False)) |
| 441 |
|
| 442 |
lib = CDLL(lib_name) |
| 443 |
signed = cast_type_to_ffitype(rffi.SIGNED) |
| 444 |
fun = lib.getrawpointer('fun', [signed, signed, signed], signed, variadic_args=2) |
| 445 |
|
| 446 |
buffer = lltype.malloc(rffi.SIGNEDP.TO, 4, flavor='raw') |
| 447 |
buffer[0] = 2 |
| 448 |
buffer[1] = 3 |
| 449 |
buffer[2] = 15 |
| 450 |
buffer[3] = 100 |
| 451 |
fun.call([rffi.cast(rffi.VOIDP, buffer), rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 1)), |
| 452 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 2))], |
| 453 |
rffi.cast(rffi.VOIDP, rffi.ptradd(buffer, 3))) |
| 454 |
assert buffer[3] == 3 + 15 |
| 455 |
lltype.free(buffer, flavor='raw') |
| 456 |
|
| 457 |
class TestWin32Handles(BaseFfiTest): |
| 458 |
def setup_class(cls): |
| 459 |
if sys.platform != 'win32': |
| 460 |
py.test.skip("Handle to libc library, Win-only test") |
| 461 |
BaseFfiTest.setup_class() |
| 462 |
|
| 463 |
def test_get_libc_handle(self): |
| 464 |
handle = get_libc_handle() |
| 465 |
print get_libc_name() |
| 466 |
print dir(handle) |
| 467 |
addr = rffi.cast(rffi.INT, handle) |
| 468 |
assert addr != 0 |
| 469 |
assert addr % 0x1000 == 0 |