1 /* Copyright (c) 2014-2019. The SimGrid Team.
2 * All rights reserved. */
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. */
16 #include "src/mc/mc_private.hpp"
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"
23 static std::default_random_engine rnd_engine;
25 static simgrid::mc::RemoteClient* process;
28 uintptr_t eval_binary_operation(
29 simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
32 ops[0].atom = DW_OP_const8u;
34 ops[1].atom = DW_OP_const8u;
38 simgrid::dwarf::ExpressionStack stack;
41 simgrid::dwarf::execute(ops, 3, state, stack);
43 catch(std::runtime_error& e) {
44 fprintf(stderr,"Expression evaluation error");
47 assert(stack.size() == 1);
52 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
57 uintptr_t a = rnd_engine();
58 uintptr_t b = rnd_engine();
60 simgrid::dwarf::ExpressionStack stack;
62 bool caught_ex = false;
64 ops[0].atom = DW_OP_drop;
65 simgrid::dwarf::execute(ops, 1, state, stack);
66 } catch (simgrid::dwarf::evaluation_error& e) {
70 fprintf(stderr, "Exception expected");
72 ops[0].atom = DW_OP_lit21;
73 simgrid::dwarf::execute(ops, 1, state, stack);
74 assert(stack.size() == 1);
75 assert(stack.top() == 21);
77 ops[0].atom = DW_OP_const8u;
79 simgrid::dwarf::execute(ops, 1, state, stack);
80 assert(stack.size() == 2);
81 assert(stack.top() == a);
83 ops[0].atom = DW_OP_drop;
84 ops[1].atom = DW_OP_drop;
85 simgrid::dwarf::execute(ops, 2, state, stack);
86 assert(stack.empty());
89 ops[0].atom = DW_OP_lit21;
90 ops[1].atom = DW_OP_plus_uconst;
92 simgrid::dwarf::execute(ops, 2, state, stack);
93 assert(stack.size() == 1);
94 assert(stack.top() == a + 21);
97 ops[0].atom = DW_OP_const8u;
99 ops[1].atom = DW_OP_dup;
100 ops[2].atom = DW_OP_plus;
101 simgrid::dwarf::execute(ops, 3, state, stack);
102 assert(stack.size() == 1);
103 assert(stack.top() == a + a);
106 ops[0].atom = DW_OP_const8u;
108 ops[1].atom = DW_OP_const8u;
110 ops[2].atom = DW_OP_over;
111 simgrid::dwarf::execute(ops, 3, state, stack);
112 assert(stack.size() == 3);
113 assert(stack.top() == a);
114 assert(stack.top(1) == b);
115 assert(stack.top(2) == a);
118 ops[0].atom = DW_OP_const8u;
120 ops[1].atom = DW_OP_const8u;
122 ops[2].atom = DW_OP_swap;
123 simgrid::dwarf::execute(ops, 3, state, stack);
124 assert(stack.size() == 2);
125 assert(stack.top() == a);
126 assert(stack.top(1) == b);
129 catch(std::runtime_error& e) {
130 fprintf(stderr,"Expression evaluation error");
135 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
141 ops[0].atom = DW_OP_const8u;
142 ops[0].number = (uintptr_t) &foo;
143 ops[1].atom = DW_OP_deref;
145 simgrid::dwarf::ExpressionStack stack;
147 simgrid::dwarf::execute(ops, 2, state, stack);
148 assert(stack.size() == 1);
149 assert(stack.top() == foo);
152 catch(std::runtime_error& e) {
153 fprintf(stderr,"Expression evaluation error");
157 int main(int argc, char** argv) {
158 process = new simgrid::mc::RemoteClient(getpid(), -1);
161 simgrid::dwarf::ExpressionContext state;
162 state.address_space = (simgrid::mc::AddressSpace*) process;
166 for(int i=0; i!=100; ++i) {
167 uintptr_t a = rnd_engine();
168 uintptr_t b = rnd_engine();
169 assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
172 for(int i=0; i!=100; ++i) {
173 uintptr_t a = rnd_engine();
174 uintptr_t b = rnd_engine();
175 assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
178 for(int i=0; i!=100; ++i) {
179 uintptr_t a = rnd_engine();
180 uintptr_t b = rnd_engine();
181 assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
184 for(int i=0; i!=100; ++i) {
185 uintptr_t a = rnd_engine();
186 uintptr_t b = rnd_engine();
187 assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));