Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / teshsuite / mc / dwarf-expression / dwarf-expression.cpp
1 /* Copyright (c) 2014-2020. 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/RemoteSimulation.hpp"
16
17 #include <array>
18 #include <cassert>
19 #include <cstdlib>
20 #include <cstring>
21 #include <random>
22
23 static std::default_random_engine rnd_engine;
24
25 static uintptr_t eval_binary_operation(simgrid::dwarf::ExpressionContext const& state, uint8_t op, uintptr_t a,
26                                        uintptr_t b)
27 {
28   std::array<Dwarf_Op, 15> ops;
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   try {
37     simgrid::dwarf::execute(ops.data(), 3, state, stack);
38   } catch (const simgrid::dwarf::evaluation_error&) {
39     fprintf(stderr,"Expression evaluation error");
40   }
41
42   assert(stack.size() == 1);
43   return stack.top();
44 }
45
46 static void basic_test(simgrid::dwarf::ExpressionContext const& state)
47 {
48   std::array<Dwarf_Op, 60> ops;
49
50   uintptr_t a = rnd_engine();
51   uintptr_t b = rnd_engine();
52
53   simgrid::dwarf::ExpressionStack stack;
54
55   bool caught_ex = false;
56   try {
57     ops[0].atom = DW_OP_drop;
58     simgrid::dwarf::execute(ops.data(), 1, state, stack);
59   } catch (const simgrid::dwarf::evaluation_error&) {
60     caught_ex = true;
61   }
62   if (not caught_ex)
63     fprintf(stderr, "Exception expected");
64
65   try {
66     ops[0].atom = DW_OP_lit21;
67     simgrid::dwarf::execute(ops.data(), 1, state, stack);
68     assert(stack.size() == 1);
69     assert(stack.top() == 21);
70
71     ops[0].atom   = DW_OP_const8u;
72     ops[0].number = a;
73     simgrid::dwarf::execute(ops.data(), 1, state, stack);
74     assert(stack.size() == 2);
75     assert(stack.top() == a);
76
77     ops[0].atom = DW_OP_drop;
78     ops[1].atom = DW_OP_drop;
79     simgrid::dwarf::execute(ops.data(), 2, state, stack);
80     assert(stack.empty());
81
82     stack.clear();
83     ops[0].atom   = DW_OP_lit21;
84     ops[1].atom   = DW_OP_plus_uconst;
85     ops[1].number = a;
86     simgrid::dwarf::execute(ops.data(), 2, state, stack);
87     assert(stack.size() == 1);
88     assert(stack.top() == a + 21);
89
90     stack.clear();
91     ops[0].atom   = DW_OP_const8u;
92     ops[0].number = a;
93     ops[1].atom   = DW_OP_dup;
94     ops[2].atom   = DW_OP_plus;
95     simgrid::dwarf::execute(ops.data(), 3, state, stack);
96     assert(stack.size() == 1);
97     assert(stack.top() == a + a);
98
99     stack.clear();
100     ops[0].atom   = DW_OP_const8u;
101     ops[0].number = a;
102     ops[1].atom   = DW_OP_const8u;
103     ops[1].number = b;
104     ops[2].atom   = DW_OP_over;
105     simgrid::dwarf::execute(ops.data(), 3, state, stack);
106     assert(stack.size() == 3);
107     assert(stack.top() == a);
108     assert(stack.top(1) == b);
109     assert(stack.top(2) == a);
110
111     stack.clear();
112     ops[0].atom   = DW_OP_const8u;
113     ops[0].number = a;
114     ops[1].atom   = DW_OP_const8u;
115     ops[1].number = b;
116     ops[2].atom   = DW_OP_swap;
117     simgrid::dwarf::execute(ops.data(), 3, state, stack);
118     assert(stack.size() == 2);
119     assert(stack.top() == a);
120     assert(stack.top(1) == b);
121   } catch (const simgrid::dwarf::evaluation_error&) {
122     fprintf(stderr,"Expression evaluation error");
123   }
124 }
125
126 static void test_deref(simgrid::dwarf::ExpressionContext const& state)
127 {
128   try {
129     uintptr_t foo = 42;
130
131     std::array<Dwarf_Op, 60> ops;
132     ops[0].atom   = DW_OP_const8u;
133     ops[0].number = (uintptr_t)&foo;
134     ops[1].atom   = DW_OP_deref;
135
136     simgrid::dwarf::ExpressionStack stack;
137
138     simgrid::dwarf::execute(ops.data(), 2, state, stack);
139     assert(stack.size() == 1);
140     assert(stack.top() == foo);
141   } catch (const simgrid::dwarf::evaluation_error&) {
142     fprintf(stderr,"Expression evaluation error");
143   }
144 }
145
146 int main()
147 {
148   auto* process = new simgrid::mc::RemoteSimulation(getpid());
149   process->init();
150
151   simgrid::dwarf::ExpressionContext state;
152   state.address_space = (simgrid::mc::AddressSpace*) process;
153
154   basic_test(state);
155
156   for(int i=0; i!=100; ++i) {
157     uintptr_t a = rnd_engine();
158     uintptr_t b = rnd_engine();
159     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
160   }
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_or, 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_and, 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_xor, a, b) == (a ^ b));
178   }
179
180   test_deref(state);
181
182   return 0;
183 }