Rizin
unix-like reverse engineering framework and cli tools
mem.h File Reference
#include <rz_util.h>

Go to the source code of this file.

Classes

struct  rz_il_mem_t
 A single memory as part of the RzIL VM. More...
 

Typedefs

typedef ut32 RzILMemIndex
 
typedef struct rz_il_mem_t RzILMem
 A single memory as part of the RzIL VM. More...
 

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)
 
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)
 

Typedef Documentation

◆ RzILMem

typedef struct rz_il_mem_t RzILMem

A single memory as part of the RzIL VM.

This can be seen as an array of bitvectors, indexed by bitvector keys, covering a certain address space. It corresponds to ('a, 'b) mem in bap where 'a and 'b statically determine the size of all keys and values, respectively. Because currently our memory can only bind to an RzBuffer, the key size is limited to a maximum of 64bits and the value size is always 8, but this can be extended in the future if necessary.

◆ RzILMemIndex

typedef ut32 RzILMemIndex

Definition at line 14 of file mem.h.

Function Documentation

◆ 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 Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * mem
Definition: libc.cpp:91
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 }
#define NULL
Definition: cris-opc.c:27
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
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
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API ut64 rz_bv_to_ut64(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1454
RZ_API RZ_OWN RzBitVector * rz_bv_new_from_ut64(ut32 length, ut64 value)
Definition: bitvector.c:1161
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

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
voidpf void * buf
Definition: ioapi.h:138
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 }
static int value
Definition: cmd_api.c:93
RZ_API ut32 rz_bv_len(RZ_NONNULL const RzBitVector *bv)
Definition: bitvector.c:1140
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().