def win32_urandom(space, n): """urandom(n) -> str Return a string of n random bytes suitable for cryptographic use. """ if n < 0: raise OperationError(space.w_ValueError, space.wrap("negative argument not allowed")) provider = get(space).cryptProviderPtr[0] if not provider: # Acquire context. # This handle is never explicitly released. The operating # system will release it when the process terminates. if not CryptAcquireContext( get(space).cryptProviderPtr, None, None, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT): raise rwin32.lastWindowsError("CryptAcquireContext") provider = get(space).cryptProviderPtr[0] # Get random data buf = lltype.malloc(rffi.CArray(rwin32.BYTE), n, zero=True, # zero seed flavor='raw') try: if not CryptGenRandom(provider, n, buf): raise rwin32.lastWindowsError("CryptGenRandom") return space.wrap( rffi.charpsize2str(rffi.cast(rffi.CCHARP, buf), n)) finally: lltype.free(buf, flavor='raw')
def descr_poll(self, space, timeout=-1.0, maxevents=-1): self.check_closed(space) if timeout < 0: timeout = -1.0 else: timeout *= 1000.0 if maxevents == -1: maxevents = FD_SETSIZE - 1 elif maxevents < 1: raise operationerrfmt(space.w_ValueError, "maxevents must be greater than 0, not %d", maxevents) with lltype.scoped_alloc(rffi.CArray(epoll_event), maxevents) as evs: nfds = epoll_wait(self.epfd, evs, maxevents, int(timeout)) if nfds < 0: raise exception_from_errno(space, space.w_IOError) elist_w = [None] * nfds for i in xrange(nfds): event = evs[i] elist_w[i] = space.newtuple([ space.wrap(event.c_data.c_fd), space.wrap(event.c_events) ]) return space.newlist(elist_w)
def push_arg_as_ffiptr(ffitp, arg, ll_buf): # This is for primitive types. Note that the exact type of 'arg' may be # different from the expected 'c_size'. To cope with that, we fall back # to a byte-by-byte copy. TP = lltype.typeOf(arg) TP_P = lltype.Ptr(rffi.CArray(TP)) TP_size = rffi.sizeof(TP) c_size = intmask(ffitp.c_size) # if both types have the same size, we can directly write the # value to the buffer if c_size == TP_size: buf = rffi.cast(TP_P, ll_buf) buf[0] = arg else: # needs byte-by-byte copying. Make sure 'arg' is an integer type. # Note that this won't work for rffi.FLOAT/rffi.DOUBLE. assert TP is not rffi.FLOAT and TP is not rffi.DOUBLE if TP_size <= rffi.sizeof(lltype.Signed): arg = rffi.cast(lltype.Unsigned, arg) else: arg = rffi.cast(lltype.UnsignedLongLong, arg) if _LITTLE_ENDIAN: for i in range(c_size): ll_buf[i] = chr(arg & 0xFF) arg >>= 8 elif _BIG_ENDIAN: for i in range(c_size-1, -1, -1): ll_buf[i] = chr(arg & 0xFF) arg >>= 8 else: raise AssertionError
def define_nongc_attached_to_gc(cls): from pypy.rpython.lltypesystem import rffi ARRAY = rffi.CArray(rffi.INT) class A: def __init__(self, n): self.buf = lltype.malloc(ARRAY, n, flavor='raw', add_memory_pressure=True) def __del__(self): lltype.free(self.buf, flavor='raw') A(6) def f(): # allocate a total of ~77GB, but if the automatic gc'ing works, # it should never need more than a few MBs at once am1 = am2 = am3 = None res = 0 for i in range(1, 100001): if am3 is not None: res += rffi.cast(lltype.Signed, am3.buf[0]) am3 = am2 am2 = am1 am1 = A(i * 4) am1.buf[0] = rffi.cast(rffi.INT, i - 50000) return res return f
def main(n): with lltype.scoped_alloc(rffi.CArray(POINT), n) as points: with lltype.scoped_alloc(rffi.CArray(POINT), 1) as result_point: for i in xrange(n): points[i].x = i * 2 points[i].y = i * 2 + 1 points = rffi.cast(rffi.CArrayPtr(lltype.Char), points) result_point[0].x = 0 result_point[0].y = 0 result_point = rffi.cast(rffi.CArrayPtr(lltype.Char), result_point) f(points, result_point, n) result_point = rffi.cast(rffi.CArrayPtr(POINT), result_point) return result_point[0].x * result_point[0].y
def test_image_pixels(): for filename in ["demo.jpg", "demo.png"]: image = RIMG.Load(os.path.join(autopath.this_dir, filename)) assert image assert rffi.getintfield(image.c_format, 'c_BytesPerPixel') in (3, 4) RSDL.LockSurface(image) result = {} try: rgb = lltype.malloc(rffi.CArray(RSDL.Uint8), 3, flavor='raw') try: for y in range(23): for x in range(y % 13, 17, 13): color = RSDL_helper.get_pixel(image, x, y) RSDL.GetRGB(color, image.c_format, rffi.ptradd(rgb, 0), rffi.ptradd(rgb, 1), rffi.ptradd(rgb, 2)) r = rffi.cast(lltype.Signed, rgb[0]) g = rffi.cast(lltype.Signed, rgb[1]) b = rffi.cast(lltype.Signed, rgb[2]) result[x, y] = r, g, b finally: lltype.free(rgb, flavor='raw') finally: RSDL.UnlockSurface(image) RSDL.FreeSurface(image) for x, y in result: f = (x * 17 + y * 23) / float(17 * 17 + 23 * 23) expected_r = int(255.0 * (1.0 - f)) expected_g = 0 expected_b = int(255.0 * f) r, g, b = result[x, y] assert abs(r - expected_r) < 10 assert abs(g - expected_g) < 10 assert abs(b - expected_b) < 10
def test_callback(self): libc = CDLL('libc.so.6') qsort = libc.getpointer('qsort', [ ffi_type_pointer, ffi_type_slong, ffi_type_slong, ffi_type_pointer ], ffi_type_void) def callback(ll_args, ll_res, stuff): a1 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[0])[0] a2 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[1])[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = 1 else: res[0] = -1 ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.INT) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.sizeof(rffi.INT)) qsort.push_arg(4) qsort.push_arg(ptr.ll_closure) qsort.call(lltype.Void) assert [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw')
def push_arg_as_ffiptr(ffitp, arg, ll_buf): # this is for primitive types. For structures and arrays # would be something different (more dynamic) TP = lltype.typeOf(arg) TP_P = lltype.Ptr(rffi.CArray(TP)) buf = rffi.cast(TP_P, ll_buf) buf[0] = arg
def __init__(self, space, environment, context, retrieveError): self.context = context if retrieveError: if environment.errorHandle: handle = environment.errorHandle handleType = roci.OCI_HTYPE_ERROR else: handle = environment.handle handleType = roci.OCI_HTYPE_ENV codeptr = lltype.malloc(rffi.CArray(roci.sb4), 1, flavor='raw') BUFSIZE = 1024 textbuf, text = rffi.alloc_buffer(BUFSIZE) try: status = roci.OCIErrorGet( handle, 1, lltype.nullptr(roci.oratext.TO), codeptr, textbuf, BUFSIZE, handleType) if status != roci.OCI_SUCCESS: raise OperationError( get(space).w_InternalError, space.wrap("No Oracle error?")) self.code = codeptr[0] self.w_message = config.w_string(space, textbuf) finally: lltype.free(codeptr, flavor='raw') rffi.keep_buffer_alive_until_here(textbuf, text) if config.WITH_UNICODE: # XXX remove double zeros at the end pass
def _build_ty_array_ptr(self, basesize, ty_item, ofs_length): pad1 = ofs_length pad2 = basesize - ofs_length - self.size_of_int assert pad1 >= 0 and pad2 >= 0 const_index_length = self._make_const_int(pad1) const_index_array = self._make_const_int(pad1 + 1 + pad2) # build the type "struct{pad1.., length, pad2.., array{type}}" typeslist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMTypeRef), pad1+pad2+2, flavor='raw') # add the first padding for n in range(pad1): typeslist[n] = self.ty_char # add the length field typeslist[pad1] = self.ty_int # add the second padding for n in range(pad1+1, pad1+1+pad2): typeslist[n] = self.ty_char # add the array field typeslist[pad1+1+pad2] = llvm_rffi.LLVMArrayType(ty_item, 0) # done ty_array = llvm_rffi.LLVMStructType(typeslist, pad1+pad2+2, 1) lltype.free(typeslist, flavor='raw') ty_array_ptr = llvm_rffi.LLVMPointerType(ty_array, 0) return (ty_array_ptr, const_index_length, const_index_array)
def test_byval_result(self): """ struct Point make_point(Signed x, Signed y) { struct Point p; p.x = x; p.y = y; return p; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e(0, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct libfoo = CDLL(self.libfoo_name) make_point = (libfoo, 'make_point', [types.signed, types.signed], ffi_point) # PTR = lltype.Ptr(rffi.CArray(rffi.SIGNED)) p = self.call(make_point, [12, 34], PTR, is_struct=True, jitif=["byval"]) assert p[0] == 12 assert p[1] == 34 lltype.free(p, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')
def test_array_fields(self): POINT = lltype.Struct( "POINT", ("x", lltype.Float), ("y", lltype.Float), ) points = lltype.malloc(rffi.CArray(POINT), 2, flavor="raw") points[0].x = 1.0 points[0].y = 2.0 points[1].x = 3.0 points[1].y = 4.0 points = rffi.cast(rffi.CArrayPtr(lltype.Char), points) assert array_getitem(types.double, 16, points, 0, 0) == 1.0 assert array_getitem(types.double, 16, points, 0, 8) == 2.0 assert array_getitem(types.double, 16, points, 1, 0) == 3.0 assert array_getitem(types.double, 16, points, 1, 8) == 4.0 # array_setitem(types.double, 16, points, 0, 0, 10.0) array_setitem(types.double, 16, points, 0, 8, 20.0) array_setitem(types.double, 16, points, 1, 0, 30.0) array_setitem(types.double, 16, points, 1, 8, 40.0) # assert array_getitem(types.double, 16, points, 0, 0) == 10.0 assert array_getitem(types.double, 16, points, 0, 8) == 20.0 assert array_getitem(types.double, 16, points, 1, 0) == 30.0 assert array_getitem(types.double, 16, points, 1, 8) == 40.0 # lltype.free(points, flavor="raw")
def test_byval_argument(self): """ struct Point { Signed x; Signed y; }; Signed sum_point(struct Point p) { return p.x + p.y; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e(0, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct sum_point = (libfoo, 'sum_point', [ffi_point], types.signed) # ARRAY = rffi.CArray(rffi.SIGNED) buf = lltype.malloc(ARRAY, 2, flavor='raw') buf[0] = 30 buf[1] = 12 adr = rffi.cast(rffi.VOIDP, buf) res = self.call(sum_point, [('arg_raw', adr)], rffi.SIGNED, jitif=["byval"]) assert res == 42 # check that we still have the ownership on the buffer assert buf[0] == 30 assert buf[1] == 12 lltype.free(buf, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')
def _do_call(self, funcsym, ll_args, RESULT): # XXX: check len(args)? ll_result = lltype.nullptr(rffi.CCHARP.TO) if self.restype != types.void: ll_result = lltype.malloc(rffi.CCHARP.TO, intmask(self.restype.c_size), flavor='raw') ffires = c_ffi_call(self.ll_cif, self.funcsym, rffi.cast(rffi.VOIDP, ll_result), rffi.cast(rffi.VOIDPP, ll_args)) if RESULT is not lltype.Void: TP = lltype.Ptr(rffi.CArray(RESULT)) buf = rffi.cast(TP, ll_result) if types.is_struct(self.restype): assert RESULT == rffi.SIGNED # for structs, we directly return the buffer and transfer the # ownership res = rffi.cast(RESULT, buf) else: res = buf[0] else: res = None self._free_buffers(ll_result, ll_args) clibffi.check_fficall_result(ffires, self.flags) return res
def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer( 'qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result)
def generate_CALL(self, op): calldescr = op.descr assert isinstance(calldescr, CallDescr) ty_function_ptr = self.cpu.get_calldescr_ty_function_ptr(calldescr) v = op.args[0] if isinstance(v, Const): func = self.cpu._make_const(v.getint(), ty_function_ptr) else: func = self.getintarg(v) func = llvm_rffi.LLVMBuildIntToPtr(self.builder, func, ty_function_ptr, "") nb_args = len(op.args) - 1 arglist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), nb_args, flavor='raw') for i in range(nb_args): v = op.args[1 + i] index = calldescr.args_indices[i] getarg = self.cpu.getarg_by_index[index] value_ref = getarg(self, v) arglist[i] = value_ref res = llvm_rffi.LLVMBuildCall(self.builder, func, arglist, nb_args, "") lltype.free(arglist, flavor='raw') if op.result is not None: assert calldescr.res_index >= 0 self.vars[op.result] = res
def _struct_setfield(TYPE, addr, offset, value): """ Write the field of type TYPE at addr+offset. addr is of type rffi.VOIDP, offset is an int. """ addr = rffi.ptradd(addr, offset) PTR_FIELD = lltype.Ptr(rffi.CArray(TYPE)) rffi.cast(PTR_FIELD, addr)[0] = value
def __init__(self, space): self.space = space self.w_environ = space.newdict() if _WIN: self.cryptProviderPtr = lltype.malloc(rffi.CArray(HCRYPTPROV), 1, zero=True, flavor='raw')
def _struct_getfield(TYPE, addr, offset): """ Read the field of type TYPE at addr+offset. addr is of type rffi.VOIDP, offset is an int. """ addr = rffi.ptradd(addr, offset) PTR_FIELD = lltype.Ptr(rffi.CArray(TYPE)) return rffi.cast(PTR_FIELD, addr)[0]
def convert_to_regdata(space, w_value, typ): buf = None if typ == rwinreg.REG_DWORD: if space.is_true(space.isinstance(w_value, space.w_int)): buflen = rffi.sizeof(rwin32.DWORD) buf1 = lltype.malloc(rffi.CArray(rwin32.DWORD), 1, flavor='raw') buf1[0] = space.uint_w(w_value) buf = rffi.cast(rffi.CCHARP, buf1) elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' else: if space.is_true(space.isinstance(w_value, space.w_unicode)): w_value = space.call_method(w_value, 'encode', space.wrap('mbcs')) buf = rffi.str2charp(space.str_w(w_value)) buflen = space.len_w(w_value) + 1 elif typ == rwinreg.REG_MULTI_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' elif space.is_true(space.isinstance(w_value, space.w_list)): strings = [] buflen = 0 # unwrap strings and compute total size w_iter = space.iter(w_value) while True: try: w_item = space.next(w_iter) if space.is_true(space.isinstance(w_item, space.w_unicode)): w_item = space.call_method(w_item, 'encode', space.wrap('mbcs')) item = space.str_w(w_item) strings.append(item) buflen += len(item) + 1 except OperationError, e: if not e.match(space, space.w_StopIteration): raise # re-raise other app-level exceptions break buflen += 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') # Now copy data buflen = 0 for string in strings: for i in range(len(string)): buf[buflen + i] = string[i] buflen += len(string) + 1 buf[buflen - 1] = '\0' buflen += 1 buf[buflen - 1] = '\0'
def test_raw_malloc_unsupported_flag(): S = rffi.CArray(lltype.Signed) v1 = varoftype(lltype.Signed) v = varoftype(lltype.Ptr(S)) flags = Constant({'flavor': 'raw', 'unsupported_flag': True}, lltype.Void) op = SpaceOperation('malloc_varsize', [Constant(S, lltype.Void), flags, v1], v) tr = Transformer(FakeCPU(), FakeResidualCallControl()) py.test.raises(UnsupportedMallocFlags, tr.rewrite_operation, op)
def test_call_time(self): libc = CDLL('libc.so.6') # XXX assume time_t is long ctime = libc.getpointer('time', [ffi_type_pointer], ffi_type_ulong) ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) t0 = ctime.call(rffi.LONG) time.sleep(2) ctime.push_arg(lltype.nullptr(rffi.CArray(rffi.LONG))) t1 = ctime.call(rffi.LONG) assert t1 > t0 l_t = lltype.malloc(rffi.CArray(rffi.LONG), 1, flavor='raw') ctime.push_arg(l_t) t1 = ctime.call(rffi.LONG) assert l_t[0] == t1 lltype.free(l_t, flavor='raw') del ctime del libc assert not ALLOCATED
def _generate_new(self, size_ref): malloc_func = self.cpu._make_const(self.cpu.malloc_fn_ptr, self.cpu.ty_malloc_fn) arglist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 1, flavor='raw') arglist[0] = size_ref res = llvm_rffi.LLVMBuildCall(self.builder, malloc_func, arglist, 1, "") lltype.free(arglist, flavor='raw') return res
def close_phi_nodes(self): incoming_blocks = lltype.malloc( rffi.CArray(llvm_rffi.LLVMBasicBlockRef), len(self.phi_incoming_blocks), flavor='raw') incoming_values = lltype.malloc( rffi.CArray(llvm_rffi.LLVMValueRef), len(self.phi_incoming_blocks), flavor='raw') for j in range(len(self.phi_incoming_blocks)): incoming_blocks[j] = self.phi_incoming_blocks[j] loop = self.loop for i in range(len(loop.inputargs)): phi = self.vars[loop.inputargs[i]] incoming = self.phi_incoming_values[i] for j in range(len(self.phi_incoming_blocks)): incoming_values[j] = incoming[j] llvm_rffi.LLVMAddIncoming(phi, incoming_values, incoming_blocks, len(self.phi_incoming_blocks)) lltype.free(incoming_values, flavor='raw') lltype.free(incoming_blocks, flavor='raw')
def _generate_len_gep(self, array_ref, ty, const_index_length): array = llvm_rffi.LLVMBuildBitCast(self.builder, array_ref, ty, "") indices = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 2, flavor='raw') indices[0] = self.cpu.const_zero indices[1] = const_index_length loc = llvm_rffi.LLVMBuildGEP(self.builder, array, indices, 2, "") lltype.free(indices, flavor='raw') return loc
def _generate_ovf_test(self, f_intrinsic, arg0, arg1, result): arglist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 2, flavor='raw') arglist[0] = arg0 arglist[1] = arg1 tmp = llvm_rffi.LLVMBuildCall(self.builder, f_intrinsic, arglist, 2, "") lltype.free(arglist, flavor='raw') self.vars[result] = llvm_rffi.LLVMBuildExtractValue(self.builder, tmp, 0, "") self.lastovf = llvm_rffi.LLVMBuildExtractValue(self.builder, tmp, 1, "")
def __init__(self): self.addrs = lltype.malloc(rffi.CArray(lltype.Signed), 20, flavor='raw') # root_stack_top self.addrs[0] = rffi.cast(lltype.Signed, self.addrs) + 3 * WORD # random stuff self.addrs[1] = 123456 self.addrs[2] = 654321 self.check_initial_and_final_state() self.callshapes = {} self.should_see = []
def call(self, RES_TP): self._check_args() c_ffi_call(self.ll_cif, self.funcsym, rffi.cast(rffi.VOIDP, self.ll_result), rffi.cast(VOIDPP, self.ll_args)) if RES_TP is not lltype.Void: TP = lltype.Ptr(rffi.CArray(RES_TP)) res = rffi.cast(TP, self.ll_result)[0] else: res = None self._clean_args() return res
def _generate_gep(self, op, ty, const_index_array): array = llvm_rffi.LLVMBuildBitCast(self.builder, self.getptrarg(op.args[0]), ty, "") indices = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 3, flavor='raw') indices[0] = self.cpu.const_zero indices[1] = const_index_array indices[2] = self.getintarg(op.args[1]) location = llvm_rffi.LLVMBuildGEP(self.builder, array, indices, 3, "") lltype.free(indices, flavor='raw') return location
def _generate_field_gep(self, v_structure, fielddescr): assert isinstance(fielddescr, FieldDescr) indices = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 1, flavor='raw') indices[0] = self.cpu._make_const_int(fielddescr.offset) location = llvm_rffi.LLVMBuildGEP(self.builder, self.getptrarg(v_structure), indices, 1, "") lltype.free(indices, flavor='raw') ty = self.cpu.types_ptr_by_index[fielddescr.size_index] location = llvm_rffi.LLVMBuildBitCast(self.builder, location, ty, "") return location