示例#1
0
void make_bc(){

	start_pass("make_bc");

	options_to_file();

	string base_path = cmd_option_str("base_path");
	string llvm_path = cmd_option_str("llvm_path");
	stringstream cmd;

	make_initial_bc();

	if(cmd_option_str("seed_function") != ""){
		cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -isolate_function < file.bc > file-2.bc";
		systm(cmd.str().c_str());
		cmd.str("");
		cmd << "mv file-2.bc file.bc";
		systm(cmd.str().c_str());
	}

	// First optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_fill_names < file.bc > file-2.bc";
	systm(cmd.str().c_str());

	// Second optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_all < file-2.bc > file-3.bc";
	systm(cmd.str().c_str());

	end_pass("make_bc");

}
示例#2
0
void view_bc(){

	start_pass("view_bc");

	string llvm_path = cmd_option_str("llvm_path");
	stringstream cmd;

	make_initial_bc();

	// First optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_fill_names < file.bc > file-2.bc";
	systm(cmd.str().c_str());

	// Disassembly
	cmd.str("");
	cmd << "llvm-dis < file-2.bc > salida1.txt";
	systm(cmd.str().c_str());

	// Visualize
	cmd.str("");
	cmd << "gedit salida1.txt &";
	systm(cmd.str().c_str());

	end_pass("view_bc");

}
示例#3
0
void add_paths(set<PathAndConds>& frontier){

	stringstream action;

	action << "echo 'select path from frontend;' | sqlite3 database.db > paths";
	systm(action.str());

	action.str("");
	action << "echo 'select conditions from frontend;' | sqlite3 database.db > conditions";
	systm(action.str());


	action.str("");
	action << "echo 'select file_initializations from frontend;' | sqlite3 database.db > file_initializations";
	systm(action.str());

	string line;

	vector<string> paths;
	vector<string> conds;
	vector<string> file_initializations;

	{
		ifstream input(tmp_file("paths").c_str());
		while( getline( input, line ) ) {
			paths.push_back(line);
		}
	}
	
	{
		ifstream input(tmp_file("conditions").c_str());
		while( getline( input, line ) ) {
			conds.push_back(line);
		}
	}

	{
		ifstream input(tmp_file("file_initializations").c_str());
		while( getline( input, line ) ) {
			file_initializations.push_back(line);
		}
	}


	assert(paths.size() == conds.size());

	for ( unsigned int i = 0; i < paths.size(); i++) {
		PathAndConds path_and_cond = {paths[i], conds[i], width(conds[i]), get_last_bb(), file_initializations[i]};
		frontier.insert(path_and_cond);
	}




}
示例#4
0
void gen_final_for_measurement(){

	string base_path = cmd_option_str("base_path");
	string llvm_path = cmd_option_str("llvm_path");
	string output_file = cmd_option_str("output_file");
	stringstream cmd;

	make_initial_bc();

	// First optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestMeasure.so -meas_fillnames < file.bc > file-2.bc";
	systm(cmd.str().c_str());

	// Second optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestMeasure.so -meas_all < file-2.bc > file-3.bc";
	systm(cmd.str().c_str());

	if(cmd_option_bool("link_bc")){
		// link
		cmd.str("");
		cmd << "llvm-link -o=" << tmp_file("final.bc") << " " << base_path << "/lib/forest.a file-3.bc";
		systm(cmd.str().c_str());

		// from .bc to .s
		cmd.str("");
		cmd << "llc final.bc -o final.s";
		systm(cmd.str().c_str());

		// From .s to .o
		cmd.str("");
		cmd << "gcc -c final.s -o final.o";
		systm(cmd.str().c_str());

		// link
		cmd.str("");
		cmd << "g++ final.o -lpthread -ldl -lrt -o " << output_file;
		systm(cmd.str().c_str());
	} else {
		// From .bc to .s
		cmd.str("");
		cmd << "llc file-3.bc -o file-3.s";
		systm(cmd.str().c_str());

		// from .s to .o
		cmd.str("");
		cmd << "gcc -c file-3.s -o file-3.o";
		systm(cmd.str().c_str());

		// link
		cmd.str("");
		cmd << "g++ file-3.o " << base_path << "/lib/forest.a -lpthread -ldl -lrt -o " << output_file;
		systm(cmd.str().c_str());
	}


}
示例#5
0
void compare_bc(){

	start_pass("compare_bc");


	string base_path = cmd_option_str("base_path");
	string llvm_path = cmd_option_str("llvm_path");
	stringstream cmd;

	make_initial_bc();

	// First optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_fill_names < file.bc > file-2.bc";
	systm(cmd.str().c_str());

	// Disassembly
	cmd.str("");
	cmd << "llvm-dis < file-2.bc > salida1.txt";
	systm(cmd.str().c_str());


	// Second optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_all < file-2.bc > file-3.bc";
	systm(cmd.str().c_str());

	// Disassembly
	cmd.str("");
	cmd << "llvm-dis < file-3.bc > salida2.txt";
	systm(cmd.str().c_str());


	// Comparison
	cmd.str("");
	cmd << "meld salida1.txt salida2.txt";
	systm(cmd.str().c_str());


	end_pass("compare_bc");

}
示例#6
0
void get_static_heuristic(){

	start_pass("heuristic");

	make_initial_bc();

	if(!is_parseable()){
		end_pass("heuristic");
		return;
	}

	stringstream cmd;
	string llvm_path   = cmd_option_str("llvm_path");

	// Clean optimization pass
	cmd.str("");
	//cmd << "opt -simplifycfg -instcombine -strip-dead-prototypes < file.bc > file-2.bc";
	cmd << "opt -strip-dead-prototypes < file.bc > file-2.bc";
	systm(cmd.str().c_str());

	// First optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestMeasure.so -meas_fillnames < file-2.bc > file-3.bc";
	systm(cmd.str().c_str());

	// Second optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestInstr.so -instr_rmmalloc -instr_rmpthread -instr_rmputs -instr_rmassume -instr_rmmemcpy -instr_rmerror -instr_rmindet -instr_addassert < file-3.bc > file-4.bc";
	systm(cmd.str().c_str());

	options_to_file();

	// Heuristic optimization pass
	cmd.str("");
	cmd << "opt -load " << llvm_path << "/Release+Asserts/lib/ForestHeuristic.so -pathfinder < file-4.bc > file-5.bc";
	systm(cmd.str().c_str());

	end_pass("heuristic");

}
示例#7
0
string get_last_bb(){
	stringstream command;
	command << "echo 'select last_bb from last_bb;' | sqlite3 database.db > " << tmp_file("last_bb");
	systm(command.str().c_str());
	

	ifstream input(tmp_file("last_bb").c_str());
	string line;
	
	input >> line;

	return line;


	
}
示例#8
0
void random_testing(){

	gen_file_free_variables_from_xml();
	gen_file_vectors_random();
	gen_final_for_measurement();

	set_option("measurement", "true");
	options_to_file();


	// Run
	
	string output_file = cmd_option_str("output_file");
	stringstream cmd;
	cmd.str("");
	cmd << "./" + output_file;
	systm(cmd.str().c_str());

}
示例#9
0
void measure_coverage(){

	start_pass("measure_coverage");

	gen_file_free_variables();
	gen_file_vectors();
	gen_final_for_measurement();

	set_option("measurement", "true");
	options_to_file();

	// Run
	
	string output_file = cmd_option_str("output_file");
	stringstream cmd;
	cmd.str("");
	cmd << "./" << output_file;
	systm(cmd.str().c_str());

	end_pass("measure_coverage");
	

}
示例#10
0
/*
 *	Print mail entries
 */
