Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix+activate rma test
[simgrid.git] / teshsuite / mc / dwarf-expression / dwarf-expression.cpp
1 /* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifdef NDEBUG
7 #undef NDEBUG
8 #endif
9
10 #include "src/mc/mc_private.hpp"
11
12 #include "src/mc/inspect/ObjectInformation.hpp"
13 #include "src/mc/inspect/Type.hpp"
14 #include "src/mc/inspect/Variable.hpp"
15 #include "src/mc/remote/RemoteClient.hpp"
16
17 #include <cassert>
18 #include <cstdlib>
19 #include <cstring>
20 #include <random>
21
22 static std::default_random_engine rnd_engine;
23
24 static simgrid::mc::RemoteClient* process;
25
26 static
27 uintptr_t eval_binary_operation(
28   simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
29
30   Dwarf_Op ops[15];
31   ops[0].atom = DW_OP_const8u;
32   ops[0].number = a;
33   ops[1].atom = DW_OP_const8u;
34   ops[1].number = b;
35   ops[2].atom = op;
36
37   simgrid::dwarf::ExpressionStack stack;
38   try {
39     simgrid::dwarf::execute(ops, 3, state, stack);
40   } catch (const simgrid::dwarf::evaluation_error&) {
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 = rnd_engine();
55   uintptr_t b = rnd_engine();
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 (const simgrid::dwarf::evaluation_error&) {
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   } catch (const simgrid::dwarf::evaluation_error&) {
126     fprintf(stderr,"Expression evaluation error");
127   }
128 }
129
130 static
131 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
132   try {
133
134   uintptr_t foo = 42;
135
136   Dwarf_Op ops[60];
137   ops[0].atom = DW_OP_const8u;
138   ops[0].number = (uintptr_t) &foo;
139   ops[1].atom = DW_OP_deref;
140
141   simgrid::dwarf::ExpressionStack stack;
142
143   simgrid::dwarf::execute(ops, 2, state, stack);
144   assert(stack.size() == 1);
145   assert(stack.top()  == foo);
146
147   } catch (const simgrid::dwarf::evaluation_error&) {
148     fprintf(stderr,"Expression evaluation error");
149   }
150 }
151
152 int main()
153 {
154   process = new simgrid::mc::RemoteClient(getpid(), -1);
155   process->init();
156
157   simgrid::dwarf::ExpressionContext state;
158   state.address_space = (simgrid::mc::AddressSpace*) process;
159
160   basic_test(state);
161
162   for(int i=0; i!=100; ++i) {
163     uintptr_t a = rnd_engine();
164     uintptr_t b = rnd_engine();
165     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
166   }
167
168   for(int i=0; i!=100; ++i) {
169     uintptr_t a = rnd_engine();
170     uintptr_t b = rnd_engine();
171     assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
172   }
173
174   for(int i=0; i!=100; ++i) {
175     uintptr_t a = rnd_engine();
176     uintptr_t b = rnd_engine();
177     assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
178   }
179
180   for(int i=0; i!=100; ++i) {
181     uintptr_t a = rnd_engine();
182     uintptr_t b = rnd_engine();
183     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
184   }
185
186   test_deref(state);
187
188   return 0;
189 }