2 * Copyright (c) 1996-2009 Barton P. Miller
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.
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.
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.
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.
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
32 // $Id: test1_21.C,v 1.1 2008/10/30 19:18:23 legendre Exp $
35 * #Desc: findFunction in module
37 * #Arch: sparc_sun_solaris2_4_test,alpha_dec_osf4_0_test,i386_unknown_solaris2_5_test,i386_unknown_linux2_0_test,ia64_unknown_linux2_4_test,mips_sgi_irix6_4_test,rs6000_ibm_aix4_1_test,x86_64_unknown_linux2_4_test
38 * #Notes: This test uses some special magic for libNameA and libNameB that should probably be altered
42 #include "BPatch_Vector.h"
43 #include "BPatch_thread.h"
44 #include "BPatch_snippet.h"
48 #ifdef os_windows_test
49 #define snprintf _snprintf
52 #include "dyninst_comp.h"
54 class test1_21_Mutator : public DyninstMutator {
55 const char *libNameAroot;
56 const char *libNameBroot;
57 char libNameA[128], libNameB[128];
61 virtual test_results_t executeTest();
62 test_results_t mutatorTest21();
65 test1_21_Mutator::test1_21_Mutator() : libNameAroot("libtestA"),
66 libNameBroot("libtestB")
69 extern "C" DLLEXPORT TestMutator *test1_21_factory()
71 return new test1_21_Mutator();
75 // Start Test Case #21 - mutator side (findFunction in module)
77 // There is no corresponding failure (test2) testing because the only
78 // bad input to replaceFunction is a non-existent BPatch_function.
79 // But this is already checked by the "non-existent function" test in
82 test_results_t test1_21_Mutator::mutatorTest21()
84 #if defined(os_aix_test) \
85 || defined(os_osf_test) \
86 || defined(os_solaris_test) \
87 || defined(os_linux_test) \
88 || defined(os_windows_test)
90 // Lookup the libtestA.so and libtestB.so modules that we've just loaded
92 BPatch_module *modA = NULL;
93 BPatch_module *modB = NULL;
94 BPatch_Vector<BPatch_module *> *mods = appImage->getModules();
96 if (!mods || mods->size() == 0)
98 logerror("**Failed test #21 (findFunction in module)\n");
99 logerror(" Mutator couldn't search modules of mutatee\n");
103 for (unsigned int i = 0; i < mods->size() && !(modA && modB); i++)
106 BPatch_module *m = (*mods)[i];
107 m->getName(buf, 1024);
109 // module names sometimes have "_module" appended
110 if( std::string(buf).find(libNameAroot) != std::string::npos )
112 else if ( std::string(buf).find(libNameBroot) != std::string::npos )
116 if (! modA || ! modB )
118 logerror("**Failed test #21 (findFunction in module)\n");
119 logerror(" Mutator couldn't find shlib in mutatee\n");
124 // Find the function CALL21_1 in each of the modules
125 BPatch_Vector<BPatch_function *> bpmv;
127 if (NULL == modA->findFunction("call21_1", bpmv, false, false, true) || !bpmv.size())
129 logerror("**Failed test #21 (findFunction in module)\n");
130 logerror(" %s[%d]: Mutator couldn't find a function in %s\n",
131 __FILE__, __LINE__, libNameA);
135 BPatch_function *funcA = bpmv[0];
138 if (NULL == modB->findFunction("call21_1", bpmv, false, false, true) || !bpmv.size())
140 logerror("**Failed test #21 (findFunction in module)\n");
141 logerror(" %s[%d]: Mutator couldn't find a function in %s\n",
142 __FILE__, __LINE__, libNameB);
146 BPatch_function *funcB = bpmv[0];
148 // Kludgily test whether the functions are distinct
149 if (funcA->getBaseAddr() == funcB->getBaseAddr())
151 logerror("**Failed test #21 (findFunction in module)\n");
152 logerror(" Mutator cannot distinguish two functions from different shlibs\n");
156 // Now test regex search
157 // Not meant to be an extensive check of all regex usage, just that
158 // the basic mechanism that deals with regexes is not broken
161 #if !defined(os_windows_test)
162 // regex "^cb" should match all functions that begin with "cb"
163 // We dont use existing "call" functions here since (at least on
164 // linux, we also find call_gmon_start(). Thus the dummy fns.
165 if (NULL == modB->findFunction("^cb", bpmv, false, false, true) || (bpmv.size() != 2))
168 logerror("**Failed test #21 (findFunction in module, regex)\n");
169 logerror(" Expected 2 functions matching ^cb, got %d\n",
172 for (unsigned int i = 0; i < bpmv.size(); ++i)
173 logerror(" matched function: %s\n",
174 bpmv[i]->getName(buf, 128));
180 if (NULL == modB->findFunction("^cbll21", bpmv, false, false, true) || (bpmv.size() != 1))
182 logerror("**Failed test #21 (findFunction in module, regex)\n");
183 logerror(" Expected 1 function matching ^cbll21, got %d\n",
189 #else // Not running on one of the specified platforms
196 // Wrapper to call readyTest
197 test_results_t test1_21_Mutator::executeTest()
199 int pointer_size = 0;
200 #if defined(arch_x86_64_test) || defined (ppc64_linux_test)
202 pointer_size = pointerSize(appImage);
205 bool isStatic = false;
206 if( NULL != appBinEdit ) {
207 isStatic = appBinEdit->isStaticExecutable();
210 strncpy(libNameA, libNameAroot, 127);
211 addLibArchExt(libNameA,127, pointer_size, isStatic);
212 strncpy(libNameB, libNameBroot, 127);
213 addLibArchExt(libNameB,127, pointer_size, isStatic);
215 char libA[128], libB[128];
216 snprintf(libA, 128, "./%s", libNameA);
217 snprintf(libB, 128, "./%s", libNameB);
219 if (! appAddrSpace->loadLibrary(libA))
221 logerror("**Failed test #21 (findFunction in module)\n");
222 logerror(" Mutator couldn't load %s into mutatee\n", libNameA);
226 if (! appAddrSpace->loadLibrary(libB))
228 logerror("**Failed test #21 (findFunction in module)\n");
229 logerror(" Mutator couldn't load %s into mutatee\n", libNameB);
233 return mutatorTest21();