Logo AND Algorithmique Numérique Distribuée

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