void
printmail()
{
	static char pn[] = "printmail";
	int	flg, curlet, showlet, k, print, aret, stret, rc;
	int	nsmbox = 0;	/* 1 ==> mailbox is in non-standard place */
	int	sav_j = -1;
	char	*p, *getarg();
	struct	stat stbuf;
	struct	stat *stbufp;
	int ttyf = isatty(1) ? TTY : ORDINARY;
	char	readbuf[LSIZE];	/* holds user's response in interactive mode */
	char	*resp;
	gid_t	savedegid;

	stbufp = &stbuf;

	/*
	 *	create working directory mbox name
	 */
	if ((hmbox = malloc(strlen(home) + strlen(mbox) + 1)) == NULL) {
		errmsg(E_MBOX, "");
		return;
	}
	cat(hmbox, home, mbox);

	/*
	 *	If we are not using an alternate mailfile, then get
	 *	the $MAIL value and build the filename for the mailfile.
	 *	If $MAIL is set, but is NOT the 'standard' place, then
	 *	use it but set flgf to circumvent :saved processing.
	 */
	if (!flgf) {
		if ((p = malloc(strlen(maildir) + strlen(my_name) + 1))
								== NULL) {
			errmsg(E_MEM, "");
			return;
		}
		cat(p, maildir, my_name);
		if (((mailfile = getenv("MAIL")) == NULL) ||
		    (strlen(mailfile) == 0)) {
			/* $MAIL not set, use standard path to mailfile */
			mailfile = p;
		} else {
			if (strcmp(mailfile, p) != 0) {
			    flgf = 1;
			    nsmbox = 1;
			    Dout(pn, 0, "$MAIL ('%s') != standard path\n",
				mailfile);
			    Dout("", 0, "\tSetting flgf to 1.\n");
			}
			free(p);
		}
	}

	/*
	 *	Get ACCESS and MODIFICATION times of mailfile BEFORE we
	 *	use it. This allows us to put them back when we are
	 *	done. If we didn't, the shell would think NEW mail had
	 *	arrived since the file times would have changed.
	 */
	stret = CERROR;
	if (access(mailfile, A_EXIST) == A_OK) {
		if ((stret = stat(mailfile, stbufp)) != A_OK) {
			errmsg(E_FILE, "Cannot stat mailfile");
			return;
		}
		mf_gid = stbufp->st_gid;
		mf_uid = stbufp->st_uid;
		utimep->actime = stbufp->st_atime;
		utimep->modtime = stbufp->st_mtime;
		file_size = stbufp->st_size;
	}

	/* Open the file as the real gid */
	savedegid = getegid();
	(void) setegid(getgid());
	malf = fopen(mailfile, "r");
	(void) setegid(savedegid);
	/*
	 *	stat succeeded, but we cannot access the mailfile
	 */
	if (stret == CSUCCESS && malf == NULL) {
		char buf[MAXFILENAME+50];
		(void) snprintf(buf, sizeof (buf),
		    "Invalid permissions on %s", mailfile);
		errmsg(E_PERM, buf);
		return;
	} else
	/*
	 *	using an alternate mailfile, but we failed on access
	 */
	if (!nsmbox && flgf && (malf == NULL)) {
		errmsg(E_FILE, "Cannot open mailfile");
		return;
	}
	/*
	 *	we failed to access OR the file is empty
	 */
	else if ((malf == NULL) || (stbuf.st_size == 0)) {
		if (!flge && !flgE) {
			printf("No mail.\n");
		}
		error = E_FLGE;
		Dout(pn, 0, "error set to %d\n", error);
		return;
	}
	if (flge)
		return;

	if (flgE) {
		if (utimep->modtime < utimep->actime) {
			error = E_FLGE_OM;
			Dout(pn, 0, "error set to %d\n", error);
		}
		return;
	}
	/*
	 *	Secure the mailfile to guarantee integrity
	 */
	lock(my_name);

	/*
	 *	copy mail to temp file and mark each letter in the
	 *	let array --- mailfile is still locked !!!
	 */
	mktmp();
	copymt(malf, tmpf);
	onlet = nlet;
	fclose(malf);
	fclose(tmpf);
	unlock();	/* All done, OK to unlock now */
	tmpf = doopen(lettmp, "r+", E_TMP);
	changed = 0;
	print = 1;
	curlet = 0;
	while (curlet < nlet) {
		/*
		 *	reverse order ?
		 */
		showlet = flgr ? curlet : nlet - curlet - 1;

		if (setjmp(sjbuf) == 0 && print != 0) {
				/* -h says to print the headers first */
				if (flgh) {
					gethead(showlet, 0);
					flgh = 0;	/* Only once */
					/* set letter # to invalid # */
					curlet--;
					showlet =
					    flgr ? curlet : nlet - curlet - 1;
				} else {
					if (showlet != sav_j) {
						/* Looking at new message. */
						/* Reset flag to override */
						/* non-display of binary */
						/* contents */
						sav_j = showlet;
						pflg = 0;
						Pflg = flgP;
					}
					copylet(showlet, stdout, ttyf);
				}
		}

		/*
		 *	print only
		 */
		if (flgp) {
			curlet++;
			continue;
		}
		/*
		 *	Interactive
		 */
		interactive = 1;
		setjmp(sjbuf);
		stat(mailfile, stbufp);
		if (stbufp->st_size != file_size) {
			/*
			 *	New mail has arrived, load it
			 */
			k = nlet;
			lock(my_name);
			malf = doopen(mailfile, "r", E_FILE);
			fclose(tmpf);
			tmpf = doopen(lettmp, "a", E_TMP);
			fseek(malf, let[nlet].adr, 0);
			copymt(malf, tmpf);
			file_size = stbufp->st_size;
			fclose(malf);
			fclose(tmpf);
			unlock();
			tmpf = doopen(lettmp, "r+", E_TMP);
			if (++k < nlet)
				printf("New mail loaded into letters %d - %d\n",
				    k, nlet);
			else
				printf("New mail loaded into letter %d\n",
				    nlet);
		}

		/* read the command */
		printf("? ");
		fflush(stdout);
		fflush(stderr);
		if (fgets(readbuf, sizeof (readbuf), stdin) == NULL) break;
		resp = readbuf;
		while (*resp == ' ' || *resp == '\t') resp++;
		print = 1;
		Dout(pn, 0, "resp = '%s'\n", resp);
		if ((rc = atoi(resp)) != 0) {
			if (!validmsg(rc)) print = 0;
			else curlet = flgr ? rc - 1 : nlet - rc;
		} else switch (resp[0]) {
			default:
				printf("Usage:\n");
			/*
			 *	help
			 */
			case '?':
				print = 0;
				for (rc = 0; help[rc]; rc++)
					printf("%s", help[rc]);
				break;
			/*
			 *	print message number of current message
			 */
			case '#':
				print = 0;
				if ((showlet == nlet) || (showlet < 0)) {
					printf("No message selected yet.\n");
				} else {
					printf("Current message number is %d\n",
					    showlet+1);
				}
				break;
			/*
			 *	headers
			 */
			case 'h':
				print = 0;
				if (resp[2] != 'd' &&
				    resp[2] != 'a' &&
				    (rc = getnumbr(resp+1)) > 0) {
					showlet = rc - 1;
					curlet = flgr ? rc - 1 : nlet - rc- 1;
				}
				if (rc == -1 && resp[2] != 'a' &&
				    resp[2] != 'd')
					break;
				if (resp[2] == 'a') rc = 1;
				else if (resp[2] == 'd') rc = 2;
					else rc = 0;

/*
 *				if (!validmsg(showlet)) break;
 */
				gethead(showlet, rc);
				break;
			/*
			 *	skip entry
			 */
			case '+':
			case 'n':
			case '\n':
				curlet++;
				break;
			case 'P':
				Pflg++;
				break;
			case 'p':
				pflg++;
				break;
			case 'x':
				changed = 0;
			case 'q':
				goto donep;
			/*
			 *	Previous entry
			 */
			case '^':
			case '-':
				if (--curlet < 0) curlet = 0;
				break;
			/*
			 *	Save in file without header
			 */
			case 'y':
			case 'w':
			/*
			 *	Save mail with header
			 */
			case 's':
				print = 0;
				if (!validmsg(curlet)) break;
				if (resp[1] == '\n' || resp[1] == '\0') {
					cat(resp+1, hmbox, "");
				} else if (resp[1] != ' ') {
					printf("Invalid command\n");
					break;
				}
				umask(umsave);
				flg = 0;
				if (getarg(lfil, resp + 1) == NULL) {
					cat(resp + 1, hmbox, "");
				}
				malf = (FILE *)NULL;
				p = resp + 1;
				while ((p = getarg(lfil, p)) != NULL) {
					if (flg) {
					    fprintf(stderr,
						"%s: File '%s' skipped\n",
						program, lfil);
					    continue;
					}
					malf = NULL;
					if ((aret = legal(lfil))) {
						malf = fopen(lfil, "a");
					}
					if ((malf == NULL) || (aret == 0)) {
					    fprintf(stderr,
						"%s: Cannot append to %s\n",
						program, lfil);
					    flg++;
					} else if (aret == 2) {
						chown(lfil, my_euid, my_gid);
					}
					if (!flg &&
					    copylet(showlet, malf, resp[0] ==
					    's'? ORDINARY: ZAP) == FALSE) {
						fprintf(stderr,
					    "%s: Cannot save mail to '%s'\n",
						    program, lfil);
						flg++;
					} else
						Dout(pn, 0, "!saved\n");
					if (malf != (FILE *)NULL) {
						fclose(malf);
					}
				}
				umask(7);
				if (!flg) {
					setletr(showlet, resp[0]);
					print = 1;
					curlet++;
				}
				break;
			/*
			 *	Reply to a letter
			 */
			case 'r':
				print = 0;
				if (!validmsg(curlet)) break;
				replying = 1;
				for (k = 1; resp[k] == ' ' || resp[k] == '\t';
				    ++k);
				resp[strlen(resp)-1] = '\0';
				(void) strlcpy(m_sendto, resp+k,
				    sizeof (m_sendto));
				goback(showlet);
				replying = 0;
				setletr(showlet, resp[0]);
				break;
			/*
			 *	Undelete
			 */
			case 'u':
				print = 0;
				if ((k = getnumbr(resp+1)) <= 0) k = showlet;
				else k--;
				if (!validmsg(k)) break;
				setletr(k, ' ');
				break;
			/*
			 *	Mail letter to someone else
			 */
			case 'm':
				{
				reciplist list;
				print = 0;
				if (!validmsg(curlet)) break;
				new_reciplist(&list);
				flg = 0;
				k = 0;
				if (substr(resp, " -") != -1 ||
					substr(resp, "\t-") != -1) {
					printf("Only users may be specified\n");
					break;
				}
				p = resp + 1;
				while ((p = getarg(lfil, p)) != NULL) {
					char *env;
					if (lfil[0] == '$') {
						if (!(env = getenv(&lfil[1]))) {
							fprintf(stderr,
				"%s: %s has no value or is not exported.\n",
							    program, lfil);
							flg++;
						} else
							add_recip(&list, env,
							    FALSE);
						k++;
					} else if (lfil[0] != '\0') {
						add_recip(&list, lfil, FALSE);
						k++;
					}
				}
				(void) strlcpy(Rpath, my_name, sizeof (Rpath));
				sending = TRUE;
				flg += sendlist(&list, showlet, 0);
				sending = FALSE;
				if (k) {
					if (!flg) {
						setletr(showlet, 'm');
						print = 1;
						curlet++;
					}
				} else
					printf("Invalid command\n");
				del_reciplist(&list);
				break;
				}
			/*
			 *	Read new letters
			 */
			case 'a':
				if (onlet == nlet) {
					printf("No new mail\n");
					print = 0;
					break;
				}
				curlet = 0;
				print = 1;
				break;
			/*
			 *	Escape to shell
			 */
			case '!':
				systm(resp + 1);
				printf("!\n");
				print = 0;
				break;
			/*
			 *	Delete an entry
			 */
			case 'd':
				print = 0;
				k = 0;
				if (strncmp("dq", resp, 2) != SAME &&
					strncmp("dp", resp, 2) != SAME)
					if ((k = getnumbr(resp+1)) == -1) break;
				if (k == 0) {
					k = showlet;
					if (!validmsg(curlet)) break;
					print = 1;
					curlet++;
				} else	k--;

				setletr(k, 'd');
				if (resp[1] == 'p') print = 1;
				else if (resp[1] == 'q') goto donep;
				break;
		}
	}
	/*
	 *	Copy updated mailfile back
	 */
donep:
	if (changed) {
		copyback();
		stamp();
	}
}
示例#11
0
void drive_frontend(){

	//printf("drive_frontend\n");

	set_option("follow_path", "true");
	set_option("single_step", "true");

	set<PathAndConds> frontier;

	load_heuristics();

	int n = 0;
	do {
		PathAndConds first;
		if(frontier.size()){

			bool pick_random = cmd_option_bool("random_branching");
			//bool pick_random = true;
			if(pick_random){
				int pick = rand() % frontier.size() - 1;
				set<PathAndConds>::iterator it = frontier.begin(); it++;
				while(pick > 0){
					it++; pick--;
				}
				first = *(it);
				frontier.erase(it);
			} else {
				first = *(frontier.begin());
				frontier.erase(frontier.begin());
			}
		}

		set_option("path", first.path);
		set_option("file_initializations", first.file_initializations);

		options_to_file();

		db_command("delete from frontend;");
		db_command("delete from results;");
		db_command("delete from last_bb;");
		db_command("delete from variables;");

		// Run resulting file
		stringstream cmd;
		cmd << "./" << cmd_option_str("output_file");
		systm(cmd.str().c_str());

		add_paths(frontier);

		if(n++ == cmd_option_int("max_depth"))
			exit(0);

		if(cmd_option_bool("show_frontier")){
			printf("last_bb %s\n", get_last_bb().c_str() ); // last one executed.
			// frontier contains the constraints to get to last_bb + the last constraint
			
			if(get_last_bb() == cmd_option_str("target_node")){
				printf("Node hitted\n");
				//exit(0);
				break;
			}

			print_frontier(frontier);

			if(cmd_option_bool("stop_in_frontier"))
				getchar();
		}


	} while(frontier.size());

}