unicorn-engine 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: faf0b69bd38165882565328df5941f6bd57a85e5
4
+ data.tar.gz: 42630cbd6e524636a2f56df7fdbe4d931162744c
5
+ SHA512:
6
+ metadata.gz: 5762554e14845beaa344001bbaad760ccee2a7f157a5a7497f5558c938f6666ec353752b8c2bf8c7114f17bb1c8f877ca6302112d2abbae6ea959ecb30efa03a
7
+ data.tar.gz: 2b7863d121ec38e8b747ad666264f684b80d061e236be390ca89d86fd03eeea687e4493380d1dd08ce42e923ff482c92a43666580c13a45087b6c294f94d0668
@@ -0,0 +1,8 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'unicorn_engine'
4
+
5
+ dir_config(extension_name)
6
+ have_library('unicorn')
7
+
8
+ create_makefile(extension_name)
@@ -0,0 +1,33 @@
1
+ /*
2
+
3
+ Ruby bindings for the Unicorn Emulator Engine
4
+
5
+ Copyright(c) 2016 Sascha Schirra
6
+
7
+ This program is free software; you can redistribute it and/or
8
+ modify it under the terms of the GNU General Public License
9
+ version 2 as published by the Free Software Foundation.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program; if not, write to the Free Software
18
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
+
20
+ */
21
+
22
+ typedef struct uc_x86_float80 {
23
+ uint64_t mantissa;
24
+ uint16_t exponent;
25
+ } uc_x86_float80;
26
+
27
+
28
+ struct hook {
29
+ uc_hook trace;
30
+ VALUE cb;
31
+ VALUE ud;
32
+ VALUE rUc;
33
+ };
@@ -0,0 +1,589 @@
1
+ /*
2
+
3
+ Ruby bindings for the Unicorn Emulator Engine
4
+
5
+ Copyright(c) 2016 Sascha Schirra
6
+
7
+ This program is free software; you can redistribute it and/or
8
+ modify it under the terms of the GNU General Public License
9
+ version 2 as published by the Free Software Foundation.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program; if not, write to the Free Software
18
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
+
20
+ */
21
+ #include "ruby.h"
22
+ #include <unicorn/unicorn.h>
23
+ #include <unicorn/x86.h>
24
+ #include "unicorn.h"
25
+ #include "types.h"
26
+
27
+ VALUE UnicornModule = Qnil;
28
+ VALUE UcClass = Qnil;
29
+ VALUE UcError = Qnil;
30
+ VALUE SavedContext = Qnil;
31
+ VALUE Hook = Qnil;
32
+
33
+
34
+ void Init_unicorn_engine() {
35
+ rb_require("unicorn_engine/unicorn_const");
36
+ UnicornModule = rb_define_module("UnicornEngine");
37
+ UcError = rb_define_class_under(UnicornModule, "UcError", rb_eStandardError);
38
+ SavedContext = rb_define_class_under(UnicornModule, "SavedContext", rb_cObject);
39
+ Hook = rb_define_class_under(UnicornModule, "Hook", rb_cObject);
40
+
41
+ UcClass = rb_define_class_under(UnicornModule, "Uc", rb_cObject);
42
+ rb_define_method(UcClass, "initialize", m_uc_initialize, 2);
43
+ rb_define_method(UcClass, "emu_start", m_uc_emu_start, -1);
44
+ rb_define_method(UcClass, "emu_stop", m_uc_emu_stop, 0);
45
+ rb_define_method(UcClass, "reg_read", m_uc_reg_read, 1);
46
+ rb_define_method(UcClass, "reg_write", m_uc_reg_write, 2);
47
+ rb_define_method(UcClass, "mem_read", m_uc_mem_read, 2);
48
+ rb_define_method(UcClass, "mem_write", m_uc_mem_write, 2);
49
+ rb_define_method(UcClass, "mem_map", m_uc_mem_map, -1);
50
+ rb_define_method(UcClass, "mem_unmap", m_uc_mem_unmap, 2);
51
+ rb_define_method(UcClass, "mem_protect", m_uc_mem_protect, 3);
52
+ rb_define_method(UcClass, "hook_add", m_uc_hook_add, -1);
53
+ rb_define_method(UcClass, "hook_del", m_uc_hook_del, 1);
54
+ rb_define_method(UcClass, "query", m_uc_query, 1);
55
+ rb_define_method(UcClass, "context_save", m_uc_context_save, 0);
56
+ rb_define_method(UcClass, "context_update", m_uc_context_update, 1);
57
+ rb_define_method(UcClass, "context_restore", m_uc_context_restore, 1);
58
+ }
59
+
60
+ VALUE m_uc_initialize(VALUE self, VALUE arch, VALUE mode) {
61
+ uc_engine *_uc;
62
+ uc_err err;
63
+ err = uc_open(NUM2INT(arch), NUM2INT(mode), &_uc);
64
+ if (err != UC_ERR_OK) {
65
+ rb_raise(UcError, "%s", uc_strerror(err));
66
+ }
67
+
68
+ VALUE uc = Data_Wrap_Struct(UcClass, 0, uc_close, _uc);
69
+ rb_iv_set(self, "@uch", uc);
70
+ rb_iv_set(self, "@hooks", rb_ary_new());
71
+
72
+ return self;
73
+ }
74
+
75
+ VALUE m_uc_emu_start(int argc, VALUE* argv, VALUE self){
76
+ VALUE begin;
77
+ VALUE until;
78
+ VALUE timeout;
79
+ VALUE count;
80
+ uc_err err;
81
+ uc_engine *_uc;
82
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
83
+
84
+ rb_scan_args(argc, argv, "22",&begin, &until, &timeout, &count);
85
+ if (NIL_P(timeout))
86
+ timeout = INT2NUM(0);
87
+
88
+ if (NIL_P(count))
89
+ count = INT2NUM(0);
90
+
91
+ err = uc_emu_start(_uc, NUM2ULL(begin), NUM2ULL(until), NUM2INT(timeout), NUM2INT(count));
92
+ if (err != UC_ERR_OK) {
93
+ rb_raise(UcError, "%s", uc_strerror(err));
94
+ }
95
+ return Qnil;
96
+ }
97
+
98
+ VALUE m_uc_emu_stop(VALUE self){
99
+ uc_err err;
100
+ uc_engine *_uc;
101
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
102
+
103
+ err = uc_emu_stop(_uc);
104
+ if (err != UC_ERR_OK) {
105
+ rb_raise(UcError, "%s", uc_strerror(err));
106
+ }
107
+ return Qnil;
108
+ }
109
+
110
+ VALUE m_uc_reg_read(VALUE self, VALUE reg_id){
111
+ uc_err err;
112
+ int32_t tmp_reg = NUM2INT(reg_id);
113
+ int64_t reg_value = 0;
114
+ VALUE to_ret;
115
+ uc_x86_mmr mmr;
116
+ uc_x86_float80 float80;
117
+
118
+ uc_engine *_uc;
119
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
120
+
121
+ uc_arch arch;
122
+ uc_query(_uc, UC_QUERY_ARCH, &arch);
123
+
124
+ if(arch == UC_ARCH_X86) {
125
+ switch(tmp_reg){
126
+ case UC_X86_REG_GDTR:
127
+ case UC_X86_REG_IDTR:
128
+ case UC_X86_REG_LDTR:
129
+ case UC_X86_REG_TR:
130
+ mmr.selector = 0;
131
+ mmr.base = 0;
132
+ mmr.limit = 0;
133
+ mmr.flags = 0;
134
+ err = uc_reg_read(_uc, tmp_reg, &mmr);
135
+
136
+ if (err != UC_ERR_OK) {
137
+ rb_raise(UcError, "%s", uc_strerror(err));
138
+ }
139
+ VALUE mmr_ary = rb_ary_new();
140
+ reg_value = mmr.selector;
141
+ rb_ary_store(mmr_ary, 0, UINT2NUM(reg_value));
142
+ rb_ary_store(mmr_ary, 1, ULL2NUM(mmr.base));
143
+ rb_ary_store(mmr_ary, 2, UINT2NUM(mmr.limit));
144
+ rb_ary_store(mmr_ary, 3, UINT2NUM(mmr.flags));
145
+ return mmr_ary;
146
+
147
+ case UC_X86_REG_FP0:
148
+ case UC_X86_REG_FP1:
149
+ case UC_X86_REG_FP2:
150
+ case UC_X86_REG_FP3:
151
+ case UC_X86_REG_FP4:
152
+ case UC_X86_REG_FP5:
153
+ case UC_X86_REG_FP6:
154
+ case UC_X86_REG_FP7:
155
+ float80.mantissa = 0;
156
+ float80.exponent = 0;
157
+
158
+ err = uc_reg_read(_uc, tmp_reg, &float80);
159
+
160
+ if (err != UC_ERR_OK) {
161
+ rb_raise(UcError, "%s", uc_strerror(err));
162
+ }
163
+
164
+ VALUE float80_ary = rb_ary_new();
165
+
166
+ rb_ary_store(float80_ary, 0, ULL2NUM(float80.mantissa));
167
+ rb_ary_store(float80_ary, 1, UINT2NUM(float80.exponent));
168
+
169
+ return float80_ary;
170
+ }
171
+ }
172
+ if(arch == UC_ARCH_ARM64) {
173
+ // V & Q registers are the same
174
+ if(tmp_reg >= UC_ARM64_REG_V0 && tmp_reg <= UC_ARM64_REG_V31) {
175
+ tmp_reg += UC_ARM64_REG_Q0 - UC_ARM64_REG_V0;
176
+ }
177
+ if(tmp_reg >= UC_ARM64_REG_Q0 && tmp_reg <= UC_ARM64_REG_Q31) {
178
+ uint64_t neon128_value[2];
179
+ err = uc_reg_read(_uc, tmp_reg, &neon128_value);
180
+ if (err != UC_ERR_OK) {
181
+ rb_raise(UcError, "%s", uc_strerror(err));
182
+ }
183
+ VALUE float128_ary = rb_ary_new();
184
+ rb_ary_store(float128_ary, 0, ULL2NUM(neon128_value[0]));
185
+ rb_ary_store(float128_ary, 1, ULL2NUM(neon128_value[1]));
186
+ return float128_ary;
187
+ }
188
+ }
189
+ err = uc_reg_read(_uc, tmp_reg, &reg_value);
190
+ if (err != UC_ERR_OK) {
191
+ rb_raise(UcError, "%s", uc_strerror(err));
192
+ }
193
+ return ULL2NUM(reg_value);
194
+ }
195
+
196
+ VALUE m_uc_reg_write(VALUE self, VALUE reg_id, VALUE reg_value){
197
+ uc_err err;
198
+ int32_t tmp_reg = NUM2INT(reg_id);
199
+ uc_x86_mmr mmr;
200
+ uc_x86_float80 float80;
201
+ int64_t tmp;
202
+ uc_engine *_uc;
203
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
204
+
205
+ uc_arch arch;
206
+ uc_query(_uc, UC_QUERY_ARCH, &arch);
207
+
208
+ if(arch == UC_ARCH_X86) {
209
+ switch(tmp_reg){
210
+ case UC_X86_REG_GDTR:
211
+ case UC_X86_REG_IDTR:
212
+ case UC_X86_REG_LDTR:
213
+ case UC_X86_REG_TR:
214
+ Check_Type(reg_value, T_ARRAY);
215
+
216
+ mmr.selector = NUM2USHORT(rb_ary_entry(reg_value,0));
217
+ mmr.base = NUM2ULL(rb_ary_entry(reg_value,1));
218
+ mmr.limit = NUM2UINT(rb_ary_entry(reg_value,2));
219
+ mmr.flags = NUM2UINT(rb_ary_entry(reg_value,3));
220
+ err = uc_reg_write(_uc, tmp_reg, &mmr);
221
+ if (err != UC_ERR_OK) {
222
+ rb_raise(UcError, "%s", uc_strerror(err));
223
+ }
224
+ return Qnil;
225
+
226
+ case UC_X86_REG_FP0:
227
+ case UC_X86_REG_FP1:
228
+ case UC_X86_REG_FP2:
229
+ case UC_X86_REG_FP3:
230
+ case UC_X86_REG_FP4:
231
+ case UC_X86_REG_FP5:
232
+ case UC_X86_REG_FP6:
233
+ case UC_X86_REG_FP7:
234
+ Check_Type(reg_value, T_ARRAY);
235
+
236
+ float80.mantissa = NUM2ULL(rb_ary_entry(reg_value,0));
237
+ float80.exponent = NUM2USHORT(rb_ary_entry(reg_value,1));
238
+
239
+ err = uc_reg_write(_uc, tmp_reg, &float80);
240
+
241
+ if (err != UC_ERR_OK) {
242
+ rb_raise(UcError, "%s", uc_strerror(err));
243
+ }
244
+
245
+ return Qnil;
246
+ }
247
+ }
248
+ if(arch == UC_ARCH_ARM64) {
249
+ // V & Q registers are the same
250
+ if(tmp_reg >= UC_ARM64_REG_V0 && tmp_reg <= UC_ARM64_REG_V31) {
251
+ tmp_reg += UC_ARM64_REG_Q0 - UC_ARM64_REG_V0;
252
+ }
253
+ if(tmp_reg >= UC_ARM64_REG_Q0 && tmp_reg <= UC_ARM64_REG_Q31) {
254
+ Check_Type(reg_value, T_ARRAY);
255
+
256
+ uint64_t neon128_value[2];
257
+ neon128_value[0] = NUM2ULL(rb_ary_entry(reg_value, 0));
258
+ neon128_value[1] = NUM2ULL(rb_ary_entry(reg_value, 1));
259
+ err = uc_reg_write(_uc, NUM2INT(reg_id), &neon128_value);
260
+ if (err != UC_ERR_OK) {
261
+ rb_raise(UcError, "%s", uc_strerror(err));
262
+ }
263
+ return Qnil;
264
+ }
265
+ }
266
+
267
+ tmp = NUM2ULL(reg_value);
268
+ err = uc_reg_write(_uc, NUM2INT(reg_id), &tmp);
269
+ if (err != UC_ERR_OK) {
270
+ rb_raise(UcError, "%s", uc_strerror(err));
271
+ }
272
+ return Qnil;
273
+ }
274
+
275
+ VALUE m_uc_mem_read(VALUE self, VALUE address, VALUE size){
276
+ size_t isize = NUM2UINT(size);
277
+ uint8_t bytes[isize];
278
+ uc_err err;
279
+ uc_engine *_uc;
280
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
281
+
282
+ err = uc_mem_read(_uc, NUM2ULL(address), &bytes, isize);
283
+ if (err != UC_ERR_OK) {
284
+ rb_raise(UcError, "%s", uc_strerror(err));
285
+ }
286
+ return rb_str_new(bytes, isize);
287
+ }
288
+
289
+ VALUE m_uc_mem_write(VALUE self, VALUE address, VALUE bytes){
290
+ uc_err err;
291
+ uc_engine *_uc;
292
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
293
+ err = uc_mem_write(_uc, NUM2ULL(address), StringValuePtr(bytes), RSTRING_LEN(bytes));
294
+ if (err != UC_ERR_OK) {
295
+ rb_raise(UcError, "%s", uc_strerror(err));
296
+ }
297
+ return Qnil;
298
+ }
299
+
300
+ VALUE m_uc_mem_map(int argc, VALUE* argv, VALUE self){
301
+ uc_err err;
302
+ VALUE address;
303
+ VALUE size;
304
+ VALUE perms;
305
+ uc_engine *_uc;
306
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
307
+ rb_scan_args(argc, argv, "21",&address, &size, &perms);
308
+ if (NIL_P(perms))
309
+ perms = INT2NUM(UC_PROT_ALL);
310
+
311
+ err = uc_mem_map(_uc, NUM2ULL(address), NUM2UINT(size), NUM2UINT(perms));
312
+ if (err != UC_ERR_OK) {
313
+ rb_raise(UcError, "%s", uc_strerror(err));
314
+ }
315
+ return Qnil;
316
+ }
317
+
318
+ VALUE m_uc_mem_unmap(VALUE self, VALUE address, VALUE size){
319
+ uc_err err;
320
+ uc_engine *_uc;
321
+ Data_Get_Struct(rb_iv_get(self, "@uch"), uc_engine, _uc);
322
+ err = uc_mem_unmap(_uc, NUM2ULL(address), NUM2UINT(size));
323
+ if (err != UC_ERR_OK) {
324
+ rb_raise(UcError, "%s", uc_strerror(err));
325
+ }
326
+ return Qnil;
327
+ }
328
+
329
+ VALUE m_uc_mem_protect(VALUE self, VALUE address, VALUE size, VALUE perms){
330
+ uc_err err;
331
+ uc_engine *_uc;
332
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
333
+ err = uc_mem_protect(_uc, NUM2ULL(address), NUM2UINT(size), NUM2UINT(perms));
334
+ if (err != UC_ERR_OK) {
335
+ rb_raise(UcError, "%s", uc_strerror(err));
336
+ }
337
+ return Qnil;
338
+ }
339
+
340
+ static void cb_hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data){
341
+ struct hook *hook = (struct hook *)user_data;
342
+ VALUE cb;
343
+ VALUE ud;
344
+ VALUE rUc;
345
+
346
+ cb = hook->cb;
347
+ ud = hook->ud;
348
+ rUc = hook->rUc;
349
+ rb_funcall(cb, rb_intern("call"), 4, rUc, ULL2NUM(address), UINT2NUM(size), ud);
350
+ }
351
+
352
+ static void cb_hook_mem_access(uc_engine *uc, uint32_t access, uint64_t address, uint32_t size, int64_t value, void *user_data){
353
+ struct hook *hook = (struct hook *)user_data;
354
+ VALUE cb;
355
+ VALUE ud;
356
+ VALUE rUc;
357
+
358
+ cb = hook->cb;
359
+ ud = hook->ud;
360
+ rUc = hook->rUc;
361
+ rb_funcall(cb, rb_intern("call"), 6, rUc, UINT2NUM(access), ULL2NUM(address), UINT2NUM(size), LL2NUM(value), ud);
362
+ }
363
+
364
+ static bool cb_hook_mem_invalid(uc_engine *uc, uint32_t access, uint64_t address, uint32_t size, int64_t value, void *user_data){
365
+ struct hook *hook = (struct hook *)user_data;
366
+ VALUE cb;
367
+ VALUE ud;
368
+ VALUE rUc;
369
+
370
+ cb = hook->cb;
371
+ ud = hook->ud;
372
+ rUc = hook->rUc;
373
+
374
+ return RTEST(rb_funcall(cb, rb_intern("call"), 6, rUc, UINT2NUM(access), ULL2NUM(address), UINT2NUM(size), LL2NUM(value), ud));
375
+ }
376
+
377
+ static uint32_t cb_hook_insn_in(uc_engine *uc, uint32_t port, int size, void *user_data){
378
+ struct hook *hook = (struct hook *)user_data;
379
+ VALUE cb;
380
+ VALUE ud;
381
+ VALUE rUc;
382
+
383
+ cb = hook->cb;
384
+ ud = hook->ud;
385
+ rUc = hook->rUc;
386
+ return NUM2UINT(rb_funcall(cb, rb_intern("call"), 4, rUc, UINT2NUM(port), INT2NUM(size), ud));
387
+ }
388
+
389
+ static void cb_hook_insn_out(uc_engine *uc, uint32_t port, int size, uint32_t value, void *user_data){
390
+ struct hook *hook = (struct hook *)user_data;
391
+ VALUE cb;
392
+ VALUE ud;
393
+ VALUE rUc;
394
+
395
+ cb = hook->cb;
396
+ ud = hook->ud;
397
+ rUc = hook->rUc;
398
+ rb_funcall(cb, rb_intern("call"), 5, rUc, UINT2NUM(port), INT2NUM(size), UINT2NUM(value), ud);
399
+ }
400
+
401
+ static void cb_hook_insn_syscall(uc_engine *uc, void *user_data){
402
+ struct hook *hook = (struct hook *)user_data;
403
+ VALUE cb;
404
+ VALUE ud;
405
+ VALUE rUc;
406
+
407
+ cb = hook->cb;
408
+ ud = hook->ud;
409
+ rUc = hook->rUc;
410
+ rb_funcall(cb, rb_intern("call"), 2, rUc, ud);
411
+ }
412
+
413
+ static void cb_hook_intr(uc_engine *uc, uint32_t intno, void *user_data){
414
+ struct hook *hook = (struct hook *)user_data;
415
+ VALUE cb;
416
+ VALUE ud;
417
+ VALUE rUc;
418
+
419
+ cb = hook->cb;
420
+ ud = hook->ud;
421
+ rUc = hook->rUc;
422
+ rb_funcall(cb, rb_intern("call"), 3, rUc, ULL2NUM(intno), ud);
423
+ }
424
+
425
+ static void mark_hook(void *p){
426
+ struct hook *hook = (struct hook *)p;
427
+ rb_gc_mark(hook->cb);
428
+ rb_gc_mark(hook->ud);
429
+ rb_gc_mark(hook->rUc); // just for completeness sake even though this should already be marked
430
+ }
431
+
432
+ VALUE m_uc_hook_add(int argc, VALUE* argv, VALUE self){
433
+ VALUE hook_type;
434
+ VALUE callback;
435
+ VALUE user_data;
436
+ VALUE begin;
437
+ VALUE end;
438
+ VALUE arg1;
439
+ uc_engine *_uc;
440
+ Data_Get_Struct(rb_iv_get(self, "@uch"), uc_engine, _uc);
441
+
442
+ rb_scan_args(argc, argv, "24",&hook_type, &callback, &user_data, &begin, &end, &arg1);
443
+ if (NIL_P(begin))
444
+ begin = ULL2NUM(1);
445
+
446
+ if (NIL_P(end))
447
+ end = ULL2NUM(0);
448
+
449
+ if (NIL_P(arg1))
450
+ arg1 = INT2NUM(0);
451
+
452
+ uc_err err;
453
+
454
+ if (rb_class_of(callback) != rb_cProc)
455
+ rb_raise(UcError, "Expected Proc callback");
456
+
457
+ struct hook *hook = (struct hook *)malloc(sizeof(struct hook));
458
+ hook->cb = callback;
459
+ hook->ud = user_data;
460
+ hook->rUc = self;
461
+ VALUE r_hook;
462
+ VALUE hooks_list;
463
+ r_hook = Data_Wrap_Struct(Hook, mark_hook, free, hook);
464
+ hooks_list = rb_iv_get(self, "@hooks");
465
+ rb_ary_push(hooks_list, r_hook);
466
+
467
+ uint32_t htype = NUM2UINT(hook_type);
468
+ if(htype == UC_HOOK_INSN){
469
+ switch(NUM2INT(arg1)){
470
+ case UC_X86_INS_IN:
471
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_insn_in,(void *)hook, NUM2ULL(begin), NUM2ULL(end), NUM2INT(arg1));
472
+ break;
473
+ case UC_X86_INS_OUT:
474
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_insn_out,(void *)hook, NUM2ULL(begin), NUM2ULL(end), NUM2INT(arg1));
475
+ break;
476
+ case UC_X86_INS_SYSCALL:
477
+ case UC_X86_INS_SYSENTER:
478
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_insn_syscall,(void *)hook, NUM2ULL(begin), NUM2ULL(end), NUM2INT(arg1));
479
+ break;
480
+ }
481
+ }
482
+ else if(htype == UC_HOOK_INTR){
483
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_intr,(void *)hook, NUM2ULL(begin), NUM2ULL(end));
484
+ }
485
+ else if(htype == UC_HOOK_CODE || htype == UC_HOOK_BLOCK){
486
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_code,(void *)hook, NUM2ULL(begin), NUM2ULL(end));
487
+ }
488
+ else if (htype & UC_HOOK_MEM_READ_UNMAPPED
489
+ || htype & UC_HOOK_MEM_WRITE_UNMAPPED
490
+ || htype & UC_HOOK_MEM_FETCH_UNMAPPED
491
+ || htype & UC_HOOK_MEM_READ_PROT
492
+ || htype & UC_HOOK_MEM_WRITE_PROT
493
+ || htype & UC_HOOK_MEM_FETCH_PROT
494
+ || htype & UC_HOOK_MEM_READ_INVALID
495
+ || htype & UC_HOOK_MEM_WRITE_INVALID
496
+ || htype & UC_HOOK_MEM_FETCH_INVALID
497
+ || htype & UC_HOOK_MEM_UNMAPPED
498
+ || htype & UC_HOOK_MEM_PROT
499
+ || htype & UC_HOOK_MEM_INVALID) {
500
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_mem_invalid,(void *)hook, NUM2ULL(begin), NUM2ULL(end));
501
+ }
502
+ else{
503
+ err = uc_hook_add(_uc, &hook->trace, htype, cb_hook_mem_access,(void *)hook, NUM2ULL(begin), NUM2ULL(end));
504
+ }
505
+
506
+ if (err != UC_ERR_OK) {
507
+ rb_raise(UcError, "%s", uc_strerror(err));
508
+ }
509
+ return r_hook;
510
+ }
511
+
512
+ VALUE m_uc_hook_del(VALUE self, VALUE hook){
513
+ uc_err err;
514
+ uc_engine *_uc;
515
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
516
+ struct hook *h;
517
+ Data_Get_Struct(hook, struct hook, h);
518
+ err = uc_hook_del(_uc, h->trace);
519
+
520
+ rb_ary_delete(rb_iv_get(self, "@hooks"), hook);
521
+
522
+ if (err != UC_ERR_OK) {
523
+ rb_raise(UcError, "%s", uc_strerror(err));
524
+ }
525
+ return Qnil;
526
+ }
527
+
528
+ VALUE m_uc_query(VALUE self, VALUE query_mode){
529
+ int qm = NUM2INT(query_mode);
530
+ size_t result;
531
+ uc_err err;
532
+ uc_engine *_uc;
533
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
534
+ err = uc_query(_uc, qm, &result);
535
+ if (err != UC_ERR_OK) {
536
+ rb_raise(UcError, "%s", uc_strerror(err));
537
+ }
538
+ return INT2NUM(result);
539
+ }
540
+
541
+ VALUE m_uc_context_save(VALUE self){
542
+ uc_err err;
543
+ uc_engine *_uc;
544
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
545
+
546
+ uc_context *_context;
547
+ err = uc_context_alloc(_uc, &_context);
548
+ if (err != UC_ERR_OK) {
549
+ rb_raise(UcError, "%s", uc_strerror(err));
550
+ }
551
+
552
+ err = uc_context_save(_uc, _context);
553
+ if (err != UC_ERR_OK) {
554
+ rb_raise(UcError, "%s", uc_strerror(err));
555
+ }
556
+
557
+ VALUE sc = Data_Wrap_Struct(SavedContext, 0, uc_free, _context);
558
+ return sc;
559
+ }
560
+
561
+ VALUE m_uc_context_update(VALUE self, VALUE context){
562
+ uc_err err;
563
+ uc_engine *_uc;
564
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
565
+
566
+ uc_context *_context;
567
+ Data_Get_Struct(context, uc_context, _context);
568
+
569
+ err = uc_context_save(_uc, _context);
570
+ if (err != UC_ERR_OK) {
571
+ rb_raise(UcError, "%s", uc_strerror(err));
572
+ }
573
+ return Qnil;
574
+ }
575
+
576
+ VALUE m_uc_context_restore(VALUE self, VALUE context){
577
+ uc_err err;
578
+ uc_engine *_uc;
579
+ Data_Get_Struct(rb_iv_get(self,"@uch"), uc_engine, _uc);
580
+
581
+ uc_context *_context;
582
+ Data_Get_Struct(context, uc_context, _context);
583
+
584
+ err = uc_context_restore(_uc, _context);
585
+ if (err != UC_ERR_OK) {
586
+ rb_raise(UcError, "%s", uc_strerror(err));
587
+ }
588
+ return Qnil;
589
+ }