Update copyright to LGPL on all files
[dyninst.git] / testsuite / src / dyninst / test1_26.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_26.C,v 1.1 2008/10/30 19:18:36 legendre Exp $
33 /*
34  * #Name: test1_26
35  * #Desc: Struct Elements
36  * #Dep: 
37  * #Arch: !mips_sgi_irix6_4_test
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 "Callbacks.h"
48 #include "dyninst_comp.h"
49
50 class test1_26_Mutator : public DyninstMutator {
51         virtual test_results_t executeTest();
52 };
53
54 extern "C" DLLEXPORT  TestMutator *test1_26_factory() 
55 {
56         return new test1_26_Mutator();
57 }
58
59 //
60 // Start Test Case #26 - struct elements
61 //
62
63 test_results_t test1_26_Mutator::executeTest() 
64 {
65         if (appImage == NULL)
66                 return SKIPPED;
67
68         if (isMutateeFortran(appImage)) 
69         {
70                 return SKIPPED;
71         }
72
73         //     First verify that we can find a local variable in call26_1
74         const char *funcName = "test1_26_call1";
75         BPatch_Vector<BPatch_function *> found_funcs;
76
77         if ((NULL == appImage->findFunction(funcName, found_funcs, 1)) || !found_funcs.size()) 
78         {
79                 logerror("    Unable to find function %s\n", funcName);
80                 return FAILED;
81         }
82
83         if (1 < found_funcs.size()) 
84         {
85                 logerror("%s[%d]:  WARNING  : found %d functions named %s.  Using the first.\n", 
86                                 __FILE__, __LINE__, found_funcs.size(), funcName);
87         }
88
89         BPatch_Vector<BPatch_point *> *point26_1 = found_funcs[0]->findPoint(BPatch_subroutine);
90         BPatch_Vector<BPatch_point *> *point26_2 = found_funcs[0]->findPoint(BPatch_subroutine);
91
92         if ( !(point26_1 && (point26_1->size() == 1)) )
93         {
94                 logerror("**Failed** test #26 (struct elements)\n");
95                 logerror("  point26_1 incorrect\n");
96                 return FAILED;
97         }
98
99         if ( ! point26_2 )
100         {
101                 logerror("**Failed** test #26 (struct elements)\n");
102                 logerror("  point26_2 NULL\n");
103                 return FAILED;
104         }
105
106
107         BPatch_variableExpr *lvar;
108         BPatch_variableExpr *gvar[14];
109
110         int i;
111         for (i=1; i <= 13; i++) 
112         {
113                 char name[80];
114
115                 sprintf (name, "test1_26_globalVariable%d", i);
116                 gvar [i] = findVariable(appImage, name, point26_2);
117
118                 if (!gvar[i]) 
119                 {
120                         logerror("**Failed** test #26 (struct elements)\n");
121                         logerror("  can't find variable %s\n", name);
122                         return FAILED;
123                 }
124         }
125
126         // start of code for globalVariable26_1
127         BPatch_Vector<BPatch_variableExpr *> *fields = gvar[1]->getComponents();
128
129         if (!fields) 
130         {
131                 logerror("**Failed** test #26 (struct elements)\n");
132                 logerror("  struct lacked correct number of elements\n");
133                 return FAILED;
134         }
135
136         for (i=0; i < 4; i++) {
137                 char fieldName[80];
138                 sprintf(fieldName, "field%d", i+1);
139                 if (!(*fields)[i]->getName())
140                         logerror("NULL NAME!\n");
141                 if (strcmp(fieldName, (*fields)[i]->getName())) {
142                         logerror("field %d of the struct is %s, not %s\n",
143                                         i+1, fieldName, (*fields)[i]->getName());
144                         return FAILED;
145                 }
146         }
147
148         //         globalVariable26_2 = globalVariable26_1.field1
149         BPatch_arithExpr assignment1(BPatch_assign, *gvar[2], *((*fields)[0]));
150         appAddrSpace->insertSnippet(assignment1, *point26_2);
151
152         //         globalVariable26_3 = globalVariable26_1.field2
153         BPatch_arithExpr assignment2(BPatch_assign, *gvar[3], *((*fields)[1]));
154         appAddrSpace->insertSnippet(assignment2, *point26_2);
155
156         //         globalVariable26_4 = globalVariable26_1.field3[0]
157         BPatch_arithExpr assignment3(BPatch_assign, *gvar[4],
158                         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(0)));
159         appAddrSpace->insertSnippet(assignment3, *point26_2);
160
161         //         globalVariable26_5 = globalVariable26_1.field3[5]
162         BPatch_arithExpr assignment4(BPatch_assign, *gvar[5],
163                         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(5)));
164         appAddrSpace->insertSnippet(assignment4, *point26_2);
165
166         BPatch_Vector<BPatch_variableExpr *> *subfields =
167                 (*fields)[3]->getComponents();
168
169         if (subfields == NULL) 
170         {
171                 logerror("**Failed** test #26 (struct elements)\n");
172                 logerror("  struct lacked correct number of elements\n");
173                 return FAILED;
174         }
175
176
177         //         globalVariable26_6 = globalVariable26_1.field4.field1
178         BPatch_arithExpr assignment5(BPatch_assign, *gvar[6], *((*subfields)[0]));
179         appAddrSpace->insertSnippet(assignment5, *point26_2);
180
181         //         globalVariable26_7 = globalVariable26_1.field4.field2
182         BPatch_arithExpr assignment6(BPatch_assign, *gvar[7], *((*subfields)[1]));
183         appAddrSpace->insertSnippet(assignment6, *point26_2);
184
185         // start of code for localVariable26_1
186         setExpectError(100);
187
188         lvar = appImage->findVariable(*(*point26_1) [0], "localVariable26_1");
189         if (!lvar)
190                 lvar = appImage->findVariable(*(*point26_1) [0], "localvariable26_1");
191
192         if (!lvar) 
193         {
194                 logerror("**Failed** test #26 (struct elements)\n");
195                 logerror("  could not find localVariable26_1\n");
196                 return FAILED;
197         }
198
199         setExpectError(DYNINST_NO_ERROR);
200
201         fields = lvar->getComponents();
202
203         if (!fields || (fields->size() < 4)) 
204         {
205                 logerror("**Failed** test #26 (struct elements)\n");
206                 logerror("  struct lacked correct number of elements\n");
207                 return FAILED;
208         }
209
210         for (i=0; i < 4; i++) 
211         {
212                 char fieldName[80];
213                 sprintf(fieldName, "field%d", i+1);
214                 if (strcmp(fieldName, (*fields)[i]->getName())) 
215                 {
216                         logerror("field %d of the local struct is %s, not %s\n",
217                                         i+1, fieldName, (*fields)[i]->getName());
218                         return FAILED;
219                 }
220         }
221
222         //         globalVariable26_8 = localVariable26_1.field1
223
224         BPatch_arithExpr assignment7(BPatch_assign, *gvar[8], *((*fields)[0]));
225         appAddrSpace->insertSnippet(assignment7, *point26_1);
226
227         //         globalVariable26_9 = localVariable26_1.field2
228
229         BPatch_arithExpr assignment8(BPatch_assign, *gvar[9], *((*fields)[1]));
230         appAddrSpace->insertSnippet(assignment8, *point26_1);
231
232         //         globalVariable26_10 = localVariable26_1.field3[0]
233
234         BPatch_arithExpr assignment9(BPatch_assign, *gvar[10],
235                         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(0)));
236         appAddrSpace->insertSnippet(assignment9, *point26_1);
237
238         //         globalVariable26_11 = localVariable26_1.field3[5]
239
240         BPatch_arithExpr assignment10(BPatch_assign, *gvar[11],
241                         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(5)));
242         appAddrSpace->insertSnippet(assignment10, *point26_1);
243
244         subfields = (*fields)[3]->getComponents();
245
246         if (subfields == NULL) 
247         {
248                 logerror("**Failed** test #26 (struct elements)\n");
249                 logerror("  subfields NULL \n");
250                 return FAILED;
251         }
252
253         //         globalVariable26_12 = localVariable26_1.field4.field1
254
255         BPatch_arithExpr assignment11(BPatch_assign, *gvar[12], *((*subfields)[0]));
256         appAddrSpace->insertSnippet(assignment11, *point26_1);
257
258         //         globalVariable26_13 = localVariable26_1.field4.field2
259
260         BPatch_arithExpr assignment12(BPatch_assign, *gvar[13], *((*subfields)[1]));
261         appAddrSpace->insertSnippet(assignment12, *point26_1);
262
263         return PASSED;
264 }