Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Catch a more specific exception.
[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   try {
40     simgrid::dwarf::execute(ops, 3, state, stack);
41   } catch (const simgrid::dwarf::evaluation_error&) {
42     fprintf(stderr,"Expression evaluation error");
43   }
44
45   assert(stack.size() == 1);
46   return stack.top();
47 }
48
49 static
50 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
51   try {
52
53   Dwarf_Op ops[60];
54
55   uintptr_t a = rnd_engine();
56   uintptr_t b = rnd_engine();
57
58   simgrid::dwarf::ExpressionStack stack;
59
60   bool caught_ex = false;
61   try {
62     ops[0].atom = DW_OP_drop;
63     simgrid::dwarf::execute(ops, 1, state, stack);
64   } catch (const simgrid::dwarf::evaluation_error&) {
65     caught_ex = true;
66   }
67   if (not caught_ex)
68     fprintf(stderr, "Exception expected");
69
70   ops[0].atom = DW_OP_lit21;
71   simgrid::dwarf::execute(ops, 1, state, stack);
72   assert(stack.size() == 1);
73   assert(stack.top() == 21);
74
75   ops[0].atom = DW_OP_const8u;
76   ops[0].number = a;
77   simgrid::dwarf::execute(ops, 1, state, stack);
78   assert(stack.size() == 2);
79   assert(stack.top() == a);
80
81   ops[0].atom = DW_OP_drop;
82   ops[1].atom = DW_OP_drop;
83   simgrid::dwarf::execute(ops, 2, state, stack);
84   assert(stack.empty());
85
86   stack.clear();
87   ops[0].atom = DW_OP_lit21;
88   ops[1].atom = DW_OP_plus_uconst;
89   ops[1].number = a;
90   simgrid::dwarf::execute(ops, 2, state, stack);
91   assert(stack.size() == 1);
92   assert(stack.top() == a + 21);
93
94   stack.clear();
95   ops[0].atom = DW_OP_const8u;
96   ops[0].number = a;
97   ops[1].atom = DW_OP_dup;
98   ops[2].atom = DW_OP_plus;
99   simgrid::dwarf::execute(ops, 3, state, stack);
100   assert(stack.size() == 1);
101   assert(stack.top() == a + a);
102
103   stack.clear();
104   ops[0].atom = DW_OP_const8u;
105   ops[0].number = a;
106   ops[1].atom = DW_OP_const8u;
107   ops[1].number = b;
108   ops[2].atom = DW_OP_over;
109   simgrid::dwarf::execute(ops, 3, state, stack);
110   assert(stack.size() == 3);
111   assert(stack.top()  == a);
112   assert(stack.top(1) == b);
113   assert(stack.top(2) == a);
114
115   stack.clear();
116   ops[0].atom = DW_OP_const8u;
117   ops[0].number = a;
118   ops[1].atom = DW_OP_const8u;
119   ops[1].number = b;
120   ops[2].atom = DW_OP_swap;
121   simgrid::dwarf::execute(ops, 3, state, stack);
122   assert(stack.size() == 2);
123   assert(stack.top()  == a);
124   assert(stack.top(1) == b);
125
126   } catch (const simgrid::dwarf::evaluation_error&) {
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   } catch (const simgrid::dwarf::evaluation_error&) {
149     fprintf(stderr,"Expression evaluation error");
150   }
151 }
152
153 int main(int argc, char** argv) {
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 }