Update copyright to LGPL on all files
[dyninst.git] / testsuite / src / dyninst / test1_7.C
1 /*
2  * Copyright (c) 1996-2009 Barton P. Miller
3  * 
4  * We provide the Paradyn Parallel Performance Tools (below
5  * described as "Paradyn") on an AS IS basis, and do not warrant its
6  * validity or performance.  We reserve the right to update, modify,
7  * or discontinue this software at any time.  We shall have no
8  * obligation to supply such updates or modifications or any other
9  * form of support to you.
10  * 
11  * By your use of Paradyn, you understand and agree that we (or any
12  * other person or entity with proprietary rights in Paradyn) are
13  * under no obligation to provide either maintenance services,
14  * update services, notices of latent defects, or correction of
15  * defects for Paradyn.
16  * 
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  * 
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Lesser General Public License for more details.
26  * 
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31
32 // $Id: test1_7.C,v 1.1 2008/10/30 19:19:53 legendre Exp $
33 /*
34  * #Name: test1_7
35  * #Desc: Mutator Side - Relational Operators
36  * #Arch: all
37  * #Dep: 
38  */
39
40 #include "BPatch.h"
41 #include "BPatch_Vector.h"
42 #include "BPatch_thread.h"
43 #include "BPatch_snippet.h"
44
45 #include "test_lib.h"
46 #include "dyninst_comp.h"
47
48 class test1_7_Mutator : public DyninstMutator {
49         virtual test_results_t executeTest();
50 };
51
52 extern "C" DLLEXPORT  TestMutator *test1_7_factory() 
53 {
54         return new test1_7_Mutator();
55 }
56
57 static int genRelTest(BPatch_image *appImage,
58                 BPatch_Vector<BPatch_snippet*> &vect7_1,
59                 BPatch_relOp op, int r1, int r2, const char *var1) 
60 {
61         const char *funcName = "test1_7_func1";
62         BPatch_Vector<BPatch_function *> found_funcs;
63         if ((NULL == appImage->findFunction(funcName, found_funcs))
64                         || !found_funcs.size()) 
65         {
66                 logerror("    Unable to find function %s\n", funcName);
67                 return -1;
68         }
69
70         if (1 < found_funcs.size()) 
71         {
72                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
73                                 __FILE__, __LINE__, found_funcs.size(), funcName);
74         }
75
76         BPatch_Vector<BPatch_point *> *point7_1 = found_funcs[0]->findPoint(BPatch_entry);
77
78         if (!point7_1 || ((*point7_1).size() == 0)) 
79         {
80                 logerror("Unable to find entry point to \"%s\".\n", funcName);
81                 return -1;
82         }
83
84         BPatch_variableExpr *expr1_1 = findVariable (appImage, var1, point7_1);
85
86         if (!expr1_1) 
87         {
88                 logerror("**Failed** test #7 (relational operators)\n");
89                 logerror("    Unable to locate variable %s\n", var1);
90                 return -1;
91         }
92
93         BPatch_ifExpr *tempExpr1 = new BPatch_ifExpr(
94                         BPatch_boolExpr(op, BPatch_constExpr(r1), BPatch_constExpr(r2)), 
95                         BPatch_arithExpr(BPatch_assign, *expr1_1, BPatch_constExpr(72)));
96         vect7_1.push_back(tempExpr1);
97
98         return 0;
99 }
100
101 static int genVRelTest(BPatch_image *appImage,
102                 BPatch_Vector<BPatch_snippet*> &vect7_1, 
103                 BPatch_relOp op, BPatch_variableExpr *r1, 
104                 BPatch_variableExpr *r2, const char *var1)
105 {
106         const char *funcName = "test1_7_func1";
107         BPatch_Vector<BPatch_function *> found_funcs;
108
109         if ((NULL == appImage->findFunction(funcName, found_funcs)) || !found_funcs.size()) 
110         {
111                 logerror("    Unable to find function %s\n", funcName);
112                 return -1;
113         }
114
115         if (1 < found_funcs.size()) 
116         {
117                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
118                                 __FILE__, __LINE__, found_funcs.size(), funcName);
119         }
120
121         BPatch_Vector<BPatch_point *> *point7_1 = found_funcs[0]->findPoint(BPatch_entry);
122
123         if (!point7_1 || ((*point7_1).size() == 0)) 
124         {
125                 logerror("Unable to find entry point to \"%s\".\n", funcName);
126                 return -1;
127         }
128
129         BPatch_variableExpr *expr1_1 = findVariable(appImage, var1, point7_1);
130
131         if (!expr1_1) 
132         {
133                 logerror("**Failed** test #7 (relational operators)\n");
134                 logerror("    Unable to locate variable %s\n", var1);
135                 return -1;
136         }
137
138         BPatch_ifExpr *tempExpr1 = new BPatch_ifExpr(
139                         BPatch_boolExpr(op, *r1, *r2), 
140                         BPatch_arithExpr(BPatch_assign, *expr1_1, BPatch_constExpr(74)));
141         vect7_1.push_back(tempExpr1);
142
143         return 0;
144 }
145
146 //
147 // Start Test Case #7 - mutator side (relational operators)
148 //
149
150 test_results_t test1_7_Mutator::executeTest() 
151 {
152         // Find the entry point to the procedure "func7_2"
153         const char *funcName = "test1_7_func2";
154         BPatch_Vector<BPatch_function *> found_funcs;
155
156         if ((NULL == appImage->findFunction(funcName, found_funcs))
157                         || !found_funcs.size()) 
158         {
159                 logerror("    Unable to find function %s\n", funcName);
160                 return FAILED;
161         }
162
163         if (1 < found_funcs.size()) 
164         {
165                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
166                                 __FILE__, __LINE__, found_funcs.size(), funcName);
167         }
168
169         BPatch_Vector<BPatch_point *> *point7_1 = found_funcs[0]->findPoint(BPatch_entry);
170
171         if (!point7_1 || ((*point7_1).size() == 0)) 
172         {
173                 logerror("Unable to find entry point to \"%s\".\n", funcName);
174                 return FAILED;
175         }
176
177         BPatch_Vector<BPatch_snippet*> vect7_1;
178
179         if ( genRelTest(appImage, vect7_1, BPatch_lt, 0, 1,
180                                 "test1_7_globalVariable1") < 0)
181                 return FAILED;
182         if ( genRelTest(appImage, vect7_1, BPatch_lt, 1, 0,
183                                 "test1_7_globalVariable2") < 0 )
184                 return FAILED;
185         if ( genRelTest(appImage, vect7_1, BPatch_eq, 2, 2,
186                                 "test1_7_globalVariable3") < 0 )
187                 return FAILED;
188         if ( genRelTest(appImage, vect7_1, BPatch_eq, 2, 3,
189                                 "test1_7_globalVariable4") < 0 )
190                 return FAILED;
191         if ( genRelTest(appImage, vect7_1, BPatch_gt, 4, 3,
192                                 "test1_7_globalVariable5") < 0 )
193                 return FAILED;
194         if ( genRelTest(appImage, vect7_1, BPatch_gt, 3, 4,
195                                 "test1_7_globalVariable6") < 0 )
196                 return FAILED;
197         if ( genRelTest(appImage, vect7_1, BPatch_le, 3, 4,
198                                 "test1_7_globalVariable7") < 0 )
199                 return FAILED;
200         if ( genRelTest(appImage, vect7_1, BPatch_le, 4, 3,
201                                 "test1_7_globalVariable8") < 0 )
202                 return FAILED;
203         if ( genRelTest(appImage, vect7_1, BPatch_ne, 5, 6,
204                                 "test1_7_globalVariable9") < 0 )
205                 return FAILED;
206         if ( genRelTest(appImage, vect7_1, BPatch_ne, 5, 5,
207                                 "test1_7_globalVariable10") < 0 )
208                 return FAILED;
209         if ( genRelTest(appImage, vect7_1, BPatch_ge, 9, 7,
210                                 "test1_7_globalVariable11") < 0 )
211                 return FAILED;
212         if ( genRelTest(appImage, vect7_1, BPatch_ge, 7, 9,
213                                 "test1_7_globalVariable12") < 0 )
214                 return FAILED;
215         if ( genRelTest(appImage, vect7_1, BPatch_and, 1, 1,
216                                 "test1_7_globalVariable13") < 0 )
217                 return FAILED;
218         if ( genRelTest(appImage, vect7_1, BPatch_and, 1, 0,
219                                 "test1_7_globalVariable14") < 0 )
220                 return FAILED;
221         if ( genRelTest(appImage, vect7_1, BPatch_or, 1, 0,
222                                 "test1_7_globalVariable15") < 0 )
223                 return FAILED;
224         if ( genRelTest(appImage, vect7_1, BPatch_or, 0, 0,
225                                 "test1_7_globalVariable16") < 0 )
226                 return FAILED;
227
228         const char *funcName2 = "test1_7_func1";
229         BPatch_Vector<BPatch_function *> found_funcs2;
230
231         if ((NULL == appImage->findFunction(funcName2, found_funcs2))
232                         || !found_funcs2.size()) 
233         {
234                 logerror("    Unable to find function %s\n", funcName2);
235                 return FAILED;
236         }
237
238         if (1 < found_funcs2.size()) 
239         {
240                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
241                                 __FILE__, __LINE__, found_funcs2.size(), funcName2);
242         }
243
244         BPatch_Vector<BPatch_point *> *func7_1 = found_funcs2[0]->findPoint(BPatch_subroutine);
245
246         if (!func7_1 || ((*func7_1).size() == 0)) 
247         {
248                 logerror("Unable to find subroutine points in \"%s\".\n", funcName2);
249                 return FAILED;
250         }
251
252         BPatch_variableExpr *constVar0, *constVar1, *constVar2, *constVar3, *constVar4, 
253                                                 *constVar5, *constVar6, *constVar7, *constVar9;
254
255         constVar0 = findVariable(appImage, "test1_7_constVar0", func7_1);
256         constVar1 = findVariable(appImage, "test1_7_constVar1", func7_1);
257         constVar2 = findVariable(appImage, "test1_7_constVar2", func7_1);
258         constVar3 = findVariable(appImage, "test1_7_constVar3", func7_1);
259         constVar4 = findVariable(appImage, "test1_7_constVar4", func7_1);
260         constVar5 = findVariable(appImage, "test1_7_constVar5", func7_1);
261         constVar6 = findVariable(appImage, "test1_7_constVar6", func7_1);
262         constVar7 = findVariable(appImage, "test1_7_constVar7", func7_1);
263         constVar9 = findVariable(appImage, "test1_7_constVar9", func7_1);
264
265         if (!constVar0 || !constVar1 || !constVar2 || !constVar3 || !constVar4 ||
266                         !constVar5 || !constVar6 || !constVar7 || !constVar9 ) 
267         {
268                 logerror("**Failed** test #7 (relational operators)\n");
269                 logerror("    Unable to locate one of test1_7_constVar?\n");
270                 return FAILED;
271         }
272
273         if ( genVRelTest(appImage, vect7_1, BPatch_lt, constVar0, constVar1,
274                                 "test1_7_globalVariable1a") < 0 )
275                 return FAILED;
276         if ( genVRelTest(appImage, vect7_1, BPatch_lt, constVar1, constVar0,
277                                 "test1_7_globalVariable2a") < 0 )
278                 return FAILED;
279         if ( genVRelTest(appImage, vect7_1, BPatch_eq, constVar2, constVar2,
280                                 "test1_7_globalVariable3a") < 0 )
281                 return FAILED;
282         if ( genVRelTest(appImage, vect7_1, BPatch_eq, constVar2, constVar3,
283                                 "test1_7_globalVariable4a") < 0 )
284                 return FAILED;
285         if ( genVRelTest(appImage, vect7_1, BPatch_gt, constVar4, constVar3,
286                                 "test1_7_globalVariable5a") < 0 )
287                 return FAILED;
288         if ( genVRelTest(appImage, vect7_1, BPatch_gt, constVar3, constVar4,
289                                 "test1_7_globalVariable6a") < 0 )
290                 return FAILED;
291         if ( genVRelTest(appImage, vect7_1, BPatch_le, constVar3, constVar4,
292                                 "test1_7_globalVariable7a") < 0 )
293                 return FAILED;
294         if ( genVRelTest(appImage, vect7_1, BPatch_le, constVar4, constVar3,
295                                 "test1_7_globalVariable8a") < 0 )
296                 return FAILED;
297         if ( genVRelTest(appImage, vect7_1, BPatch_ne, constVar5, constVar6,
298                                 "test1_7_globalVariable9a") < 0 )
299                 return FAILED;
300         if ( genVRelTest(appImage, vect7_1, BPatch_ne, constVar5, constVar5,
301                                 "test1_7_globalVariable10a") < 0 )
302                 return FAILED;
303         if ( genVRelTest(appImage, vect7_1, BPatch_ge, constVar9, constVar7,
304                                 "test1_7_globalVariable11a") < 0 )
305                 return FAILED;
306         if ( genVRelTest(appImage, vect7_1, BPatch_ge, constVar7, constVar9,
307                                 "test1_7_globalVariable12a") < 0 )
308                 return FAILED;
309         if ( genVRelTest(appImage, vect7_1, BPatch_and, constVar1, constVar1,
310                                 "test1_7_globalVariable13a") < 0 )
311                 return FAILED;
312         if ( genVRelTest(appImage, vect7_1, BPatch_and, constVar1, constVar0,
313                                 "test1_7_globalVariable14a") < 0 )
314                 return FAILED;
315         if ( genVRelTest(appImage, vect7_1, BPatch_or, constVar1, constVar0,
316                                 "test1_7_globalVariable15a") < 0 )
317                 return FAILED;
318         if ( genVRelTest(appImage, vect7_1, BPatch_or, constVar0, constVar0,
319                                 "test1_7_globalVariable16a") < 0 )
320                 return FAILED;
321
322         dprintf("relops test vector length is %d\n", vect7_1.size());
323
324         checkCost(BPatch_sequence(vect7_1));
325         appAddrSpace->insertSnippet( BPatch_sequence(vect7_1), *point7_1);
326
327         return PASSED;
328 }