Rizin
unix-like reverse engineering framework and cli tools
io.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2019 condret <condr3t@protonmail.com>
2 // SPDX-FileCopyrightText: 2008-2019 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2008-2019 alvaro_fe <alvaro.felipe91@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include <rz_io.h>
7 #include <sdb.h>
8 #include <config.h>
9 #include "io_private.h"
10 
11 #if __WINDOWS__
12 #include <rz_windows.h>
13 #include <w32dbg_wrap.h>
14 #endif
15 
17 
18 static int fd_read_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user) {
19  return rz_io_fd_read_at(io, fd, addr, buf, len);
20 }
21 
22 static int fd_write_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user) {
23  return rz_io_fd_write_at(io, fd, addr, buf, len);
24 }
25 
26 typedef int (*cbOnIterMap)(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user);
27 
28 // If prefix_mode is true, returns the number of bytes of operated prefix; returns < 0 on error.
29 // If prefix_mode is false, operates in non-stop mode and returns true iff all IO operations on overlapped maps are complete.
30 static st64 on_map_skyline(RzIO *io, ut64 vaddr, ut8 *buf, int len, int match_flg, cbOnIterMap op, bool prefix_mode) {
31  RzVector *skyline = &io->map_skyline.v;
32  ut64 addr = vaddr;
33  size_t i;
34  bool ret = true, wrap = !prefix_mode && vaddr + len < vaddr;
35 #define CMP(addr, part) ((addr) < rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? -1 : (addr) > rz_itv_end(((RzSkylineItem *)(part))->itv) - 1 ? 1 \
36  : 0)
37  // Let i be the first skyline part whose right endpoint > addr
38  if (!len) {
39  i = rz_vector_len(skyline);
40  } else {
41  rz_vector_lower_bound(skyline, addr, i, CMP);
42  if (i == rz_vector_len(skyline) && wrap) {
43  wrap = false;
44  i = 0;
45  addr = 0;
46  }
47  }
48 #undef CMP
49  while (i < rz_vector_len(skyline)) {
50  const RzSkylineItem *part = rz_vector_index_ptr(skyline, i);
51  // Right endpoint <= addr
52  if (rz_itv_end(part->itv) - 1 < addr) {
53  i++;
54  if (wrap && i == rz_vector_len(skyline)) {
55  wrap = false;
56  i = 0;
57  addr = 0;
58  }
59  continue;
60  }
61  if (addr < part->itv.addr) {
62  // [addr, part->itv.addr) is a gap
63  if (prefix_mode || len <= part->itv.addr - vaddr) {
64  break;
65  }
66  addr = part->itv.addr;
67  }
68  // Now left endpoint <= addr < right endpoint
69  ut64 len1 = RZ_MIN(vaddr + len - addr, rz_itv_end(part->itv) - addr);
70  if (len1 < 1) {
71  break;
72  }
73  RzIOMap *map = part->user;
74  // The map satisfies the permission requirement or p_cache is enabled
75  if (((map->perm & match_flg) == match_flg || io->p_cache)) {
76  st64 result = op(io, map->fd, map->delta + addr - map->itv.addr,
77  buf + (addr - vaddr), len1, map, NULL);
78  if (prefix_mode) {
79  if (result < 0) {
80  return result;
81  }
82  addr += result;
83  if (result != len1) {
84  break;
85  }
86  } else {
87  if (result != len1) {
88  ret = false;
89  }
90  addr += len1;
91  }
92  } else if (prefix_mode) {
93  break;
94  } else {
95  addr += len1;
96  ret = false;
97  }
98  // Reaches the end
99  if (addr == vaddr + len) {
100  break;
101  }
102  // Wrap to the beginning of skyline if address wraps
103  if (!addr) {
104  i = 0;
105  }
106  }
107  return prefix_mode ? addr - vaddr : ret;
108 }
109 
111  return rz_io_init(RZ_NEW0(RzIO));
112 }
113 
116  io->addrbytes = 1;
117  rz_io_desc_init(io);
119  rz_io_map_init(io);
120  rz_io_cache_init(io);
121  rz_io_plugin_init(io);
122  io->event = rz_event_new(io);
123  return io;
124 }
125 
126 RZ_API void rz_io_free(RzIO *io) {
127  if (io) {
128  rz_io_fini(io);
129  free(io);
130  }
131 }
132 
134  ut64 bufSize = rz_buf_size(b);
135  char *uri = rz_str_newf("malloc://%" PFMT64d, bufSize);
136  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
137  if (desc) {
138  const ut8 *tmp = rz_buf_data(b, &bufSize);
139  rz_io_desc_write(desc, tmp, bufSize);
140  }
141  free(uri);
142  return desc;
143 }
144 
145 RZ_API RzIODesc *rz_io_open_nomap(RzIO *io, const char *uri, int perm, int mode) {
146  rz_return_val_if_fail(io && uri, NULL);
147  RzIODesc *desc = rz_io_desc_open(io, uri, perm, mode);
148  if ((io->autofd || !io->desc) && desc) {
149  io->desc = desc;
150  }
151  // set desc as current if autofd or io->desc==NULL
152  return desc;
153 }
154 
155 /* opens a file and maps it to 0x0 */
156 RZ_API RzIODesc *rz_io_open(RzIO *io, const char *uri, int perm, int mode) {
158  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
159  if (!desc) {
160  return NULL;
161  }
162  rz_io_map_new(io, desc->fd, desc->perm, 0LL, 0LL, rz_io_desc_size(desc));
163  return desc;
164 }
165 
177 RZ_API RzIODesc *rz_io_open_at(RzIO *io, const char *uri, int perm, int mode, ut64 at, RZ_NULLABLE RZ_OUT RzIOMap **map) {
178  rz_return_val_if_fail(io && uri, NULL);
179 
180  RzIODesc *desc = rz_io_open_nomap(io, uri, perm, mode);
181  if (!desc) {
182  return NULL;
183  }
185  // second map
186  if (size && ((UT64_MAX - size + 1) < at)) {
187  // split map into 2 maps if only 1 big map results into interger overflow
188  io_map_new(io, desc->fd, desc->perm, UT64_MAX - at + 1, 0LL, size - (UT64_MAX - at) - 1);
189  // someone pls take a look at this confusing stuff
190  size = UT64_MAX - at + 1;
191  }
192  // skyline not updated
193  RzIOMap *m = rz_io_map_new(io, desc->fd, desc->perm, 0LL, at, size);
194  if (map) {
195  *map = m;
196  }
197  return desc;
198 }
199 
200 /* opens many files, without mapping them. This should be discussed */
201 RZ_API RzList *rz_io_open_many(RzIO *io, const char *uri, int perm, int mode) {
202  RzList *desc_list;
203  RzListIter *iter;
204  RzIODesc *desc;
205  rz_return_val_if_fail(io && io->files && uri, NULL);
206  RzIOPlugin *plugin = rz_io_plugin_resolve(io, uri, 1);
207  if (!plugin || !plugin->open_many || !plugin->close) {
208  return NULL;
209  }
210  if (!(desc_list = plugin->open_many(io, uri, perm, mode))) {
211  return NULL;
212  }
213  rz_list_foreach (desc_list, iter, desc) {
214  if (desc) {
215  if (!desc->plugin) {
216  desc->plugin = plugin;
217  }
218  if (!desc->uri) {
219  desc->uri = strdup(uri);
220  }
221  // should autofd be honored here?
222  rz_io_desc_add(io, desc);
223  if (!io->desc) {
224  io->desc = desc;
225  }
226  }
227  }
228  return desc_list;
229 }
230 
231 RZ_API bool rz_io_reopen(RzIO *io, int fd, int perm, int mode) {
232  RzIODesc *old, *new;
233  char *uri;
234  if (!(old = rz_io_desc_get(io, fd))) {
235  return false;
236  }
237  // does this really work, or do we have to handler debuggers ugly
238  uri = old->referer ? old->referer : old->uri;
239 #if __WINDOWS__ // TODO: workaround, see https://github.com/rizinorg/rizin/issues/8840
240  if (old->plugin->close && old->plugin->close(old)) {
241  return false; // TODO: this is an unrecoverable scenario
242  }
243  if (!(new = rz_io_open_nomap(io, uri, perm, mode))) {
244  return false;
245  }
246  rz_io_desc_exchange(io, old->fd, new->fd);
247  rz_io_desc_del(io, old->fd);
248  return true;
249 #else
250  if (!(new = rz_io_open_nomap(io, uri, perm, mode))) {
251  return false;
252  }
253  rz_io_desc_exchange(io, old->fd, new->fd);
254  return rz_io_desc_close(old); // magic
255 #endif // __WINDOWS__
256 }
257 
258 RZ_API int rz_io_close_all(RzIO *io) { // what about undo?
259  if (!io) {
260  return false;
261  }
262  rz_io_desc_fini(io);
263  rz_io_map_reset(io);
264  rz_io_desc_init(io);
265  rz_io_cache_fini(io);
266  return true;
267 }
268 
269 RZ_API int rz_io_pread_at(RzIO *io, ut64 paddr, ut8 *buf, int len) {
270  rz_return_val_if_fail(io && buf && len >= 0, -1);
271  if (io->ff) {
272  memset(buf, io->Oxff, len);
273  }
274  return rz_io_desc_read_at(io->desc, paddr, buf, len);
275 }
276 
277 RZ_API int rz_io_pwrite_at(RzIO *io, ut64 paddr, const ut8 *buf, int len) {
278  rz_return_val_if_fail(io && buf && len > 0, -1);
279  return rz_io_desc_write_at(io->desc, paddr, buf, len);
280 }
281 
282 // Returns true iff all reads on mapped regions are successful and complete.
283 RZ_API bool rz_io_vread_at_mapped(RzIO *io, ut64 vaddr, ut8 *buf, int len) {
284  rz_return_val_if_fail(io && buf && len > 0, false);
285  if (io->ff) {
286  memset(buf, io->Oxff, len);
287  }
288  return on_map_skyline(io, vaddr, buf, len, RZ_PERM_R, fd_read_at_wrap, false);
289 }
290 
291 static bool rz_io_vwrite_at(RzIO *io, ut64 vaddr, const ut8 *buf, int len) {
292  return on_map_skyline(io, vaddr, (ut8 *)buf, len, RZ_PERM_W, fd_write_at_wrap, false);
293 }
294 
295 // Deprecated, use either rz_io_read_at_mapped or rz_io_nread_at instead.
296 // For virtual mode, returns true if all reads on mapped regions are successful
297 // and complete.
298 // For physical mode, the interface is broken because the actual read bytes are
299 // not available. This requires fixes in all call sites.
301  rz_return_val_if_fail(io && buf && len >= 0, false);
302  if (len == 0) {
303  return false;
304  }
305  bool ret = (io->va)
307  : rz_io_pread_at(io, addr, buf, len) > 0;
308  if (io->cached & RZ_PERM_R) {
309  (void)rz_io_cache_read(io, addr, buf, len);
310  }
311  return ret;
312 }
313 
314 // Returns true iff all reads on mapped regions are successful and complete.
315 // Unmapped regions are filled with io->Oxff in both physical and virtual modes.
316 // Use this function if you want to ignore gaps or do not care about the number
317 // of read bytes.
319  bool ret;
320  rz_return_val_if_fail(io && buf, false);
321  if (io->ff) {
322  memset(buf, io->Oxff, len);
323  }
324  if (io->va) {
325  ret = on_map_skyline(io, addr, buf, len, RZ_PERM_R, fd_read_at_wrap, false);
326  } else {
327  ret = rz_io_pread_at(io, addr, buf, len) > 0;
328  }
329  if (io->cached & RZ_PERM_R) {
330  (void)rz_io_cache_read(io, addr, buf, len);
331  }
332  return ret;
333 }
334 
335 // For both virtual and physical mode, returns the number of bytes of read
336 // prefix.
337 // Returns -1 on error.
339  int ret;
340  rz_return_val_if_fail(io && buf && len >= 0, -1);
341  if (len == 0) {
342  return 0;
343  }
344  if (io->va) {
345  if (io->ff) {
346  memset(buf, io->Oxff, len);
347  }
348  ret = on_map_skyline(io, addr, buf, len, RZ_PERM_R, fd_read_at_wrap, true);
349  } else {
350  ret = rz_io_pread_at(io, addr, buf, len);
351  }
352  if (ret > 0 && io->cached & RZ_PERM_R) {
353  (void)rz_io_cache_read(io, addr, buf, len);
354  }
355  return ret;
356 }
357 
358 RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len) {
359  int i;
360  bool ret = false;
361  ut8 *mybuf = (ut8 *)buf;
362  rz_return_val_if_fail(io && buf && len > 0, false);
363  if (io->write_mask) {
364  mybuf = rz_mem_dup((void *)buf, len);
365  for (i = 0; i < len; i++) {
366  // TODO: this needs some love because it is not optimal.
367  mybuf[i] &= io->write_mask[i % io->write_mask_len];
368  }
369  }
370  if (io->cached & RZ_PERM_W) {
371  ret = rz_io_cache_write(io, addr, mybuf, len);
372  } else if (io->va) {
373  ret = rz_io_vwrite_at(io, addr, mybuf, len);
374  } else {
375  ret = rz_io_pwrite_at(io, addr, mybuf, len) > 0;
376  }
377  if (buf != mybuf) {
378  free(mybuf);
379  }
380  return ret;
381 }
382 
383 RZ_API bool rz_io_read(RzIO *io, ut8 *buf, int len) {
384  if (io && rz_io_read_at(io, io->off, buf, len)) {
385  io->off += len;
386  return true;
387  }
388  return false;
389 }
390 
391 RZ_API bool rz_io_write(RzIO *io, const ut8 *buf, int len) {
392  if (io && buf && len > 0 && rz_io_write_at(io, io->off, buf, len)) {
393  io->off += len;
394  return true;
395  }
396  return false;
397 }
398 
400  // TODO: rethink this, maybe not needed
401  return io ? rz_io_desc_size(io->desc) : 0LL;
402 }
403 
405  if (io && io->desc && io->desc->plugin && io->desc->plugin->listener) {
406  return io->desc->plugin->listener(io->desc);
407  }
408  return false;
409 }
410 
411 RZ_API char *rz_io_system(RzIO *io, const char *cmd) {
412  if (io && io->desc && io->desc->plugin && io->desc->plugin->system && RZ_STR_ISNOTEMPTY(cmd)) {
413  return io->desc->plugin->system(io, io->desc, cmd);
414  }
415  return NULL;
416 }
417 
418 RZ_API bool rz_io_resize(RzIO *io, ut64 newsize) {
419  if (io) {
420  RzList *maps = rz_io_map_get_for_fd(io, io->desc->fd);
421  RzIOMap *current_map;
422  RzListIter *iter;
423  ut64 fd_size = rz_io_fd_size(io, io->desc->fd);
424  bool ret = rz_io_desc_resize(io->desc, newsize);
425  if (!ret) {
427  return false;
428  }
429  rz_list_foreach (maps, iter, current_map) {
430  // we just resize map of the same size of its fd
431  if (current_map->itv.size == fd_size) {
432  rz_io_map_resize(io, current_map->id, newsize);
433  }
434  }
436  return true;
437  }
438  return false;
439 }
440 
442  return io ? rz_io_desc_close(io->desc) : false;
443 }
444 
454 #define IO_EXTEND_BLOCK_SZ 256
455  rz_return_val_if_fail(io, false);
456 
457  if (!io->desc || !io->desc->plugin) {
458  return false;
459  }
460  if (size == 0) {
461  return true;
462  }
463 
464  if ((io->desc->perm & RZ_PERM_RW) != RZ_PERM_RW) {
465  return false;
466  }
467  ut64 cur_size = rz_io_desc_size(io->desc);
468  if (addr > cur_size) {
469  return false;
470  }
471 
472  // Extend the file to include the additional <size> bytes
473  if (UT64_ADD_OVFCHK(cur_size, size)) {
474  return false;
475  }
476  if (!rz_io_resize(io, cur_size + size)) {
477  return false;
478  }
479 
480  // Shift old data to make space for the zero bytes
481  ut64 tmp = cur_size >= IO_EXTEND_BLOCK_SZ ? RZ_MAX(cur_size - IO_EXTEND_BLOCK_SZ, addr) : addr;
482  ut64 remaining = cur_size - addr;
483 
485  if (!buffer) {
486  return false;
487  }
488  while (remaining) {
490  rz_io_pwrite_at(io, tmp + size, buffer, sz);
491 
493  remaining = remaining > sz ? remaining - sz : 0;
494  }
495  free(buffer);
496 
497  // Put the zero bytes at the right place
498  ut8 *empty = RZ_NEWS0(ut8, size);
499  if (!empty) {
500  return false;
501  }
502  rz_io_pwrite_at(io, addr, empty, size);
503  free(empty);
504 
505  return true;
506 #undef IO_EXTEND_BLOCK_SZ
507 }
508 
517 RZ_API bool rz_io_set_write_mask(RzIO *io, const ut8 *mask, int len) {
518  rz_return_val_if_fail(io, false);
519  rz_return_val_if_fail(mask || len == 0, false);
520 
521  free(io->write_mask);
522  if (!mask) {
523  io->write_mask = NULL;
524  io->write_mask_len = 0;
525  return true;
526  }
527  io->write_mask = (ut8 *)malloc(len);
528  memcpy(io->write_mask, mask, len);
529  io->write_mask_len = len;
530  return true;
531 }
532 
534  RzIOMap *map = rz_io_map_get_paddr(io, pa);
535  if (map) {
536  return pa - map->delta + map->itv.addr;
537  }
538  return UT64_MAX;
539 }
540 
542  RzIOMap *map = rz_io_map_get(io, va);
543  if (map) {
544  st64 delta = va - map->itv.addr;
545  return map->delta + delta;
546  }
547  return UT64_MAX;
548 }
549 
550 RZ_API void rz_io_bind(RzIO *io, RzIOBind *bnd) {
551  rz_return_if_fail(io && bnd);
552 
553  bnd->io = io;
554  bnd->init = true;
556  bnd->desc_use = rz_io_use_fd;
557  bnd->desc_get = rz_io_desc_get;
558  bnd->desc_size = rz_io_desc_size;
559  bnd->p2v = rz_io_p2v;
560  bnd->v2p = rz_io_v2p;
561  bnd->open = rz_io_open_nomap;
562  bnd->open_at = rz_io_open_at;
563  bnd->close = rz_io_fd_close;
564  bnd->read_at = rz_io_read_at;
565  bnd->write_at = rz_io_write_at;
566  bnd->system = rz_io_system;
567  bnd->fd_open = rz_io_fd_open;
568  bnd->fd_close = rz_io_fd_close;
569  bnd->fd_seek = rz_io_fd_seek;
570  bnd->fd_size = rz_io_fd_size;
571  bnd->fd_resize = rz_io_fd_resize;
572  bnd->fd_read = rz_io_fd_read;
573  bnd->fd_write = rz_io_fd_write;
576  bnd->fd_is_dbg = rz_io_fd_is_dbg;
582  bnd->map_get = rz_io_map_get;
585  bnd->map_add = rz_io_map_add;
586 #if HAVE_PTRACE
587  bnd->ptrace = rz_io_ptrace;
588  bnd->ptrace_func = rz_io_ptrace_func;
589 #endif
590 #if __WINDOWS__
591  bnd->get_w32dbg_wrap = rz_io_get_w32dbg_wrap;
592 #endif
593 }
594 
595 /* moves bytes up (+) or down (-) within the specified range */
597  ut8 *buf;
598  ut64 chunksize = 0x10000;
599  ut64 saved_off = io->off;
600  ut64 src, shiftsize = rz_num_abs(move);
601  if (!shiftsize || (end - start) <= shiftsize) {
602  return false;
603  }
604  ut64 rest = (end - start) - shiftsize;
605  if (!(buf = calloc(1, chunksize + 1))) {
606  return false;
607  }
608  if (move > 0) {
609  src = end - shiftsize;
610  } else {
611  src = start + shiftsize;
612  }
613  while (rest > 0) {
614  if (chunksize > rest) {
615  chunksize = rest;
616  }
617  if (move > 0) {
618  src -= chunksize;
619  }
621  rz_io_write_at(io, src + move, buf, chunksize);
622  if (move < 0) {
623  src += chunksize;
624  }
625  rest -= chunksize;
626  }
627  free(buf);
628  io->off = rz_io_desc_seek(io->desc, saved_off, RZ_IO_SEEK_SET);
629  return true;
630 }
631 
632 RZ_API ut64 rz_io_seek(RzIO *io, ut64 offset, int whence) {
633  if (!io) {
634  return 0LL;
635  }
636  switch (whence) {
637  case RZ_IO_SEEK_SET:
638  io->off = offset;
639  break;
640  case RZ_IO_SEEK_CUR:
641  io->off += offset;
642  break;
643  case RZ_IO_SEEK_END:
644  default:
645  io->off = rz_io_desc_seek(io->desc, offset, whence);
646  break;
647  }
648  return io->off;
649 }
650 
651 #if HAVE_PTRACE
652 
653 #if USE_PTRACE_WRAP
654 #include <ptrace_wrap.h>
655 #include <errno.h>
656 
657 static ptrace_wrap_instance *io_ptrace_wrap_instance(RzIO *io) {
658  if (!io->ptrace_wrap) {
659  io->ptrace_wrap = RZ_NEW(ptrace_wrap_instance);
660  if (!io->ptrace_wrap) {
661  return NULL;
662  }
663  if (ptrace_wrap_instance_start(io->ptrace_wrap) < 0) {
664  RZ_FREE(io->ptrace_wrap);
665  return NULL;
666  }
667  }
668  return io->ptrace_wrap;
669 }
670 #endif
671 
672 RZ_API long rz_io_ptrace(RzIO *io, rz_ptrace_request_t request, pid_t pid, void *addr, rz_ptrace_data_t data) {
673 #if USE_PTRACE_WRAP
674  ptrace_wrap_instance *wrap = io_ptrace_wrap_instance(io);
675  if (!wrap) {
676  errno = 0;
677  return -1;
678  }
679  return ptrace_wrap(wrap, request, pid, addr, data);
680 #else
681  return ptrace(request, pid, addr, data);
682 #endif
683 }
684 
685 RZ_API pid_t rz_io_ptrace_fork(RzIO *io, void (*child_callback)(void *), void *child_callback_user) {
686 #if USE_PTRACE_WRAP
687  ptrace_wrap_instance *wrap = io_ptrace_wrap_instance(io);
688  if (!wrap) {
689  errno = 0;
690  return -1;
691  }
692  return ptrace_wrap_fork(wrap, child_callback, child_callback_user);
693 #else
694  pid_t r = rz_sys_fork();
695  if (r == 0) {
696  child_callback(child_callback_user);
697  }
698  return r;
699 #endif
700 }
701 
702 RZ_API void *rz_io_ptrace_func(RzIO *io, void *(*func)(void *), void *user) {
703 #if USE_PTRACE_WRAP
704  ptrace_wrap_instance *wrap = io_ptrace_wrap_instance(io);
705  if (wrap) {
706  return ptrace_wrap_func(wrap, func, user);
707  }
708 #endif
709  return func(user);
710 }
711 #endif
712 
713 #if __WINDOWS__
715 RZ_API struct w32dbg_wrap_instance_t *rz_io_get_w32dbg_wrap(RzIO *io) {
716  if (!io->priv_w32dbg_wrap) {
717  io->priv_w32dbg_wrap = (struct w32dbg_wrap_instance_t *)w32dbg_wrap_new();
718  }
719  return io->priv_w32dbg_wrap;
720 }
721 #endif
722 
723 // remove all descs and maps
725  if (!io) {
726  return false;
727  }
729  rz_io_desc_fini(io);
730  rz_io_map_fini(io);
731  rz_list_free(io->plugins);
732  rz_io_cache_fini(io);
733  if (io->runprofile) {
734  RZ_FREE(io->runprofile);
735  }
736  rz_event_free(io->event);
737  free(io->envprofile);
738 #if RZ_IO_USE_PTRACE_WRAP
739  if (io->ptrace_wrap) {
740  ptrace_wrap_instance_stop(io->ptrace_wrap);
741  free(io->ptrace_wrap);
742  }
743 #endif
744 #if __WINDOWS__
745  w32dbg_wrap_free((W32DbgWInst *)io->priv_w32dbg_wrap);
746 #endif
747  return true;
748 }
size_t len
Definition: 6502dis.c:15
ut8 op
Definition: 6502dis.c:13
#define mask()
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
const char * desc
Definition: bin_vsf.c:19
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
static static sync static getppid static getegid const char static filename request
Definition: sflib.h:62
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_io_pwrite_at(RzIO *io, ut64 paddr, const ut8 *buf, int len)
Definition: io.c:277
RZ_API char * rz_io_system(RzIO *io, const char *cmd)
Definition: io.c:411
RZ_API RzIODesc * rz_io_open_nomap(RzIO *io, const char *uri, int perm, int mode)
Definition: io.c:145
RZ_API ut64 rz_io_seek(RzIO *io, ut64 offset, int whence)
Definition: io.c:632
RZ_API bool rz_io_read_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:300
RZ_API RzList * rz_io_open_many(RzIO *io, const char *uri, int perm, int mode)
Definition: io.c:201
#define IO_EXTEND_BLOCK_SZ
RZ_API bool rz_io_extend_at(RzIO *io, ut64 addr, ut64 size)
Extend the RzIODesc at addr by inserting size 0 bytes.
Definition: io.c:453
RZ_API bool rz_io_shift(RzIO *io, ut64 start, ut64 end, st64 move)
Definition: io.c:596
RZ_API bool rz_io_resize(RzIO *io, ut64 newsize)
Definition: io.c:418
RZ_API bool rz_io_vread_at_mapped(RzIO *io, ut64 vaddr, ut8 *buf, int len)
Definition: io.c:283
RZ_API void rz_io_free(RzIO *io)
Definition: io.c:126
#define CMP(addr, part)
RZ_API bool rz_io_set_write_mask(RzIO *io, const ut8 *mask, int len)
Set a mask that is used on all following write operations.
Definition: io.c:517
RZ_LIB_VERSION(rz_io)
RZ_API ut64 rz_io_size(RzIO *io)
Definition: io.c:399
RZ_API ut64 rz_io_v2p(RzIO *io, ut64 va)
Definition: io.c:541
static int fd_read_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
Definition: io.c:18
RZ_API RzIODesc * rz_io_open_buffer(RzIO *io, RzBuffer *b, int perm, int mode)
Definition: io.c:133
RZ_API bool rz_io_reopen(RzIO *io, int fd, int perm, int mode)
Definition: io.c:231
RZ_API RzIODesc * rz_io_open_at(RzIO *io, const char *uri, int perm, int mode, ut64 at, RZ_NULLABLE RZ_OUT RzIOMap **map)
Open a file and directly map it at the given offset.
Definition: io.c:177
static int fd_write_at_wrap(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
Definition: io.c:22
RZ_API void rz_io_bind(RzIO *io, RzIOBind *bnd)
Definition: io.c:550
RZ_API bool rz_io_is_listener(RzIO *io)
Definition: io.c:404
RZ_API int rz_io_close_all(RzIO *io)
Definition: io.c:258
static bool rz_io_vwrite_at(RzIO *io, ut64 vaddr, const ut8 *buf, int len)
Definition: io.c:291
RZ_API bool rz_io_read_at_mapped(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:318
RZ_API ut64 rz_io_p2v(RzIO *io, ut64 pa)
Definition: io.c:533
RZ_API int rz_io_fini(RzIO *io)
Definition: io.c:724
int(* cbOnIterMap)(RzIO *io, int fd, ut64 addr, ut8 *buf, int len, RzIOMap *map, void *user)
Definition: io.c:26
RZ_API bool rz_io_close(RzIO *io)
Definition: io.c:441
RZ_API bool rz_io_read(RzIO *io, ut8 *buf, int len)
Definition: io.c:383
RZ_API RzIO * rz_io_init(RzIO *io)
Definition: io.c:114
RZ_API RzIODesc * rz_io_open(RzIO *io, const char *uri, int perm, int mode)
Definition: io.c:156
RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io.c:358
RZ_API int rz_io_pread_at(RzIO *io, ut64 paddr, ut8 *buf, int len)
Definition: io.c:269
RZ_API int rz_io_nread_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:338
RZ_API bool rz_io_write(RzIO *io, const ut8 *buf, int len)
Definition: io.c:391
RZ_API RzIO * rz_io_new(void)
Definition: io.c:110
static st64 on_map_skyline(RzIO *io, ut64 vaddr, ut8 *buf, int len, int match_flg, cbOnIterMap op, bool prefix_mode)
Definition: io.c:30
RzIOMap * io_map_new(RzIO *io, int fd, int perm, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:24
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid ptrace
Definition: sflib.h:57
#define chunksize
void * ptrace_wrap_func(ptrace_wrap_instance *inst, ptrace_wrap_func_func func, void *user)
Definition: ptrace_wrap.c:130
void ptrace_wrap_instance_stop(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:36
pid_t ptrace_wrap_fork(ptrace_wrap_instance *inst, void(*child_callback)(void *), void *child_callback_user)
Definition: ptrace_wrap.c:108
long ptrace_wrap(ptrace_wrap_instance *inst, ptrace_wrap_ptrace_request request, pid_t pid, void *addr, void *data)
Definition: ptrace_wrap.c:90
int ptrace_wrap_instance_start(ptrace_wrap_instance *inst)
Definition: ptrace_wrap.c:14
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
RZ_API void rz_event_free(RzEvent *ev)
Definition: event.c:37
RZ_API RzEvent * rz_event_new(void *user)
Definition: event.c:17
RZ_API bool rz_io_plugin_init(RzIO *io)
Definition: io_plugin.c:32
RZ_API bool rz_io_fd_resize(RzIO *io, int fd, ut64 newsize)
Definition: io_fd.c:58
RZ_API bool rz_io_fd_is_dbg(RzIO *io, int fd)
Definition: io_fd.c:86
RZ_API ut64 rz_io_fd_size(RzIO *io, int fd)
Definition: io_fd.c:42
RZ_API void rz_io_cache_init(RzIO *io)
Definition: io_cache.c:21
RZ_API const char * rz_io_fd_get_name(RzIO *io, int fd)
Definition: io_fd.c:112
RZ_IPI bool rz_io_desc_init(RzIO *io)
Definition: io_desc.c:368
RZ_API bool rz_io_addr_is_mapped(RzIO *io, ut64 vaddr)
Definition: ioutils.c:11
RZ_API void rz_io_map_init(RzIO *io)
Definition: io_map.c:101
RZ_API bool rz_io_cache_read(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io_cache.c:131
#define RZ_IO_SEEK_CUR
Definition: rz_io.h:16
RZ_API int rz_io_desc_write(RzIODesc *desc, const ut8 *buf, int count)
Definition: io_desc.c:183
RZ_API ut64 rz_io_desc_seek(RzIODesc *desc, ut64 offset, int whence)
Definition: io_desc.c:217
RZ_API int rz_io_fd_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_fd.c:6
RZ_API bool rz_io_desc_add(RzIO *io, RzIODesc *desc)
Definition: io_desc.c:49
RZ_API bool rz_io_map_remap_fd(RzIO *io, int fd, ut64 addr)
Definition: io_map.c:69
RZ_API bool rz_io_cache_write(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io_cache.c:93
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API bool rz_io_desc_exchange(RzIO *io, int fd, int fdx)
Definition: io_desc.c:275
RZ_API ut8 * rz_io_fd_get_buf(RzIO *io, int fd, RZ_OUT RZ_NONNULL ut64 *size)
Returns the underlying buffer of the file descriptor.
Definition: io_fd.c:53
RZ_API int rz_io_fd_read(RzIO *io, int fd, ut8 *buf, int len)
Definition: io_fd.c:16
RZ_API bool rz_io_desc_resize(RzIODesc *desc, ut64 newsize)
Definition: io_desc.c:250
RZ_API int rz_io_desc_read_at(RzIODesc *desc, ut64 addr, ut8 *buf, int len)
Definition: io_desc.c:351
RZ_API int rz_io_fd_read_at(RzIO *io, int fd, ut64 addr, ut8 *buf, int len)
Definition: io_fd.c:71
RZ_API RzIOMap * rz_io_map_get_paddr(RzIO *io, ut64 paddr)
Definition: io_map.c:163
RZ_API void rz_io_map_reset(RzIO *io)
Definition: io_map.c:186
RZ_API RzIOMap * rz_io_map_get(RzIO *io, ut64 addr)
Definition: io_map.c:176
RZ_API RzIODesc * rz_io_desc_get(RzIO *io, int fd)
Definition: io_desc.c:73
RZ_API RzList * rz_io_map_get_for_fd(RzIO *io, int fd)
Definition: io_map.c:388
RZ_API void rz_io_cache_fini(RzIO *io)
Definition: io_cache.c:28
RZ_API int rz_io_desc_write_at(RzIODesc *desc, ut64 addr, const ut8 *buf, int len)
Definition: io_desc.c:358
RZ_API bool rz_io_map_resize(RzIO *io, ut32 id, ut64 newsize)
Definition: io_map.c:403
RZ_IPI bool rz_io_desc_fini(RzIO *io)
Definition: io_desc.c:390
RZ_API int rz_io_fd_write_at(RzIO *io, int fd, ut64 addr, const ut8 *buf, int len)
Definition: io_fd.c:80
RZ_API bool rz_io_desc_del(RzIO *io, int fd)
Definition: io_desc.c:61
RZ_API bool rz_io_is_valid_offset(RzIO *io, ut64 offset, int hasperm)
Definition: ioutils.c:20
RZ_API int rz_io_fd_write(RzIO *io, int fd, const ut8 *buf, int len)
Definition: io_fd.c:26
RZ_API int rz_io_fd_get_current(RzIO *io)
Definition: io_fd.c:135
#define RZ_IO_SEEK_SET
Definition: rz_io.h:15
RZ_API ut64 rz_io_fd_seek(RzIO *io, int fd, ut64 addr, int whence)
Definition: io_fd.c:35
RZ_API void rz_io_desc_cache_fini_all(RzIO *io)
Definition: p_cache.c:362
RZ_API bool rz_io_use_fd(RzIO *io, int fd)
Definition: io_fd.c:118
RZ_API RzIOMap * rz_io_map_new(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:50
RZ_API RzIOMap * rz_io_map_add(RzIO *io, int fd, int flags, ut64 delta, ut64 addr, ut64 size)
Definition: io_map.c:151
RZ_API void rz_io_map_fini(RzIO *io)
Definition: io_map.c:318
RZ_API bool rz_io_fd_close(RzIO *io, int fd)
Definition: io_fd.c:11
RZ_API RzIOPlugin * rz_io_plugin_resolve(RzIO *io, const char *filename, bool many)
Definition: io_plugin.c:54
#define RZ_IO_SEEK_END
Definition: rz_io.h:17
RZ_API bool rz_io_desc_close(RzIODesc *desc)
Definition: io_desc.c:165
RZ_API RzIODesc * rz_io_desc_open(RzIO *io, const char *uri, int flags, int mode)
Definition: io_desc.c:112
static ut64 rz_itv_end(RzInterval itv)
Definition: rz_itv.h:42
RZ_API void * rz_mem_dup(const void *s, int l)
Definition: mem.c:319
static st64 rz_num_abs(st64 num)
Definition: rz_num.h:108
static void rz_skyline_init(RzSkyline *skyline)
Definition: rz_skyline.h:19
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_sys_fork(void)
Definition: sys.c:1679
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
#define PFMT64d
Definition: rz_types.h:394
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_PERM_RW
Definition: rz_types.h:96
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_PERM_W
Definition: rz_types.h:94
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_MIN(x, y)
#define st64
Definition: rz_types_base.h:10
#define RZ_MAX(x, y)
#define UT64_MAX
Definition: rz_types_base.h:86
#define UT64_ADD_OVFCHK(x, y)
#define rz_vector_lower_bound(vec, x, i, cmp)
Definition: rz_vector.h:190
static void * rz_vector_index_ptr(RzVector *vec, size_t index)
Definition: rz_vector.h:88
static size_t rz_vector_len(const RzVector *vec)
Definition: rz_vector.h:82
static int
Definition: sfsocketcall.h:114
int pid_t
Definition: sftypes.h:38
#define b(i)
Definition: sha256.c:42
Definition: buffer.h:15
ut64 addr
Definition: rz_itv.h:15
ut64 size
Definition: rz_itv.h:16
RzIOFdResize fd_resize
Definition: rz_io.h:247
RzIOOpen open
Definition: rz_io.h:237
RzIOReadAt read_at
Definition: rz_io.h:240
RzIOIsValidOff is_valid_offset
Definition: rz_io.h:257
RzIOFdOpen fd_open
Definition: rz_io.h:243
RzIOClose close
Definition: rz_io.h:239
RzIOV2P v2p
Definition: rz_io.h:262
RzIOMapGet map_get
Definition: rz_io.h:259
RzIOP2V p2v
Definition: rz_io.h:263
RzIODescSize desc_size
Definition: rz_io.h:236
RzIOAddrIsMapped addr_is_mapped
Definition: rz_io.h:258
RzIOSystem system
Definition: rz_io.h:242
RzIOMapAdd map_add
Definition: rz_io.h:261
RzIODescUse desc_use
Definition: rz_io.h:234
RzIOOpenAt open_at
Definition: rz_io.h:238
RzIOGetCurrentFd fd_get_current
Definition: rz_io.h:233
RzIOFdSeek fd_seek
Definition: rz_io.h:245
RzIOWriteAt write_at
Definition: rz_io.h:241
RzIOFdGetBuf fd_getbuf
Definition: rz_io.h:256
RzIO * io
Definition: rz_io.h:232
RzIOFdWrite fd_write
Definition: rz_io.h:249
RzIOFdGetMap fd_get_map
Definition: rz_io.h:254
RzIOFdIsDbg fd_is_dbg
Definition: rz_io.h:252
RzIOFdReadAt fd_read_at
Definition: rz_io.h:250
RzIOFdClose fd_close
Definition: rz_io.h:244
RzIOFdGetName fd_get_name
Definition: rz_io.h:253
RzIOFdRemap fd_remap
Definition: rz_io.h:255
RzIOFdWriteAt fd_write_at
Definition: rz_io.h:251
RzIOFdSize fd_size
Definition: rz_io.h:246
RzIOFdRead fd_read
Definition: rz_io.h:248
RzIOMapGetPaddr map_get_paddr
Definition: rz_io.h:260
int init
Definition: rz_io.h:231
RzIODescGet desc_get
Definition: rz_io.h:235
int fd
Definition: rz_io.h:96
char * uri
Definition: rz_io.h:98
struct rz_io_plugin_t * plugin
Definition: rz_io.h:103
int perm
Definition: rz_io.h:97
char * referer
Definition: rz_io.h:100
ut32 id
Definition: rz_io.h:148
RzInterval itv
Definition: rz_io.h:149
int(* listener)(RzIODesc *io)
Definition: rz_io.h:122
int(* close)(RzIODesc *desc)
Definition: rz_io.h:132
char *(* system)(RzIO *io, RzIODesc *fd, const char *)
Definition: rz_io.h:126
RzList *(* open_many)(RzIO *io, const char *, int perm, int mode)
Definition: rz_io.h:128
Definition: rz_io.h:59
int ff
Definition: rz_io.h:64
int Oxff
Definition: rz_io.h:65
int autofd
Definition: rz_io.h:68
char * runprofile
Definition: rz_io.h:81
ut8 * write_mask
Definition: rz_io.h:78
RzEvent * event
Definition: rz_io.h:90
int cached
Definition: rz_io.h:69
char * envprofile
Definition: rz_io.h:82
ut64 off
Definition: rz_io.h:61
RzIDStorage * files
Definition: rz_io.h:75
size_t addrbytes
Definition: rz_io.h:66
int write_mask_len
Definition: rz_io.h:79
RzSkyline map_skyline
Definition: rz_io.h:74
struct rz_io_desc_t * desc
Definition: rz_io.h:60
int p_cache
Definition: rz_io.h:71
int va
Definition: rz_io.h:63
RzList * plugins
Definition: rz_io.h:80
RzInterval itv
Definition: rz_skyline.h:8
RzVector v
Definition: rz_skyline.h:13
Definition: dis.c:32
static st64 delta
Definition: vmenus.c:2425
W32DbgWInst * w32dbg_wrap_new(void)
Definition: w32dbg_wrap.c:48
void w32dbg_wrap_free(W32DbgWInst *inst)
Definition: w32dbg_wrap.c:58
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int addr
Definition: z80asm.c:58