Update copyright to LGPL on all files
[dyninst.git] / testsuite / src / dyninst / test1_36.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_36.C,v 1.1 2008/10/30 19:19:19 legendre Exp $
33 /*
34  * #Name: test1_36
35  * #Desc: Callsite Parameter Referencing
36  * #Dep: 
37  * #Arch:
38  * #Notes:
39  */
40
41 #include "BPatch.h"
42 #include "BPatch_Vector.h"
43 #include "BPatch_thread.h"
44 #include "BPatch_snippet.h"
45
46 #include "test_lib.h"
47 #include "dyninst_comp.h"
48
49 class test1_36_Mutator : public DyninstMutator {
50
51         virtual test_results_t executeTest();
52
53         BPatch_arithExpr *makeTest36paramExpr(BPatch_snippet *expr, int paramId);
54 };
55
56 extern "C" DLLEXPORT  TestMutator *test1_36_factory() 
57 {
58         return new test1_36_Mutator();
59 }
60
61 BPatch_arithExpr *test1_36_Mutator::makeTest36paramExpr(BPatch_snippet *expr,
62                 int paramId)
63 {
64         if (isMutateeFortran(appImage)) 
65         {
66                 // Fortran is call by reference
67                 BPatch_arithExpr *derefExpr = new BPatch_arithExpr(BPatch_deref, *(new BPatch_paramExpr(paramId)));
68                 assert(derefExpr);
69                 return new BPatch_arithExpr(BPatch_assign, *expr, *derefExpr);
70         } 
71         else 
72         {
73                 return new BPatch_arithExpr(BPatch_assign, *expr, *(new BPatch_paramExpr(paramId)));
74         }
75 }
76
77 //
78 // Start Test Case #36 - (callsite parameter referencing)
79 //
80
81 test_results_t test1_36_Mutator::executeTest() 
82 {
83         const char *funcName = "test1_36_func1";
84         BPatch_Vector<BPatch_function *> found_funcs;
85
86         if ((NULL == appImage->findFunction(funcName, found_funcs))
87                         || !found_funcs.size()) 
88         {
89                 logerror("    Unable to find function %s\n", funcName);
90                 return FAILED;
91         }
92
93         if (1 < found_funcs.size()) 
94         {
95                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
96                                 __FILE__, __LINE__, found_funcs.size(), funcName);
97         }
98
99         BPatch_Vector<BPatch_point *> *all_points36_1 =
100                 found_funcs[0]->findPoint(BPatch_subroutine);
101
102         if (!all_points36_1 || (all_points36_1->size() < 1)) 
103         {
104                 logerror("Unable to find point %s - subroutines.\n", funcName);
105                 return FAILED;
106         }
107
108         const char *funcName2 = "test1_36_call1";
109         BPatch_point *point36_1 = NULL;
110
111         for (unsigned i=0; i<(*all_points36_1).size(); i++) 
112         {
113                 BPatch_point *cur_point = (*all_points36_1)[i];
114                 if (cur_point == NULL) 
115                         continue;
116
117                 BPatch_function *func = cur_point->getCalledFunction();
118                 char funcname[100];
119
120                 if (!func) 
121                         continue;
122
123                 if (func->getName(funcname, 99)) 
124                 {
125                         if (strstr(funcname, funcName2))
126                                 point36_1 = cur_point;
127                 }
128         }
129
130         if (point36_1 == NULL) 
131         {
132                 logerror("Unable to find callsite %s\n", funcName2);
133                 return FAILED;
134         }
135
136         BPatch_variableExpr *expr36_1 = findVariable(appImage,
137                         "test1_36_globalVariable1", 
138                         all_points36_1);
139         BPatch_variableExpr *expr36_2 = findVariable(appImage,
140                         "test1_36_globalVariable2", 
141                         all_points36_1);
142         BPatch_variableExpr *expr36_3 = findVariable(appImage,
143                         "test1_36_globalVariable3", 
144                         all_points36_1);
145         BPatch_variableExpr *expr36_4 = findVariable(appImage,
146                         "test1_36_globalVariable4", 
147                         all_points36_1);
148         BPatch_variableExpr *expr36_5 = findVariable(appImage,
149                         "test1_36_globalVariable5", 
150                         all_points36_1);
151         BPatch_variableExpr *expr36_6 = findVariable(appImage,
152                         "test1_36_globalVariable6", 
153                         all_points36_1);
154         BPatch_variableExpr *expr36_7 = findVariable(appImage,
155                         "test1_36_globalVariable7", 
156                         all_points36_1);
157         BPatch_variableExpr *expr36_8 = findVariable(appImage,
158                         "test1_36_globalVariable8", 
159                         all_points36_1);
160         BPatch_variableExpr *expr36_9 = findVariable(appImage,
161                         "test1_36_globalVariable9", 
162                         all_points36_1);
163         BPatch_variableExpr *expr36_10 = findVariable(appImage,
164                         "test1_36_globalVariable10", 
165                         all_points36_1);
166
167         if (expr36_1 == NULL || expr36_2 == NULL || expr36_3 == NULL ||
168                         expr36_4 == NULL || expr36_5 == NULL || expr36_6 == NULL ||
169                         expr36_7 == NULL || expr36_8 == NULL || expr36_9 == NULL ||
170                         expr36_10 == NULL)
171         {
172                 logerror("**Failed** test #36 (callsite parameter referencing)\n");
173                 logerror("    Unable to locate at least one of "
174                                 "test1_36_globalVariable{1...10}\n");
175                 return FAILED;
176         }
177
178         BPatch_Vector<BPatch_snippet *> snippet_seq;
179         snippet_seq.push_back(makeTest36paramExpr(expr36_1, 0));
180         snippet_seq.push_back(makeTest36paramExpr(expr36_2, 1));
181         snippet_seq.push_back(makeTest36paramExpr(expr36_3, 2));
182         snippet_seq.push_back(makeTest36paramExpr(expr36_4, 3));
183         snippet_seq.push_back(makeTest36paramExpr(expr36_5, 4));
184         snippet_seq.push_back(makeTest36paramExpr(expr36_6, 5));
185         snippet_seq.push_back(makeTest36paramExpr(expr36_7, 6));
186         snippet_seq.push_back(makeTest36paramExpr(expr36_8, 7));
187
188         // Solaris Fortran skips 9th paramter
189 #if defined(sparc_sun_solaris2_4_test) 
190         if (!isMutateeFortran(appImage))
191 #endif
192                 snippet_seq.push_back(makeTest36paramExpr(expr36_9, 8));
193
194 #if !defined(sparc_sun_solaris2_4_test)
195         snippet_seq.push_back(makeTest36paramExpr(expr36_10, 9));
196 #endif
197         BPatch_sequence seqExpr(snippet_seq);
198
199         checkCost(seqExpr);
200         appAddrSpace->insertSnippet(seqExpr, *point36_1);
201
202         return PASSED;
203 }
204