Rizin
unix-like reverse engineering framework and cli tools
rz_il_validate.h File Reference

Validation/Type Checking of RzIL Code. More...

#include <rz_il/rz_il_vm.h>

Go to the source code of this file.

Typedefs

typedef char * RzILValidateReport
 
typedef struct rz_il_validate_global_context_t RzILValidateGlobalContext
 

Functions

RZ_API RzILValidateGlobalContextrz_il_validate_global_context_new_empty (ut32 pc_len)
 
RZ_API void rz_il_validate_global_context_add_var (RzILValidateGlobalContext *ctx, RZ_NONNULL const char *name, RzILSortPure sort)
 
RZ_API void rz_il_validate_global_context_add_mem (RzILValidateGlobalContext *ctx, RzILMemIndex idx, ut32 key_len, ut32 val_len)
 
RZ_API RzILValidateGlobalContextrz_il_validate_global_context_new_from_vm (RZ_NONNULL RzILVM *vm)
 
RZ_API void rz_il_validate_global_context_free (RzILValidateGlobalContext *ctx)
 
RZ_API bool rz_il_validate_pure (RZ_NULLABLE RzILOpPure *op, RZ_NONNULL RzILValidateGlobalContext *ctx, RZ_NULLABLE RZ_OUT RzILSortPure *sort_out, RZ_NULLABLE RZ_OUT RzILValidateReport *report_out)
 
RZ_API bool rz_il_validate_effect (RZ_NULLABLE RzILOpEffect *op, RZ_NONNULL RzILValidateGlobalContext *ctx, RZ_NULLABLE RZ_OUT HtPP **local_var_sorts_out, RZ_NULLABLE RZ_OUT RzILTypeEffect *type_out, RZ_NULLABLE RZ_OUT RzILValidateReport *report_out)
 

Detailed Description

Validation/Type Checking of RzIL Code.

Definition in file rz_il_validate.h.

Typedef Documentation

◆ RzILValidateGlobalContext

Global context for validation, defining:

  • all global variables with their sorts
  • all mems with their key and value lengths Validation can only be performes when this context is known, as for example the type of a var op referring to a global var depends on this.

Definition at line 18 of file rz_il_validate.h.

◆ RzILValidateReport

typedef char* RzILValidateReport

Definition at line 18 of file rz_il_validate.h.

Function Documentation

◆ rz_il_validate_effect()

RZ_API bool rz_il_validate_effect ( RZ_NULLABLE RzILOpEffect op,
RZ_NONNULL RzILValidateGlobalContext ctx,
RZ_NULLABLE RZ_OUT HtPP **  local_var_sorts_out,
RZ_NULLABLE RZ_OUT RzILTypeEffect type_out,
RZ_NULLABLE RZ_OUT RzILValidateReport report_out 
)

Run validation (type-checking and other checks) on an effect. op the op to be checked. May be null, which will always be reported as invalid. ctx global context, defining available global vars and mems local_var_sorts_out optionally returns a map of local variable names defined in the effect to their sorts type_put optionally returns the type of effects that the ops perform, i.e. ctrl, data, both or none report_out optionally returns a readable report containing details about why the validation failed

Returns
whether the given op is valid under ctx

Definition at line 846 of file il_validate.c.

849  {
850  LocalContext local_ctx;
851  if (!local_context_init(&local_ctx, ctx)) {
852  if (report_out) {
853  *report_out = NULL;
854  }
855  return false;
856  }
858  RzStrBuf report_builder;
859  rz_strbuf_init(&report_builder);
860  bool valid = validate_effect(op, &type, &report_builder, &local_ctx);
861  if (valid && local_var_sorts_out) {
862  *local_var_sorts_out = local_ctx.local_vars_known;
863  local_ctx.local_vars_known = NULL;
864  }
865  local_context_fini(&local_ctx);
866  if (type_out) {
867  *type_out = type;
868  }
869  if (report_out) {
870  *report_out = rz_strbuf_is_empty(&report_builder) ? NULL : rz_str_trim_tail(rz_strbuf_drain_nofree(&report_builder));
871  }
872  rz_strbuf_fini(&report_builder);
873  return valid;
874 }
#define NULL
Definition: cris-opc.c:27
static void local_context_fini(LocalContext *ctx)
Definition: il_validate.c:136
static bool validate_effect(VALIDATOR_EFFECT_ARGS)
Definition: il_validate.c:830
static bool local_context_init(LocalContext *ctx, const RzILValidateGlobalContext *global_ctx)
Definition: il_validate.c:122
int type
Definition: mipsasm.c:17
RZ_API RZ_BORROW char * rz_str_trim_tail(RZ_NONNULL char *str)
Removes whitespace characters (space, tab, newline etc.) from the end of a string and replaces them w...
Definition: str_trim.c:125
RZ_API RZ_OWN char * rz_strbuf_drain_nofree(RzStrBuf *sb)
Definition: strbuf.c:349
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
RZ_API bool rz_strbuf_is_empty(RzStrBuf *sb)
Definition: strbuf.c:24
RzILTypeEffect
Definition: sort.h:66
@ RZ_IL_TYPE_EFFECT_NONE
nop
Definition: sort.h:67
HtPP * local_vars_known
Definition: il_validate.c:117
bool valid
Definition: core.c:77
Definition: dis.c:32

