Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / teshsuite / mc / dwarf-expression / dwarf-expression.cpp
1 /* Copyright (c) 2014-2018. 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 <cassert>
12 #include <cstdlib>
13 #include <cstring>
14
15 #include "src/mc/mc_private.hpp"
16
17 #include "src/mc/ObjectInformation.hpp"
18 #include "src/mc/Type.hpp"
19 #include "src/mc/Variable.hpp"
20 #include "src/mc/remote/RemoteClient.hpp"
21
22 static simgrid::mc::RemoteClient* process;
23
24 static
25 uintptr_t eval_binary_operation(
26   simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
27
28   Dwarf_Op ops[15];
29   ops[0].atom = DW_OP_const8u;
30   ops[0].number = a;
31   ops[1].atom = DW_OP_const8u;
32   ops[1].number = b;
33   ops[2].atom = op;
34
35   simgrid::dwarf::ExpressionStack stack;
36
37   try {
38     simgrid::dwarf::execute(ops, 3, state, stack);
39   }
40   catch(std::runtime_error& e) {
41     fprintf(stderr,"Expression evaluation error");
42   }
43
44   assert(stack.size() == 1);
45   return stack.top();
46 }
47
48 static
49 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
50   try {
51
52   Dwarf_Op ops[60];
53
54   uintptr_t a = rand();
55   uintptr_t b = rand();
56
57   simgrid::dwarf::ExpressionStack stack;
58
59   bool caught_ex = false;
60   try {
61     ops[0].atom = DW_OP_drop;
62     simgrid::dwarf::execute(ops, 1, state, stack);
63   } catch (simgrid::dwarf::evaluation_error& e) {
64     caught_ex = true;
65   }
66   if (not caught_ex)
67     fprintf(stderr, "Exception expected");
68
69   ops[0].atom = DW_OP_lit21;
70   simgrid::dwarf::execute(ops, 1, state, stack);
71   assert(stack.size() == 1);
72   assert(stack.top() == 21);
73
74   ops[0].atom = DW_OP_const8u;
75   ops[0].number = a;
76   simgrid::dwarf::execute(ops, 1, state, stack);
77   assert(stack.size() == 2);
78   assert(stack.top() == a);
79
80   ops[0].atom = DW_OP_drop;
81   ops[1].atom = DW_OP_drop;
82   simgrid::dwarf::execute(ops, 2, state, stack);
83   assert(stack.empty());
84
85   stack.clear();
86   ops[0].atom = DW_OP_lit21;
87   ops[1].atom = DW_OP_plus_uconst;
88   ops[1].number = a;
89   simgrid::dwarf::execute(ops, 2, state, stack);
90   assert(stack.size() == 1);
91   assert(stack.top() == a + 21);
92
93   stack.clear();
94   ops[0].atom = DW_OP_const8u;
95   ops[0].number = a;
96   ops[1].atom = DW_OP_dup;
97   ops[2].atom = DW_OP_plus;
98   simgrid::dwarf::execute(ops, 3, state, stack);
99   assert(stack.size() == 1);
100   assert(stack.top() == a + a);
101
102   stack.clear();
103   ops[0].atom = DW_OP_const8u;
104   ops[0].number = a;
105   ops[1].atom = DW_OP_const8u;
106   ops[1].number = b;
107   ops[2].atom = DW_OP_over;
108   simgrid::dwarf::execute(ops, 3, state, stack);
109   assert(stack.size() == 3);
110   assert(stack.top()  == a);
111   assert(stack.top(1) == b);
112   assert(stack.top(2) == a);
113
114   stack.clear();
115   ops[0].atom = DW_OP_const8u;
116   ops[0].number = a;
117   ops[1].atom = DW_OP_const8u;
118   ops[1].number = b;
119   ops[2].atom = DW_OP_swap;
120   simgrid::dwarf::execute(ops, 3, state, stack);
121   assert(stack.size() == 2);
122   assert(stack.top()  == a);
123   assert(stack.top(1) == b);
124
125   }
126   catch(std::runtime_error& e) {
127     fprintf(stderr,"Expression evaluation error");
128   }
129 }
130
131 static
132 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
133   try {
134
135   uintptr_t foo = 42;
136
137   Dwarf_Op ops[60];
138   ops[0].atom = DW_OP_const8u;
139   ops[0].number = (uintptr_t) &foo;
140   ops[1].atom = DW_OP_deref;
141
142   simgrid::dwarf::ExpressionStack stack;
143
144   simgrid::dwarf::execute(ops, 2, state, stack);
145   assert(stack.size() == 1);
146   assert(stack.top()  == foo);
147
148   }
149   catch(std::runtime_error& e) {
150     fprintf(stderr,"Expression evaluation error");
151   }
152 }
153
154 int main(int argc, char** argv) {
155   process = new simgrid::mc::RemoteClient(getpid(), -1);
156   process->init();
157
158   simgrid::dwarf::ExpressionContext state;
159   state.address_space = (simgrid::mc::AddressSpace*) process;
160
161   basic_test(state);
162
163   for(int i=0; i!=100; ++i) {
164     uintptr_t a = rand();
165     uintptr_t b = rand();
166     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
167   }
168
169   for(int i=0; i!=100; ++i) {
170     uintptr_t a = rand();
171     uintptr_t b = rand();
172     assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
173   }
174
175   for(int i=0; i!=100; ++i) {
176     uintptr_t a = rand();
177     uintptr_t b = rand();
178     assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
179   }
180
181   for(int i=0; i!=100; ++i) {
182     uintptr_t a = rand();
183     uintptr_t b = rand();
184     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
185   }
186
187   test_deref(state);
188
189   return 0;
190 }