Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not take NULL to mean 'the current address space' in dwarf expressions
[simgrid.git] / teshsuite / mc / dwarf_expression / dwarf_expression.c
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifdef NDEBUG
8 #undef NDEBUG
9 #endif
10
11 #include <string.h>
12 #include <assert.h>
13 #include <stdlib.h>
14
15 #include "../src/mc/mc_private.h"
16 #include "../src/mc/mc_object_info.h"
17
18 static s_mc_process_t process;
19
20 static
21 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
22   state->stack_size = 0;
23
24   Dwarf_Op ops[15];
25   ops[0].atom = DW_OP_const8u;
26   ops[0].number = a;
27   ops[1].atom = DW_OP_const8u;
28   ops[1].number = b;
29   ops[2].atom = op;
30
31   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
32   assert(state->stack_size==1);
33   return state->stack[state->stack_size - 1];
34 }
35
36 static
37 void basic_test(mc_expression_state_t state) {
38   Dwarf_Op ops[60];
39
40   uintptr_t a = rand();
41   uintptr_t b = rand();
42
43   ops[0].atom = DW_OP_drop;
44   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
45
46   ops[0].atom = DW_OP_lit21;
47   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
48   assert(state->stack_size==1);
49   assert(state->stack[state->stack_size-1]==21);
50
51   ops[0].atom = DW_OP_const8u;
52   ops[0].number = a;
53   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
54   assert(state->stack_size==2);
55   assert(state->stack[state->stack_size-1] == a);
56
57   ops[0].atom = DW_OP_drop;
58   ops[1].atom = DW_OP_drop;
59   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
60   assert(state->stack_size==0);
61
62   ops[0].atom = DW_OP_lit21;
63   ops[1].atom = DW_OP_plus_uconst;
64   ops[1].number = a;
65   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
66   assert(state->stack_size==1);
67   assert(state->stack[state->stack_size-1]== a + 21);
68
69   state->stack_size = 0;
70   ops[0].atom = DW_OP_const8u;
71   ops[0].number = a;
72   ops[1].atom = DW_OP_dup;
73   ops[2].atom = DW_OP_plus;
74   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
75   assert(state->stack_size==1);
76   assert(state->stack[state->stack_size-1]== a + a);
77
78   state->stack_size = 0;
79   ops[0].atom = DW_OP_const8u;
80   ops[0].number = a;
81   ops[1].atom = DW_OP_const8u;
82   ops[1].number = b;
83   ops[2].atom = DW_OP_over;
84   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
85   assert(state->stack_size==3);
86   assert(state->stack[state->stack_size-1]== a);
87   assert(state->stack[state->stack_size-2]== b);
88   assert(state->stack[state->stack_size-3]== a);
89
90   state->stack_size = 0;
91   ops[0].atom = DW_OP_const8u;
92   ops[0].number = a;
93   ops[1].atom = DW_OP_const8u;
94   ops[1].number = b;
95   ops[2].atom = DW_OP_swap;
96   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
97   assert(state->stack_size=2);
98   assert(state->stack[state->stack_size-1]== a);
99   assert(state->stack[state->stack_size-2]== b);
100 }
101
102 static
103 void test_deref(mc_expression_state_t state) {
104   uintptr_t foo = 42;
105
106   Dwarf_Op ops[60];
107   ops[0].atom = DW_OP_const8u;
108   ops[0].number = (uintptr_t) &foo;
109   ops[1].atom = DW_OP_deref;
110   state->stack_size = 0;
111
112   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
113   assert(state->stack_size==1);
114   assert(state->stack[state->stack_size-1] == foo);
115 }
116
117 int main(int argc, char** argv) {
118   MC_process_init(&process, getpid(), -1);
119
120   s_mc_expression_state_t state;
121   memset(&state, 0, sizeof(s_mc_expression_state_t));
122   state.address_space = (mc_address_space_t) &process;
123
124   basic_test(&state);
125
126   for(int i=0; i!=100; ++i) {
127     uintptr_t a = rand();
128     uintptr_t b = rand();
129     assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
130   }
131
132   for(int i=0; i!=100; ++i) {
133     uintptr_t a = rand();
134     uintptr_t b = rand();
135     assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
136   }
137
138   for(int i=0; i!=100; ++i) {
139     uintptr_t a = rand();
140     uintptr_t b = rand();
141     assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
142   }
143
144   for(int i=0; i!=100; ++i) {
145     uintptr_t a = rand();
146     uintptr_t b = rand();
147     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
148   }
149
150   test_deref(&state);
151
152   return 0;
153 }