Rizin
unix-like reverse engineering framework and cli tools
io_cache.c File Reference
#include <rz_io.h>
#include <rz_skyline.h>

Go to the source code of this file.

Functions

static void cache_item_free (RzIOCache *cache)
 
RZ_API bool rz_io_cache_at (RzIO *io, ut64 addr)
 
RZ_API void rz_io_cache_init (RzIO *io)
 
RZ_API void rz_io_cache_fini (RzIO *io)
 
RZ_API void rz_io_cache_commit (RzIO *io, ut64 from, ut64 to)
 
RZ_API void rz_io_cache_reset (RzIO *io, int set)
 
RZ_API int rz_io_cache_invalidate (RzIO *io, ut64 from, ut64 to)
 
RZ_API bool rz_io_cache_write (RzIO *io, ut64 addr, const ut8 *buf, int len)
 
RZ_API bool rz_io_cache_read (RzIO *io, ut64 addr, ut8 *buf, int len)
 

Function Documentation

◆ cache_item_free()

static void cache_item_free ( RzIOCache cache)
static

Definition at line 7 of file io_cache.c.

7  {
8  if (!cache) {
9  return;
10  }
11  free(cache->data);
12  free(cache->odata);
13  free(cache);
14 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ut8 * odata
Definition: rz_io.h:165
ut8 * data
Definition: rz_io.h:164

References rz_io_cache_t::data, free(), and rz_io_cache_t::odata.

Referenced by rz_io_cache_init().

◆ rz_io_cache_at()

RZ_API bool rz_io_cache_at ( RzIO io,
ut64  addr 
)

Definition at line 16 of file io_cache.c.

16  {
17  rz_return_val_if_fail(io, false);
19 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static bool rz_skyline_contains(RzSkyline *skyline, ut64 addr)
Definition: rz_skyline.h:51
RzSkyline cache_skyline
Definition: rz_io.h:77
static int addr
Definition: z80asm.c:58

References addr, rz_io_t::cache_skyline, rz_return_val_if_fail, and rz_skyline_contains().

Referenced by rz_io_desc_read().

◆ rz_io_cache_commit()

RZ_API void rz_io_cache_commit ( RzIO io,
ut64  from,
ut64  to 
)

Definition at line 35 of file io_cache.c.

35  {
36  void **iter;
37  RzIOCache *c;
40  rz_pvector_foreach (&io->cache, iter) {
41  // if (from <= c->to - 1 && c->from <= to - 1) {
42  c = *iter;
43  if (rz_itv_overlap(c->itv, range)) {
44  int cached = io->cached;
45  io->cached = 0;
46  if (rz_io_write_at(io, rz_itv_begin(c->itv), c->data, rz_itv_size(c->itv))) {
47  c->written = true;
48  } else {
49  eprintf("Error writing change at 0x%08" PFMT64x "\n", rz_itv_begin(c->itv));
50  }
51  io->cached = cached;
52  // break; // XXX old behavior, revisit this
53  }
54  }
55 }
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io.c:358
static ut64 rz_itv_begin(RzInterval itv)
Definition: rz_itv.h:34
struct rz_interval_t RzInterval
static bool rz_itv_overlap(RzInterval itv, RzInterval x)
Definition: rz_itv.h:64
static ut64 rz_itv_size(RzInterval itv)
Definition: rz_itv.h:38
#define PFMT64x
Definition: rz_types.h:393
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
#define c(i)
Definition: sha256.c:43
int cached
Definition: rz_io.h:69
RzPVector cache
Definition: rz_io.h:76

References c, rz_io_t::cache, rz_io_t::cached, eprintf, from, PFMT64x, capstone::range, rz_io_write_at(), rz_itv_begin(), rz_itv_overlap(), rz_itv_size(), rz_pvector_foreach, rz_return_if_fail, and to.

Referenced by rz_write_cache_commit_all_handler(), and rz_write_cache_commit_handler().

◆ rz_io_cache_fini()

RZ_API void rz_io_cache_fini ( RzIO io)

Definition at line 28 of file io_cache.c.

28  {
30  rz_pvector_fini(&io->cache);
32  io->cached = 0;
33 }
static void rz_skyline_fini(RzSkyline *skyline)
Definition: rz_skyline.h:24
RZ_API void rz_pvector_fini(RzPVector *vec)
Definition: vector.c:331

References rz_io_t::cache, rz_io_t::cache_skyline, rz_io_t::cached, rz_pvector_fini(), rz_return_if_fail, and rz_skyline_fini().

Referenced by rz_core_disasm_pde(), rz_io_close_all(), and rz_io_fini().

◆ rz_io_cache_init()

RZ_API void rz_io_cache_init ( RzIO io)

Definition at line 21 of file io_cache.c.

21  {
25  io->cached = 0;
26 }
static void cache_item_free(RzIOCache *cache)
Definition: io_cache.c:7
static void rz_skyline_init(RzSkyline *skyline)
Definition: rz_skyline.h:19
RZ_API void rz_pvector_init(RzPVector *vec, RzPVectorFree free)
Definition: vector.c:298
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43

References rz_io_t::cache, cache_item_free(), rz_io_t::cache_skyline, rz_io_t::cached, rz_pvector_init(), rz_return_if_fail, and rz_skyline_init().

Referenced by rz_core_disasm_pde(), and rz_io_init().

◆ rz_io_cache_invalidate()

RZ_API int rz_io_cache_invalidate ( RzIO io,
ut64  from,
ut64  to 
)

Definition at line 64 of file io_cache.c.

64  {
65  rz_return_val_if_fail(io, 0);
66  int invalidated = 0;
67  void **iter;
68  RzIOCache *c;
71  c = *iter;
72  if (rz_itv_overlap(c->itv, range)) {
73  int cached = io->cached;
74  io->cached = 0;
75  rz_io_write_at(io, rz_itv_begin(c->itv), c->odata, rz_itv_size(c->itv));
76  io->cached = cached;
77  c->written = false;
79  free(c->data);
80  free(c->odata);
81  free(c);
82  invalidated++;
83  }
84  }
86  rz_pvector_foreach (&io->cache, iter) {
87  c = *iter;
88  rz_skyline_add(&io->cache_skyline, c->itv, c);
89  }
90  return invalidated;
91 }
static void rz_skyline_clear(RzSkyline *skyline)
Definition: rz_skyline.h:29
RZ_API bool rz_skyline_add(RzSkyline *skyline, RzInterval itv, void *user)
Definition: skyline.c:12
RZ_API void rz_pvector_remove_data(RzPVector *vec, void *x)
Definition: vector.c:362
#define rz_pvector_foreach_prev(vec, it)
Definition: rz_vector.h:338

References c, rz_io_t::cache, rz_io_t::cache_skyline, rz_io_t::cached, free(), from, capstone::range, rz_io_write_at(), rz_itv_begin(), rz_itv_overlap(), rz_itv_size(), rz_pvector_foreach, rz_pvector_foreach_prev, rz_pvector_remove_data(), rz_return_val_if_fail, rz_skyline_add(), rz_skyline_clear(), and to.

Referenced by rz_write_cache_remove_handler().

◆ rz_io_cache_read()

RZ_API bool rz_io_cache_read ( RzIO io,
ut64  addr,
ut8 buf,
int  len 
)

Definition at line 131 of file io_cache.c.

131  {
132  rz_return_val_if_fail(io && buf, false);
133  RzSkyline *skyline = &io->cache_skyline;
134  if (!len) {
135  return true;
136  }
137  if (UT64_ADD_OVFCHK(addr, len)) {
138  const ut64 first_len = UT64_MAX - addr;
139  rz_io_cache_read(io, 0, buf + first_len, len - first_len);
140  len = first_len;
141  }
143  if (!iter) {
144  return false;
145  }
146  const RzSkylineItem *last = (RzSkylineItem *)skyline->v.a + skyline->v.len;
147  bool covered = false;
148  while (iter != last) {
149  const ut64 begin = rz_itv_begin(iter->itv);
150  const st64 addr_offset = begin - addr;
151  const ut64 buf_offset = addr_offset > 0 ? addr_offset : 0;
152  const ut64 cur_addr = addr + buf_offset;
153  const ut64 left = len - buf_offset;
154  if (begin > cur_addr + left) {
155  break;
156  }
157  RzIOCache *cache = iter->user;
158  const ut64 cache_shift = addr_offset < 0 ? -addr_offset : 0;
159  const ut64 cache_offset = begin - rz_itv_begin(cache->itv) + cache_shift;
160  const ut64 read = RZ_MIN(left, rz_itv_size(iter->itv) - cache_shift);
161  memcpy(buf + buf_offset, cache->data + cache_offset, read);
162  covered = true;
163  if (left - read <= 0) {
164  break;
165  }
166  iter++;
167  }
168  return covered;
169 }
size_t len
Definition: 6502dis.c:15
RZ_API bool rz_io_cache_read(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io_cache.c:131
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
while(len< limit &&buf1[len]==buf2[len])++len
RZ_API const RzSkylineItem * rz_skyline_get_item_intersect(RzSkyline *skyline, ut64 addr, ut64 len)
Definition: skyline.c:60
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define UT64_MAX
Definition: rz_types_base.h:86
#define UT64_ADD_OVFCHK(x, y)
RzInterval itv
Definition: rz_io.h:163
RzVector v
Definition: rz_skyline.h:13
void * a
Definition: rz_vector.h:46
size_t len
Definition: rz_vector.h:47
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References rz_vector_t::a, addr, rz_io_t::cache_skyline, rz_io_cache_t::data, rz_io_cache_t::itv, len, rz_vector_t::len, memcpy(), read(), rz_io_cache_read(), rz_itv_begin(), rz_itv_size(), RZ_MIN, rz_return_val_if_fail, rz_skyline_get_item_intersect(), st64, ut64(), UT64_ADD_OVFCHK, UT64_MAX, rz_skyline_t::v, and while().

Referenced by rz_io_cache_read(), rz_io_desc_read(), rz_io_nread_at(), rz_io_read_at(), and rz_io_read_at_mapped().

◆ rz_io_cache_reset()

RZ_API void rz_io_cache_reset ( RzIO io,
int  set 
)

Definition at line 57 of file io_cache.c.

57  {
59  io->cached = set;
60  rz_pvector_clear(&io->cache);
62 }
RZ_API void rz_pvector_clear(RzPVector *vec)
Definition: vector.c:326

References rz_io_t::cache, rz_io_t::cache_skyline, rz_io_t::cached, rz_pvector_clear(), rz_return_if_fail, and rz_skyline_clear().

Referenced by rz_core_link_stroff(), and rz_write_cache_remove_all_handler().

◆ rz_io_cache_write()

RZ_API bool rz_io_cache_write ( RzIO io,
ut64  addr,
const ut8 buf,
int  len 
)

Definition at line 93 of file io_cache.c.

93  {
94  rz_return_val_if_fail(io && buf, false);
96  if (!ch) {
97  return false;
98  }
99  if (UT64_ADD_OVFCHK(addr, len)) {
100  const ut64 first_len = UT64_MAX - addr;
101  rz_io_cache_write(io, 0, buf + first_len, len - first_len);
102  len = first_len;
103  }
104  ch->itv = (RzInterval){ addr, len };
105  ch->odata = (ut8 *)calloc(1, len + 1);
106  if (!ch->odata) {
107  free(ch);
108  return false;
109  }
110  ch->data = (ut8 *)calloc(1, len + 1);
111  if (!ch->data) {
112  free(ch->odata);
113  free(ch);
114  return false;
115  }
116  ch->written = false;
117  {
118  const bool cm = io->cachemode;
119  io->cachemode = false;
120  rz_io_read_at(io, addr, ch->odata, len);
121  io->cachemode = cm;
122  }
123  memcpy(ch->data, buf, len);
124  rz_pvector_push(&io->cache, ch);
125  rz_skyline_add(&io->cache_skyline, ch->itv, ch);
126  RzEventIOWrite iow = { addr, buf, len };
128  return true;
129 }
RZ_API bool rz_io_cache_write(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io_cache.c:93
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API void rz_event_send(RzEvent *ev, int type, void *data)
Definition: event.c:115
@ RZ_EVENT_IO_WRITE
Definition: rz_event.h:42
RZ_API bool rz_io_read_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:300
#define RZ_NEW0(x)
Definition: rz_types.h:284
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
int written
Definition: rz_io.h:166
bool cachemode
Definition: rz_io.h:70
RzEvent * event
Definition: rz_io.h:90

References addr, rz_io_t::cache, rz_io_t::cache_skyline, rz_io_t::cachemode, calloc(), rz_io_cache_t::data, rz_io_t::event, free(), rz_io_cache_t::itv, len, memcpy(), rz_io_cache_t::odata, RZ_EVENT_IO_WRITE, rz_event_send(), rz_io_cache_write(), rz_io_read_at(), RZ_NEW0, rz_pvector_push(), rz_return_val_if_fail, rz_skyline_add(), ut64(), UT64_ADD_OVFCHK, UT64_MAX, and rz_io_cache_t::written.

Referenced by rz_io_cache_write(), rz_io_desc_read(), and rz_io_write_at().