Rizin
unix-like reverse engineering framework and cli tools
mem.c File Reference

Go to the source code of this file.

Macros

#define KEY_LEN_MAX   64
 
#define return_val_if_key_len_wrong(mem, key, ret)
 

Functions

RZ_API RzILMemrz_il_mem_new (RzBuffer *buf, ut32 key_len)
 
RZ_API void rz_il_mem_free (RzILMem *mem)
 
RZ_API ut32 rz_il_mem_key_len (RzILMem *mem)
 Get the bit-size of a key (address) into the memory. More...
 
RZ_API ut32 rz_il_mem_value_len (RzILMem *mem)
 Get the bit-size of a value in the memory. More...
 
RZ_API RzBitVectorrz_il_mem_load (RzILMem *mem, RzBitVector *key)
 
RZ_API bool rz_il_mem_store (RzILMem *mem, RzBitVector *key, RzBitVector *value)
 
static RzBitVectorread_n_bits (RzBuffer *buf, ut32 n_bits, RzBitVector *key, bool big_endian)
 
static bool write_n_bits (RzBuffer *buf, RzBitVector *key, RzBitVector *value, bool big_endian)
 
RZ_API RzBitVectorrz_il_mem_loadw (RzILMem *mem, RzBitVector *key, ut32 n_bits, bool big_endian)
 
RZ_API bool rz_il_mem_storew (RzILMem *mem, RzBitVector *key, RzBitVector *value, bool big_endian)
 

Macro Definition Documentation

◆ KEY_LEN_MAX

#define KEY_LEN_MAX   64

Definition at line 8 of file mem.c.

◆ return_val_if_key_len_wrong