References local_context_fini(), local_context_init(), LocalContext::local_vars_known, NULL, RZ_IL_TYPE_EFFECT_NONE, rz_str_trim_tail(), rz_strbuf_drain_nofree(), rz_strbuf_fini(), rz_strbuf_init(), rz_strbuf_is_empty(), type, valid, and validate_effect().

Referenced by print_and_check_il().

◆ rz_il_validate_global_context_add_mem()

RZ_API void rz_il_validate_global_context_add_mem ( RzILValidateGlobalContext ctx,
RzILMemIndex  idx,
ut32  key_len,
ut32  val_len 
)

Define a new memory in ctx

Definition at line 69 of file il_validate.c.

69  {
70  rz_return_if_fail(ctx && key_len && val_len);
71  ht_uu_update(ctx->mems, idx, ((ut64)key_len << 32) | (ut64)val_len);
72 }
int idx
Definition: setup.py:197
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References setup::idx, rz_return_if_fail, and ut64().

Referenced by rz_il_validate_global_context_new_from_vm().

◆ rz_il_validate_global_context_add_var()

RZ_API void rz_il_validate_global_context_add_var ( RzILValidateGlobalContext ctx,
RZ_NONNULL const char *  name,
RzILSortPure  sort 
)

Define a new global variable in ctx

Definition at line 56 of file il_validate.c.

56  {
59  if (!hts) {
60  return;
61  }
62  *hts = sort;
63  ht_pp_update(ctx->global_vars, name, hts);
64 }
#define RZ_NEW(x)
Definition: rz_types.h:285
Definition: z80asm.h:102

References RZ_NEW, and rz_return_if_fail.

Referenced by rz_il_validate_global_context_new_from_vm().

◆ rz_il_validate_global_context_free()

RZ_API void rz_il_validate_global_context_free ( RzILValidateGlobalContext ctx)

Definition at line 99 of file il_validate.c.

