Logo AND Algorithmique Numérique Distribuée

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