#define return_val_if_key_len_wrong (   mem,
  key,
  ret 
)
Value:
do { \
RZ_LOG_ERROR("RzIL: Memory key size mismatch (expected size = %u, but got %u)\n", \
(unsigned int)rz_il_mem_key_len(mem), (unsigned int)rz_bv_len(key)); \
return ret; \
} \
} while (0);
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 key
Definition: sflib.h:118
RZ_API ut32 rz_il_mem_key_len(RzILMem *mem)
Get the bit-size of a key (address) into the memory.
Definition: mem.c:49
void * mem
Definition: libc.cpp:91
RZ_API ut32 rz_bv_len(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1140

Definition at line 64 of file mem.c.

Function Documentation

◆ read_n_bits()

static RzBitVector* read_n_bits ( RzBuffer buf,
ut32  n_bits,
RzBitVector key,
bool  big_endian 
)
static

Definition at line 105 of file mem.c.

105  {
106  RzBitVector *value = rz_bv_new_zero(n_bits);
107  if (!value) {
109  return NULL;
110  }
111 
112  ut64 address = rz_bv_to_ut64(key);
113  ut32 n_bytes = rz_bv_len_bytes(value);
114 
115  ut8 *data = calloc(n_bytes, 1);
116  if (!data) {
117  return value;
118  }
119 
120  // we ignore bad reads. RzBuffer fills up with its "overflow byte" on failure.
121  rz_buf_read_at(buf, address, data, n_bytes);
122  if (big_endian) {
123  rz_bv_set_from_bytes_be(value, data, 0, n_bits);
124  } else {
125  rz_bv_set_from_bytes_le(value, data, 0, n_bits);
126  }
127  free(data);
128  return value;
129 }
static int value
Definition: cmd_api.c:93
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define rz_warn_if_reached()
Definition: rz_assert.h:29
RZ_API void rz_bv_set_from_bytes_le(RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Definition: bitvector.c:1265
RZ_API ut64 rz_bv_to_ut64(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1454
RZ_API void rz_bv_set_from_bytes_be(RZ_NONNULL RzBitVector *bv, RZ_IN RZ_NONNULL const ut8 *buf, ut32 bit_offset, ut32 size)
Set the bitvector's contents from the given bits. The bitvector's size is unchanged....
Definition: bitvector.c:1296
RZ_API ut32 rz_bv_len_bytes(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1150
#define rz_bv_new_zero(l)
Definition: rz_bitvector.h:105
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
structure for bitvector
Definition: rz_bitvector.h:19
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References calloc(), free(), key, NULL, rz_buf_read_at(), rz_bv_len_bytes(), rz_bv_new_zero, rz_bv_set_from_bytes_be(), rz_bv_set_from_bytes_le(), rz_bv_to_ut64(), rz_warn_if_reached, ut64(), and value.

Referenced by rz_il_mem_loadw().

◆ rz_il_mem_free()

RZ_API void rz_il_mem_free ( RzILMem mem)

Free a Mem

Parameters
memmemory to be free

Definition at line 34 of file mem.c.

34  {
35  if (!mem) {
36  return;
37  }
38  rz_buf_free(mem->buf);
39  free(mem);
40 }
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253

References free(), mem, and rz_buf_free().

Referenced by rz_il_vm_init().

◆ rz_il_mem_key_len()

RZ_API ut32 rz_il_mem_key_len ( RzILMem mem)

Get the bit-size of a key (address) into the memory.

For all k, rz_bv_len(rz_il_mem_load(mem, k)) == rz_il_mem_value_len(mem). So this could be seen as the size of a byte. Because we only support RzBuffer-based mems at the moment, this is always 8, but more options may be available in the future.

Definition at line 49 of file mem.c.

49  {
50  return mem->key_len;
51 }

References mem.

Referenced by rz_il_validate_global_context_new_from_vm().

◆ rz_il_mem_load()

RZ_API RzBitVector* rz_il_mem_load ( RzILMem mem,
RzBitVector key 
)

Load a single memory value (bitvector) from current address (bitvector)

Parameters
memMemory
keyaddress (bitvector)
Returns
data (bitvector)

Definition at line 79 of file mem.c.

79  {
82  ut8 v = 0;
83  rz_buf_read_at(mem->buf, rz_bv_to_ut64(key), &v, 1);
85 }
const char * v
Definition: dsignal.c:12
#define return_val_if_key_len_wrong(mem, key, ret)
Definition: mem.c:64
RZ_API ut32 rz_il_mem_value_len(RzILMem *mem)
Get the bit-size of a value in the memory.
Definition: mem.c:60
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_ut64(ut32 length, ut64 value)
Definition: bitvector.c:1161

References key, mem, NULL, return_val_if_key_len_wrong, rz_buf_read_at(), rz_bv_new_from_ut64(), rz_bv_to_ut64(), rz_il_mem_value_len(), rz_return_val_if_fail, and v.

Referenced by rz_il_vm_mem_load(), and rz_il_vm_mem_store().

◆ rz_il_mem_loadw()

RZ_API RzBitVector* rz_il_mem_loadw ( RzILMem mem,
RzBitVector key,
ut32  n_bits,
bool  big_endian 
)

Load an entire work of the given size from the given address

Parameters
keyaddress (bitvector)
n_bitsHow many bits to read. This also determines the size of the returned bitvector
Returns
data (bitvector)

Definition at line 157 of file mem.c.

157  {
158  rz_return_val_if_fail(mem && key && n_bits, NULL);
160  return read_n_bits(mem->buf, n_bits, key, big_endian);
161 }
static RzBitVector * read_n_bits(RzBuffer *buf, ut32 n_bits, RzBitVector *key, bool big_endian)
Definition: mem.c:105

References key, mem, NULL, read_n_bits(), return_val_if_key_len_wrong, and rz_return_val_if_fail.

Referenced by rz_il_vm_mem_loadw(), and rz_il_vm_mem_storew().

◆ rz_il_mem_new()

RZ_API RzILMem* rz_il_mem_new ( RzBuffer buf,
ut32  key_len 
)

Create a memory for accessing the given buffer.

Definition at line 13 of file mem.c.

13  {
14  rz_return_val_if_fail(buf && key_len, NULL);
15  if (key_len > KEY_LEN_MAX) {
16  // no assertion because it's not stricly a programming error to call this
17  // with a higher len. It's just not supported.
18  return NULL;
19  }
20  RzILMem *ret = RZ_NEW0(RzILMem);
21  if (!ret) {
22  return NULL;
23  }
24  rz_buf_ref(buf);
25  ret->buf = buf;
26  ret->key_len = key_len;
27  return ret;
28 }
#define KEY_LEN_MAX
Definition: mem.c:8
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
#define RZ_NEW0(x)
Definition: rz_types.h:284
A single memory as part of the RzIL VM.
Definition: mem.h:26
RzBuffer * buf
Definition: mem.h:27
ut32 key_len
Definition: mem.h:28

References rz_il_mem_t::buf, rz_il_mem_t::key_len, KEY_LEN_MAX, NULL, rz_buf_ref(), RZ_NEW0, and rz_return_val_if_fail.

Referenced by setup_vm_from_config().

◆ rz_il_mem_store()

RZ_API bool rz_il_mem_store ( RzILMem mem,
RzBitVector key,
RzBitVector value 
)

Store a single memory value (bitvector) into an address (bitvector)

Parameters
keyaddress
valuedata
Returns
whether the store succeeded

Definition at line 93 of file mem.c.

93  {
94  rz_return_val_if_fail(mem && key && value, false);
97  RZ_LOG_ERROR("RzIL: Memory write value size mismatch (expected size = %u, but got %u)\n",
98  (unsigned int)rz_il_mem_value_len(mem), (unsigned int)rz_bv_len(value));
99  return false;
100  }
101  ut8 v = rz_bv_to_ut8(value);
102  return rz_buf_write_at(mem->buf, rz_bv_to_ut64(key), &v, 1) == 1;
103 }
RZ_API ut8 rz_bv_to_ut8(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1397
RZ_API st64 rz_buf_write_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL const ut8 *buf, ut64 len)
Write len bytes of the buffer at the specified address.
Definition: buf.c:1197
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References key, mem, return_val_if_key_len_wrong, rz_buf_write_at(), rz_bv_len(), rz_bv_to_ut64(), rz_bv_to_ut8(), rz_il_mem_value_len(), RZ_LOG_ERROR, rz_return_val_if_fail, v, and value.

Referenced by rz_il_vm_mem_store().

◆ rz_il_mem_storew()

RZ_API bool rz_il_mem_storew ( RzILMem mem,
RzBitVector key,
RzBitVector value,
bool  big_endian 
)

Store an entire word or arbitrary size at an address

Parameters
keyaddress
valuedata
Returns
whether the store succeeded

Definition at line 169 of file mem.c.

169  {
170  rz_return_val_if_fail(mem && key && value, false);
172  return write_n_bits(mem->buf, key, value, big_endian);
173 }
static bool write_n_bits(RzBuffer *buf, RzBitVector *key, RzBitVector *value, bool big_endian)
Definition: mem.c:131

References key, mem, return_val_if_key_len_wrong, rz_return_val_if_fail, value, and write_n_bits().

Referenced by rz_il_vm_mem_storew().

◆ rz_il_mem_value_len()

RZ_API ut32 rz_il_mem_value_len ( RzILMem mem)

Get the bit-size of a value in the memory.

For all k, rz_bv_len(rz_il_mem_load(mem, k)) == rz_il_mem_value_len(mem). So this could be seen as the size of a byte. Because we only support RzBuffer-based mems at the moment, this is always 8, but more options may be available in the future.

Definition at line 60 of file mem.c.

60  {
61  return 8;
62 }

Referenced by rz_il_mem_load(), rz_il_mem_store(), and rz_il_validate_global_context_new_from_vm().

◆ write_n_bits()

static bool write_n_bits ( RzBuffer buf,
RzBitVector key,
RzBitVector value,
bool  big_endian 
)
static

Definition at line 131 of file mem.c.

131  {
132  ut64 address = rz_bv_to_ut64(key);
133  ut32 n_bytes = rz_bv_len_bytes(value);
134 
135  ut8 *data = calloc(n_bytes, 1);
136  if (!data) {
137  return false;
138  }
139 
140  if (big_endian) {
142  } else {
144  }
145 
146  bool succ = rz_buf_write_at(buf, address, data, n_bytes) == n_bytes;
147  free(data);
148  return succ;
149 }
RZ_API void rz_bv_set_to_bytes_le(RZ_NONNULL const RzBitVector *bv, RZ_OUT RZ_NONNULL ut8 *buf)
Set the buffer contents from the given bitvector's bits in little endian format.
Definition: bitvector.c:1318
RZ_API void rz_bv_set_to_bytes_be(RZ_NONNULL const RzBitVector *bv, RZ_OUT RZ_NONNULL ut8 *buf)
Set the buffer contents from the given bitvector's bits in big endian format.
Definition: bitvector.c:1349

References calloc(), free(), key, rz_buf_write_at(), rz_bv_len_bytes(), rz_bv_set_to_bytes_be(), rz_bv_set_to_bytes_le(), rz_bv_to_ut64(), ut64(), and value.

Referenced by rz_il_mem_storew().