File: map_write.c

package info (click to toggle)
unicorn-engine 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,912 kB
  • sloc: ansic: 379,830; python: 9,213; sh: 9,011; java: 8,609; ruby: 4,241; pascal: 1,805; haskell: 1,379; xml: 490; cs: 424; makefile: 348; cpp: 298; asm: 64
file content (54 lines) | stat: -rw-r--r-- 1,131 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <unicorn/unicorn.h>
#include <stdio.h>
#include <stdlib.h>

#define ADDR 0x00400000
#define SIZE 1024*64
#define OVERFLOW 1

int main(void)
{
    uc_engine *uc = NULL;
    uint8_t *buf = NULL, *buf2 = NULL;
    int i;
    uc_err err;

    err = uc_open (UC_ARCH_X86, UC_MODE_64, &uc);
    if (err) {
        printf ("uc_open %d\n", err);
        goto exit;
    }
    err = uc_mem_map (uc, ADDR, SIZE, UC_PROT_ALL);
    if (err) {
        printf ("uc_mem_map %d\n", err);
        goto exit;
    }
    buf = calloc (SIZE*2, 1);
    buf2 = calloc (SIZE, 1);
    for (i=0;i<SIZE; i++) {
        buf[i] = i & 0xff;
    }
    /* crash here */
    err = uc_mem_write (uc, ADDR, buf, SIZE+OVERFLOW); 
    if (err) {
        printf ("uc_mem_write %d\n", err);
        goto exit;
    }
    err = uc_mem_read (uc, ADDR+10, buf2, 4);
    if (err) {
        printf ("uc_mem_read %d\n", err);
        goto exit;
    }
    if (buf2[0] != 0xa) {
        printf ("mem contents are wrong\n");
        goto exit;
    }
    printf ("OK\n");

exit:
    if (uc)
        uc_close (uc);
    free (buf);
    free (buf2);
    return err ? 1 : 0;
}