Rizin
unix-like reverse engineering framework and cli tools
fuzzy-all.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 /* implementation */
5 
6 static int iscallret(RzDebug *dbg, ut64 addr) {
7  ut8 buf[32];
8  if (addr == 0LL || addr == UT64_MAX)
9  return 0;
10  /* check if region is executable */
11  /* check if previous instruction is a call */
12  /* if x86 expect CALL to be 5 byte length */
13  if (dbg->arch && !strcmp(dbg->arch, "x86")) {
14  (void)dbg->iob.read_at(dbg->iob.io, addr - 5, buf, 5);
15  if (buf[0] == 0xe8) {
16  return 1;
17  }
18  if (buf[3] == 0xff /* bits 4-5 (from right) of next byte must be 01 */
19  && ((buf[4] & 0xf0) == 0xd0 /* Mod is 11 */
20  || ((buf[4] & 0xf0) == 0x10 /* Mod is 00 */
21  && (buf[4] & 0x06) != 0x04))) { /* R/M not 10x */
22  return 1;
23  }
24  // IMMAMISSINGANYOP
25  } else {
27  (void)dbg->iob.read_at(dbg->iob.io, addr - 8, buf, 8);
30  return 1;
31  }
32  /* delay slot */
35  return 1;
36  }
37  }
38  return 0;
39 }
40 
42  ut8 *stack, *ptr;
43  int wordsize = dbg->bits; // XXX, dbg->bits is wordsize not bits
44  ut64 sp;
45  RzIOBind *bio = &dbg->iob;
46  int i, stacksize;
47  ut64 *p64, addr = 0LL;
48  ut32 *p32;
49  ut16 *p16;
50  ut64 cursp, oldsp;
51  RzList *list;
52 
53  stacksize = 1024 * 512; // 512KB .. should get the size from the regions if possible
54  stack = malloc(stacksize);
55  if (at == UT64_MAX) {
56  RzRegItem *ri;
57  RzReg *reg = dbg->reg;
58  const char *spname = rz_reg_get_name(reg, RZ_REG_NAME_SP);
59  if (!spname) {
60  eprintf("Cannot find stack pointer register\n");
61  free(stack);
62  return NULL;
63  }
64  ri = rz_reg_get(reg, spname, RZ_REG_TYPE_GPR);
65  if (!ri) {
66  eprintf("Cannot find stack pointer register\n");
67  free(stack);
68  return NULL;
69  }
70  sp = rz_reg_get_value(reg, ri);
71  } else {
72  sp = at;
73  }
74 
75  list = rz_list_new();
76  list->free = free;
77  cursp = oldsp = sp;
78  (void)bio->read_at(bio->io, sp, stack, stacksize);
79  ptr = stack;
80  for (i = 0; i < dbg->btdepth; i++) {
81  p64 = (ut64 *)ptr;
82  p32 = (ut32 *)ptr;
83  p16 = (ut16 *)ptr;
84  switch (wordsize) {
85  case 8: addr = *p64; break;
86  case 4: addr = *p32; break;
87  case 2: addr = *p16; break;
88  default:
89  eprintf("Invalid word size with asm.bits\n");
91  return NULL;
92  }
93  if (iscallret(dbg, addr)) {
95  frame->addr = addr;
96  frame->size = cursp - oldsp;
97  frame->sp = cursp;
98  frame->bp = oldsp; // addr + (i * wordsize); // -4 || -8
99  // eprintf ("--------------> 0x%llx (%d)\n", addr, frame->size);
100  rz_list_append(list, frame);
101  oldsp = cursp;
102  }
103  ptr += wordsize;
104  cursp += wordsize;
105  }
106  return list;
107 }
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
uint32_t ut32
RzDebug * dbg
Definition: desil.c:30
static RzList * backtrace_fuzzy(RzDebug *dbg, ut64 at)
Definition: fuzzy-all.c:41
static int iscallret(RzDebug *dbg, ut64 addr)
Definition: fuzzy-all.c:6
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
#define reg(n)
uint8_t ut8
Definition: lh5801.h:11
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
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
RZ_API int rz_analysis_op(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len, RzAnalysisOpMask mask)
Definition: op.c:96
RZ_API RzRegItem * rz_reg_get(RzReg *reg, const char *name, int type)
Definition: reg.c:344
RZ_API const char * rz_reg_get_name(RzReg *reg, int role)
Definition: reg.c:147
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API ut64 rz_reg_get_value(RzReg *reg, RzRegItem *item)
Definition: rvalue.c:114
@ RZ_ANALYSIS_OP_MASK_BASIC
Definition: rz_analysis.h:440
@ RZ_ANALYSIS_OP_TYPE_CALL
Definition: rz_analysis.h:378
@ RZ_ANALYSIS_OP_TYPE_UCALL
Definition: rz_analysis.h:379
@ RZ_REG_TYPE_GPR
Definition: rz_reg.h:21
@ RZ_REG_NAME_SP
Definition: rz_reg.h:44
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define UT64_MAX
Definition: rz_types_base.h:86
int btdepth
Definition: rz_debug.h:259
RzAnalysis * analysis
Definition: rz_debug.h:305
char * arch
Definition: rz_debug.h:242
RzReg * reg
Definition: rz_debug.h:286
int bits
Definition: rz_debug.h:243
RzIOBind iob
Definition: rz_debug.h:293
RzIOReadAt read_at
Definition: rz_io.h:240
RzIO * io
Definition: rz_io.h:232
Definition: z80asm.h:140
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int sp
Definition: z80asm.c:91
static int addr
Definition: z80asm.c:58
static struct stack stack[MAX_INCLUDE]
Definition: z80asm.c:92