1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5 
6 #include "libgccjit.h"
7 
8 #include "harness.h"
9 
10 #define BIG_BLOB_SIZE (1 << 12) /* 4KB.  */
11 
12 static signed char test_blob1[] = { 0xc, 0xa, 0xf, 0xf, 0xe };
13 static unsigned test_blob2[] = { 0x3, 0x2, 0x1, 0x0, 0x1, 0x2, 0x3 };
14 static unsigned char test_blob3[BIG_BLOB_SIZE];
15 
16 void
create_code(gcc_jit_context * ctxt,void * user_data)17 create_code (gcc_jit_context *ctxt, void *user_data)
18 {
19   /* Let's try to inject the equivalent of:
20 
21      signed char bin_blob1[] = { 0xc, 0xa, 0xf, 0xf, 0xe };
22      unsigned bin_blob2[] = { 0x3, 0x2, 0x1, 0x0, 0x1, 0x2, 0x3 };
23      unsigned char bin_blob3[4096]...
24   */
25   gcc_jit_type *unsigned_char_type =
26     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_CHAR);
27   gcc_jit_type *signed_char_type =
28     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIGNED_CHAR);
29   gcc_jit_type *unsigned_type =
30     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT);
31 
32   gcc_jit_lvalue *glob =
33     gcc_jit_context_new_global (
34       ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
35       gcc_jit_context_new_array_type (ctxt, NULL, signed_char_type,
36 				      sizeof (test_blob1)),
37       "bin_blob1");
38   gcc_jit_global_set_initializer (glob, test_blob1, sizeof (test_blob1));
39 
40   glob =
41     gcc_jit_context_new_global (
42       ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
43       gcc_jit_context_new_array_type (
44 	ctxt, NULL, unsigned_type,
45 	sizeof (test_blob2) / sizeof (*test_blob2)),
46       "bin_blob2");
47   gcc_jit_global_set_initializer (glob, test_blob2,
48 				  sizeof (test_blob2));
49 
50   for (size_t i = 0; i < BIG_BLOB_SIZE; i++)
51     test_blob3[i] = i * i + i;
52   glob =
53     gcc_jit_context_new_global (
54       ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
55       gcc_jit_context_new_array_type (ctxt, NULL, unsigned_char_type,
56 				      sizeof (test_blob3)),
57       "bin_blob3");
58   gcc_jit_global_set_initializer (glob, test_blob3, sizeof (test_blob3));
59 }
60 
61 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)62 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
63 {
64   CHECK_NON_NULL (result);
65   void *glob = gcc_jit_result_get_global (result, "bin_blob1");
66   CHECK_NON_NULL (glob);
67   CHECK_VALUE (memcmp (test_blob1, glob, sizeof (test_blob1)), 0);
68 
69   glob = gcc_jit_result_get_global (result, "bin_blob2");
70   CHECK_NON_NULL (glob);
71   CHECK_VALUE (memcmp (test_blob2, glob,
72 		       sizeof (test_blob2)), 0);
73 
74   glob = gcc_jit_result_get_global (result, "bin_blob3");
75   CHECK_NON_NULL (glob);
76   CHECK_VALUE (memcmp (test_blob3, glob, sizeof (test_blob3)), 0);
77 
78 }
79