1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stddef.h>
4 
5 #include "libgccjit.h"
6 
7 #include "harness.h"
8 
9 void
create_code(gcc_jit_context * ctxt,void * user_data)10 create_code (gcc_jit_context *ctxt, void *user_data)
11 {
12   /* Let's try to inject the equivalent of:
13 
14      void
15      test_trap (void)
16      {
17        *((int *)0) = 42;
18      }
19   */
20   gcc_jit_type *void_type
21     = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
22   gcc_jit_type *int_type
23     = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
24   gcc_jit_type *int_ptr_type
25     = gcc_jit_type_get_pointer (int_type);
26 
27   /* Build the test_fn.  */
28   gcc_jit_function *func
29     = gcc_jit_context_new_function (ctxt, NULL,
30 				    GCC_JIT_FUNCTION_EXPORTED,
31 				    void_type,
32 				    "test_trap",
33 				    0, NULL,
34 				    0);
35 
36   gcc_jit_block *initial = gcc_jit_function_new_block (func, "initial");
37 
38   gcc_jit_rvalue *null_ptr
39     = gcc_jit_context_new_rvalue_from_ptr (ctxt, int_ptr_type, NULL);
40 
41   /* "*((int *)0) = 42;" */
42   gcc_jit_block_add_assignment (
43     initial, NULL,
44     gcc_jit_rvalue_dereference (null_ptr, NULL),
45     gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
46 
47   gcc_jit_block_end_with_void_return (initial, NULL);
48 }
49 
50 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)51 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
52 {
53   typedef void (*fn_type) (void);
54   CHECK_NON_NULL (result);
55   fn_type test_array =
56     (fn_type)gcc_jit_result_get_code (result, "test_trap");
57   CHECK_NON_NULL (test_array);
58   /* Don't attempt to call it.  */
59 }
60