Merge pull request #718 from pcercuei/update-lightrec-20230224
[pcsx_rearmed.git] / deps / lightning / check / protect.c
1 /*
2  * Simple test of (un)protecting a code buffer.
3  */
4
5 #include <lightning.h>
6 #include <stdio.h>
7 #include <assert.h>
8
9 #define MARKER 10
10
11 int
12 main(int argc, char *argv[])
13 {
14     jit_state_t          *_jit;
15     jit_node_t           *load, *label, *ok;
16     unsigned char        *ptr;
17     void                (*function)(void);
18     int                   mmap_prot, mmap_flags;
19
20     init_jit(argv[0]);
21     _jit = jit_new_state();
22
23     jit_prolog();
24
25     load = jit_movi(JIT_R0, 0);
26     jit_ldr_c(JIT_R0, JIT_R0);
27     ok = jit_forward();
28     jit_patch_at(jit_beqi(JIT_R0, MARKER), ok);
29     jit_prepare();
30     jit_pushargi(1);
31     jit_finishi(exit);
32     label = jit_indirect();
33     jit_skip(1);              /* Reserves enough space for a byte.  */
34     jit_patch_at(load, label);
35     jit_link(ok);
36     jit_prepare();
37     jit_pushargi((jit_word_t)"%s\n");
38     jit_ellipsis();
39     jit_pushargi((jit_word_t)"ok");
40     jit_finishi(printf);
41
42     function = jit_emit();
43     if (function == NULL)
44         abort();
45
46     jit_unprotect ();
47     ptr = jit_address (label);
48     *ptr = MARKER;
49     jit_protect ();
50
51     jit_clear_state();
52
53     (*function)();
54
55     jit_destroy_state();
56     finish_jit();
57
58     return (0);
59 }