1 /* Copyright (c) 2014-2021. The SimGrid Team. All rights reserved. */
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. */
10 #include "src/mc/mc_private.hpp"
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"
21 #include <xbt/random.hpp>
23 static uintptr_t rnd_engine()
25 return simgrid::xbt::random::uniform_int(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
28 static uintptr_t eval_binary_operation(simgrid::dwarf::ExpressionContext const& state, uint8_t op, uintptr_t a,
31 std::array<Dwarf_Op, 15> ops;
32 ops[0].atom = DW_OP_const8u;
34 ops[1].atom = DW_OP_const8u;
38 simgrid::dwarf::ExpressionStack stack;
40 simgrid::dwarf::execute(ops.data(), 3, state, stack);
41 } catch (const simgrid::dwarf::evaluation_error&) {
42 fprintf(stderr,"Expression evaluation error");
45 assert(stack.size() == 1);
49 static void basic_test(simgrid::dwarf::ExpressionContext const& state)
51 std::array<Dwarf_Op, 60> ops;
53 uintptr_t a = rnd_engine();
54 uintptr_t b = rnd_engine();
56 simgrid::dwarf::ExpressionStack stack;
58 bool caught_ex = false;
60 ops[0].atom = DW_OP_drop;
61 simgrid::dwarf::execute(ops.data(), 1, state, stack);
62 } catch (const simgrid::dwarf::evaluation_error&) {
66 fprintf(stderr, "Exception expected");
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);
74 ops[0].atom = DW_OP_const8u;
76 simgrid::dwarf::execute(ops.data(), 1, state, stack);
77 assert(stack.size() == 2);
78 assert(stack.top() == a);
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());
86 ops[0].atom = DW_OP_lit21;
87 ops[1].atom = DW_OP_plus_uconst;
89 simgrid::dwarf::execute(ops.data(), 2, state, stack);
90 assert(stack.size() == 1);
91 assert(stack.top() == a + 21);
94 ops[0].atom = DW_OP_const8u;
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);
103 ops[0].atom = DW_OP_const8u;
105 ops[1].atom = DW_OP_const8u;
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);
115 ops[0].atom = DW_OP_const8u;
117 ops[1].atom = DW_OP_const8u;
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");
129 static void test_deref(simgrid::dwarf::ExpressionContext const& state)
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;
139 simgrid::dwarf::ExpressionStack stack;
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");
151 auto* process = new simgrid::mc::RemoteProcess(getpid());
152 process->init(nullptr, nullptr, nullptr, nullptr);
154 simgrid::dwarf::ExpressionContext state;
155 state.address_space = (simgrid::mc::AddressSpace*) process;
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));
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));
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));
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));