Logo AND Algorithmique Numérique Distribuée

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