99  {
100  if (!ctx) {
101  return;
102  }
103  ht_pp_free(ctx->global_vars);
104  ht_uu_free(ctx->mems);
105  free(ctx);
106 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

Referenced by print_and_check_il().

◆ rz_il_validate_global_context_new_empty()

RZ_API RzILValidateGlobalContext* rz_il_validate_global_context_new_empty ( ut32  pc_len)

Create a new global context for validation Vars and mems can be added manually with rz_il_validate_global_context_add_* functions.

Definition at line 32 of file il_validate.c.

32  {
33  rz_return_val_if_fail(pc_len, NULL);
35  if (!ctx) {
36  return NULL;
37  }
38  ctx->pc_len = pc_len;
39  ctx->global_vars = ht_pp_new(NULL, var_kv_free, NULL);
40  if (!ctx->global_vars) {
41  free(ctx);
42  return NULL;
43  }
44  ctx->mems = ht_uu_new0();
45  if (!ctx->mems) {
46  ht_pp_free(ctx->global_vars);
47  free(ctx);
48  return NULL;
49  }
50  return ctx;
51 }
static void var_kv_free(HtPPKv *kv)
Definition: il_validate.c:19
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_NEW0(x)
Definition: rz_types.h:284

References free(), NULL, RZ_NEW0, rz_return_val_if_fail, and var_kv_free().

Referenced by rz_il_validate_global_context_new_from_vm().

◆ rz_il_validate_global_context_new_from_vm()

RZ_API RzILValidateGlobalContext* rz_il_validate_global_context_new_from_vm ( RZ_NONNULL RzILVM vm)

Create a new context for IL validation based on the global vars and mems in vm

Definition at line 77 of file il_validate.c.

77  {
81  if (vars) {
82  void **it;
83  rz_pvector_foreach (vars, it) {
84  RzILVar *var = *it;
86  }
87  rz_pvector_free(vars);
88  }
89  for (size_t i = 0; i < rz_pvector_len(&vm->vm_memory); i++) {
90  RzILMem *mem = rz_pvector_at(&vm->vm_memory, i);
91  if (!mem) {
92  continue;
93  }
95  }
96  return ctx;
97 }
lzma_index ** i
Definition: index.h:629
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
RZ_API ut32 rz_il_mem_value_len(RzILMem *mem)
Get the bit-size of a value in the memory.
Definition: mem.c:60
RZ_API void rz_il_validate_global_context_add_mem(RzILValidateGlobalContext *ctx, RzILMemIndex idx, ut32 key_len, ut32 val_len)
Definition: il_validate.c:69
RZ_API void rz_il_validate_global_context_add_var(RzILValidateGlobalContext *ctx, RZ_NONNULL const char *name, RzILSortPure sort)
Definition: il_validate.c:56
RZ_API RzILValidateGlobalContext * rz_il_validate_global_context_new_empty(ut32 pc_len)
Definition: il_validate.c:32
RZ_API RZ_OWN RzPVector * rz_il_vm_get_all_vars(RZ_NONNULL RzILVM *vm, RzILVarKind kind)
Definition: il_vm.c:256
RZ_API ut32 rz_il_vm_get_pc_len(RzILVM *vm)
Definition: il_vm.c:145
void * mem
Definition: libc.cpp:91
ut32 RzILMemIndex
Definition: mem.h:14
static size_t rz_pvector_len(const RzPVector *vec)
Definition: rz_vector.h:231
RZ_API void rz_pvector_free(RzPVector *vec)
Definition: vector.c:336
static void * rz_pvector_at(const RzPVector *vec, size_t index)
Definition: rz_vector.h:236
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
A single memory as part of the RzIL VM.
Definition: mem.h:26
Definition of a variable inside the vm.
Definition: variable.h:19
RzILSortPure sort
"type" of the variable
Definition: variable.h:21
char * name
Definition: variable.h:20
@ RZ_IL_VAR_KIND_GLOBAL
global var, usually bound to a physical representation like a register.
Definition: variable.h:47

References i, mem, rz_il_var_t::name, NULL, rz_il_mem_key_len(), rz_il_mem_value_len(), rz_il_validate_global_context_add_mem(), rz_il_validate_global_context_add_var(), rz_il_validate_global_context_new_empty(), RZ_IL_VAR_KIND_GLOBAL, rz_il_vm_get_all_vars(), rz_il_vm_get_pc_len(), rz_pvector_at(), rz_pvector_foreach, rz_pvector_free(), rz_pvector_len(), rz_return_val_if_fail, and rz_il_var_t::sort.

Referenced by print_and_check_il().

◆ rz_il_validate_pure()

Run validation (type-checking and other checks) on a pure expression and determine its sort. op the op to be checked. May be null, which will always be reported as invalid. ctx global context, defining available global vars and mems sort_out optionally returns the sort of the expression, if it is valid report_out optionally returns a readable report containing details about why the validation failed

Returns
whether the given op is valid under ctx

Definition at line 584 of file il_validate.c.

585  {
586  LocalContext local_ctx;
587  if (!local_context_init(&local_ctx, ctx)) {
588  if (report_out) {
589  *report_out = NULL;
590  }
591  return false;
592  }
593  RzStrBuf report_builder;
594  rz_strbuf_init(&report_builder);
595  RzILSortPure sort = { 0 };
596  bool valid = validate_pure(op, &sort, &report_builder, &local_ctx, NULL);
597  local_context_fini(&local_ctx);
598  if (sort_out) {
599  *sort_out = sort;
600  }
601  if (report_out) {
602  *report_out = rz_strbuf_is_empty(&report_builder) ? NULL : rz_str_trim_tail(rz_strbuf_drain_nofree(&report_builder));
603  }
604  rz_strbuf_fini(&report_builder);
605  return valid;
606 }
static bool validate_pure(VALIDATOR_PURE_ARGS)
Definition: il_validate.c:569

References local_context_fini(), local_context_init(), NULL, rz_str_trim_tail(), rz_strbuf_drain_nofree(), rz_strbuf_fini(), rz_strbuf_init(), rz_strbuf_is_empty(), local_pure_var_t::sort, valid, and validate_pure().