C/C++ determination/reporting.
[dyninst.git] / dyninstAPI / tests / src / test1.C
1 // $Id: test1.C,v 1.52 2000/03/22 19:08:48 tikir Exp 
2 //
3 // libdyninst validation suite test #1
4 //    Author: Jeff Hollingsworth (1/7/97)
5 //        derived from a previous test by Bryan Buck
6 //
7
8 //  This program tests the basic features of the dyninst API.  
9 //      The mutatee that goes with this file is test1.mutatee.c
10 //      
11 //  Naming conventions:
12 //      All functions, variables, etc are name funcXX_YY, exprXX_YY, etc.
13 //          XX is the test number
14 //          YY is the instance withing the test
15 //          func1_2 is the second function used in test case #1.
16 //
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <assert.h>
21 #ifdef i386_unknown_nt4_0
22 #include <windows.h>
23 #include <winbase.h>
24 #else
25 #include <unistd.h>
26 #endif
27
28 #include "BPatch.h"
29 #include "BPatch_Vector.h"
30 #include "BPatch_thread.h"
31 #include "BPatch_snippet.h"
32 #include "test_util.h"
33
34 // #include <vector.h>
35
36 extern "C" const char V_libdyninstAPI[];
37
38 int debugPrint = 0; // internal "mutator" tracing
39 int errorPrint = 0; // external "dyninst" tracing (via errorFunc)
40
41 int mutateeCplusplus = 0;
42 bool runAllTests = true;
43 const unsigned int MAX_TEST = 44;
44 bool runTest[MAX_TEST+1];
45 bool passedTest[MAX_TEST+1];
46
47 template class BPatch_Vector<BPatch_variableExpr*>;
48
49 BPatch *bpatch;
50
51 static char *mutateeNameRoot = "test1.mutatee";
52 static char *libNameAroot = "libtestA";
53 static char *libNameBroot = "libtestB";
54 char libNameA[64], libNameB[64];
55
56 // control debug printf statements
57 #define dprintf if (debugPrint) printf
58
59 /**************************************************************************
60  * Error callback
61  **************************************************************************/
62
63 #define DYNINST_NO_ERROR -1
64
65 int expectError = DYNINST_NO_ERROR;
66
67 void errorFunc(BPatchErrorLevel level, int num, const char **params)
68 {
69     if (num == 0) {
70         // conditional reporting of warnings and informational messages
71         if (errorPrint) {
72             if (level == BPatchInfo)
73               { if (errorPrint > 1) printf("%s\n", params[0]); }
74             else
75                 printf("%s", params[0]);
76         }
77     } else {
78         // reporting of actual errors
79         char line[256];
80         const char *msg = bpatch->getEnglishErrorString(num);
81         bpatch->formatErrorString(line, sizeof(line), msg, params);
82         
83         if (num != expectError) {
84             printf("Error #%d (level %d): %s\n", num, level, line);
85         
86             // We consider some errors fatal.
87             if (num == 101) {
88                exit(-1);
89             }
90         }
91     }
92 }
93
94 /**************************************************************************
95  * Utility functions
96  **************************************************************************/
97
98 // check that the cost of a snippet is sane.  Due to differences between
99 //   platforms, it is impossible to check this exactly in a machine independent
100 //   manner.
101 void checkCost(BPatch_snippet snippet)
102 {
103     float cost;
104     BPatch_snippet copy;
105
106     // test copy constructor too.
107     copy = snippet;
108
109     cost = snippet.getCost();
110     dprintf("Snippet cost=%g\n", cost);
111     if (cost < 0.0) {
112         printf("*Error*: negative snippet cost\n");
113     } else if (cost == 0.0) {
114         printf("*Warning*: zero snippet cost\n");
115     } else if (cost > 0.01) {
116         printf("*Error*: snippet cost of %f, exceeds max expected of 0.1",
117             cost);
118     }
119 }
120
121 //
122 // Replace all calls in "inFunction" to "callTo" with calls to "replacement."
123 // If "replacement" is NULL, them use removeFunctionCall instead of
124 // replaceFunctionCall.
125 // Returns the number of replacements that were performed.
126 //
127 int replaceFunctionCalls(BPatch_thread *appThread, BPatch_image *appImage,
128                          char *inFunction, char *callTo, char *replacement,
129                          int testNo, char *testName,
130                          int callsExpected = -1)
131 {
132     int numReplaced = 0;
133
134     BPatch_Vector<BPatch_point *> *points =
135         appImage->findProcedurePoint(inFunction, BPatch_subroutine);
136     if (!points || (points->size() < 1)) {
137         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
138         fprintf(stderr, "    Unable to find point %s - subroutine calls\n",
139                 inFunction);
140         exit(1);
141     }
142
143     BPatch_function *call_replacement;
144     if (replacement != NULL) {
145         call_replacement = appImage->findFunction(replacement);
146         if (call_replacement == NULL) {
147             fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
148             fprintf(stderr, "    Unable to find function %s\n", replacement);
149             exit(1);
150         }
151     }
152
153     for (int n = 0; n < points->size(); n++) {
154         BPatch_function *func;
155
156         if ((func = (*points)[n]->getCalledFunction()) == NULL) continue;
157
158         char fn[256];
159         if (func->getName(fn, 256) == NULL) {
160             fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
161             fprintf(stderr, "    Can't get name of called function in %s\n",
162                     inFunction);
163             exit(1);
164         }
165         if (strcmp(fn, callTo) == 0) {
166             if (replacement == NULL)
167                 appThread->removeFunctionCall(*((*points)[n]));
168             else
169                 appThread->replaceFunctionCall(*((*points)[n]),
170                                                *call_replacement);
171             numReplaced++;
172         }
173     }
174
175     if (callsExpected > 0 && callsExpected != numReplaced) {
176         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
177         fprintf(stderr, "    Expected to find %d %s to %s in %s, found %d\n",
178                 callsExpected, callsExpected == 1 ? "call" : "calls",
179                 callTo, inFunction, numReplaced);
180         exit(1);
181     }
182
183
184     return numReplaced;
185 }
186
187
188 //
189 // Return a pointer to a string identifying a BPatch_procedureLocation
190 //
191 char *locationName(BPatch_procedureLocation l)
192 {
193     switch(l) {
194       case BPatch_entry:
195         return "entry";
196       case BPatch_exit:
197         return "exit";
198       case BPatch_subroutine:
199         return "call points";
200       case BPatch_longJump:
201         return "long jump";
202       case BPatch_allLocations:
203         return "all";
204       default:
205         return "<invalid BPatch_procedureLocation>";
206     };
207 }
208
209
210 //
211 // Insert "snippet" at the location "loc" in the function "inFunction."
212 // Returns the value returned by BPatch_thread::insertSnippet.
213 //
214 BPatchSnippetHandle *insertSnippetAt(BPatch_thread *appThread,
215         BPatch_image *appImage, char *inFunction, BPatch_procedureLocation loc,
216         BPatch_snippet &snippet, int testNo, char *testName)
217 {
218     // Find the point(s) we'll be instrumenting
219     BPatch_Vector<BPatch_point *> *points =
220         appImage->findProcedurePoint(inFunction, loc);
221
222     if (!points) {
223         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
224         fprintf(stderr, "    Unable to find point %s - %s\n",
225                 inFunction, locationName(loc));
226         exit(-1);
227     }
228
229     checkCost(snippet);
230     return appThread->insertSnippet(snippet, *points);
231 }
232
233 //
234 // Create a snippet that calls the function "funcName" with no arguments
235 //
236 BPatch_snippet *makeCallSnippet(BPatch_image *appImage, char *funcName,
237                                 int testNo, char *testName)
238 {
239     BPatch_function *call_func = appImage->findFunction(funcName);
240     if (call_func == NULL) {
241         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
242         fprintf(stderr, "    Unable to find function %s\n", funcName);
243         exit(1);
244     }
245
246     BPatch_Vector<BPatch_snippet *> nullArgs;
247     BPatch_snippet *ret = new BPatch_funcCallExpr(*call_func, nullArgs);
248
249     if (ret == NULL) {
250         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
251         fprintf(stderr, "    Unable to create snippet to call %s\n", funcName);
252         exit(1);
253     }
254
255     return ret;
256 }
257
258 //
259 // Insert a snippet to call function "funcName" with no arguments into the
260 // procedure "inFunction" at the points given by "loc."
261 //
262 BPatchSnippetHandle *insertCallSnippetAt(BPatch_thread *appThread,
263         BPatch_image *appImage, char *inFunction, BPatch_procedureLocation loc,
264         char *funcName, int testNo, char *testName)
265 {
266     BPatch_snippet *call_expr =
267         makeCallSnippet(appImage, funcName, testNo, testName);
268
269     BPatchSnippetHandle *ret = insertSnippetAt(appThread, appImage,
270                                                inFunction, loc, *call_expr,
271                                                testNo, testName);
272     if (ret == NULL) {
273         fprintf(stderr, "**Failed** test #%d (%s)\n", testNo, testName);
274         fprintf(stderr, "    Unable to insert snippet to call function %s\n",
275                 funcName);
276         exit(-1);
277     }
278
279     delete call_expr;
280     
281     return ret;
282 }
283
284
285 /**************************************************************************
286  * Tests
287  **************************************************************************/
288
289 //
290 // Start Test Case #1 - (zero arg function call)
291 //
292 void mutatorTest1(BPatch_thread *appThread, BPatch_image *appImage)
293 {
294     // Find the entry point to the procedure "func1_1"
295     BPatch_Vector<BPatch_point *> *point1_1 =
296         appImage->findProcedurePoint("func1_1", BPatch_entry);
297
298     if (!point1_1 || ((*point1_1).size() == 0)) {
299         fprintf(stderr, "**Failed** test #1 (zero arg function call)\n");
300         fprintf(stderr, "    Unable to find entry point to \"func1_1.\"\n");
301         exit(1);
302     }
303
304     BPatch_function *call1_func = appImage->findFunction("call1_1");
305     if (call1_func == NULL) {
306         fprintf(stderr, "**Failed** test #1 (zero arg function call)\n");
307         fprintf(stderr, "Unable to find function \"call1_1\"\n");
308         exit(1);
309     }
310
311     BPatch_Vector<BPatch_snippet *> call1_args;
312     BPatch_funcCallExpr call1Expr(*call1_func, call1_args);
313
314     dprintf("Inserted snippet2\n");
315     checkCost(call1Expr);
316     appThread->insertSnippet(call1Expr, *point1_1);
317 }
318
319 //
320 // Start Test Case #2 - mutator side (call a three argument function)
321 //
322 void mutatorTest2(BPatch_thread *appThread, BPatch_image *appImage)
323 {
324
325     // Find the entry point to the procedure "func2_1"
326     BPatch_Vector<BPatch_point *> *point2_1 =
327         appImage->findProcedurePoint("func2_1", BPatch_entry);
328
329     if (!point2_1 || ((*point2_1).size() == 0)) {
330         fprintf(stderr, "**Failed** test #2 (three parameter function)\n");
331         fprintf(stderr, "    Unable to find entry point to \"func2_1.\"\n");
332         exit(1);
333     }
334
335     BPatch_function *call2_func = appImage->findFunction("call2_1");
336     if (call2_func == NULL) {
337         fprintf(stderr, "**Failed** test #2 (three parameter function)\n");
338         fprintf(stderr, "    Unable to find function \"call2_1.\"\n");
339         exit(1);
340     }
341
342     BPatch_Vector<BPatch_snippet *> call2_args;
343     BPatch_constExpr expr2_1(1);
344     BPatch_constExpr expr2_2(2);
345     BPatch_constExpr expr2_3("testString2_1");
346     call2_args.push_back(&expr2_1);
347     call2_args.push_back(&expr2_2);
348     call2_args.push_back(&expr2_3);
349
350     BPatch_funcCallExpr call2Expr(*call2_func, call2_args);
351
352     dprintf("Inserted snippet2\n");
353     checkCost(call2Expr);
354     appThread->insertSnippet(call2Expr, *point2_1);
355 }
356
357 //
358 // Start Test Case #3 - mutator side (passing variables to function)
359 //
360 void mutatorTest3(BPatch_thread *appThread, BPatch_image *appImage)
361 {
362
363     // Find the entry point to the procedure "func3_1"
364     BPatch_Vector<BPatch_point *> *point3_1 =
365         appImage->findProcedurePoint("func3_1", BPatch_entry);
366
367     if (!point3_1 || ((*point3_1).size() == 0)) {
368         fprintf(stderr, "Unable to find entry point to \"func3_1.\"\n");
369         exit(1);
370     }
371
372     BPatch_function *call3_func = appImage->findFunction("call3_1");
373     if (call3_func == NULL) {
374         fprintf(stderr, "Unable to find function \"call3_1.\"\n");
375         exit(1);
376     }
377
378     BPatch_Vector<BPatch_snippet *> call3_args;
379
380     BPatch_variableExpr *expr3_1 = appImage->findVariable("globalVariable3_1");
381     if (!expr3_1) {
382         fprintf(stderr, "**Failed** test #3 (passing variables)\n");
383         fprintf(stderr, "    Unable to locate variable globalVariable3_1\n");
384         exit(1);
385     }
386     // see if we can find the address
387     if (expr3_1->getBaseAddr() <= 0) {
388         printf("*Error*: address %p for globalVariable3_1 is not valid\n",
389                expr3_1->getBaseAddr());
390     }
391
392     BPatch_variableExpr *expr3_2 = appThread->malloc(*appImage->findType("int"));
393     if (!expr3_2) {
394         fprintf(stderr, "**Failed** test #3 (passing variables)\n");
395         fprintf(stderr, "    Unable to create new int variable\n");
396         exit(1);
397     }
398
399     call3_args.push_back(expr3_1);
400     call3_args.push_back(expr3_2);
401
402     BPatch_funcCallExpr call3Expr(*call3_func, call3_args);
403     checkCost(call3Expr);
404     appThread->insertSnippet(call3Expr, *point3_1);
405
406     BPatch_arithExpr expr3_3(BPatch_assign, *expr3_2, BPatch_constExpr(32));
407     checkCost(expr3_3);
408     appThread->insertSnippet(expr3_3, *point3_1);
409
410     dprintf("Inserted snippet3\n");
411 }
412
413 //
414 // Start Test Case #4 - mutator side (sequence)
415 //      Use the BPatch sequence operation to glue to expressions togehter.
416 //      The test is constructed to verify the correct exectuion order.
417 //
418 void mutatorTest4(BPatch_thread *appThread, BPatch_image *appImage)
419 {
420     // Find the entry point to the procedure "func4_1"
421     BPatch_Vector<BPatch_point *> *point4_1 =
422         appImage->findProcedurePoint("func4_1", BPatch_entry);
423
424     if (!point4_1 || ((*point4_1).size() == 0)) {
425         fprintf(stderr, "Unable to find entry point to \"func4_1\".\n");
426         exit(1);
427     }
428
429     BPatch_variableExpr *expr4_1 = appImage->findVariable("globalVariable4_1");
430     if (!expr4_1) {
431         fprintf(stderr, "**Failed** test #4 (sequence)\n");
432         fprintf(stderr, "    Unable to locate variable globalVariable4_1\n");
433         exit(1);
434     }
435
436     BPatch_arithExpr expr4_2(BPatch_assign, *expr4_1, BPatch_constExpr(42));
437     BPatch_arithExpr expr4_3(BPatch_assign, *expr4_1, BPatch_constExpr(43));
438
439     BPatch_Vector<BPatch_snippet*> vect4_1;
440     vect4_1.push_back(&expr4_2);
441     vect4_1.push_back(&expr4_3);
442
443     BPatch_sequence expr4_4(vect4_1);
444     checkCost(expr4_4);
445     appThread->insertSnippet(expr4_4, *point4_1);
446 }
447
448 //
449 // Start Test Case #5 - mutator side (if w.o. else)
450 //
451 void mutatorTest5(BPatch_thread *appThread, BPatch_image *appImage)
452 {
453
454     // Find the entry point to the procedure "func5_2"
455     BPatch_Vector<BPatch_point *> *point5_1 =
456         appImage->findProcedurePoint("func5_2", BPatch_entry);
457
458     if (!point5_1 || ((*point5_1).size() == 0)) {
459         fprintf(stderr, "Unable to find entry point to \"func5_2\".\n");
460         exit(1);
461     }
462
463     BPatch_variableExpr *expr5_1 = appImage->findVariable("globalVariable5_1");
464     BPatch_variableExpr *expr5_2 = appImage->findVariable("globalVariable5_2");
465     if (!expr5_1 || !expr5_2) {
466         fprintf(stderr, "**Failed** test #5 (1f w.o. else)\n");
467         fprintf(stderr, "    Unable to locate variable globalVariable5_1 or ");
468         fprintf(stderr, "    variable globalVariable5_2\n");
469         exit(1);
470     }
471
472     BPatch_Vector<BPatch_snippet*> vect5_1;
473
474     // if (0 == 1) globalVariable5_1 = 52;
475     BPatch_ifExpr expr5_3(
476         BPatch_boolExpr(BPatch_eq, BPatch_constExpr(0), BPatch_constExpr(1)), 
477         BPatch_arithExpr(BPatch_assign, *expr5_1, BPatch_constExpr(52)));
478
479     // if (1 == 1) globalVariable5_2 = 53;
480     BPatch_ifExpr expr5_4(
481         BPatch_boolExpr(BPatch_eq, BPatch_constExpr(1), BPatch_constExpr(1)), 
482         BPatch_arithExpr(BPatch_assign, *expr5_2, BPatch_constExpr(53)));
483
484     vect5_1.push_back(&expr5_3);
485     vect5_1.push_back(&expr5_4);
486
487     BPatch_sequence expr5_5(vect5_1);
488     checkCost(expr5_5);
489     appThread->insertSnippet(expr5_5, *point5_1);
490 }
491
492 //
493 // Start Test Case #6 - mutator side (arithmetic operators)
494 //
495 void mutatorTest6(BPatch_thread *appThread, BPatch_image *appImage)
496 {
497     // Find the entry point to the procedure "func6_2"
498     BPatch_Vector<BPatch_point *> *point6_1 =
499         appImage->findProcedurePoint("func6_2", BPatch_entry);
500
501     if (!point6_1 || ((*point6_1).size() == 0)) {
502         fprintf(stderr, "Unable to find entry point to \"func6_2\".\n");
503         exit(1);
504     }
505
506     BPatch_variableExpr *expr6_1 = appImage->findVariable("globalVariable6_1");
507     BPatch_variableExpr *expr6_2 = appImage->findVariable("globalVariable6_2");
508     BPatch_variableExpr *expr6_3 = appImage->findVariable("globalVariable6_3");
509     BPatch_variableExpr *expr6_4 = appImage->findVariable("globalVariable6_4");
510     BPatch_variableExpr *expr6_5 = appImage->findVariable("globalVariable6_5");
511     BPatch_variableExpr *expr6_6 = appImage->findVariable("globalVariable6_6");
512     BPatch_variableExpr *expr6_1a =appImage->findVariable("globalVariable6_1a");
513     BPatch_variableExpr *expr6_2a =appImage->findVariable("globalVariable6_2a");
514     BPatch_variableExpr *expr6_3a =appImage->findVariable("globalVariable6_3a");
515     BPatch_variableExpr *expr6_4a =appImage->findVariable("globalVariable6_4a");
516     BPatch_variableExpr *expr6_5a =appImage->findVariable("globalVariable6_5a");
517     BPatch_variableExpr *expr6_6a =appImage->findVariable("globalVariable6_6a");
518     if (!expr6_1 || !expr6_2 || !expr6_3 || !expr6_4 || 
519         !expr6_5 || !expr6_6 || !expr6_1a || !expr6_2a || !expr6_3a || 
520         !expr6_4a || !expr6_5a || !expr6_6a) {
521         fprintf(stderr, "**Failed** test #6 (arithmetic operators)\n");
522         fprintf(stderr, "    Unable to locate one of globalVariable6_?\n");
523         exit(1);
524     }
525
526     BPatch_variableExpr *constVar1 = appImage->findVariable("constVar1");
527     BPatch_variableExpr *constVar2 = appImage->findVariable("constVar2");
528     BPatch_variableExpr *constVar3 = appImage->findVariable("constVar3");
529     BPatch_variableExpr *constVar5 = appImage->findVariable("constVar5");
530     BPatch_variableExpr *constVar6 = appImage->findVariable("constVar6");
531     BPatch_variableExpr *constVar10 = appImage->findVariable("constVar10");
532     BPatch_variableExpr *constVar60 = appImage->findVariable("constVar60");
533     BPatch_variableExpr *constVar64 = appImage->findVariable("constVar64");
534     BPatch_variableExpr *constVar66 = appImage->findVariable("constVar66");
535     BPatch_variableExpr *constVar67 = appImage->findVariable("constVar67");
536     if (!constVar1 || !constVar2 || !constVar3 || !constVar5 || 
537         !constVar6 || !constVar10 || !constVar60 || !constVar64 || 
538         !constVar66 || !constVar67) {
539         fprintf(stderr, "**Failed** test #6 (arithmetic operators)\n");
540         fprintf(stderr, "    Unable to locate one of constVar?\n");
541         exit(1);
542     }
543
544     BPatch_Vector<BPatch_snippet*> vect6_1;
545
546     // globalVariable6_1 = 60 + 2
547     BPatch_arithExpr arith6_1 (BPatch_assign, *expr6_1, 
548       BPatch_arithExpr(BPatch_plus,BPatch_constExpr(60), BPatch_constExpr(2)));
549     vect6_1.push_back(&arith6_1);
550
551     // globalVariable6_2 = 64 - 1
552     BPatch_arithExpr arith6_2 (BPatch_assign, *expr6_2, 
553       BPatch_arithExpr(BPatch_minus,BPatch_constExpr(64),BPatch_constExpr(1)));
554     vect6_1.push_back(&arith6_2);
555
556     // globalVariable6_3 = 553648128 / 25165824 = 22
557     //    - make these big constants to test loading constants larger than
558     //      small immediate - jkh 6/22/98
559     BPatch_arithExpr arith6_3 (BPatch_assign, *expr6_3, BPatch_arithExpr(
560       BPatch_divide,BPatch_constExpr(553648128),BPatch_constExpr(25165824)));
561     vect6_1.push_back(&arith6_3);
562
563     // globalVariable6_4 = 67 / 3
564     BPatch_arithExpr arith6_4 (BPatch_assign, *expr6_4, BPatch_arithExpr(
565       BPatch_divide,BPatch_constExpr(67),BPatch_constExpr(3)));
566     vect6_1.push_back(&arith6_4);
567     // globalVariable6_5 = 6 * 5
568     BPatch_arithExpr arith6_5 (BPatch_assign, *expr6_5, BPatch_arithExpr(
569       BPatch_times,BPatch_constExpr(6),BPatch_constExpr(5)));
570     vect6_1.push_back(&arith6_5);
571
572     // globalVariable6_6 = 10,3
573     BPatch_arithExpr arith6_6 (BPatch_assign, *expr6_6, 
574         BPatch_arithExpr(BPatch_seq,BPatch_constExpr(10),BPatch_constExpr(3)));
575     vect6_1.push_back(&arith6_6);
576
577     // globalVariable6_1a = 60 + 2
578     BPatch_arithExpr arith6_1a (BPatch_assign, *expr6_1a, 
579       BPatch_arithExpr(BPatch_plus, *constVar60, *constVar2));
580     vect6_1.push_back(&arith6_1a);
581
582     // globalVariable6_2a = 64 - 1
583     BPatch_arithExpr arith6_2a (BPatch_assign, *expr6_2a, 
584       BPatch_arithExpr(BPatch_minus, *constVar64, *constVar1));
585     vect6_1.push_back(&arith6_2a);
586
587     // globalVariable6_3 = 66 / 3
588     BPatch_arithExpr arith6_3a (BPatch_assign, *expr6_3a, BPatch_arithExpr(
589       BPatch_divide, *constVar66, *constVar3));
590     vect6_1.push_back(&arith6_3a);
591
592     // globalVariable6_4 = 67 / 3
593     BPatch_arithExpr arith6_4a (BPatch_assign, *expr6_4a, BPatch_arithExpr(
594       BPatch_divide, *constVar67, *constVar3));
595     vect6_1.push_back(&arith6_4a);
596
597     // globalVariable6_5 = 6 * 5
598     BPatch_arithExpr arith6_5a (BPatch_assign, *expr6_5a, BPatch_arithExpr(
599       BPatch_times, *constVar6, *constVar5));
600     vect6_1.push_back(&arith6_5a);
601
602     // globalVariable6_6 = 10,3
603     // BPatch_arithExpr arith6_6a (BPatch_assign, *expr6_6a, *constVar3);
604     //  BPatch_arithExpr(BPatch_seq, *constVar10, BPatch_constExpr(3)));
605     BPatch_arithExpr arith6_6a (BPatch_assign, *expr6_6a,
606         BPatch_arithExpr(BPatch_seq, *constVar10, *constVar3));
607     vect6_1.push_back(&arith6_6a);
608
609     checkCost(BPatch_sequence(vect6_1));
610     appThread->insertSnippet( BPatch_sequence(vect6_1), *point6_1);
611 }
612
613 void genRelTest(BPatch_image *appImage,BPatch_Vector<BPatch_snippet*> &vect7_1, 
614                 BPatch_relOp op, int r1, int r2, char *var1)
615 {
616     BPatch_variableExpr *varExpr1 = appImage->findVariable(var1);
617     if (!varExpr1) {
618         fprintf(stderr, "**Failed** test #7 (relational operators)\n");
619         fprintf(stderr, "    Unable to locate variable %s\n", var1);
620         exit(1);
621     }
622     BPatch_ifExpr *tempExpr1 = new BPatch_ifExpr(
623         BPatch_boolExpr(op, BPatch_constExpr(r1), BPatch_constExpr(r2)), 
624         BPatch_arithExpr(BPatch_assign, *varExpr1, BPatch_constExpr(72)));
625     vect7_1.push_back(tempExpr1);
626
627 }
628
629 void genVRelTest(BPatch_image *appImage,
630                  BPatch_Vector<BPatch_snippet*> &vect7_1, 
631                  BPatch_relOp op, BPatch_variableExpr *r1, 
632                  BPatch_variableExpr *r2, char *var1)
633 {
634     BPatch_variableExpr *varExpr1 = appImage->findVariable(var1);
635     if (!varExpr1) {
636         fprintf(stderr, "**Failed** test #7 (relational operators)\n");
637         fprintf(stderr, "    Unable to locate variable %s\n", var1);
638         exit(1);
639     }
640     BPatch_ifExpr *tempExpr1 = new BPatch_ifExpr(
641         BPatch_boolExpr(op, *r1, *r2), 
642         BPatch_arithExpr(BPatch_assign, *varExpr1, BPatch_constExpr(74)));
643     vect7_1.push_back(tempExpr1);
644
645 }
646
647 //
648 // Start Test Case #7 - mutator side (relational operators)
649 //
650 void mutatorTest7(BPatch_thread *appThread, BPatch_image *appImage)
651 {
652     // Find the entry point to the procedure "func7_2"
653     BPatch_Vector<BPatch_point *> *point7_1 =
654         appImage->findProcedurePoint("func7_2", BPatch_entry);
655
656     if (!point7_1 || ((*point7_1).size() == 0)) {
657         fprintf(stderr, "Unable to find entry point to \"func7_2\".\n");
658         exit(1);
659     }
660
661     BPatch_Vector<BPatch_snippet*> vect7_1;
662
663     genRelTest(appImage, vect7_1, BPatch_lt, 0, 1, "globalVariable7_1");
664     genRelTest(appImage, vect7_1, BPatch_lt, 1, 0, "globalVariable7_2");
665     genRelTest(appImage, vect7_1, BPatch_eq, 2, 2, "globalVariable7_3");
666     genRelTest(appImage, vect7_1, BPatch_eq, 2, 3, "globalVariable7_4");
667     genRelTest(appImage, vect7_1, BPatch_gt, 4, 3, "globalVariable7_5");
668     genRelTest(appImage, vect7_1, BPatch_gt, 3, 4, "globalVariable7_6");
669     genRelTest(appImage, vect7_1, BPatch_le, 3, 4, "globalVariable7_7");
670     genRelTest(appImage, vect7_1, BPatch_le, 4, 3, "globalVariable7_8");
671     genRelTest(appImage, vect7_1, BPatch_ne, 5, 6, "globalVariable7_9");
672     genRelTest(appImage, vect7_1, BPatch_ne, 5, 5, "globalVariable7_10");
673     genRelTest(appImage, vect7_1, BPatch_ge, 9, 7, "globalVariable7_11");
674     genRelTest(appImage, vect7_1, BPatch_ge, 7, 9, "globalVariable7_12");
675     genRelTest(appImage, vect7_1, BPatch_and, 1, 1, "globalVariable7_13");
676     genRelTest(appImage, vect7_1, BPatch_and, 1, 0, "globalVariable7_14");
677     genRelTest(appImage, vect7_1, BPatch_or, 1, 0, "globalVariable7_15");
678     genRelTest(appImage, vect7_1, BPatch_or, 0, 0, "globalVariable7_16");
679
680     BPatch_variableExpr *constVar0 = appImage->findVariable("constVar0");
681     BPatch_variableExpr *constVar1 = appImage->findVariable("constVar1");
682     BPatch_variableExpr *constVar2 = appImage->findVariable("constVar2");
683     BPatch_variableExpr *constVar3 = appImage->findVariable("constVar3");
684     BPatch_variableExpr *constVar4 = appImage->findVariable("constVar4");
685     BPatch_variableExpr *constVar5 = appImage->findVariable("constVar5");
686     BPatch_variableExpr *constVar6 = appImage->findVariable("constVar6");
687     BPatch_variableExpr *constVar7 = appImage->findVariable("constVar7");
688     BPatch_variableExpr *constVar9 = appImage->findVariable("constVar9");
689     if (!constVar0 || !constVar1 || !constVar2 || !constVar3 || !constVar4 ||
690         !constVar5 || !constVar6 || !constVar7 || !constVar9 ) {
691         fprintf(stderr, "**Failed** test #7 (relational operators)\n");
692         fprintf(stderr, "    Unable to locate one of constVar?\n");
693         exit(1);
694     }
695
696     genVRelTest(appImage, vect7_1, BPatch_lt, constVar0, constVar1,
697         "globalVariable7_1a");
698     genVRelTest(appImage, vect7_1, BPatch_lt, constVar1, constVar0, 
699         "globalVariable7_2a");
700
701     genVRelTest(appImage, vect7_1, BPatch_eq, constVar2, constVar2, 
702         "globalVariable7_3a");
703     genVRelTest(appImage, vect7_1, BPatch_eq, constVar2, constVar3, 
704         "globalVariable7_4a");
705     genVRelTest(appImage, vect7_1, BPatch_gt, constVar4, constVar3, 
706         "globalVariable7_5a");
707     genVRelTest(appImage, vect7_1, BPatch_gt, constVar3, constVar4, 
708         "globalVariable7_6a");
709     genVRelTest(appImage, vect7_1, BPatch_le, constVar3, constVar4, 
710         "globalVariable7_7a");
711     genVRelTest(appImage, vect7_1, BPatch_le, constVar4, constVar3, 
712         "globalVariable7_8a");
713     genVRelTest(appImage, vect7_1, BPatch_ne, constVar5, constVar6, 
714         "globalVariable7_9a");
715     genVRelTest(appImage, vect7_1, BPatch_ne, constVar5, constVar5, 
716         "globalVariable7_10a");
717     genVRelTest(appImage, vect7_1, BPatch_ge, constVar9, constVar7, 
718         "globalVariable7_11a");
719     genVRelTest(appImage, vect7_1, BPatch_ge, constVar7, constVar9, 
720         "globalVariable7_12a");
721     genVRelTest(appImage, vect7_1, BPatch_and, constVar1, constVar1, 
722         "globalVariable7_13a");
723     genVRelTest(appImage, vect7_1, BPatch_and, constVar1, constVar0, 
724         "globalVariable7_14a");
725     genVRelTest(appImage, vect7_1, BPatch_or, constVar1, constVar0, 
726         "globalVariable7_15a");
727     genVRelTest(appImage, vect7_1, BPatch_or, constVar0, constVar0, 
728         "globalVariable7_16a");
729
730     dprintf("relops test vector length is %d\n", vect7_1.size());
731
732     checkCost(BPatch_sequence(vect7_1));
733     appThread->insertSnippet( BPatch_sequence(vect7_1), *point7_1);
734 }
735
736 //
737 // Start Test Case #8 - mutator side (preserve registers - expr)
738 //
739 void mutatorTest8(BPatch_thread *appThread, BPatch_image *appImage)
740 {
741     // Find the entry point to the procedure "func8_1"
742     BPatch_Vector<BPatch_point *> *point8_1 =
743         appImage->findProcedurePoint("func8_1", BPatch_entry);
744
745     if (!point8_1 || ((*point8_1).size() == 0)) {
746         fprintf(stderr, "Unable to find entry point to \"func8_1\".\n");
747         exit(1);
748     }
749
750     BPatch_Vector<BPatch_snippet*> vect8_1;
751
752     BPatch_variableExpr *expr8_1 = appImage->findVariable("globalVariable8_1");
753     if (!expr8_1) {
754         fprintf(stderr, "**Failed** test #3 (passing variables)\n");
755         fprintf(stderr, "    Unable to locate variable globalVariable8_1\n");
756         exit(1);
757     }
758
759     BPatch_arithExpr arith8_1 (BPatch_assign, *expr8_1, 
760       BPatch_arithExpr(BPatch_plus, 
761             BPatch_arithExpr(BPatch_plus, 
762                 BPatch_arithExpr(BPatch_plus, BPatch_constExpr(81), 
763                                               BPatch_constExpr(82)),
764                 BPatch_arithExpr(BPatch_plus, BPatch_constExpr(83), 
765                                               BPatch_constExpr(84))),
766             BPatch_arithExpr(BPatch_plus, 
767                 BPatch_arithExpr(BPatch_plus, BPatch_constExpr(85), 
768                                               BPatch_constExpr(86)),
769                 BPatch_arithExpr(BPatch_plus, BPatch_constExpr(87), 
770                                               BPatch_constExpr(88)))));
771     vect8_1.push_back(&arith8_1);
772
773     checkCost(BPatch_sequence(vect8_1));
774     appThread->insertSnippet( BPatch_sequence(vect8_1), *point8_1);
775 }
776
777 //
778 // Start Test Case #9 - mutator side (preserve registers - funcCall)
779 //
780 void mutatorTest9(BPatch_thread *appThread, BPatch_image *appImage)
781 {
782     // Find the entry point to the procedure "func9_1"
783     BPatch_Vector<BPatch_point *> *point9_1 =
784         appImage->findProcedurePoint("func9_1", BPatch_entry);
785
786     if (!point9_1 || ((*point9_1).size() == 0)) {
787         fprintf(stderr, "Unable to find entry point to \"func9_1\".\n");
788         exit(1);
789     }
790
791     BPatch_function *call9_func = appImage->findFunction("call9_1");
792     if (call9_func == NULL) {
793         fprintf(stderr, "Unable to find function \"call9_1.\"\n");
794         exit(1);
795     }
796
797     BPatch_Vector<BPatch_snippet *> call9_args;
798
799     BPatch_constExpr constExpr91(91);
800     call9_args.push_back(&constExpr91);
801
802     BPatch_constExpr constExpr92(92);
803     call9_args.push_back(&constExpr92);
804
805     BPatch_constExpr constExpr93(93);
806     call9_args.push_back(&constExpr93);
807
808     BPatch_constExpr constExpr94(94);
809     call9_args.push_back(&constExpr94);
810
811     BPatch_constExpr constExpr95(95);
812     call9_args.push_back(&constExpr95);
813
814     BPatch_funcCallExpr call9Expr(*call9_func, call9_args);
815
816     checkCost(call9Expr);
817     appThread->insertSnippet( call9Expr, *point9_1);
818 }
819
820
821 //
822 // Start Test Case #10 - mutator side (insert snippet order)
823 //
824 void mutatorTest10(BPatch_thread *appThread, BPatch_image *appImage)
825 {
826     // Find the entry point to the procedure "func10_1"
827     BPatch_Vector<BPatch_point *> *point10_1 =
828         appImage->findProcedurePoint("func10_1", BPatch_entry);
829
830     if (!point10_1 || ((*point10_1).size() == 0)) {
831         fprintf(stderr, "Unable to find entry point to \"func10_1\".\n");
832         exit(1);
833     }
834
835     BPatch_function *call10_1_func = appImage->findFunction("call10_1");
836     if (call10_1_func == NULL) {
837         fprintf(stderr, "Unable to find function \"call10_1.\"\n");
838         exit(1);
839     }
840
841     BPatch_function *call10_2_func = appImage->findFunction("call10_2");
842     if (call10_2_func == NULL) {
843         fprintf(stderr, "Unable to find function \"call10_2.\"\n");
844         exit(1);
845     }
846
847     BPatch_function *call10_3_func = appImage->findFunction("call10_3");
848     if (call10_3_func == NULL) {
849         fprintf(stderr, "Unable to find function \"call10_3.\"\n");
850         exit(1);
851     }
852
853     BPatch_Vector<BPatch_snippet *> nullArgs;
854     BPatch_funcCallExpr call10_1Expr(*call10_1_func, nullArgs);
855     BPatch_funcCallExpr call10_2Expr(*call10_2_func, nullArgs);
856     BPatch_funcCallExpr call10_3Expr(*call10_3_func, nullArgs);
857
858     checkCost(call10_2Expr);
859     appThread->insertSnippet( call10_2Expr, *point10_1);
860
861     checkCost(call10_1Expr);
862     appThread->insertSnippet( call10_1Expr, *point10_1, BPatch_callBefore, 
863                                                         BPatch_firstSnippet);
864
865     checkCost(call10_3Expr);
866     appThread->insertSnippet( call10_3Expr, *point10_1, BPatch_callBefore, 
867                                                         BPatch_lastSnippet);
868 }
869
870
871 //
872 // Start Test Case #11 - mutator side (snippets at entry,exit,call)
873 //
874 void mutatorTest11(BPatch_thread *appThread, BPatch_image *appImage)
875 {
876     // Find the entry point to the procedure "func11_1"
877     BPatch_Vector<BPatch_point *> *point11_1 =
878         appImage->findProcedurePoint("func11_1", BPatch_entry);
879     if (!point11_1 || (point11_1->size() < 1)) {
880         fprintf(stderr, "Unable to find point func11_1 - entry.\n");
881         exit(-1);
882     }
883
884     // Find the subroutine points for the procedure "func11_1"
885     BPatch_Vector<BPatch_point *> *point11_2 =
886         appImage->findProcedurePoint("func11_1", BPatch_subroutine);
887     if (!point11_2 || (point11_2->size() < 1)) {
888         fprintf(stderr, "Unable to find point func11_1 - calls.\n");
889         exit(-1);
890     }
891
892     // Find the exit point to the procedure "func11_1"
893     BPatch_Vector<BPatch_point *> *point11_3 =
894         appImage->findProcedurePoint("func11_1", BPatch_exit);
895     if (!point11_3 || (point11_3->size() < 1)) {
896         fprintf(stderr, "Unable to find point func11_1 - exit.\n");
897         exit(-1);
898     }
899
900     BPatch_function *call11_1_func = appImage->findFunction("call11_1");
901     if (call11_1_func == NULL) {
902         fprintf(stderr, "Unable to find function \"call11_1.\"\n");
903         exit(1);
904     }
905
906     BPatch_function *call11_2_func = appImage->findFunction("call11_2");
907     if (call11_2_func == NULL) {
908         fprintf(stderr, "Unable to find function \"call11_2.\"\n");
909         exit(1);
910     }
911
912     BPatch_function *call11_3_func = appImage->findFunction("call11_3");
913     if (call11_3_func == NULL) {
914         fprintf(stderr, "Unable to find function \"call11_3.\"\n");
915         exit(1);
916     }
917
918     BPatch_function *call11_4_func = appImage->findFunction("call11_4");
919     if (call11_4_func == NULL) {
920         fprintf(stderr, "Unable to find function \"call11_4.\"\n");
921         exit(1);
922     }
923
924     BPatch_Vector<BPatch_snippet *> nullArgs;
925     BPatch_funcCallExpr call11_1Expr(*call11_1_func, nullArgs);
926     BPatch_funcCallExpr call11_2Expr(*call11_2_func, nullArgs);
927     BPatch_funcCallExpr call11_3Expr(*call11_3_func, nullArgs);
928     BPatch_funcCallExpr call11_4Expr(*call11_4_func, nullArgs);
929
930     checkCost(call11_1Expr);
931     appThread->insertSnippet(call11_1Expr, *point11_1);
932
933     checkCost(call11_2Expr);
934     appThread->insertSnippet(call11_2Expr, *point11_2, BPatch_callBefore);
935
936     checkCost(call11_3Expr);
937     appThread->insertSnippet(call11_3Expr, *point11_2, BPatch_callAfter);
938
939     checkCost(call11_4Expr);
940     appThread->insertSnippet(call11_4Expr, *point11_3);
941 }
942
943 BPatchSnippetHandle *snippetHandle12_1;
944 BPatch_variableExpr *varExpr12_1;
945
946 const int HEAP_TEST_UNIT_SIZE = 5000;
947
948 //
949 // Start Test Case #12 - mutator side (insert/remove and malloc/free)
950 //
951 void mutatorTest12a(BPatch_thread *appThread, BPatch_image *appImage)
952 {
953     // Find the entry point to the procedure "func12_2"
954     BPatch_Vector<BPatch_point *> *point12_2 =
955         appImage->findProcedurePoint("func12_2", BPatch_entry);
956     if (!point12_2 || (point12_2->size() < 1)) {
957         fprintf(stderr, "Unable to find point func12_2 - entry.\n");
958         exit(-1);
959     }
960
961     varExpr12_1 = appThread->malloc(100);
962     if (!varExpr12_1) {
963         fprintf(stderr, "Unable to allocate 100 bytes in mutatee\n");
964         exit(-1);
965     }
966
967     // Heap stress test - allocate memory until we run out, free it all
968     //   and then allocate a small amount of memory.
969     expectError = 66; // We're expecting a heap overflow error
970     BPatch_variableExpr* memStuff[30000];
971     BPatch_variableExpr *temp;
972     temp = appThread->malloc(HEAP_TEST_UNIT_SIZE); 
973     int count = 0;
974     while (temp) {
975 #if defined(USES_DYNAMIC_INF_HEAP)
976         if (! temp) {
977              printf("*** Inferior malloc stress test failed\n"); 
978              exit(-1);
979         }
980 #endif /* USES_DYNAMIC_INF_HEAP */
981         memStuff[count++] = temp;
982         temp = appThread->malloc(HEAP_TEST_UNIT_SIZE);
983 #if defined(USES_DYNAMIC_INF_HEAP)
984         // heap will grow indefinitely on dynamic heap platforms
985         if (count == 10000) break; 
986 #endif /* USES_DYNAMIC_INF_HEAP */
987         assert(count < 30000);
988     }
989     expectError = DYNINST_NO_ERROR;
990
991     int freeCount = 0;
992     for (int i =0; i < count; i++) {
993         appThread->free(*memStuff[i]);
994         freeCount++;
995     }
996
997     temp = appThread->malloc(500); 
998     if (!temp) {
999         printf("*** Unable to allocate memory after using then freeing heap\n");
1000     }
1001
1002     BPatch_function *call12_1_func = appImage->findFunction("call12_1");
1003     if (call12_1_func == NULL) {
1004         fprintf(stderr, "Unable to find function \"call12_1.\"\n");
1005         exit(1);
1006     }
1007
1008     BPatch_Vector<BPatch_snippet *> nullArgs;
1009     BPatch_funcCallExpr call12_1Expr(*call12_1_func, nullArgs);
1010
1011     checkCost(call12_1Expr);
1012     snippetHandle12_1 = appThread->insertSnippet(call12_1Expr, *point12_2);
1013     if (!snippetHandle12_1) {
1014         fprintf(stderr,
1015                 "Unable to insert snippet to call function \"call12_1.\"\n");
1016         exit(-1);
1017     }
1018 }
1019
1020 void mutatorTest12b(BPatch_thread *appThread, BPatch_image * /*appImage*/)
1021 {
1022     waitUntilStopped(bpatch, appThread, 12, "insert/remove and malloc/free");
1023
1024     // remove instrumentation and free memory
1025     if (!appThread->deleteSnippet(snippetHandle12_1)) {
1026         printf("**Failed test #12 (insert/remove and malloc/free)\n");
1027         printf("    deleteSnippet returned an error\n");
1028         exit(-1);
1029     }
1030     appThread->free(*varExpr12_1);
1031
1032     // continue process
1033     appThread->continueExecution();
1034 }
1035
1036
1037 //
1038 // Start Test Case #13 - mutator side (paramExpr,retExpr,nullExpr)
1039 //
1040 void mutatorTest13(BPatch_thread *appThread, BPatch_image *appImage)
1041 {
1042     // Find the entry point to the procedure "func13_1"
1043     BPatch_Vector<BPatch_point *> *point13_1 =
1044         appImage->findProcedurePoint("func13_1", BPatch_entry);
1045     if (!point13_1 || (point13_1->size() < 1)) {
1046         fprintf(stderr, "Unable to find point func13_1 - entry.\n");
1047         exit(-1);
1048     }
1049
1050     BPatch_function *call13_1_func = appImage->findFunction("call13_1");
1051     if (call13_1_func == NULL) {
1052         fprintf(stderr, "Unable to find function \"call13_1.\"\n");
1053         exit(1);
1054     }
1055
1056     BPatch_Vector<BPatch_snippet *> funcArgs;
1057     funcArgs.push_back(new BPatch_paramExpr(0));
1058     funcArgs.push_back(new BPatch_paramExpr(1));
1059     funcArgs.push_back(new BPatch_paramExpr(2));
1060     funcArgs.push_back(new BPatch_paramExpr(3));
1061     funcArgs.push_back(new BPatch_paramExpr(4));
1062     BPatch_funcCallExpr call13_1Expr(*call13_1_func, funcArgs);
1063
1064     checkCost(call13_1Expr);
1065     appThread->insertSnippet(call13_1Expr, *point13_1);
1066
1067     BPatch_nullExpr call13_2Expr;
1068     checkCost(call13_2Expr);
1069     appThread->insertSnippet(call13_2Expr, *point13_1);
1070
1071     // now test that a return value can be read.
1072     BPatch_Vector<BPatch_point *> *point13_2 =
1073         appImage->findProcedurePoint("func13_2", BPatch_exit);
1074     if (!point13_2 || (point13_2->size() < 1)) {
1075         fprintf(stderr, "Unable to find point func13_2 - exit.\n");
1076         exit(-1);
1077     }
1078
1079     BPatch_function *call13_2_func = appImage->findFunction("call13_2");
1080     if (call13_2_func == NULL) {
1081         fprintf(stderr, "Unable to find function \"call13_2.\"\n");
1082         exit(1);
1083     }
1084
1085     BPatch_Vector<BPatch_snippet *> funcArgs2;
1086     funcArgs2.push_back(new BPatch_retExpr());
1087     BPatch_funcCallExpr call13_3Expr(*call13_2_func, funcArgs2);
1088
1089     checkCost(call13_1Expr);
1090     appThread->insertSnippet(call13_3Expr, *point13_2);
1091 }
1092
1093
1094 //
1095 // Start Test Case #14 - mutator side (replace function call)
1096 //
1097 void mutatorTest14(BPatch_thread *appThread, BPatch_image *appImage)
1098 {
1099     replaceFunctionCalls(appThread, appImage,
1100                          "func14_1", "func14_2", "call14_1",
1101                          14, "replace/remove function call", 1);
1102     replaceFunctionCalls(appThread, appImage,
1103                          "func14_1", "func14_3", NULL,
1104                          14, "replace/remove function call", 1);
1105 }
1106
1107
1108 //
1109 // Start Test Case #15 - mutator side (setMutationsActive)
1110 //
1111 void mutatorTest15a(BPatch_thread *appThread, BPatch_image *appImage)
1112 {
1113     insertCallSnippetAt(appThread, appImage, "func15_2", BPatch_entry,
1114                         "call15_1", 15, "setMutationsActive");
1115
1116 #if defined(sparc_sun_sunos4_1_3) || defined(sparc_sun_solaris2_4)
1117     // On the Sparc, functions containing system calls are relocated into the
1118     // heap when instrumented, making a special case we should check.
1119
1120     // "access" makes the "access" system call, so we'll instrument it
1121     insertCallSnippetAt(appThread, appImage, "access", BPatch_entry,
1122                         "call15_2", 15, "setMutationsActive");
1123     // We want to instrument more than one point, so do exit as well
1124     insertCallSnippetAt(appThread, appImage, "access", BPatch_exit,
1125                         "call15_2", 15, "setMutationsActive");
1126 #endif
1127
1128     replaceFunctionCalls(appThread, appImage, "func15_4", "func15_3",
1129                          "call15_3", 15, "setMutationsActive", 1);
1130 }
1131
1132
1133 void mutatorTest15b(BPatch_thread *appThread, BPatch_image * /*appImage*/)
1134 {
1135     waitUntilStopped(bpatch, appThread, 15, "setMutationsActive");
1136
1137     // disable mutations and continue process
1138     appThread->setMutationsActive(false);
1139     appThread->continueExecution();
1140     
1141     waitUntilStopped(bpatch, appThread, 15, "setMutationsActive");
1142
1143     // re-enable mutations and continue process
1144     appThread->setMutationsActive(true);
1145     appThread->continueExecution();
1146 }
1147
1148 BPatch_Vector<BPatch_snippet *> genLongExpr(BPatch_arithExpr *tail)
1149 {
1150     BPatch_Vector<BPatch_snippet *> *ret;
1151     
1152     ret = new(BPatch_Vector<BPatch_snippet *>);
1153     for (int i=0; i < 1000; i++) {
1154         ret->push_back(tail);
1155     }
1156     return *ret;
1157 }
1158
1159 //
1160 // Start Test Case #16 - mutator side (if-else)
1161 //
1162 void mutatorTest16(BPatch_thread *appThread, BPatch_image *appImage)
1163 {
1164     BPatch_variableExpr *expr16_1=appImage->findVariable("globalVariable16_1");
1165     BPatch_variableExpr *expr16_2=appImage->findVariable("globalVariable16_2");
1166     BPatch_variableExpr *expr16_3=appImage->findVariable("globalVariable16_3");
1167     BPatch_variableExpr *expr16_4=appImage->findVariable("globalVariable16_4");
1168     BPatch_variableExpr *expr16_5=appImage->findVariable("globalVariable16_5");
1169     BPatch_variableExpr *expr16_6=appImage->findVariable("globalVariable16_6");
1170     BPatch_variableExpr *expr16_7=appImage->findVariable("globalVariable16_7");
1171     BPatch_variableExpr *expr16_8=appImage->findVariable("globalVariable16_8");
1172     BPatch_variableExpr *expr16_9=appImage->findVariable("globalVariable16_9");
1173     BPatch_variableExpr *expr16_10=appImage->findVariable("globalVariable16_10");
1174     if (!expr16_1 || !expr16_2 || !expr16_3 || !expr16_4 || !expr16_5 ||
1175         !expr16_6 || !expr16_7 || !expr16_8 || !expr16_9 || !expr16_10) {
1176         fprintf(stderr, "**Failed** test #16 (if-else)\n");
1177         fprintf(stderr, "    Unable to locate one of globalVariable16_?\n");
1178         exit(1);
1179     }
1180
1181     BPatch_arithExpr assign16_1(BPatch_assign, *expr16_1, BPatch_constExpr(1));
1182     BPatch_arithExpr assign16_2(BPatch_assign, *expr16_2, BPatch_constExpr(1));
1183
1184     BPatch_ifExpr if16_2(BPatch_boolExpr(BPatch_eq,
1185                                          BPatch_constExpr(1),
1186                                          BPatch_constExpr(1)),
1187                          assign16_1, assign16_2);
1188
1189     BPatch_arithExpr assign16_3(BPatch_assign, *expr16_3, BPatch_constExpr(1));
1190     BPatch_arithExpr assign16_4(BPatch_assign, *expr16_4, BPatch_constExpr(1));
1191
1192     BPatch_ifExpr if16_3(BPatch_boolExpr(BPatch_eq,
1193                                          BPatch_constExpr(0),
1194                                          BPatch_constExpr(1)),
1195                          assign16_3, assign16_4);
1196
1197     BPatch_arithExpr assign16_5(BPatch_assign, *expr16_5, BPatch_constExpr(1));
1198     BPatch_arithExpr assign16_6(BPatch_assign, *expr16_6, BPatch_constExpr(1));
1199     BPatch_sequence longExpr16_1(genLongExpr(&assign16_5));
1200
1201
1202     BPatch_ifExpr if16_4(BPatch_boolExpr(BPatch_eq,
1203                                          BPatch_constExpr(0),
1204                                          BPatch_constExpr(1)),
1205                          longExpr16_1, assign16_6);
1206
1207     BPatch_arithExpr assign16_7(BPatch_assign, *expr16_7, BPatch_constExpr(1));
1208     BPatch_arithExpr assign16_8(BPatch_assign, *expr16_8, BPatch_constExpr(1));
1209     BPatch_sequence longExpr16_2(genLongExpr(&assign16_8));
1210
1211     BPatch_ifExpr if16_5(BPatch_boolExpr(BPatch_eq,
1212                                          BPatch_constExpr(0),
1213                                          BPatch_constExpr(1)),
1214                          assign16_7, longExpr16_2);
1215
1216     BPatch_arithExpr assign16_9(BPatch_assign, *expr16_9, BPatch_constExpr(1));
1217     BPatch_arithExpr assign16_10(BPatch_assign, *expr16_10,BPatch_constExpr(1));
1218     BPatch_sequence longExpr16_3(genLongExpr(&assign16_9));
1219     BPatch_sequence longExpr16_4(genLongExpr(&assign16_10));
1220
1221     BPatch_ifExpr if16_6(BPatch_boolExpr(BPatch_eq,
1222                                          BPatch_constExpr(0),
1223                                          BPatch_constExpr(1)),
1224                          longExpr16_3, longExpr16_4);
1225
1226     insertSnippetAt(appThread, appImage, "func16_2", BPatch_entry, if16_2,
1227                     16, "if-else");
1228     insertSnippetAt(appThread, appImage, "func16_3", BPatch_entry, if16_3,
1229                     16, "if-else");
1230
1231     insertSnippetAt(appThread, appImage, "func16_4", BPatch_entry, if16_4,
1232                     16, "if-else");
1233     insertSnippetAt(appThread, appImage, "func16_4", BPatch_entry, if16_5,
1234                     16, "if-else");
1235     insertSnippetAt(appThread, appImage, "func16_4", BPatch_entry, if16_6,
1236                     16, "if-else");
1237 }
1238
1239 //
1240 // Start Test Case #17 - mutator side (return values from func calls)
1241 // Verify that instrumentation inserted at a subroutine's exit point
1242 // doesn't clobber its return value.
1243 // Method: the mutatee's func17_1 (first and only) exit is instrumented to
1244 // call call17_1 with parameter (constant) "1"; func17_2's (first and only)
1245 // exit is similarly instrumented to call call17_2(1); a subsequent test in
1246 // the mutatee compares the return values of func17_1 and func17_2.
1247 // (No examination is made of the return values of call17_1 or call17_2.)
1248 //
1249 void mutatorTest17(BPatch_thread *appThread, BPatch_image *appImage)
1250 {
1251     // Find the entry point to the procedure "func17_1"
1252     BPatch_Vector<BPatch_point *> *point17_1 =
1253         appImage->findProcedurePoint("func17_1", BPatch_exit);
1254     if (!point17_1 || (point17_1->size() < 1)) {
1255         fprintf(stderr, "Unable to find point func17_1 - exit.\n");
1256         exit(-1);
1257     }
1258
1259     BPatch_function *call17_1_func = appImage->findFunction("call17_1");
1260     if (call17_1_func == NULL) {
1261         fprintf(stderr, "Unable to find function \"call17_1.\"\n");
1262         exit(1);
1263     }
1264
1265     BPatch_Vector<BPatch_snippet *> funcArgs;
1266     funcArgs.push_back(new BPatch_constExpr(1));
1267     BPatch_funcCallExpr call17_1Expr(*call17_1_func, funcArgs);
1268     checkCost(call17_1Expr);
1269     appThread->insertSnippet(call17_1Expr, *point17_1);
1270
1271     // Find the exit point to the procedure "func17_2"
1272     BPatch_Vector<BPatch_point *> *point17_2 =
1273         appImage->findProcedurePoint("func17_2", BPatch_exit);
1274     if (!point17_2 || (point17_2->size() < 1)) {
1275         fprintf(stderr, "Unable to find point func17_2 - exit.\n");
1276         exit(-1);
1277     }
1278
1279     BPatch_function *call17_2_func = appImage->findFunction("call17_2");
1280     if (call17_2_func == NULL) {
1281         fprintf(stderr, "Unable to find function \"call17_2.\"\n");
1282         exit(1);
1283     }
1284
1285     BPatch_Vector<BPatch_snippet *> funcArgs2;
1286     funcArgs2.push_back(new BPatch_constExpr(1));
1287     BPatch_funcCallExpr call17_2Expr(*call17_2_func, funcArgs2);
1288     checkCost(call17_2Expr);
1289
1290     // test interface to call into insertSnippet with only one parameter
1291     BPatch_point aPoint = *(*point17_2)[0];
1292     appThread->insertSnippet(call17_2Expr, aPoint, 
1293         BPatch_callAfter, BPatch_lastSnippet);
1294 }
1295
1296 //
1297 // Start Test Case #18 - mutator side (read/write a variable in the mutatee)
1298 //
1299 void mutatorTest18(BPatch_thread *, BPatch_image *appImage)
1300 {
1301     BPatch_variableExpr *expr18_1 =appImage->findVariable("globalVariable18_1");
1302     if (expr18_1 == NULL) {
1303         fprintf(stderr, "**Failed** test #18 (read/write a variable in the mutatee)\n");
1304         fprintf(stderr, "    Unable to locate globalVariable18_1\n");
1305         exit(1);
1306     }
1307
1308     int n;
1309     expr18_1->readValue(&n);
1310
1311     if (n != 42) {
1312         fprintf(stderr, "**Failed** test #18 (read/write a variable in the mutatee)\n");
1313         fprintf(stderr, "    value read from globalVariable18_1 was %d, not 42 as expected\n", n);
1314         exit(1);
1315     }
1316
1317     n = 17;
1318     expr18_1->writeValue(&n);
1319 }
1320
1321 //
1322 // Start Test Case #19 - mutator side (oneTimeCode)
1323 //
1324 void mutatorTest19(BPatch_thread *appThread, BPatch_image *appImage)
1325 {
1326     waitUntilStopped(bpatch, appThread, 19, "oneTimeCode");
1327
1328     BPatch_function *call19_1func = appImage->findFunction("call19_1");
1329     if (call19_1func == NULL) {
1330         fprintf(stderr, "Unable to find function \"call19_1.\"\n");
1331         exit(1);
1332     }
1333
1334     BPatch_Vector<BPatch_snippet *> nullArgs;
1335     BPatch_funcCallExpr call19_1Expr(*call19_1func, nullArgs);
1336     checkCost(call19_1Expr);
1337
1338     appThread->oneTimeCode(call19_1Expr);
1339
1340     appThread->continueExecution();
1341     P_sleep(1);           /* wait for child to continue */
1342
1343     BPatch_function *call19_2func = appImage->findFunction("call19_2");
1344     if (call19_2func == NULL) {
1345         fprintf(stderr, "Unable to find function \"call19_2.\"\n");
1346         exit(1);
1347     }
1348
1349     BPatch_funcCallExpr call19_2Expr(*call19_2func, nullArgs);
1350     checkCost(call19_2Expr);
1351
1352     appThread->oneTimeCode(call19_2Expr);
1353 }
1354
1355 //
1356 // Start Test Case #20 - mutator side (instrumentation at arbitrary points)
1357 //
1358 void mutatorTest20(BPatch_thread *appThread, BPatch_image *appImage)
1359 {
1360 #if defined(rs6000_ibm_aix4_1) || \
1361     defined(alpha_dec_osf4_0) 
1362
1363     BPatch_function *call20_1func = appImage->findFunction("call20_1");
1364     if (call20_1func == NULL) {
1365         fprintf(stderr, "Unable to find function \"call20_1.\"\n");
1366         exit(1);
1367     }
1368
1369     BPatch_Vector<BPatch_snippet *> nullArgs;
1370     BPatch_funcCallExpr call20_1Expr(*call20_1func, nullArgs);
1371     checkCost(call20_1Expr);
1372
1373
1374     BPatch_function *f = appImage->findFunction("func20_2");
1375     if (f == NULL) {
1376         fprintf(stderr, "Unable to find function \"func20_2.\"\n");
1377         exit(1);
1378     }
1379
1380     BPatch_point *p = NULL;
1381     bool found_one = false;
1382
1383     if (f->getSize() == 0) {
1384         fprintf(stderr, "**Failed** test #20 (instrumentation at arbitrary points)\n");
1385         fprintf(stderr, "    getSize returned a size of 0 for the function \"func20_2\"\n");
1386         exit(1);
1387     }
1388
1389     for (unsigned int i = 0; i < f->getSize(); i+= 4) {
1390         void *addr = (char *)f->getBaseAddr() + i;
1391
1392         p = appImage->createInstPointAtAddr((char *)f->getBaseAddr() + i);
1393
1394         if (p) {
1395             if (p->getPointType() == BPatch_instruction) {
1396                 found_one = true;
1397                 if (appThread->insertSnippet(call20_1Expr, *p) == NULL) {
1398                     fprintf(stderr,
1399                       "Unable to insert snippet into function \"func20_2.\"\n");
1400                     exit(1);
1401                 }
1402             }
1403             delete p;
1404         }
1405     }
1406
1407     if (!found_one) {
1408         fprintf(stderr, "Unable to find a point to instrument in function \"func20_2.\"\n");
1409         exit(1);
1410     }
1411 #endif
1412 }
1413
1414
1415 //
1416 // Start Test Case #21 - mutator side (findFunction in module)
1417 //
1418 // There is no corresponding failure (test2) testing because the only
1419 // bad input to replaceFunction is a non-existent BPatch_function.
1420 // But this is already checked by the "non-existent function" test in
1421 // test2.
1422
1423 void readyTest21or22(BPatch_thread *appThread)
1424 {
1425     char libA[128], libB[128];
1426     sprintf(libA, "./%s", libNameA);
1427     sprintf(libB, "./%s", libNameB);
1428 #if !defined(rs6000_ibm_aix4_1) && !defined(i386_unknown_nt4_0)
1429     if (! appThread->loadLibrary(libA)) {
1430          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1431          fprintf(stderr, "  Mutator couldn't load %s into mutatee\n", libNameA);
1432          exit(1);
1433     }
1434     if (! appThread->loadLibrary(libB)) {
1435          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1436          fprintf(stderr, "  Mutator couldn't load %s into mutatee\n", libNameB);
1437          exit(1);
1438     }
1439 #endif
1440 }
1441
1442 void mutatorTest21(BPatch_thread *, BPatch_image *appImage)
1443 {
1444 #if defined(sparc_sun_solaris2_4) \
1445  || defined(i386_unknown_solaris2_5) \
1446  || defined(i386_unknown_linux2_0) \
1447  || defined(mips_sgi_irix6_4) \
1448  || defined(alpha_dec_osf4_0)
1449
1450     // Lookup the libtestA.so and libtestB.so modules that we've just loaded
1451      
1452     BPatch_module *modA = NULL;
1453     BPatch_module *modB = NULL;
1454     BPatch_Vector<BPatch_module *> *mods = appImage->getModules();
1455     if (!mods || mods->size() == 0) {
1456          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1457          fprintf(stderr, "  Mutator couldn't search modules of mutatee\n");
1458          exit(1);
1459     }
1460     for (int i = 0; i < mods->size() && !(modA && modB); i++) {
1461          char buf[1024];
1462          BPatch_module *m = (*mods)[i];
1463          m->getName(buf, 1024);
1464          // module names sometimes have "_module" appended
1465          if (!strncmp(libNameA, buf, strlen(libNameA)))
1466               modA = m;
1467          else if (!strncmp(libNameB, buf, strlen(libNameB)))
1468               modB = m;
1469     }
1470     if (! modA || ! modB) {
1471          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1472          fprintf(stderr, "  Mutator couldn't find shlib in mutatee\n");
1473          fflush(stdout);
1474          exit(1);
1475     }
1476
1477     // Find the function CALL21_1 in each of the modules
1478     BPatch_function *funcA = modA->findFunction("call21_1");
1479     BPatch_function *funcB = modB->findFunction("call21_1");
1480     if (! funcA) {
1481          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1482          fprintf(stderr, "  Mutator couldn't find a function in %s\n", libNameA);
1483          exit(1);
1484     }
1485     if (! funcB) {
1486          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1487          fprintf(stderr, "  Mutator couldn't find a function in %s\n", libNameB);
1488          exit(1);
1489     }
1490     // Kludgily test whether the functions are distinct
1491     if (funcA->getBaseAddr() == funcB->getBaseAddr()) {
1492          fprintf(stderr, "**Failed test #21 (findFunction in module)\n");
1493          fprintf(stderr,
1494                 "  Mutator cannot distinguish two functions from different shlibs\n");
1495          exit(1);
1496     }
1497 #endif
1498 }
1499
1500
1501 //
1502 // Start Test Case #22 - mutator side (replace function)
1503 //
1504 // There is no corresponding failure (test2) testing because the only
1505 // invalid input to replaceFunction is a non-existent BPatch_function.
1506 // But this is already checked by the "non-existent function" test in
1507 // test2.
1508 void mutatorTest22(BPatch_thread *appThread, BPatch_image *appImage)
1509 {
1510 #if defined(sparc_sun_solaris2_4) || \
1511     defined(alpha_dec_osf4_0)
1512
1513     char errbuf[1024]; errbuf[0] = '\0';
1514     BPatch_module *modA = NULL;
1515     BPatch_module *modB = NULL;
1516
1517     // Assume that a prior test (mutatorTest21) has loaded the
1518     // libraries libtestA.so and libtestB.so into the mutator.
1519     BPatch_Vector<BPatch_module *> *mods = appImage->getModules();
1520     if (!mods || mods->size() == 0) {
1521          fprintf(stderr, "**Failed test #22 (replace function)\n");
1522          fprintf(stderr, "  Mutator couldn't find shlib in mutatee\n");
1523          exit(1);
1524     }
1525     // Lookup the libtestA.so and libtestB.so modules
1526     for (int i = 0; i < mods->size() && !(modA && modB); i++) {
1527          char buf[1024];
1528          BPatch_module *m = (*mods)[i];
1529          m->getName(buf, 1024);
1530          // module names sometimes have "_module" appended
1531          if (!strncmp(libNameA, buf, strlen(libNameA)))
1532               modA = m;
1533          else if (!strncmp(libNameB, buf, strlen(libNameB)))
1534               modB = m;
1535     }
1536     if (! modA || ! modB) {
1537          fprintf(stderr, "**Failed test #22 (replace function)\n");
1538          fprintf(stderr, "  Mutator couldn't find dynamically loaded modules\n");
1539          exit(1);
1540     }
1541     
1542     //  Mutatee function replacement scheme:
1543
1544     //  function      module     replaced    global
1545     //                         or called?   
1546
1547     //  call22_1       a.out     replaced         1       global is the index
1548     //  call22_2       a.out       called         1       of the global variable
1549     //  call22_3       a.out     replaced         2       in test1.mutatee updated
1550     //  call22_4    libtestA       called         2       by the function
1551     //  call22_5A   libtestA     replaced         3
1552     //  call22_5B   libtestB       called         3
1553     //  call22_6    libtestA     replaced         4
1554     //  call22_7       a.out       called         4
1555
1556     // Both of each pair of functions (e.g., call22_1, call22_2)
1557     // increments a global variable.  The mutatee will test that the
1558     // variable has been updated only be the "called" function.
1559
1560     // Replace an a.out with another a.out function
1561     BPatch_function *call22_1func = appImage->findFunction("call22_1");
1562     BPatch_function *call22_2func = appImage->findFunction("call22_2");
1563     if (! call22_1func || ! call22_2func) {
1564          fprintf(stderr, "**Failed test #22 (replace function)\n");
1565          fprintf(stderr, "  Mutator couldn't find functions in mutatee\n");
1566          exit(1);
1567     }
1568     if (! appThread->replaceFunction(*call22_1func, *call22_2func)) {
1569          fprintf(stderr, "**Failed test #22 (replace function)\n");
1570          fprintf(stderr, "  Mutator couldn't replaceFunction (a.out -> a.out)\n");
1571          exit(1);
1572     }
1573
1574     // Replace an a.out function with a shlib function
1575     BPatch_function *call22_3func = appImage->findFunction("call22_3");
1576     BPatch_function *call22_4func = modA->findFunction("call22_4");
1577     if (! call22_3func || ! call22_4func) {
1578          fprintf(stderr, "**Failed test #22 (replace function)\n");
1579          fprintf(stderr, "  Mutator couldn't find functions in mutatee\n");
1580          exit(1);
1581     }
1582     if (! appThread->replaceFunction(*call22_3func, *call22_4func)) {
1583          fprintf(stderr, "**Failed test #22 (replace function)\n");
1584          fprintf(stderr, "  Mutator couldn't replaceFunction (a.out -> shlib)\n");
1585          exit(1);
1586     }
1587
1588     // Replace a shlib function with a shlib function
1589     BPatch_function *call22_5Afunc = modA->findFunction("call22_5");
1590     BPatch_function *call22_5Bfunc = modB->findFunction("call22_5");
1591     if (! call22_5Afunc || ! call22_5Bfunc) {
1592          fprintf(stderr, "**Failed test #22 (replace function)\n");
1593          fprintf(stderr, "  Mutator couldn't find functions in mutatee\n");
1594          exit(1);
1595     }
1596     if (! appThread->replaceFunction(*call22_5Afunc, *call22_5Bfunc)) {
1597          fprintf(stderr, "**Failed test #22 (replace function)\n");
1598          fprintf(stderr, "  Mutator couldn't replaceFunction (shlib -> shlib)\n");
1599     }
1600
1601     // Replace a shlib function with an a.out function
1602     BPatch_function *call22_6func = modA->findFunction("call22_6");
1603     BPatch_function *call22_7func = appImage->findFunction("call22_7");
1604     if (! call22_6func || ! call22_7func) {
1605          fprintf(stderr, "**Failed test #22 (replace function)\n");
1606          fprintf(stderr, "  Mutator couldn't find functions in mutatee\n");
1607          exit(1);
1608     }
1609     if (! appThread->replaceFunction(*call22_6func, *call22_7func)) {
1610          fprintf(stderr, "**Failed test #22 (replace function)\n");
1611          fprintf(stderr, "  Mutator couldn't replaceFunction (shlib -> a.out)\n");
1612          exit(1);
1613     }
1614
1615 #endif
1616 }
1617
1618 //
1619 // Start Test Case #23 - local variables
1620 //
1621 void mutatorTest23(BPatch_thread *appThread, BPatch_image *appImage)
1622 {
1623 #if defined(sparc_sun_solaris2_4) || \
1624     defined(rs6000_ibm_aix4_1) || \
1625     defined(alpha_dec_osf4_0) || \
1626     defined(i386_unknown_linux2_0) || \
1627     defined(i386_unknown_solaris2_5)
1628
1629     //     First verify that we can find a local variable in call23_1
1630     BPatch_Vector<BPatch_point *> *point23_1 =
1631         appImage->findProcedurePoint("call23_1", BPatch_subroutine);
1632     
1633     assert(point23_1);
1634     BPatch_variableExpr *var1 = appImage->findVariable(*(*point23_1)[0], 
1635         "localVariable23_1");
1636     BPatch_variableExpr *var2 = appImage->findVariable(*(*point23_1)[0], 
1637         "shadowVariable23_1");
1638     BPatch_variableExpr *var3 = appImage->findVariable("shadowVariable23_2");
1639     BPatch_variableExpr *var4 = appImage->findVariable("globalVariable23_1");
1640     if (!var1 || !var2 || !var3 || !var4) {
1641         fprintf(stderr, "**Failed** test #23 (local variables)\n");
1642         if (!var1) 
1643             fprintf(stderr, "  can't find local variable localVariable23_1\n");
1644         if (!var2) 
1645             fprintf(stderr, "  can't find local variable shadowVariable23_1\n");
1646         if (!var3) 
1647             fprintf(stderr,"  can't find global variable shadowVariable23_2\n");
1648         return;
1649     }
1650
1651     BPatch_arithExpr expr23_1(BPatch_assign, *var1, BPatch_constExpr(2300001));
1652     BPatch_arithExpr expr23_2(BPatch_assign, *var2, BPatch_constExpr(2300012));
1653     BPatch_arithExpr expr23_3(BPatch_assign, *var3, BPatch_constExpr(2300023));
1654     BPatch_arithExpr expr23_4(BPatch_assign, *var4, *var1);
1655
1656     BPatch_Vector<BPatch_snippet *> exprs;
1657
1658     exprs.push_back(&expr23_4); // put this one first so it isn't clobbered
1659     exprs.push_back(&expr23_1);
1660     exprs.push_back(&expr23_2);
1661     exprs.push_back(&expr23_3);
1662
1663     BPatch_sequence allParts(exprs);
1664
1665     BPatch_Vector<BPatch_point *> *points =
1666         appImage->findProcedurePoint("call23_1", BPatch_subroutine);
1667     if (!points || (points->size() < 1)) {
1668         fprintf(stderr, "**Failed** test #23 (local variables)\n");
1669         fprintf(stderr, "  Unable to find point call23_1 - subroutine calls\n");
1670         exit(1);
1671     }
1672     appThread->insertSnippet(allParts, *points);
1673 #endif
1674 }
1675
1676
1677 //
1678 // Start Test Case #24 - array variables
1679 //
1680 void mutatorTest24(BPatch_thread *appThread, BPatch_image *appImage)
1681 {
1682 #if defined(sparc_sun_solaris2_4) || \
1683     defined(rs6000_ibm_aix4_1) || \
1684     defined(alpha_dec_osf4_0) || \
1685     defined(i386_unknown_linux2_0) || \
1686     defined(i386_unknown_solaris2_5)
1687
1688     //     First verify that we can find function call24_1
1689     BPatch_function *call24_1func = appImage->findFunction("call24_1");
1690     if (call24_1func == NULL) {
1691         fprintf(stderr, "Unable to find function \"call24_1\".\n");
1692         return;
1693     }
1694
1695     //     Then verify that we can find a local variable in call24_1
1696     BPatch_Vector<BPatch_point *> *temp =
1697         appImage->findProcedurePoint("call24_1", BPatch_subroutine);
1698     if (!temp) {
1699         fprintf(stderr, "**Failed** test #24 (array variables)\n");
1700         fprintf(stderr, "  can't find function call24_1\n");
1701         return;
1702     } else {
1703         dprintf("Found %d callsites in function call24_1\n", temp->size());
1704     }
1705
1706     BPatch_Vector<BPatch_point *> *point24_1  = 
1707         new(BPatch_Vector<BPatch_point *>);
1708     point24_1->push_back((*temp)[0]);
1709
1710     BPatch_Vector<BPatch_point *> *point24_2 =
1711         appImage->findProcedurePoint("call24_1", BPatch_exit);
1712     
1713     assert(point24_1 && point24_2);
1714
1715     BPatch_variableExpr *lvar;
1716     BPatch_variableExpr *gvar[10];
1717
1718     for (int i=1; i <= 9; i++) {
1719         char name[80];
1720
1721         sprintf(name, "globalVariable24_%d", i);
1722         gvar[i] = appImage->findVariable(name);
1723         if (!gvar[i]) {
1724             fprintf(stderr, "**Failed** test #24 (array variables)\n");
1725             fprintf(stderr, "  can't find variable globalVariable24_%d\n", i);
1726             return;
1727         }
1728     }
1729
1730     lvar = appImage->findVariable(*(*point24_1)[0], "localVariable24_1");
1731     if (!lvar) {
1732         fprintf(stderr, "**Failed** test #24 (array variables)\n");
1733         fprintf(stderr, "  can't find variable localVariable24_1\n");
1734         return;
1735     }
1736
1737     //     globalVariable24_1[1] = 2400001
1738     BPatch_arithExpr assignment1(BPatch_assign, 
1739         BPatch_arithExpr(BPatch_ref, *gvar[1], BPatch_constExpr(1)),
1740         BPatch_constExpr(2400001));
1741     appThread->insertSnippet(assignment1, *point24_1);
1742
1743     //     globalVariable24_1[globalVariable24_2] = 2400002
1744     BPatch_arithExpr assignment2(BPatch_assign, 
1745         BPatch_arithExpr(BPatch_ref, *gvar[1], *gvar[2]),
1746         BPatch_constExpr(2400002));
1747     appThread->insertSnippet(assignment2, *point24_1);
1748
1749     //     globalVariable24_3 = globalVariable24_1[79]
1750     BPatch_arithExpr assignment3(BPatch_assign, *gvar[3],
1751         BPatch_arithExpr(BPatch_ref, *gvar[1], BPatch_constExpr(79)));
1752     appThread->insertSnippet(assignment3, *point24_1);
1753
1754     //     globalVariable24_5 = globalVariable24_1[globalVariable24_4]
1755     BPatch_arithExpr assignment4(BPatch_assign, *gvar[5],
1756         BPatch_arithExpr(BPatch_ref, *gvar[1], *gvar[4]));
1757     appThread->insertSnippet(assignment4, *point24_1);
1758
1759     // now the local variables
1760     //     localVariable24_1[1] = 2400005
1761     BPatch_arithExpr assignment5(BPatch_assign, 
1762         BPatch_arithExpr(BPatch_ref, *lvar, BPatch_constExpr(1)),
1763         BPatch_constExpr(2400005));
1764     appThread->insertSnippet(assignment5, *point24_1);
1765
1766     //     localVariable24_1[globalVariable24_2] = 2400006
1767     BPatch_arithExpr assignment6(BPatch_assign, 
1768         BPatch_arithExpr(BPatch_ref, *lvar, *gvar[2]),
1769         BPatch_constExpr(2400006));
1770     appThread->insertSnippet(assignment6, *point24_1);
1771
1772     //     globalVariable24_6 = localVariable24_1[79]
1773     BPatch_arithExpr assignment7(BPatch_assign, *gvar[6],
1774         BPatch_arithExpr(BPatch_ref, *lvar, BPatch_constExpr(79)));
1775     appThread->insertSnippet(assignment7, *point24_1);
1776
1777     //     globalVariable24_7 = localVariable24_1[globalVariable24_4]
1778     BPatch_arithExpr assignment8(BPatch_assign, *gvar[7],
1779         BPatch_arithExpr(BPatch_ref, *lvar, *gvar[4]));
1780     appThread->insertSnippet(assignment8, *point24_1);
1781
1782     // now test multi-dimensional arrays
1783     //     globalVariable24_8[2][3] = 2400011
1784     BPatch_arithExpr assignment9(BPatch_assign, 
1785         BPatch_arithExpr(BPatch_ref, BPatch_arithExpr(BPatch_ref, *gvar[8], 
1786                                                       BPatch_constExpr(2)), 
1787              BPatch_constExpr(3)), BPatch_constExpr(2400011));
1788     appThread->insertSnippet(assignment9, *point24_1);
1789
1790     // globalVariable24_9 = globalVariable24_8[7][9]
1791     BPatch_arithExpr assignment10(BPatch_assign, *gvar[9],
1792         BPatch_arithExpr(BPatch_ref, BPatch_arithExpr(BPatch_ref, *gvar[8], 
1793                                                       BPatch_constExpr(7)), 
1794                                      BPatch_constExpr(9)));
1795     appThread->insertSnippet(assignment10, *point24_1);
1796 #endif
1797 }
1798
1799 //
1800 // Start Test Case #25 - unary operators
1801 //
1802 void mutatorTest25(BPatch_thread *appThread, BPatch_image *appImage)
1803 {
1804 #ifndef mips_sgi_irix6_4
1805     //     First verify that we can find a local variable in call25_1
1806     BPatch_Vector<BPatch_point *> *point25_1 =
1807         appImage->findProcedurePoint("call25_1", BPatch_entry);
1808
1809     assert(point25_1 && (point25_1->size() == 1));
1810
1811     BPatch_variableExpr *gvar[8];
1812
1813     for (int i=1; i <= 7; i++) {
1814         char name[80];
1815
1816         sprintf(name, "globalVariable25_%d", i);
1817         gvar[i] = appImage->findVariable(name);
1818         if (!gvar[i]) {
1819             fprintf(stderr, "**Failed** test #25 (unary operaors)\n");
1820             fprintf(stderr, "  can't find variable globalVariable25_%d\n", i);
1821             exit(-1);
1822         }
1823     }
1824
1825     //     globalVariable25_2 = &globalVariable25_1
1826 #if !defined(sparc_sun_solaris2_4) && \
1827     !defined(rs6000_ibm_aix4_1) && \
1828     !defined(alpha_dec_osf4_0) && \
1829     !defined(i386_unknown_linux2_0) && \
1830     !defined(i386_unknown_solaris2_5)
1831
1832     // without type info need to inform
1833     BPatch_type *type = appImage->findType("void *");
1834     assert(type);
1835     gvar[2]->setType(type);
1836 #endif
1837
1838     BPatch_arithExpr assignment1(BPatch_assign, *gvar[2],
1839         BPatch_arithExpr(BPatch_addr, *gvar[1]));
1840
1841     appThread->insertSnippet(assignment1, *point25_1);
1842
1843     //     globalVariable25_3 = *globalVariable25_2
1844     //          Need to make sure this happens after the first one
1845     BPatch_arithExpr assignment2(BPatch_assign, *gvar[3],
1846         BPatch_arithExpr(BPatch_deref, *gvar[2]));
1847     appThread->insertSnippet(assignment2, *point25_1,  BPatch_callBefore,
1848             BPatch_lastSnippet);
1849
1850     //     globalVariable25_5 = -globalVariable25_4
1851     BPatch_arithExpr assignment3(BPatch_assign, *gvar[5],
1852         BPatch_arithExpr(BPatch_negate, *gvar[4]));
1853     appThread->insertSnippet(assignment3, *point25_1);
1854
1855     //     globalVariable25_7 = -globalVariable25_6
1856     BPatch_arithExpr assignment4(BPatch_assign, *gvar[7],
1857         BPatch_arithExpr(BPatch_negate, *gvar[6]));
1858     appThread->insertSnippet(assignment4, *point25_1);
1859 #endif
1860 }
1861
1862
1863 //
1864 // Start Test Case #26 - struct elements
1865 //
1866 void mutatorTest26(BPatch_thread *appThread, BPatch_image *appImage)
1867 {
1868 #if defined(sparc_sun_solaris2_4) || \
1869     defined(rs6000_ibm_aix4_1) || \
1870     defined(alpha_dec_osf4_0) || \
1871     defined(i386_unknown_linux2_0) || \
1872     defined(i386_unknown_solaris2_5)
1873
1874     //     First verify that we can find a local variable in call26_1
1875     BPatch_Vector<BPatch_point *> *point26_1 =
1876         appImage->findProcedurePoint("call26_1", BPatch_subroutine);
1877
1878     assert(point26_1 && (point26_1->size() == 1));
1879
1880     BPatch_variableExpr *lvar;
1881     BPatch_variableExpr *gvar[14];
1882
1883     for (int i=1; i <= 13; i++) {
1884         char name[80];
1885
1886         sprintf(name, "globalVariable26_%d", i);
1887         gvar[i] = appImage->findVariable(name);
1888         if (!gvar[i]) {
1889             fprintf(stderr, "**Failed** test #26 (struct elements)\n");
1890             fprintf(stderr, "  can't find variable globalVariable26_%d\n", i);
1891             exit(-1);
1892         }
1893     }
1894
1895     // start of code for globalVariable26_1
1896     BPatch_Vector<BPatch_variableExpr *> *fields = gvar[1]->getComponents();
1897     //if (!fields || (fields->size() != 4)) {
1898     if (!fields) {
1899         fprintf(stderr, "**Failed** test #26 (struct elements)\n");
1900         fprintf(stderr, "  struct lacked correct number of elements\n");
1901         exit(-1);
1902     }
1903
1904     for (int i=0; i < 4; i++) {
1905          char fieldName[80];
1906          sprintf(fieldName, "field%d", i+1);
1907          if (strcmp(fieldName, (*fields)[i]->getName())) {
1908               printf("field %d of the struct is %s, not %s\n",
1909                   i+1, fieldName, (*fields)[i]->getName());
1910               return;
1911          }
1912     }
1913
1914     //     globalVariable26_2 = globalVariable26_1.field1
1915     BPatch_arithExpr assignment1(BPatch_assign, *gvar[2], *((*fields)[0]));
1916     appThread->insertSnippet(assignment1, *point26_1);
1917
1918     //     globalVariable26_3 = globalVariable26_1.field2
1919     BPatch_arithExpr assignment2(BPatch_assign, *gvar[3], *((*fields)[1]));
1920     appThread->insertSnippet(assignment2, *point26_1);
1921
1922     //     globalVariable26_4 = globalVariable26_1.field3[0]
1923     BPatch_arithExpr assignment3(BPatch_assign, *gvar[4], 
1924         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(0)));
1925     appThread->insertSnippet(assignment3, *point26_1);
1926
1927     //     globalVariable26_5 = globalVariable26_1.field3[5]
1928     BPatch_arithExpr assignment4(BPatch_assign, *gvar[5], 
1929         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(5)));
1930     appThread->insertSnippet(assignment4, *point26_1);
1931
1932     BPatch_Vector<BPatch_variableExpr *> *subfields = 
1933         (*fields)[3]->getComponents();
1934     assert(subfields != NULL);
1935
1936     //     globalVariable26_6 = globalVariable26_1.field4.field1
1937     BPatch_arithExpr assignment5(BPatch_assign, *gvar[6], *((*subfields)[0]));
1938     appThread->insertSnippet(assignment5, *point26_1);
1939
1940     //     globalVariable26_7 = globalVariable26_1.field4.field2
1941     BPatch_arithExpr assignment6(BPatch_assign, *gvar[7], *((*subfields)[1]));
1942     appThread->insertSnippet(assignment6, *point26_1);
1943
1944     // start of code for localVariable26_1
1945     lvar = appImage->findVariable(*(*point26_1)[0], "localVariable26_1");
1946
1947     fields = lvar->getComponents();
1948     //assert(fields && (fields->size() == 4));
1949     assert(fields);
1950
1951     for (int i=0; i < 4; i++) {
1952          char fieldName[80];
1953          sprintf(fieldName, "field%d", i+1);
1954          if (strcmp(fieldName, (*fields)[i]->getName())) {
1955               printf("field %d of the local struct is %s, not %s\n",
1956                   i+1, fieldName, (*fields)[i]->getName());
1957               return;
1958          }
1959     }
1960
1961     //     globalVariable26_8 = localVariable26_1.field1
1962     BPatch_arithExpr assignment7(BPatch_assign, *gvar[8], *((*fields)[0]));
1963     appThread->insertSnippet(assignment7, *point26_1);
1964
1965     //     globalVariable26_9 = localVariable26_1.field2
1966     BPatch_arithExpr assignment8(BPatch_assign, *gvar[9], *((*fields)[1]));
1967     appThread->insertSnippet(assignment8, *point26_1);
1968
1969     //     globalVariable26_10 = localVariable26_1.field3[0]
1970     BPatch_arithExpr assignment9(BPatch_assign, *gvar[10], 
1971         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(0)));
1972     appThread->insertSnippet(assignment9, *point26_1);
1973
1974     //     globalVariable26_11 = localVariable26_1.field3[5]
1975     BPatch_arithExpr assignment10(BPatch_assign, *gvar[11], 
1976         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(5)));
1977     appThread->insertSnippet(assignment10, *point26_1);
1978
1979     subfields = (*fields)[3]->getComponents();
1980     assert(subfields != NULL);
1981
1982     //     globalVariable26_12 = localVariable26_1.field4.field1
1983     BPatch_arithExpr assignment11(BPatch_assign, *gvar[12], *((*subfields)[0]));
1984     appThread->insertSnippet(assignment11, *point26_1);
1985
1986     //     globalVariable26_13 = localVariable26_1.field4.field2
1987     BPatch_arithExpr assignment12(BPatch_assign, *gvar[13], *((*subfields)[1]));
1988     appThread->insertSnippet(assignment12, *point26_1);
1989
1990 #endif
1991 }
1992
1993 //
1994 // Start Test Case #27 - type compatibility
1995 //
1996 void mutatorTest27(BPatch_thread *appThread, BPatch_image *appImage)
1997 {
1998 #if defined(sparc_sun_solaris2_4) || \
1999     defined(rs6000_ibm_aix4_1) || \
2000     defined(alpha_dec_osf4_0) || \
2001     defined(i386_unknown_linux2_0) || \
2002     defined(i386_unknown_solaris2_5)
2003
2004     BPatch_type *type27_1 = appImage->findType("type27_1");
2005     BPatch_type *type27_2 = appImage->findType("type27_2");
2006     BPatch_type *type27_3 = appImage->findType("type27_3");
2007     BPatch_type *type27_4 = appImage->findType("type27_4");
2008
2009     if (!type27_1 || !type27_2 || !type27_3 || !type27_4) {
2010         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2011         fprintf(stderr, "    Unable to locate one of type27_{1,2,3,4}\n");
2012         return;
2013     }
2014
2015     if (!type27_1->isCompatible(type27_2)) {
2016         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2017         fprintf(stderr,"    type27_1 reported as incompatibile with type27_2\n");
2018         return;
2019     }
2020
2021     if (!type27_2->isCompatible(type27_1)) {
2022         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2023         fprintf(stderr,"    type27_2 reported as incompatibile with type27_1\n");
2024         return;
2025     }
2026
2027     if (!type27_3->isCompatible(type27_3)) {
2028         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2029         fprintf(stderr,"    type27_3 reported as incompatibile with type27_4\n");
2030         return;
2031     }
2032
2033     if (!type27_4->isCompatible(type27_3)) {
2034         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2035         fprintf(stderr,"    type27_4 reported as incompatibile with type27_3\n");
2036         return;
2037     }
2038
2039     expectError = 112; // We're expecting type conflicts here
2040     if (type27_1->isCompatible(type27_3)) {
2041         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2042         fprintf(stderr,"    type27_1 reported as compatibile with type27_3\n");
2043         return;
2044     }
2045
2046     if (type27_4->isCompatible(type27_2)) {
2047         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2048         fprintf(stderr,"    type27_4 reported as compatibile with type27_2\n");
2049         return;
2050     }
2051     expectError = DYNINST_NO_ERROR;
2052
2053     BPatch_variableExpr *expr27_5 =appImage->findVariable("globalVariable27_5");
2054     BPatch_variableExpr *expr27_6 =appImage->findVariable("globalVariable27_6");
2055     BPatch_variableExpr *expr27_7 =appImage->findVariable("globalVariable27_7");
2056     BPatch_variableExpr *expr27_8 =appImage->findVariable("globalVariable27_8");
2057     assert(expr27_5 && expr27_6 && expr27_7 && expr27_8);
2058
2059     BPatch_type *type27_5 = const_cast<BPatch_type *> (expr27_5->getType());
2060     BPatch_type *type27_6 = const_cast<BPatch_type *> (expr27_6->getType());
2061     BPatch_type *type27_7 = const_cast<BPatch_type *> (expr27_7->getType());
2062     BPatch_type *type27_8 = const_cast<BPatch_type *> (expr27_8->getType());
2063     assert(type27_5 && type27_6 && type27_7 && type27_8);
2064
2065     if (!type27_5->isCompatible(type27_6)) {
2066         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2067         fprintf(stderr,"    type27_5 reported as incompatibile with type27_6\n");
2068         return;
2069     }
2070
2071     // difderent number of elements
2072     expectError = 112; // We're expecting type conflicts here
2073     if (type27_5->isCompatible(type27_7)) {
2074         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2075         fprintf(stderr,"    type27_5 reported as compatibile with type27_7\n");
2076         return;
2077     }
2078
2079     // same # of elements, different type
2080     if (type27_5->isCompatible(type27_8)) {
2081         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2082         fprintf(stderr,"    type27_5 reported as compatibile with type27_8\n");
2083         return;
2084     }
2085
2086     // all ok, set the global variable, depends on test 18 working
2087     BPatch_variableExpr *expr27_1 =appImage->findVariable("globalVariable27_1");
2088     if (expr27_1 == NULL) {
2089         fprintf(stderr, "**Failed** test #27 (type compatibility)\n");
2090         fprintf(stderr, "    Unable to locate globalVariable27_1\n");
2091         return;
2092     }
2093     expectError = DYNINST_NO_ERROR;
2094
2095     int n = 1;
2096     expr27_1->writeValue(&n);
2097 #endif
2098 }
2099
2100 //
2101 // Start Test Case #28 - user defined fields
2102 //
2103 void mutatorTest28(BPatch_thread *appThread, BPatch_image *appImage)
2104 {
2105     int i;
2106
2107     //     Create the types
2108     BPatch_type *intType = appImage->findType("int");
2109     assert(intType);
2110
2111     BPatch_Vector<char *> names;
2112     BPatch_Vector<BPatch_type *> types;
2113
2114     names.push_back("field1");
2115     names.push_back("field2");
2116     types.push_back(intType);
2117     types.push_back(intType);
2118
2119     //  struct28_1 { int field1, int field 2; }
2120     BPatch_type *struct28_1 = bpatch->createStruct("struct28_1", names, types);
2121
2122     names.push_back("field3");
2123     names.push_back("field4");
2124     BPatch_type *intArray = bpatch->createArray("intArray", intType, 0, 9);
2125     types.push_back(intArray);
2126     types.push_back(struct28_1);
2127
2128     // struct28_2 { int field1, int field 2, int field3[10],struct26_1 field4 } 
2129     BPatch_type *struct28_2 = bpatch->createStruct("struct28_2", names, types);
2130
2131     // now create variables of these types.
2132     BPatch_variableExpr *globalVariable28_1 = 
2133         appImage->findVariable("globalVariable28_1");
2134     globalVariable28_1->setType(struct28_2);
2135
2136     //     Next verify that we can find a local variable in call28
2137     BPatch_Vector<BPatch_point *> *point28 =
2138         appImage->findProcedurePoint("call28_1", BPatch_entry);
2139
2140     assert(point28 && (point28->size() == 1));
2141
2142     BPatch_variableExpr *gvar[8];
2143
2144     for (i=1; i <= 7; i++) {
2145         char name[80];
2146
2147         sprintf(name, "globalVariable28_%d", i);
2148         gvar[i] = appImage->findVariable(name);
2149         if (!gvar[i]) {
2150             fprintf(stderr, "**Failed** test #28 (user defined fields)\n");
2151             fprintf(stderr, "  can't find variable globalVariable28_%d\n", i);
2152             exit(-1);
2153         }
2154     }
2155
2156     // start of code for globalVariable28
2157     BPatch_Vector<BPatch_variableExpr *> *fields = gvar[1]->getComponents();
2158     assert(fields && (fields->size() == 4));
2159
2160     for (i=0; i < 4; i++) {
2161          char fieldName[80];
2162          sprintf(fieldName, "field%d", i+1);
2163          if (strcmp(fieldName, (*fields)[i]->getName())) {
2164               printf("field %d of the struct is %s, not %s\n",
2165                   i+1, fieldName, (*fields)[i]->getName());
2166               return;
2167          }
2168     }
2169
2170     //     globalVariable28 = globalVariable28.field1
2171     BPatch_arithExpr assignment1(BPatch_assign, *gvar[2], *((*fields)[0]));
2172     appThread->insertSnippet(assignment1, *point28);
2173
2174     //     globalVariable28 = globalVariable28.field2
2175     BPatch_arithExpr assignment2(BPatch_assign, *gvar[3], *((*fields)[1]));
2176     appThread->insertSnippet(assignment2, *point28);
2177
2178     //     globalVariable28 = globalVariable28.field3[0]
2179     BPatch_arithExpr assignment3(BPatch_assign, *gvar[4], 
2180         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(0)));
2181     appThread->insertSnippet(assignment3, *point28);
2182
2183     //     globalVariable28 = globalVariable28.field3[5]
2184     BPatch_arithExpr assignment4(BPatch_assign, *gvar[5], 
2185         BPatch_arithExpr(BPatch_ref, *((*fields)[2]), BPatch_constExpr(5)));
2186     appThread->insertSnippet(assignment4, *point28);
2187
2188     BPatch_Vector<BPatch_variableExpr *> *subfields = 
2189         (*fields)[3]->getComponents();
2190     assert(subfields != NULL);
2191
2192     //     globalVariable28 = globalVariable28.field4.field1
2193     BPatch_arithExpr assignment5(BPatch_assign, *gvar[6], *((*subfields)[0]));
2194     appThread->insertSnippet(assignment5, *point28);
2195
2196     //     globalVariable28 = globalVariable28.field4.field2
2197     BPatch_arithExpr assignment6(BPatch_assign, *gvar[7], *((*subfields)[1]));
2198     appThread->insertSnippet(assignment6, *point28);
2199 }
2200
2201 bool printSrcObj(BPatch_sourceObj *p, int level)
2202 {
2203     int i;
2204     char name[80];
2205     bool ret = true;
2206
2207     BPatch_Vector<BPatch_sourceObj *> *curr;
2208
2209     if (!p) return(true);
2210
2211     switch (p->getSrcType()) {
2212         case BPatch_sourceProgram:
2213             if (level != 0) ret = false;
2214             break;
2215
2216         case BPatch_sourceModule: 
2217             if (level != 1) ret = false;
2218             break;
2219
2220         case BPatch_sourceFunction: 
2221             if (level != 2) ret = false;
2222             break;
2223
2224         default:
2225             printf("<unknown type>");
2226     }
2227
2228     curr = p->getSourceObj();
2229     if (!curr) {
2230         // eveything down to functions should have something
2231         return((level == 2) ? true : false);
2232     }
2233
2234     for (i=0; i < curr->size(); i++) {
2235         p = (*curr)[i];
2236         ret = printSrcObj(p, level+1) && ret;
2237     }
2238
2239     return ret;
2240 }
2241
2242 //
2243 // Start Test Case #29 - getParent/Child
2244 //
2245 void mutatorTest29(BPatch_thread *appThread, BPatch_image *appImage)
2246 {
2247     bool ret;
2248     BPatch_sourceObj *p;
2249     BPatch_Vector<BPatch_sourceObj *> *curr;
2250
2251     p = (BPatch_sourceObj *) appImage;
2252     passedTest[29] = printSrcObj(p, 0);
2253
2254     if (!passedTest[29]) {
2255         fprintf(stderr, "**Failed** test #29 (class BPatch_srcObj)\n");
2256         return;
2257     }
2258
2259     BPatch_variableExpr *expr29_1 =appImage->findVariable("globalVariable29_1");
2260     if (expr29_1 == NULL) {
2261         fprintf(stderr, "**Failed** test #29 (class BPatch_srcObj)\n");
2262         fprintf(stderr, "    Unable to locate globalVariable27_1\n");
2263         return;
2264     }
2265     expectError = DYNINST_NO_ERROR;
2266
2267     int n = 1;
2268     expr29_1->writeValue(&n);
2269 }
2270
2271 //
2272 // Start Test Case #30 - (line information)
2273 //
2274 void mutatorTest30(BPatch_thread *appThread, BPatch_image *appImage)
2275 {
2276
2277 #if defined(sparc_sun_solaris2_4) || \
2278     defined(i386_unknown_solaris2_5) || \
2279     defined(i386_unknown_linux2_0) || \
2280     defined(rs6000_ibm_aix4_1) || \
2281     defined(alpha_dec_osf4_0)
2282
2283   unsigned n;
2284   unsigned long baseAddr,lastAddr;
2285   unsigned int call30_1_line_no;
2286   unsigned short lineNo;
2287   char fileName[256];
2288
2289         //instrument with the function that will set the line number
2290         BPatch_Vector<BPatch_point *> *point30_1 =
2291                 appImage->findProcedurePoint("func30_1", BPatch_entry);
2292         if (!point30_1 || (point30_1->size() < 1)) {
2293                 fprintf(stderr, "Unable to find point func30_1 - entry.\n");
2294                 exit(-1);
2295         }
2296         BPatch_function *call30_1func = appImage->findFunction("call30_1");
2297         if (call30_1func == NULL) {
2298                 fprintf(stderr, "Unable to find function \"call30_1.\"\n");
2299                 exit(1);
2300         }
2301         BPatch_Vector<BPatch_snippet *> nullArgs;
2302         BPatch_funcCallExpr call30_1Expr(*call30_1func, nullArgs);
2303
2304         checkCost(call30_1Expr);
2305         appThread->insertSnippet(call30_1Expr, *point30_1);
2306
2307         //get the line number of the function call30_1
2308         BPatch_variableExpr *expr30_7 = 
2309                 appImage->findVariable("globalVariable30_7");
2310         if (expr30_7 == NULL) {
2311                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2312                 fprintf(stderr, "    Unable to locate globalVariable30_7\n");
2313                 exit(1);
2314         }
2315         expr30_7->readValue(&n);
2316         call30_1_line_no = (unsigned)(n+1);
2317
2318         //get the base addr and last addr of the function call30_1
2319         baseAddr = (unsigned long)(call30_1func->getBaseAddr());
2320         lastAddr = baseAddr + call30_1func->getSize();
2321
2322         //now write the base address and last address of the function
2323         BPatch_variableExpr *expr30_8 = 
2324                         appImage->findVariable("globalVariable30_8");
2325         if (expr30_8 == NULL) {
2326                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2327                 fprintf(stderr, "    Unable to locate globalVariable30_8\n");
2328         }
2329
2330         BPatch_variableExpr *expr30_9 = 
2331                         appImage->findVariable("globalVariable30_9");
2332         if (expr30_9 == NULL) {
2333                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2334                 fprintf(stderr, "    Unable to locate globalVariable30_9\n");
2335         }
2336
2337         expr30_8->writeValue(&baseAddr);
2338         expr30_9->writeValue(&lastAddr);
2339         
2340         
2341         //check getLineAddr for appImage
2342         BPatch_variableExpr *expr30_3 =
2343                         appImage->findVariable("globalVariable30_3");
2344         if (expr30_3 == NULL) {
2345                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2346                 fprintf(stderr, "    Unable to locate globalVariable30_3\n");
2347                 exit(1);
2348         }
2349         BPatch_Vector<unsigned long> buffer1; 
2350         if(appImage->getLineToAddr("test1.mutatee.c",call30_1_line_no,buffer1))
2351         {
2352                 n = buffer1[0];
2353                 expr30_3->writeValue(&n);
2354         }
2355
2356         //check getLineAddr for module
2357         BPatch_variableExpr *expr30_4 =
2358                         appImage->findVariable("globalVariable30_4");
2359         if (expr30_4 == NULL) {
2360                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2361                 fprintf(stderr, "    Unable to locate globalVariable30_4\n");
2362                 exit(1);
2363         }
2364         BPatch_Vector<BPatch_module*>* appModules = appImage->getModules();
2365         for(int i=0;i<appModules->size();i++){
2366                 char mname[256];
2367                 (*appModules)[i]->getName(mname,255);mname[255] = '\0';
2368                 if(!strncmp(mname,"test1.mutatee.c",15)){
2369                         BPatch_Vector<unsigned long> buffer2;
2370                         if((*appModules)[i]->getLineToAddr(
2371                                         call30_1_line_no,buffer2))
2372                         {
2373                                 n = buffer2[0];
2374                                 expr30_4->writeValue(&n);
2375                         }
2376                         break;
2377                 }
2378         }
2379
2380         //check getLineAddr works for the function
2381         BPatch_variableExpr *expr30_5 =
2382                 appImage->findVariable("globalVariable30_5");
2383         if (expr30_5 == NULL) {
2384                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2385                 fprintf(stderr, "    Unable to locate globalVariable30_5\n");
2386                 exit(1);
2387         }
2388         BPatch_Vector<unsigned long> buffer3; 
2389         if(call30_1func->getLineToAddr(call30_1_line_no,buffer3))
2390         {
2391                 n = buffer3[0];
2392                 expr30_5->writeValue(&n);
2393         }
2394
2395         //check whether getLineFile works for appThread
2396         BPatch_variableExpr *expr30_6 =
2397                 appImage->findVariable("globalVariable30_6");
2398         if (expr30_6 == NULL) {
2399                 fprintf(stderr, "**Failed** test #30 (line information)\n");
2400                 fprintf(stderr, "    Unable to locate globalVariable30_6\n");
2401                 exit(1);
2402         }
2403         /* since the first line address of a function changes with the
2404            compiler type (gcc,native) we need to check with next address
2405            etc. Instead I use the last address of the function*/
2406         if(appThread->getLineAndFile(lastAddr-1,lineNo,fileName,256)){
2407                 n = lineNo;
2408                 expr30_6->writeValue(&n);
2409         }
2410 #endif
2411 }
2412
2413 /*******************************************************************************/
2414 /*******************************************************************************/
2415 /*******************************************************************************/
2416
2417 typedef BPatch_Vector<BPatch_point * > point_vector;
2418 // typedef vector<BPatchSnippetHandle * > handle_vector;
2419
2420 void instrument_entry_points( BPatch_thread * app_thread,
2421                               BPatch_image * ,
2422                               BPatch_function * func,
2423                               BPatch_snippet * code )
2424 {
2425   assert( func != 0 );
2426   assert( code != 0 );
2427
2428 //   handle_vector * list_of_handles = new handle_vector;
2429
2430   int null_entry_point_count = 0;
2431   int failed_snippet_insertion_count = 0;
2432
2433   point_vector * entries = func->findPoint( BPatch_entry );
2434   assert( entries != 0 );
2435
2436   for( int i = 0; i < entries->size(); i++ )
2437     {
2438       BPatch_point * point = ( * entries )[ i ];
2439       if( point == 0 )
2440         {
2441           null_entry_point_count++;
2442         }
2443       else
2444         {
2445           BPatchSnippetHandle * result =
2446             app_thread->insertSnippet( * code,
2447                                        * point, BPatch_callBefore, BPatch_firstSnippet );
2448           if( result == 0 )
2449             {
2450               failed_snippet_insertion_count++;
2451             }
2452 //        else
2453 //          {
2454 //            list_of_handles->push_back( result );
2455 //          }
2456         }
2457     }
2458
2459   delete code;
2460
2461 //   return * list_of_handles;
2462 }
2463
2464 /*******************************************************************************/
2465 /*******************************************************************************/
2466 /*******************************************************************************/
2467
2468 void instrument_exit_points( BPatch_thread * app_thread,
2469                              BPatch_image * ,
2470                              BPatch_function * func,
2471                              BPatch_snippet * code )
2472 {
2473   assert( func != 0 );
2474   assert( code != 0 );
2475
2476 //   handle_vector * list_of_handles = new handle_vector;
2477
2478   int null_exit_point_count = 0;
2479   int failed_snippet_insertion_count = 0;
2480
2481   point_vector * exits = func->findPoint( BPatch_exit );
2482   assert( exits != 0 );
2483
2484   for( int i = 0; i < exits->size(); i++ )
2485     {
2486       BPatch_point * point = ( * exits )[ i ];
2487       if( point == 0 )
2488         {
2489           null_exit_point_count++;
2490         }
2491       else
2492         {
2493           BPatchSnippetHandle * result =
2494             app_thread->insertSnippet( * code,
2495                                        * point, BPatch_callAfter, BPatch_firstSnippet );
2496           if( result == 0 )
2497             {
2498               failed_snippet_insertion_count++;
2499             }
2500 //        else
2501 //          {
2502 //            list_of_handles->push_back( result );
2503 //          }
2504         }
2505     }
2506
2507   delete code;
2508
2509 //   return * list_of_handles;
2510 }
2511
2512 /*******************************************************************************/
2513 /*******************************************************************************/
2514 /*******************************************************************************/
2515
2516 //
2517 // Start Test Case #31 - (non-recursive base tramp)
2518 //
2519 void mutatorTest31( BPatch_thread * appThread, BPatch_image * appImage )
2520 {
2521   char * foo_name = "func31_2";
2522   char * bar_name = "func31_3";
2523   char * baz_name = "func31_4";
2524
2525   BPatch_image * app_image = appImage;
2526   BPatch_thread * app_thread = appThread;
2527
2528   BPatch_function * foo_function = app_image->findFunction( foo_name );
2529   if( foo_function == 0 )
2530     {
2531       fprintf( stderr, "Cannot find \"%s\" function.",
2532                foo_name );
2533       exit( -1 );
2534     }
2535   BPatch_function * bar_function = app_image->findFunction( bar_name );
2536   if( bar_function == 0 )
2537     {
2538       fprintf( stderr, "Cannot find \"%s\" function.",
2539                bar_name );
2540       exit( -1 );
2541     }
2542   BPatch_function * baz_function = app_image->findFunction( baz_name );
2543   if( baz_function == 0 )
2544     {
2545       fprintf( stderr, "Cannot find \"%s\" function.",
2546                baz_name );
2547       exit( -1 );
2548     }
2549
2550   bool old_value = BPatch::bpatch->isTrampRecursive();
2551   BPatch::bpatch->setTrampRecursive( false );
2552
2553   BPatch_Vector<BPatch_snippet *> foo_args;
2554   BPatch_snippet * foo_snippet =
2555     new BPatch_funcCallExpr( * bar_function,
2556                              foo_args );
2557   instrument_entry_points( app_thread, app_image, foo_function, foo_snippet );
2558
2559   BPatch_Vector<BPatch_snippet *> bar_args_1;
2560   bar_args_1.push_back( new BPatch_constExpr( 1 ) );
2561   BPatch_snippet * bar_snippet_1 =
2562     new BPatch_funcCallExpr( * baz_function,
2563                              bar_args_1 );
2564   instrument_entry_points( app_thread, app_image, bar_function, bar_snippet_1 );
2565
2566   BPatch_Vector<BPatch_snippet *> bar_args_2;
2567   bar_args_2.push_back( new BPatch_constExpr( 2 ) );
2568   BPatch_snippet * bar_snippet_2 =
2569     new BPatch_funcCallExpr( * baz_function,
2570                              bar_args_2 );
2571   instrument_exit_points( app_thread, app_image, bar_function, bar_snippet_2 );
2572
2573   BPatch::bpatch->setTrampRecursive( old_value );
2574 }
2575
2576 /*******************************************************************************/
2577 /*******************************************************************************/
2578 /*******************************************************************************/
2579
2580 //
2581 // Start Test Case #32 - (recursive base tramp)
2582 //
2583 void mutatorTest32( BPatch_thread * appThread, BPatch_image * appImage )
2584 {
2585   char * foo_name = "func32_2";
2586   char * bar_name = "func32_3";
2587   char * baz_name = "func32_4";
2588
2589   BPatch_image * app_image = appImage;
2590   BPatch_thread * app_thread = appThread;
2591
2592   BPatch_function * foo_function = app_image->findFunction( foo_name );
2593   if( foo_function == 0 )
2594     {
2595       fprintf( stderr, "Cannot find \"%s\" function.",
2596                foo_name );
2597       exit( -1 );
2598     }
2599   BPatch_function * bar_function = app_image->findFunction( bar_name );
2600   if( bar_function == 0 )
2601     {
2602       fprintf( stderr, "Cannot find \"%s\" function.",
2603                bar_name );
2604       exit( -1 );
2605     }
2606   BPatch_function * baz_function = app_image->findFunction( baz_name );
2607   if( baz_function == 0 )
2608     {
2609       fprintf( stderr, "Cannot find \"%s\" function.",
2610                baz_name );
2611       exit( -1 );
2612     }
2613
2614   bool old_value = BPatch::bpatch->isTrampRecursive();
2615   BPatch::bpatch->setTrampRecursive( true );
2616
2617   BPatch_Vector<BPatch_snippet *> foo_args;
2618   BPatch_snippet * foo_snippet =
2619     new BPatch_funcCallExpr( * bar_function,
2620                              foo_args );
2621   instrument_entry_points( app_thread, app_image, foo_function, foo_snippet );
2622
2623   BPatch_Vector<BPatch_snippet *> bar_args_1;
2624   bar_args_1.push_back( new BPatch_constExpr( 1 ) );
2625   BPatch_snippet * bar_snippet_1 =
2626     new BPatch_funcCallExpr( * baz_function,
2627                              bar_args_1 );
2628   instrument_entry_points( app_thread, app_image, bar_function, bar_snippet_1 );
2629
2630   BPatch_Vector<BPatch_snippet *> bar_args_2;
2631   bar_args_2.push_back( new BPatch_constExpr( 2 ) );
2632   BPatch_snippet * bar_snippet_2 =
2633     new BPatch_funcCallExpr( * baz_function,
2634                              bar_args_2 );
2635   instrument_exit_points( app_thread, app_image, bar_function, bar_snippet_2 );
2636
2637   BPatch::bpatch->setTrampRecursive( old_value );
2638 }
2639
2640 //  
2641 // Start Test Case #33 - (C++ argument pass)
2642 //       
2643 void mutatorTest33(BPatch_thread *appThread, BPatch_image *appImage)
2644 {
2645    BPatch_Vector<BPatch_point *> *point33_1 =
2646       appImage->findProcedurePoint("arg_test::call_cpp", BPatch_subroutine);
2647
2648    assert(point33_1);
2649
2650    // check the paramter passing modes
2651    BPatch_variableExpr *arg0 = appImage->findVariable(*(*point33_1)[0],
2652        "reference");
2653    BPatch_variableExpr *arg1 = appImage->findVariable(*(*point33_1)[0],
2654        "arg1");
2655    BPatch_variableExpr *arg2 = appImage->findVariable(*(*point33_1)[0],
2656        "arg2");
2657    BPatch_variableExpr *arg3 = appImage->findVariable(*(*point33_1)[0],
2658        "arg3");
2659    BPatch_variableExpr *arg4 = appImage->findVariable(*(*point33_1)[0],
2660        "m");
2661
2662    if (!arg0 || !arg1 || !arg2 || !arg3 || !arg4) {
2663       fprintf(stderr, "**Failed** test #33 (argument passing)\n");
2664       if ( !arg0 )
2665          fprintf(stderr, "  can't find local variable arg0\n");
2666       if ( !arg1 )
2667          fprintf(stderr, "  can't find local variable arg1\n");
2668       if ( !arg2 )
2669          fprintf(stderr, "  can't find local variable arg2\n");
2670       if ( !arg3 )
2671          fprintf(stderr, "  can't find local variable arg3\n");
2672       if ( !arg4 )
2673          fprintf(stderr, "  can't find local variable arg4\n");
2674       return;
2675    }
2676
2677    BPatch_type *type33_0 = const_cast<BPatch_type *> (arg0->getType());
2678    BPatch_type *type33_1 = const_cast<BPatch_type *> (arg1->getType());
2679    BPatch_type *type33_2 = const_cast<BPatch_type *> (arg2->getType());
2680    BPatch_type *type33_3 = const_cast<BPatch_type *> (arg4->getType());
2681    assert(type33_0 && type33_1 && type33_2 && type33_3);
2682
2683    if (!type33_1->isCompatible(type33_3)) {
2684        fprintf(stderr, "**Failed** test #33 (C++ argument pass)\n");
2685        fprintf(stderr,"    type33_1 reported as incompatibile with type33_3\n");
2686        return;
2687    }
2688
2689    if (!type33_2->isCompatible(type33_0)) {
2690         fprintf(stderr, "**Failed** test #33 (C++ argument pass)\n");
2691         fprintf(stderr,"    type33_2 reported as incompatibile with type33_0\n")
2692 ;
2693         return;
2694    }
2695
2696    BPatch_arithExpr expr33_1(BPatch_assign, *arg3, BPatch_constExpr(33));
2697    checkCost(expr33_1);
2698    appThread->insertSnippet(expr33_1, *point33_1);
2699
2700    // pass a paramter to a class member function
2701    BPatch_Vector<BPatch_point *> *point33_2 =
2702      appImage->findProcedurePoint("arg_test::func_cpp", BPatch_exit);
2703    if (!point33_2 || (point33_2->size() < 1)) {
2704       fprintf(stderr, "Unable to find point arg_test::func_cpp - exit.\n");
2705       exit(-1);
2706    }
2707
2708    BPatch_function *call33_func = appImage->findFunction("arg_test::arg_pass");
2709    if (call33_func == NULL) {
2710        fprintf(stderr, "Unable to find function \"cpp_test_util::call_cpp.\"\n");
2711        exit(1);
2712    }
2713
2714    BPatch_Vector<BPatch_snippet *> call33_args;
2715    BPatch_constExpr expr33_2(33);
2716    call33_args.push_back(&expr33_2);
2717    BPatch_funcCallExpr call33Expr(*call33_func, call33_args);
2718
2719    checkCost(call33Expr);
2720    appThread->insertSnippet(call33Expr, *point33_2);
2721 }
2722
2723 //
2724 // Start Test Case #34 - (overload function)
2725 // 
2726 void mutatorTest34(BPatch_thread *appThread, BPatch_image *appImage)
2727 {
2728     BPatch_Vector<BPatch_point *> *point34_1 =
2729        appImage->findProcedurePoint("overload_func_test::func_cpp", BPatch_subroutine);
2730     if (!point34_1 || (point34_1->size() < 2)) {
2731          fprintf(stderr, "Unable to find point overload_func_test::func_cpp - calls. \n");
2732          exit(-1);
2733     }
2734
2735     for (int n=0; n<point34_1->size(); n++) {
2736        BPatch_function *func;
2737
2738        if ((func = (*point34_1)[n]->getCalledFunction()) == NULL) continue;
2739
2740        char fn[256];
2741        if (func->getName(fn, 256) == NULL) {
2742             fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2743             fprintf(stderr, "    Can't get name of called function in overload_func_test::func_cpp\n");
2744             exit(1);
2745        }
2746        if (strcmp(fn, "overload_func_test::call_cpp")) {
2747            fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2748            fprintf(stderr, "    The called function has a wrong name\n");
2749            exit(1);
2750        }
2751        BPatch_Vector<BPatch_point *> *point34_2 = func->findPoint(BPatch_entry);
2752        BPatch_Vector<BPatch_localVar *> *param = func->getParams();
2753        assert(point34_2 && param);
2754
2755        switch (n) {
2756           case 0 : {
2757
2758               if ( param->size() != 1 ) {
2759                  fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2760                  fprintf(stderr, "    The overloaded function has wrong number of parameters\n");
2761                  return;
2762               }
2763               BPatch_variableExpr *var1 = appImage->findVariable(*(*point34_2)[0],
2764                   "arg1");
2765               break;
2766           }
2767           case 1 : {
2768               if ( param->size() != 1 ) {
2769                  fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2770                  fprintf(stderr, "    The overloaded function has wrong number of parameters\n");
2771                  return;
2772               }
2773               BPatch_variableExpr *var1 = appImage->findVariable(*(*point34_2)[0],
2774                 "arg1");
2775               break;
2776           }
2777           case 2 : {
2778               if ( param->size() != 2 ) {
2779                  fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2780                  fprintf(stderr, "    The overloaded function has wrong number of parameters\n");
2781                  return;
2782               }
2783
2784               BPatch_variableExpr *var1 = appImage->findVariable(*(*point34_2)[0],
2785                 "arg1");
2786               BPatch_variableExpr *var2 = appImage->findVariable(*(*point34_2)[0],
2787                 "arg2");
2788               break;
2789           }
2790           default : {
2791               fprintf(stderr, "**Failed** test #34 (overloaded functions)\n");
2792               fprintf(stderr, "    Incorrect number of subroutine calls from overload_func_test::func_cpp\n");
2793               return;
2794           }
2795        };
2796     }
2797
2798     BPatch_Vector<BPatch_point *> *point34_3 =
2799     appImage->findProcedurePoint("overload_func_test::func_cpp", BPatch_exit);
2800     if (!point34_3 || point34_3->size() <1) {
2801          fprintf(stderr, "Unable to find point overload_func_test::func_cpp - exit.\n");
2802          exit(-1);
2803     }
2804
2805     BPatch_function *call34_func = appImage->findFunction("cpp_test_util::call_cpp");
2806     if (call34_func == NULL) {
2807         fprintf(stderr, "Unable to find function \"cpp_test_util::call_cpp.\"\n");
2808         exit(1);
2809     }
2810
2811     BPatch_Vector<BPatch_snippet *> call34_args;
2812     BPatch_constExpr expr34_0(34);
2813     call34_args.push_back(&expr34_0);
2814     BPatch_funcCallExpr call34Expr(*call34_func, call34_args);
2815
2816     checkCost(call34Expr);
2817     appThread->insertSnippet(call34Expr, *point34_3);
2818 }
2819
2820 //
2821 // Start Test Case #35 - (overload operator)
2822 //      
2823 void mutatorTest35(BPatch_thread *appThread, BPatch_image *appImage)
2824 {
2825
2826    BPatch_Vector<BPatch_point *> *point35_1 =
2827      appImage->findProcedurePoint("overload_op_test::func_cpp", BPatch_subroutine);
2828    assert(point35_1);
2829
2830   int index = 0;
2831   BPatch_function *func;
2832   while (index < point35_1->size()) {
2833      if ((func = (*point35_1)[index]->getCalledFunction()) == NULL) {
2834         fprintf(stderr, "**Failed** test #35 (overload operation)\n");
2835         fprintf(stderr, "    Can't find the overload operator\n");
2836         exit(1);
2837      }
2838      char fn[256];
2839      if (!strcmp("overload_op_test::operator++", func->getName(fn, 256)))
2840         break;
2841      index ++;
2842   }
2843
2844    BPatch_Vector<BPatch_point *> *point35_2 = func->findPoint(BPatch_exit);
2845    assert(point35_2);
2846
2847    BPatch_function *call35_1 = appImage->findFunction("overload_op_test_call_cpp");
2848    if (call35_1 == NULL) {
2849        fprintf(stderr, "Unable to find function \"overload_op_test_call_cpp\"\n");
2850        exit(1);
2851    }
2852
2853    BPatch_Vector<BPatch_snippet *> opArgs;
2854    opArgs.push_back(new BPatch_retExpr());
2855    BPatch_funcCallExpr call35_1Expr(*call35_1, opArgs);
2856
2857    checkCost(call35_1Expr);
2858    appThread->insertSnippet(call35_1Expr, *point35_2);
2859 }
2860
2861 //  
2862 // Start Test Case #36 - (static member)
2863 // 
2864 void mutatorTest36(BPatch_thread *appThread, BPatch_image *appImage)
2865 {
2866    BPatch_Vector<BPatch_point *> *point36_1 =
2867      appImage->findProcedurePoint("static_test::func_cpp", BPatch_subroutine);
2868    assert(point36_1);
2869
2870    int index = 0;
2871    BPatch_function *func;
2872    int bound = point36_1->size();
2873    BPatch_Vector<BPatch_variableExpr *> vect36_1;
2874   
2875    while ((index < bound) && (vect36_1.size() < 2)) {
2876      if ((func = (*point36_1)[index]->getCalledFunction()) == NULL) {
2877         fprintf(stderr, "**Failed** test #36 (static member)\n");
2878         fprintf(stderr, "    Can't find the invoked function\n");
2879         exit(1);
2880      }
2881
2882      char fn[256];
2883      if (!strcmp("static_test::call_cpp", func->getName(fn, 256))) {
2884         BPatch_Vector<BPatch_point *> *point36_2 = func->findPoint(BPatch_exit);
2885         assert(point36_2);
2886
2887         // use getComponent to access this "count". However, getComponent is
2888         // causing core dump at this point
2889         BPatch_variableExpr *var36_1 = appImage->findVariable(*(*point36_2)[0],
2890              "count");
2891         if (!var36_1) {
2892            fprintf(stderr, "**Failed** test #36 (static member)\n");
2893            fprintf(stderr, "  Can't find static variable count\n");
2894            return;
2895         }
2896         vect36_1.push_back(var36_1);
2897      }
2898      index ++;
2899    }
2900
2901    if (2 != vect36_1.size()) {
2902        fprintf(stderr, "**Failed** test #36 (static member)\n");
2903        fprintf(stderr, "  Incorrect size of an vector\n");
2904        exit(1);
2905    }
2906    if (vect36_1[0]->getBaseAddr() != vect36_1[1]->getBaseAddr()) {
2907        fprintf(stderr, "**Failed** test #36 (static member)\n");
2908        fprintf(stderr, "  Static member does not have a same address\n");
2909        exit(1);
2910    };
2911
2912    BPatch_Vector<BPatch_point *> *point36_3 =
2913    appImage->findProcedurePoint("static_test::func_cpp", BPatch_exit);
2914    assert(point36_3);
2915
2916    BPatch_function *call36_func = appImage->findFunction("static_test_call_cpp");
2917    if (call36_func == NULL) {
2918        fprintf(stderr, "Unable to find function \"cpp_test_util::call_cpp.\"\n");
2919        exit(1);
2920    }
2921
2922    BPatch_Vector<BPatch_snippet *> call36_args;
2923    BPatch_constExpr expr36_0(36);
2924    call36_args.push_back(&expr36_0);
2925    BPatch_funcCallExpr call36Expr(*call36_func, call36_args);
2926
2927    checkCost(call36Expr);
2928    appThread->insertSnippet(call36Expr, *point36_3);
2929 }
2930
2931
2932 //  
2933 // Start Test Case #37 - (namespace)
2934 // 
2935 void mutatorTest37(BPatch_thread *appThread, BPatch_image *appImage)
2936 {
2937     BPatch_Vector<BPatch_point *> *point37_1 =
2938       appImage->findProcedurePoint("namespace_test::func_cpp", BPatch_exit);
2939
2940     assert(point37_1);
2941     BPatch_variableExpr *var1 = appImage->findVariable(*(*point37_1)[0],
2942             "local_fn_var");
2943     BPatch_variableExpr *var2 = appImage->findVariable(*(*point37_1)[0],
2944             "local_file_var");
2945     BPatch_variableExpr *var3 = appImage->findVariable(*(*point37_1)[0],
2946             "CPP_DEFLT_ARG");
2947
2948     if (!var1 || !var2 || !var3) {
2949         fprintf(stderr, "**Failed** test #37 (namespace)\n");
2950         if (!var1)
2951            fprintf(stderr, "  can't find local variable local_fn_var\n");
2952         if (!var2)
2953            fprintf(stderr, "  can't find local variable file local_file_var\n");
2954         if (!var3)
2955            fprintf(stderr, "  can't find global variable CPP_DEFLT_ARG\n");
2956         return;
2957     }
2958     BPatch_Vector<BPatch_point *> *point37_2 =
2959       appImage->findProcedurePoint("main", BPatch_allLocations);
2960
2961     if (!point37_2 || (point37_2->size() < 1)) {
2962       fprintf(stderr, "Unable to find point in main.\n");
2963       exit(-1);
2964     }
2965     BPatch_variableExpr *expr37_1=appImage->findVariable(*(*point37_2)[0], "test37");
2966     if (!expr37_1) {
2967          fprintf(stderr, "**Failed** test #37 (namespace)\n");
2968          fprintf(stderr, "    Unable to locate test37 in main\n");
2969     }
2970
2971     BPatch_Vector<BPatch_variableExpr *> *fields = expr37_1->getComponents();
2972     if (!fields || fields->size() == 0 ) {
2973        fprintf(stderr, "**Failed** test #37 (namespace)\n");
2974        fprintf(stderr, "  struct lacked correct number of elements\n");
2975        exit(-1);
2976     }
2977
2978     int index = 0;
2979     while ( index < fields->size() ) {
2980      if (!strcmp("class_variable", (*fields)[index]->getName()) ) {
2981          BPatch_function *call37_func = appImage->findFunction("cpp_test_util::call_cpp");
2982          if (call37_func == NULL) {
2983              fprintf(stderr, "**Failed** test #37 (namespace)\n");
2984              fprintf(stderr, "Unable to find function \"cpp_test_util::call_cpp.\"\n");
2985              exit(1);
2986          }
2987
2988          BPatch_Vector<BPatch_snippet *> call37_args;
2989
2990          BPatch_constExpr expr37_0(37);
2991          call37_args.push_back(&expr37_0);
2992          BPatch_funcCallExpr call37Expr(*call37_func, call37_args);
2993          checkCost(call37Expr);
2994          appThread->insertSnippet(call37Expr, *point37_1);
2995          return;
2996      }
2997      index ++;
2998     }
2999     fprintf(stderr, "**Failed** test #37 (namespace)\n");
3000     fprintf(stderr, "    Can't find class member variables\n");
3001 }
3002
3003
3004 //  
3005 // Start Test Case #38 - (exception)
3006 // 
3007 void mutatorTest38(BPatch_thread *appThread, BPatch_image *appImage)
3008 {
3009    BPatch_Vector<BPatch_point *> *point38_1 =
3010      appImage->findProcedurePoint("exception_test::func_cpp", BPatch_subroutine);
3011    assert(point38_1);
3012
3013    int index = 0;
3014    BPatch_function *func;
3015    int bound = point38_1->size();
3016
3017    BPatch_variableExpr *testno = appImage->findVariable(*(*point38_1)[0],
3018             "testno");
3019    if (!testno) {
3020       fprintf(stderr, "**Failed** test #38 (exception)\n");
3021       fprintf(stderr, "    Can't find the variable in try branch of exception statement\n");
3022       exit(1);
3023    }
3024
3025    while (index < bound) {
3026      if ((func = (*point38_1)[index]->getCalledFunction()) == NULL) {
3027         fprintf(stderr, "**Failed** test #38 (exception)\n");
3028         fprintf(stderr, "    Can't find the invoked function\n");
3029         exit(1);
3030      }
3031      char fn[256];
3032      if (!strcmp("sample_exception::response", func->getName(fn, 256))) {
3033          BPatch_Vector<BPatch_point *> *point38_2 = func->findPoint(BPatch_exit);
3034          assert(point38_2);
3035
3036          BPatch_function *call38_func =
3037            appImage->findFunction("exception_test_call_cpp");
3038          if (call38_func == NULL) {
3039              fprintf(stderr, "Unable to find function \"exception_test_call_cpp.\"\n");
3040              exit(1);
3041          }
3042
3043          BPatch_Vector<BPatch_snippet *> call38_args;
3044          BPatch_constExpr expr38_0(38);
3045          call38_args.push_back(&expr38_0);
3046          BPatch_funcCallExpr call38Expr(*call38_func, call38_args);
3047
3048          checkCost(call38Expr);
3049          appThread->insertSnippet(call38Expr, *point38_2);
3050          return;
3051      }
3052      index++;
3053    }
3054 }
3055
3056 //
3057 // Start Test Case #39 - (template)
3058 //
3059 void mutatorTest39(BPatch_thread *appThread, BPatch_image *appImage)
3060 {
3061    BPatch_Vector<BPatch_point *> *point39_1 =
3062      appImage->findProcedurePoint("template_test::func_cpp", BPatch_subroutine);
3063    assert(point39_1);
3064
3065    int index = 0;
3066    int flag = 0;
3067    BPatch_function *func;
3068    int bound = point39_1->size();
3069    BPatch_variableExpr *content39_1;
3070    BPatch_variableExpr *content39_2;
3071
3072    while (index < bound) {
3073      if ((func = (*point39_1)[index]->getCalledFunction()) == NULL) {
3074         fprintf(stderr, "**Failed** test #39 (template)\n");
3075         fprintf(stderr, "    Can't find the invoked function\n");
3076         exit(1);
3077      }
3078
3079      char fn[256];
3080      if (!strcmp("sample_template<int>::content", func->getName(fn, 256))) {
3081          BPatch_Vector<BPatch_point *> *point39_2 = func->findPoint(BPatch_entry);
3082          assert(point39_2);
3083
3084          content39_1 = appImage->findVariable(*(*point39_2)[0], "ret");
3085          if (!content39_1) {
3086             fprintf(stderr, "**Failed** test #39 (template)\n");
3087             fprintf(stderr, "  Can't find local variable ret\n");
3088             return;
3089          }
3090          flag++;
3091      } else if (!strcmp("sample_template<char>::content", func->getName(fn, 256))) {
3092
3093             BPatch_Vector<BPatch_point *> *point39_3 = func->findPoint(BPatch_entry);
3094             assert(point39_3);
3095
3096             content39_2 = appImage->findVariable(*(*point39_3)[0], "ret");
3097             if (!content39_2) {
3098                fprintf(stderr, "**Failed** test #39 (template)\n");
3099                fprintf(stderr, "  Can't find local variable ret\n");
3100                return;
3101             }
3102             flag++;
3103      }
3104      index ++;
3105   }
3106
3107   if (flag != 2) {
3108      fprintf(stderr, "**Failed** test #39 (template)\n");
3109      exit(1);
3110   }
3111
3112    BPatch_type *type39_0 = const_cast<BPatch_type *> (new BPatch_type("int"));
3113    BPatch_type *type39_1 = const_cast<BPatch_type *> (content39_1->getType());
3114    BPatch_type *type39_2 = const_cast<BPatch_type *> (new BPatch_type("char"));
3115    BPatch_type *type39_3 = const_cast<BPatch_type *> (content39_2->getType());
3116
3117    if (!type39_0->isCompatible(type39_1)) {
3118       fprintf(stderr, "**Failed** test #39 (template)\n");
3119       fprintf(stderr,"    type39_0 reported as incompatibile with type39_1\n");
3120       return;
3121    }
3122
3123    if (!type39_2->isCompatible(type39_3)) {
3124       fprintf(stderr, "**Failed** test #39 (template)\n");
3125       fprintf(stderr,"    type39_2 reported as incompatibile with type39_3\n");
3126       return;
3127    }
3128    
3129    BPatch_Vector<BPatch_point *> *point39_4 =
3130    appImage->findProcedurePoint("template_test::func_cpp", BPatch_exit);
3131    assert(point39_4);
3132
3133    BPatch_function *call39_func = appImage->findFunction("template_test_call_cpp");
3134    if (call39_func == NULL) {
3135        fprintf(stderr, "Unable to find function \"cpp_test_util::call_cpp.\"\n");
3136        exit(1);
3137    }
3138
3139    BPatch_Vector<BPatch_snippet *> call39_args;
3140    BPatch_constExpr expr39_0(39);
3141    call39_args.push_back(&expr39_0);
3142    BPatch_funcCallExpr call39Expr(*call39_func, call39_args);
3143
3144    checkCost(call39Expr);
3145    appThread->insertSnippet(call39Expr, *point39_4);
3146 }
3147
3148 //
3149 // Start Test Case #40 - (declaration)
3150 //   
3151 void mutatorTest40(BPatch_thread *appThread, BPatch_image *appImage)
3152 {
3153      // Find the exit point to the procedure "func_cpp"
3154      BPatch_Vector<BPatch_point *> *point40_1 =
3155          appImage->findProcedurePoint("decl_test::func_cpp", BPatch_exit);
3156      if (!point40_1 || (point40_1->size() < 1)) {
3157            fprintf(stderr, "Unable to find point decl_test::func_cpp - exit.\n");
3158            exit(-1);
3159      }
3160
3161      BPatch_Vector<BPatch_point *> *point40_2 =
3162        appImage->findProcedurePoint("main", BPatch_allLocations);
3163
3164      if (!point40_2 || (point40_2->size() < 1)) {
3165             fprintf(stderr, "Unable to find point in main.\n");
3166             exit(-1);
3167      }
3168
3169      BPatch_function *call40_func  = appImage->findFunction("decl_test::call_cpp");
3170      if (call40_func == NULL ) {
3171         fprintf(stderr, "**Failed** test #40 (declaration)\n");
3172         fprintf(stderr, "Unable to find function \"decl_test::call_cpp\"\n");
3173         exit(1);
3174      }
3175
3176      BPatch_Vector<BPatch_snippet *> call40_args;
3177      BPatch_constExpr expr40_0(40);
3178      call40_args.push_back(&expr40_0);
3179      BPatch_funcCallExpr call40Expr(*call40_func, call40_args);
3180
3181      // find the variables of different scopes
3182      BPatch_variableExpr *expr40_1=appImage->findVariable("CPP_DEFLT_ARG");
3183      BPatch_variableExpr *expr40_2=appImage->findVariable(*(*point40_2)[0], "test40");
3184      BPatch_variableExpr *expr40_3=appImage->findVariable(*(*point40_1)[0], "CPP_DEFLT_ARG");
3185      if (!expr40_1 || !expr40_2 || !expr40_3) {
3186            fprintf(stderr, "**Failed** test #40 (delcaration)\n");
3187            fprintf(stderr, "    Unable to locate one of variables\n");
3188            exit(1);
3189      }
3190
3191     BPatch_Vector<BPatch_variableExpr *> *fields = expr40_2->getComponents();
3192     if (!fields || fields->size() == 0 ) {
3193           fprintf(stderr, "**Failed** test #40 (declaration)\n");
3194           fprintf(stderr, "  struct lacked correct number of elements\n");
3195           exit(-1);
3196      }
3197
3198     int index = 0;
3199     while ( index < fields->size() ) {
3200        if ( !strcmp("CPP_TEST_UTIL_VAR", (*fields)[index]->getName()) ) {
3201            dprintf("Inserted snippet2\n");
3202            checkCost(call40Expr);
3203            appThread->insertSnippet(call40Expr, *point40_1);
3204            return;
3205        }
3206        index ++;
3207     }
3208     fprintf(stderr, "**Failed** test #40 (declaration)\n");
3209     fprintf(stderr, "    Can't find inherited class member variables\n");
3210 }
3211
3212 //
3213 // Start Test Case #41 - (derivation)
3214 //
3215 void mutatorTest41(BPatch_thread *appThread, BPatch_image *appImage)
3216 {
3217    bool found = false;
3218    
3219    // Find the exit point to the procedure "func_cpp"
3220    BPatch_Vector<BPatch_point *> *point41_1 =
3221       appImage->findProcedurePoint("derivation_test::func_cpp", BPatch_exit);
3222    if (!point41_1 || (point41_1->size() < 1)) {
3223          fprintf(stderr, "Unable to find point derivation_test::func_cpp - exit.\n");
3224          exit(-1);
3225    }
3226
3227    // access inherited class member variables has been examined in the test 40
3228    // now let's try to access the inherited class member function.
3229
3230    BPatch_Vector<BPatch_point *> *point41_2 =
3231       appImage->findProcedurePoint("main", BPatch_allLocations);
3232
3233    if (!point41_2 || (point41_2->size() < 1)) {
3234           fprintf(stderr, "Unable to find point in main.\n");
3235           exit(-1);
3236    }
3237
3238    BPatch_variableExpr *expr41_0=appImage->findVariable(*(*point41_2)[0], "test41");
3239    if (!expr41_0) {
3240       fprintf(stderr, "**Failed** test #41 (derivation)\n");
3241       fprintf(stderr, "    Unable to locate one of variables\n");
3242       exit(1);
3243    }
3244
3245    BPatch_Vector<BPatch_variableExpr *> *fields = expr41_0->getComponents();
3246    if (!fields || fields->size() == 0 ) {
3247          fprintf(stderr, "**Failed** test #41 (derivation)\n");
3248          fprintf(stderr, "  struct lacked correct number of elements\n");
3249          exit(-1);
3250    }
3251
3252    int index = 0;
3253    while ( index < fields->size() ) {
3254        if ( !strcmp("cpp_test_util::call_cpp", (*fields)[index]->getName()) ) {
3255            found = true;
3256            break;
3257        }
3258        index ++;
3259    }
3260    
3261    if ( !found ) {
3262      fprintf(stderr, "**Failed** test #41 (derivation)\n");
3263      fprintf(stderr, "    Can't find inherited class member functions\n");
3264   }
3265 }
3266
3267 //
3268 // Start Test Case #42 - (find standard C++ library)
3269 //
3270 void mutatorTest42(BPatch_thread *appThread, BPatch_image *appImage)
3271 {
3272 #if defined(sparc_sun_solaris2_4) \
3273  || defined(i386_unknown_solaris2_5) \
3274  || defined(i386_unknown_linux2_0) \
3275  || defined(mips_sgi_irix6_4) \
3276  || defined(alpha_dec_osf4_0)
3277
3278    char libStdC[64];
3279    BPatch_module *modStdC = NULL;
3280    BPatch_Vector<BPatch_module *> *mods = appImage->getModules();
3281
3282    strcpy(libStdC, "libstdc++");
3283
3284    // Lookup the libstdc++.so standard library
3285    if (!mods || mods->size() == 0) {
3286      fprintf(stderr, "**Failed test #42 (find standard C++ library)\n");
3287      fprintf(stderr, "  Mutator couldn't search modules of standard library\n");
3288      exit(1);
3289    }
3290    for (int i = 0; i < mods->size() && !(modStdC); i++) {
3291        char buf[1024];
3292        BPatch_module *m = (*mods)[i];
3293        m->getName(buf, 1024);
3294        if (!strncmp(libStdC, buf, strlen(libStdC)))
3295           modStdC = m;
3296    }
3297    if (! modStdC ) {
3298       fprintf(stderr, "**Failed test #42 (find standard C++ library)\n");
3299       fprintf(stderr, "  Mutator couldn't find shlib in standard library\n");
3300       fflush(stdout);
3301       exit(1);
3302    }
3303
3304    // find ostream::operator<< function in the standard library
3305    BPatch_function *func = modStdC->findFunction("ostream::operator<<");
3306    if (! func) {
3307        fprintf(stderr, "**Failed test #42 (find standard C++ library)\n");
3308        fprintf(stderr, "  Mutator couldn't find a function in %s\n", libStdC);
3309        exit(1);
3310    }
3311
3312 #endif
3313 }
3314
3315 //
3316 // Start Test Case #43 - (replace function in standard C++ library)
3317 //
3318 void mutatorTest43(BPatch_thread *appThread, BPatch_image *appImage)
3319 {
3320 // There is no corresponding failure (test2) testing because the only
3321 // bad input to replaceFunction is a non-existent BPatch_function.
3322
3323 #if defined(sparc_sun_solaris2_4) \
3324  || defined(alpha_dec_osf4_0)
3325
3326    char libStdC[64];
3327    BPatch_module *modStdC = NULL;
3328    BPatch_Vector<BPatch_module *> *mods = appImage->getModules();
3329
3330    strcpy(libStdC, "libstdc++");
3331
3332    // Lookup the libstdc++.so standard library
3333    if (!mods || mods->size() == 0) {
3334      fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3335      fprintf(stderr, "  Mutator couldn't search modules of standard library\n");
3336      exit(1);
3337    }
3338    for (int i = 0; i < mods->size() && !(modStdC); i++) {
3339        char buf[1024];
3340        BPatch_module *m = (*mods)[i];
3341        m->getName(buf, 1024);
3342        if (!strncmp(libStdC, buf, strlen(libStdC)))
3343           modStdC = m;
3344    }
3345    if (! modStdC ) {
3346       fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3347       fprintf(stderr, "  Mutator couldn't find shlib in standard library\n");
3348       fflush(stdout);
3349       exit(1);
3350    }
3351
3352    // Replace a shlib function with a shlib function
3353    char buf1[64], buf2[64];
3354
3355    BPatch_function *func1 = modStdC->findFunction("ostream::operator<<");
3356    BPatch_function *func2 = modStdC->findFunction("istream::operator>>");
3357    func1->getName(buf1, 64);
3358    func2->getName(buf2, 64);
3359
3360    if (! func1 || ! func2) {
3361        fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3362        fprintf(stderr, "  Mutator couldn't find a function in %s\n", libStdC);
3363        exit(1);
3364    }
3365    if (! appThread->replaceFunction(*func1, *func2)) {
3366         fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3367         fprintf(stderr, "  Mutator couldn't replaceFunction (shlib -> shlib)\n");
3368    }
3369
3370    // Replace a shlib function with an a.out function
3371    BPatch_function *func3 = appImage->findFunction("stdlib_test2::call_cpp");
3372    if (! func3 ) {
3373       fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3374       fprintf(stderr, "Unable to find function \"stdlib_test2::call_cpp\"\n");
3375       exit(1);
3376    }
3377    if (! appThread->replaceFunction(*func1, *func3)) {
3378       fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3379       fprintf(stderr, "  Mutator couldn't replaceFunction (shlib -> a.out)\n");
3380       exit(1);
3381    }
3382
3383    // Replace an a.out function with a shlib function
3384     if (! appThread->replaceFunction(*func3, *func2) ) {
3385       fprintf(stderr, "**Failed test #43 (replace function in standard C++ library)\n");
3386       fprintf(stderr, "  Mutator couldn't replaceFunction (a.out -> shlib)\n");
3387       exit(1);
3388     }
3389
3390 #endif
3391 }
3392
3393 //
3394 // Start Test Case #44 - (C++ member function - virtual, const and inline)
3395 //
3396 void mutatorTest44(BPatch_thread *appThread, BPatch_image *appImage)
3397 {
3398   BPatch_Vector<BPatch_point *> *point44_0 =
3399     appImage->findProcedurePoint("cpp_test::func2_cpp", BPatch_allLocations);
3400
3401   BPatch_Vector<BPatch_point *> *point44_1 =
3402     appImage->findProcedurePoint("cpp_test::func_cpp", BPatch_allLocations);
3403
3404   BPatch_Vector<BPatch_point *> *point44_2 =
3405     appImage->findProcedurePoint("func_test::func_cpp", BPatch_allLocations);
3406
3407   BPatch_Vector<BPatch_point *> *point44_3 =
3408     appImage->findProcedurePoint("func_test::call_cpp", BPatch_allLocations);
3409
3410   if ( !point44_0 || (point44_0->size() < 1) ||
3411        !point44_1 || (point44_1->size() < 1) ||
3412        !point44_2 || (point44_2->size() < 1) ||
3413        !point44_3 || (point44_3->size() < 1)  ) {
3414
3415        if ( !point44_0 || (point44_0->size() < 1) ) {
3416          fprintf(stderr, "**Failed** test #44 (C++ Member functions)\n");
3417          fprintf(stderr, "     Unable to find point in an virtual function \"cpp_test::func2_cpp.\"\n");
3418        }
3419        if ( !point44_1 || (point44_1->size() < 1) ) {
3420          fprintf(stderr, "**Warning** test #44 (C++ Member functions)\n");
3421          fprintf(stderr, "    Unable to find point in a pure virtual function \"cpp_test::func_cpp.\"\n");
3422        }
3423        if ( !point44_2 || (point44_2->size() < 1) ) {
3424          fprintf(stderr, "**Failed** test #44 (C++ Member functions)\n");
3425          fprintf(stderr, "     Unable to find point in a const function \"func_test::func_cpp.\"\n");
3426        }
3427        if ( !point44_3 || (point44_3->size() < 1) ) {
3428          fprintf(stderr, "**Failed** test #44 (C++ Member functions)\n");
3429          fprintf(stderr, "     Unable to find point in an inline function \"func_test::call_cpp.\"\n");
3430        }
3431   }
3432
3433   for (int n=0; n<point44_2->size(); n++) {
3434      BPatch_function *func;
3435
3436       if ((func = (*point44_2)[n]->getCalledFunction()) == NULL) continue;
3437
3438       char fn[256];
3439       if (func->getName(fn, 256) == NULL) {
3440            fprintf(stderr, "**Failed** test #44 (C++ member function)\n");
3441            fprintf(stderr, "    Can't get name of called function in func_test::func_cpp\n");
3442            exit(1);
3443       }
3444
3445       if (! strcmp(fn, "func_test::call_cpp") ) {
3446         BPatch_Vector<BPatch_localVar *> *param = func->getParams();
3447         assert(param);
3448
3449         if ( param->size() != 0 ) {
3450           fprintf(stderr, "**Failed** test #44 (C++ member function)\n");
3451           fprintf(stderr, "    The inline function is not inlined\n");
3452           exit(1);
3453         }
3454
3455         BPatch_variableExpr *var1 = appImage->findVariable(*(*point44_2)[0],
3456                        "tmp");
3457
3458         if (var1 == NULL) {
3459           fprintf(stderr, "**Failed** test #44 (C++ member function)\n");
3460           fprintf(stderr, "    The inline function is not inlined\n");
3461           exit(1);
3462         }
3463        return;
3464       }
3465   }
3466
3467   fprintf(stderr, "**Failed** test #44 (C++ member function)\n");
3468   fprintf(stderr, "  Mutator couldn't find inline function in the caller\n");
3469   exit(1);
3470 }
3471
3472
3473 /*******************************************************************************/
3474 /*******************************************************************************/
3475 /*******************************************************************************/
3476
3477 int mutatorMAIN(char *pathname, bool useAttach)
3478 {
3479     BPatch_thread *appThread;
3480
3481     // Create an instance of the BPatch library
3482     bpatch = new BPatch;
3483
3484     // Register a callback function that prints any error messages
3485     bpatch->registerErrorCallback(errorFunc);
3486
3487     // Start the mutatee
3488     printf("Starting \"%s\"\n", pathname);
3489
3490     char *child_argv[MAX_TEST+5];
3491    
3492     int n = 0;
3493     child_argv[n++] = pathname;
3494     if (debugPrint) child_argv[n++] = "-verbose";
3495
3496     if (!runAllTests) {
3497         child_argv[n++] = "-run";
3498         for (unsigned int j=0; j <= MAX_TEST; j++) {
3499             if (runTest[j]) {
3500                 char str[5];
3501                 sprintf(str, "%d", j);
3502                 child_argv[n++] = strdup(str);
3503             }
3504         }
3505     }
3506
3507     child_argv[n] = NULL;
3508
3509     if (useAttach) {
3510         int pid = startNewProcessForAttach(pathname, child_argv);
3511         if (pid < 0) {
3512             printf("*ERROR*: unable to start tests due to error creating mutatee process\n");
3513             exit(-1);
3514         } else {
3515             dprintf("New mutatee process pid %d started; attaching...\n", pid);
3516         }
3517         P_sleep(1); // let the mutatee catch its breath for a moment
3518         appThread = bpatch->attachProcess(pathname, pid);
3519     } else {
3520         appThread = bpatch->createProcess(pathname, child_argv,NULL);
3521     }
3522
3523     if (appThread == NULL) {
3524         fprintf(stderr, "Unable to run test program.\n");
3525         exit(1);
3526     }
3527
3528     // Read the program's image and get an associated image object
3529     BPatch_image *appImage = appThread->getImage();
3530
3531     // Signal the child that we've attached
3532     if (useAttach) {
3533         signalAttached(appThread, appImage);
3534     }
3535
3536     // determine whether mutatee is C or C++
3537     BPatch_variableExpr *isCxx = appImage->findVariable("mutateeCplusplus");
3538     if (isCxx == NULL) {
3539         fprintf(stderr, "  Unable to locate variable \"mutateeCplusplus\""
3540                  " -- assuming 0!\n");
3541     } else {
3542         isCxx->readValue(&mutateeCplusplus);
3543         dprintf("Mutatee is %s.\n", mutateeCplusplus ? "C++" : "C");
3544     }
3545
3546     int i;
3547     BPatch_Vector<BPatch_module *> *m = appImage->getModules();
3548     for (i=0; i < m->size(); i++) {
3549         // dprintf("func %s\n", (*m)[i]->name());
3550     }
3551     BPatch_Vector<BPatch_function *> *p1 = (*m)[0]->getProcedures();
3552
3553     BPatch_Vector<BPatch_function *> *p = appImage->getProcedures();
3554     for (i=0; i < p->size(); i++) {
3555         // dprintf("func %s\n", (*p)[i]->name());
3556     }
3557
3558     if (runTest[1]) mutatorTest1(appThread, appImage);
3559     if (runTest[2]) mutatorTest2(appThread, appImage);
3560     if (runTest[3]) mutatorTest3(appThread, appImage);
3561     if (runTest[4]) mutatorTest4(appThread, appImage);
3562     if (runTest[5]) mutatorTest5(appThread, appImage);
3563     if (runTest[6]) mutatorTest6(appThread, appImage);
3564     if (runTest[7]) mutatorTest7(appThread, appImage);
3565     if (runTest[8]) mutatorTest8(appThread, appImage);
3566     if (runTest[9]) mutatorTest9(appThread, appImage);
3567     if (runTest[10]) mutatorTest10(appThread, appImage);
3568     if (runTest[11]) mutatorTest11(appThread, appImage);
3569
3570     if (runTest[12]) mutatorTest12a(appThread, appImage);
3571
3572     if (runTest[13]) mutatorTest13(appThread, appImage);
3573     if (runTest[14]) mutatorTest14(appThread, appImage);
3574
3575     if (runTest[15]) mutatorTest15a(appThread, appImage);
3576
3577     if (runTest[16]) mutatorTest16(appThread, appImage);
3578
3579     if (runTest[17]) mutatorTest17(appThread, appImage);
3580     if (runTest[18]) mutatorTest18(appThread, appImage);
3581     if (runTest[20]) mutatorTest20(appThread, appImage);
3582
3583     if (runTest[21] || runTest[22]) readyTest21or22(appThread);
3584     if (runTest[21]) mutatorTest21(appThread, appImage); 
3585     if (runTest[22]) mutatorTest22(appThread, appImage);
3586     if (runTest[23]) mutatorTest23(appThread, appImage);
3587     if (runTest[24]) mutatorTest24(appThread, appImage);
3588     if (runTest[25]) mutatorTest25(appThread, appImage);
3589     if (runTest[26]) mutatorTest26(appThread, appImage);
3590     if (runTest[27]) mutatorTest27(appThread, appImage);
3591     if (runTest[28]) mutatorTest28(appThread, appImage);
3592     if (runTest[29]) mutatorTest29(appThread, appImage);
3593     if (runTest[30]) mutatorTest30(appThread, appImage);
3594
3595     if( runTest[ 31 ] ) mutatorTest31( appThread, appImage );<