def test_cast_adr_to_int(): S = Struct('S') p = malloc(S, immortal=True) def fn(n): a = llmemory.cast_ptr_to_adr(p) if n == 2: return llmemory.cast_adr_to_int(a, "emulated") elif n == 4: return llmemory.cast_adr_to_int(a, "symbolic") else: return llmemory.cast_adr_to_int(a, "forced") res = interpret(fn, [2]) assert is_valid_int(res) assert res == cast_ptr_to_int(p) # res = interpret(fn, [4]) assert isinstance(res, llmemory.AddressAsInt) assert llmemory.cast_int_to_adr(res) == llmemory.cast_ptr_to_adr(p) # res = interpret(fn, [6]) assert is_valid_int(res) from pypy.rpython.lltypesystem import rffi assert res == rffi.cast(Signed, p)
def cast_int_to_whatever(T, value): if isinstance(T, lltype.Ptr): return lltype.cast_int_to_ptr(T, value) elif T is llmemory.Address: return llmemory.cast_int_to_adr(value) else: return lltype.cast_primitive(T, value)
def follow_stack_frame_of_assembler(callback, gc, addr): frame_addr = addr.signed[1] addr = llmemory.cast_int_to_adr(frame_addr + self.force_index_ofs) force_index = addr.signed[0] if force_index < 0: force_index = ~force_index callshape = self._callshapes[force_index] n = 0 while True: offset = rffi.cast(lltype.Signed, callshape[n]) if offset == 0: break addr = llmemory.cast_int_to_adr(frame_addr + offset) if gc.points_to_valid_gc_object(addr): callback(gc, addr) n += 1
def revealconst(self, T): if isinstance(T, lltype.Ptr): return lltype.cast_int_to_ptr(T, self.get_integer_value()) elif T is llmemory.Address: return llmemory.cast_int_to_adr(self.get_integer_value()) else: return lltype.cast_primitive(T, self.get_integer_value())
def _next_id(self): # return an id not currently in use (as an address instead of an int) if self.id_free_list.non_empty(): result = self.id_free_list.pop() # reuse a dead id else: # make up a fresh id number result = llmemory.cast_int_to_adr(self.next_free_id) self.next_free_id += 2 # only odd numbers, to make lltype # and llmemory happy and to avoid # clashes with real addresses return result
def compact(self, resizing): fromaddr = self.space size_gc_header = self.gcheaderbuilder.size_gc_header start = fromaddr end = fromaddr num = 0 while fromaddr < self.free: obj = fromaddr + size_gc_header hdr = llmemory.cast_adr_to_ptr(fromaddr, lltype.Ptr(self.HDR)) objsize = self.get_size_from_backup(obj, num) totalsize = size_gc_header + objsize if not self.surviving(obj): # this object dies. Following line is a noop in C, # we clear it to make debugging easier llarena.arena_reset(fromaddr, totalsize, False) else: ll_assert(self.is_forwarded(obj), "not forwarded, surviving obj") forward_ptr = hdr.forward_ptr if resizing: end = fromaddr val = (self.get_typeid_from_backup(num) << 16) + 1 hdr.forward_ptr = llmemory.cast_int_to_adr(val) if fromaddr != forward_ptr: #llop.debug_print(lltype.Void, "Copying from to", # fromaddr, forward_ptr, totalsize) llmemory.raw_memmove(fromaddr, forward_ptr, totalsize) if resizing and end - start > GC_CLEARANCE: diff = end - start #llop.debug_print(lltype.Void, "Cleaning", start, diff) diff = (diff / GC_CLEARANCE) * GC_CLEARANCE #llop.debug_print(lltype.Void, "Cleaning", start, diff) end = start + diff if we_are_translated(): # XXX wuaaaaa.... those objects are freed incorrectly # here in case of test_gc llarena.arena_reset(start, diff, True) start += diff num += 1 fromaddr += totalsize
def init_gc_object(self, addr, typeid, flags=1): hdr = llmemory.cast_adr_to_ptr(addr, lltype.Ptr(self.HDR)) hdr.forward_ptr = llmemory.cast_int_to_adr((typeid << 16) | flags)
def next(iself, gc, next, range_highest): # Return the "next" valid GC object' address. This usually # means just returning "next", until we reach "range_highest", # except that we are skipping NULLs. If "next" contains a # MARKER instead, then we go into JIT-frame-lookup mode. # while True: # # If we are not iterating right now in a JIT frame if iself.frame_addr == 0: # # Look for the next shadowstack address that # contains a valid pointer while next != range_highest: if next.signed[0] == self.MARKER: break if gc.points_to_valid_gc_object(next): return next next += llmemory.sizeof(llmemory.Address) else: return llmemory.NULL # done # # It's a JIT frame. Save away 'next' for later, and # go into JIT-frame-exploring mode. next += llmemory.sizeof(llmemory.Address) frame_addr = next.signed[0] iself.saved_next = next iself.frame_addr = frame_addr addr = llmemory.cast_int_to_adr(frame_addr + self.force_index_ofs) addr = iself.translateptr(iself.context, addr) force_index = addr.signed[0] if force_index < 0: force_index = ~force_index # NB: the next line reads a still-alive _callshapes, # because we ensure that just before we called this # piece of assembler, we put on the (same) stack a # pointer to a loop_token that keeps the force_index # alive. callshape = self._callshapes[force_index] else: # Continuing to explore this JIT frame callshape = iself.callshape # # 'callshape' points to the next INT of the callshape. # If it's zero we are done with the JIT frame. while rffi.cast(lltype.Signed, callshape[0]) != 0: # # Non-zero: it's an offset inside the JIT frame. # Read it and increment 'callshape'. offset = rffi.cast(lltype.Signed, callshape[0]) callshape = lltype.direct_ptradd(callshape, 1) addr = llmemory.cast_int_to_adr(iself.frame_addr + offset) addr = iself.translateptr(iself.context, addr) if gc.points_to_valid_gc_object(addr): # # The JIT frame contains a valid GC pointer at # this address (as opposed to NULL). Save # 'callshape' for the next call, and return the # address. iself.callshape = callshape return addr # # Restore 'prev' and loop back to the start. iself.frame_addr = 0 next = iself.saved_next next += llmemory.sizeof(llmemory.Address)
def op_cast_int_to_adr(int): return llmemory.cast_int_to_adr(int)
def get_aid(): """Return the thread identifier, cast to an (opaque) address.""" return llmemory.cast_int_to_adr(ll_thread.get_ident())
def int2adr(int): return llmemory.cast_int_to_adr(int)
def f(): a1 = llmemory.cast_ptr_to_adr(s1) i1 = llmemory.cast_adr_to_int(a1) a2 = llmemory.cast_int_to_adr(i1) s2 = llmemory.cast_adr_to_ptr(a2, lltype.Ptr(S1)) return int(s1 == s2)
def discard_translations(data, size): if we_are_translated() and VALGRIND_DISCARD_TRANSLATIONS is not None: VALGRIND_DISCARD_TRANSLATIONS(llmemory.cast_int_to_adr(data), size)
def force(self, force_token): token = llmemory.cast_int_to_adr(force_token) frame = llimpl.get_forced_token_frame(token) fail_index = llimpl.force(frame) self.latest_frame = frame return self.get_fail_descr_from_number(fail_index)
def init_gc_object_immortal(self, addr, typeid, flags=1): hdr = llmemory.cast_adr_to_ptr(addr, lltype.Ptr(self.HDR)) flags |= GCFLAG_EXTERNAL hdr.forward_ptr = llmemory.cast_int_to_adr((typeid << 16) | flags)
def mark(self, obj): previous = self.get_tid(obj) self.header(obj).forward_ptr = llmemory.cast_int_to_adr(previous | GCFLAG_MARKBIT)