Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
And now cleanup the App-side of cruft that was needed for Checker to read actor infor...
[simgrid.git] / teshsuite / mc / dwarf-expression / dwarf-expression.cpp
1 /* Copyright (c) 2014-2022. 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/RemoteProcess.hpp"
16
17 #include <array>
18 #include <cassert>
19 #include <cstdlib>
20 #include <limits>
21 #include <xbt/random.hpp>
22
23 static uintptr_t rnd_engine()
24 {
25   return simgrid::xbt::random::uniform_int(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
26 }
27
28 static uintptr_t eval_binary_operation(simgrid::dwarf::ExpressionContext const& state, uint8_t op, uintptr_t a,
29                                        uintptr_t b)
30 {
31   std::array<Dwarf_Op, 15> ops;
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.data(), 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 void basic_test(simgrid::dwarf::ExpressionContext const& state)
50 {
51   std::array<Dwarf_Op, 60> ops;
52
53   uintptr_t a = rnd_engine();
54   uintptr_t b = rnd_engine();
55
56   simgrid::dwarf::ExpressionStack stack;
57
58   bool caught_ex = false;
59   try {
60     ops[0].atom = DW_OP_drop;
61     simgrid::dwarf::execute(ops.data(), 1, state, stack);
62   } catch (const simgrid::dwarf::evaluation_error&) {
63     caught_ex = true;
64   }
65   if (not caught_ex)
66     fprintf(stderr, "Exception expected");
67
68   try {
69     ops[0].atom = DW_OP_lit21;
70     simgrid::dwarf::execute(ops.data(), 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.data(), 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.data(), 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.data(), 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.data(), 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.data(), 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.data(), 3, state, stack);
121     assert(stack.size() == 2);
122     assert(stack.top() == a);
123     assert(stack.top(1) == b);
124   } catch (const simgrid::dwarf::evaluation_error&) {
125     fprintf(stderr,"Expression evaluation error");
126   }
127 }
128
129 static void test_deref(simgrid::dwarf::ExpressionContext const& state)
130 {
131   try {
132     uintptr_t foo = 42;
133
134     std::array<Dwarf_Op, 60> ops;
135     ops[0].atom   = DW_OP_const8u;
136     ops[0].number = (uintptr_t)&foo;
137     ops[1].atom   = DW_OP_deref;
138
139     simgrid::dwarf::ExpressionStack stack;
140
141     simgrid::dwarf::execute(ops.data(), 2, state, stack);
142     assert(stack.size() == 1);
143     assert(stack.top() == foo);
144   } catch (const simgrid::dwarf::evaluation_error&) {
145     fprintf(stderr,"Expression evaluation error");
146   }
147 }
148
149 int main()
150 {
151   auto* process = new simgrid::mc::RemoteProcess(getpid());
152   process->init(nullptr, nullptr);
153
154   simgrid::dwarf::ExpressionContext state;
155   state.address_space = (simgrid::mc::AddressSpace*) process;
156
157   basic_test(state);
158
159   for(int i=0; i!=100; ++i) {
160     uintptr_t a = rnd_engine();
161     uintptr_t b = rnd_engine();
162     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
163   }
164
165   for(int i=0; i!=100; ++i) {
166     uintptr_t a = rnd_engine();
167     uintptr_t b = rnd_engine();
168     assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
169   }
170
171   for(int i=0; i!=100; ++i) {
172     uintptr_t a = rnd_engine();
173     uintptr_t b = rnd_engine();
174     assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
175   }
176
177   for(int i=0; i!=100; ++i) {
178     uintptr_t a = rnd_engine();
179     uintptr_t b = rnd_engine();
180     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
181   }
182
183   test_deref(state);
184
185   return 0;
186 }