コード例 #1
0
ファイル: cracker.c プロジェクト: bend/Zip_cracker
void create_process(unsigned int nb_process, char* file_to_crack, char* dictionary_file){
	params* shared;
	unsigned int i;
	pid_t *pids;
	sem_unlink(SEM_EMPTY);
	sem_unlink(SEM_FULL);
		pids = malloc(sizeof(pid_t)*(nb_process +1));
		if(pids == NULL){
			perror("malloc failed");
			exit(-1);
		}
		shared = create_mem_segment(PARAMS_KEY);
		shared->buf = bounded_buffer_proc_new(BB_KEY);
		shared->dictionary=dictionary_file;
		shared->zipfile = file_to_crack;
		shared->found = false;
		sem_unlink(SEM_EMPTY);
		sem_unlink(SEM_FULL);
		shared->empty = sem_open(SEM_EMPTY,O_CREAT,0666,shared->buf->size);  
		shared->full = sem_open(SEM_FULL,O_CREAT,0666,0);             
		for(i=0; i<nb_process+1; i++){
			pids[i]=fork();
			if(i==(nb_process)){
				if(pids[i]==0){
					fill_buffer(shared);
				}else if(pids[i]<0){
					perror("fork error");
					exit(-1);
				}
			}else{	
				if(pids[i]==0){
					crack_password(shared);
				}else if(pids[i]<0){
					perror("fork error");
					exit(-1);
				}
			}	  
		}
		for(i=0; i<nb_process; i++)
			waitpid(pids[i], NULL, 0);	
		sem_unlink(SEM_EMPTY);
		sem_unlink(SEM_FULL);
		bounded_buffer_proc_free(BB_KEY);
		free_mem_segment(PARAMS_KEY);
}
コード例 #2
0
//main simulator for the PUMP analysis
void simulate(  gzFile* inputFile
		, gzFile* allocFile 
		, FILE *result_file
		, gzFile dfile
		, FILE *mvec_file
		, FILE *l2misses_file
		, unsigned int size_l1
		, unsigned int size_l2
		)
{ 
	int cycle = 0;

//	int key_size = 
//	int val_size = 

	//initialize the pump structures...
	init_pumps(
			  size_l1
			, size_l2
	);
	
	char instring[1024];
	char* ptr;
	char* sstr;

	char astring[1024];
	char* asstr;

	unsigned int pc, opgroup;
	unsigned int if1 = 0, if2 = 0, if3 = 0;
	unsigned int r1, r2, r3;
	unsigned int op1, op2, op3, mem_addr;
	
	unsigned int alloc_ptr, alloc_size, alloc_tag=0;
	unsigned int dealloc_ptr, dealloc_size;

	unsigned int m[6];
	unsigned int r[2];

	struct PUMP_Output pump_out;


	unsigned int memcare, care, rescare;
	unsigned int tmpval;


	unsigned int inc = 1e3;
	unsigned int checkpoint = inc;

	//Processing trace...
  	while ((ptr=gzgets(*inputFile, instring, 1000))!= NULL) {
		//printf("%d - %s", cycle, instring);

		cycle++;

		instring[strlen(instring)] = '\0';	
		sstr = strtok(instring, " \n");

		pc = strToLInt(sstr);

		sstr = strtok(NULL, " ");

		if(opcodeToInt(sstr, &opgroup, &care, &rescare) == 0)
		{
			printf("%s\n", sstr);
			exit(0);
		}

		if((strcmp(sstr, "call_pal") != 0) && (strcmp(sstr,"wh64") != 0))
		{
			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r1 = atoi(sstr+1);
				if(*sstr == 'r')
					if1 = 0;
				else
					if1 = 1;
			}
			else {
				r1 = 0;
				if1 = 0;
			}

			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r2 = atoi(sstr+1);
				if(*sstr == 'r')
					if2 = 0;
				else
					if2 = 1;
			}
			else {
				r2 = 0;
				if2 = 0;
			}

			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r3 = atoi(sstr+1);
				if(*sstr == 'r')
					if3 = 0;
				else
					if3 = 1;
			}
			else {
				r3 = 0;
				if3 = 0;
			}

			if((opgroup==arith2s1d) && (*sstr == '-'))
				opgroup = arith1s1d;


			op1=r1;
			op2=r2;
			op3=r3;

			int t;

			if((opgroup==arith2s1d) || (opgroup==arith1s1d) || (opgroup==move))
			{
				op3 = r1;
				op1 = r2;
				op2 = r3;

				t = if3;
				if3 = if1;
				if1 = if2;
				if2 = t;
			}
			else if(opgroup==other)
			{
				op1 = r2;

				if1 = if2;
			}
			else if((opgroup==bload) || (opgroup==wload))
			{
				op3 = r1;
				op1 = r2;

				if3 = if1;
				if1 = if2;
			}
			else if((opgroup==bstore) || (opgroup==wstore))
			{ 
				op1 = r1;
				op2 = r2;
			}
			else if((opgroup==ret) || (opgroup==icall) || (opgroup==dcall) || (opgroup==ijump))
			{
				op1 = r2;
				if1 = if2;
			}

			sstr = strtok(NULL, " ");
			if(*sstr == 'm')
			{
				sstr++;
				sstr[strlen(sstr)-1] = '\0';
				mem_addr = strToLInt(sstr);
			}
			else 
				mem_addr = 0;

			if(pc == pc_malloc_start)
			{
				//new allocation
				if(skip_alloc==0)
				{
					gzgets(*allocFile, astring, 128);
					//printf("%s", astring);
					astring[strlen(astring)-1] = '\0';
					asstr = strtok(astring, " ");
					asstr++;
					alloc_size = (unsigned int) strToLInt(asstr);
					gzgets(*allocFile, astring, 128);
					astring[strlen(astring)-1] = '\0';
					asstr = strtok(astring, " ");
					asstr++;
					alloc_ptr = (unsigned int) strToLInt(asstr);
					alloc_tag = create_new_tag_for_allocation();//create_new_segment(alloc_size, alloc_ptr);
					create_new_segment(alloc_size, alloc_ptr);
					//printf("alloc_size: %d\n", alloc_size);

					num_allocs++;
					while (alloc_size>0)
					{
						ht_insert(mem, alloc_ptr, alloc_tag);
						alloc_ptr+=4;
						alloc_size--;
					}
				}
				else
				{
					gzgets(*allocFile, astring, 128);
					gzgets(*allocFile, astring, 128);

					skip_alloc--;
				}	
			}
			else if(pc == pc_malloc_end)
			{
				if(num_allocs >= 1)
				{
					irf.tags[0] = alloc_tag;
					//printf("alloc_tag: %d (%d)\n", alloc_tag, irf.tags[0]);
				}
			} 
			else if(pc == pc_free_start)
			{
				//new deallocation
				gzgets(*allocFile, astring, 1000);
				astring[strlen(astring)-1] = '\0';
				asstr = strtok(astring, " ");
				asstr++;
				dealloc_ptr = (unsigned int) strToLInt(asstr);
				dealloc_size = free_mem_segment(&dealloc_ptr);

				if(dealloc_size == 0)
				{
//					printf("%x double free...exiting()\n", dealloc_ptr);
//					exit(0);
				}
				while(dealloc_size > 0)
				{
					ht_insert(mem, dealloc_ptr, BOTTOM);
					dealloc_ptr+=4;
					dealloc_size--;
				}
			}


			m[M_PC] = BOTTOM;//pctag;

//			if (ht_search(mem, pc, &tmpval) == -1)
//			{
//				//ht_insert(mem, pc, INSN_TAG);
//				//tmpval = INSN_TAG;
//				printf("%08x not found\n", pc);
//				exit(0);
//			}
			m[M_CI] = BOTTOM;//tmpval;

			m[M_OP] = opgroup;	

			if(if1 == 0)
				if(op1<32)
					m[M_OP1] = irf.tags[op1];
				else 
					m[M_OP1] = 0;
			else
				if(op1<32)
					m[M_OP1] = frf.tags[op1];
				else
					m[M_OP1] = 0;

			if(if2 == 0)
				if(op2 < 32)
					m[M_OP2] = irf.tags[op2];
				else
					m[M_OP2] = 0;
			else
				if(op2<32)
					m[M_OP2] = frf.tags[op2];
				else
					m[M_OP2] = 0;

			memcare = care & 0x1;
			if(memcare == 1)
			{
				if (ht_search(mem, mem_addr, &tmpval) == -1)
				{
					ht_insert(mem, mem_addr, BOTTOM);
					tmpval = BOTTOM;
				}
				m[M_MR] = tmpval;
			}
			else {
				m[M_MR] = BOTTOM;
			}


			unsigned int pump_input[6] = {m[M_PC], m[M_CI], m[M_OP], m[M_OP1], m[M_OP2], m[M_MR]};	//these are long tags
			//unsigned int i;
			mask_dc(pump_input, care);
			//printf("\t"); 
			//for(i=0;i<6;i++)
			//{
			//	printf("%d ", pump_input[i]);
			//}
			//printf("\n");

			digested_rule(pump_input, dfile);

			pump_out = pump_lookup(pump_input, care);	//pump_out is in long tags

			if(pump_out.found == 1)
			{
				if((rescare&1) == 1)
				{
					ht_insert(mem, mem_addr, pump_out.r[R_RES]);

				}
				if(((rescare&2)>>1) == 1)
				{
					if(if3 == 0)
					{
						irf.tags[op3] = pump_out.r[R_RES];
					}
					else
					{
						frf.tags[op3] = pump_out.r[R_RES];
					}
				}
				if(((rescare&4)>>2) == 1)
				{
					pctag = pump_out.r[R_PC];
				}
			}
			else {
				fprintf(l2misses_file, "%d\n", cycle);

				mvec_out(m, mvec_file);


				lm_memsafety(	  m
						, r
						);

				unsigned int pump_m[6] = {m[M_PC], m[M_CI], m[M_OP], m[M_OP1], m[M_OP2], m[M_MR]};
				unsigned int pump_r[2] = {r[R_PC], r[R_RES]};

				install_new_mr(pump_m, pump_r, care);

				//re-execute the instruction, this time with a hit!
				if((rescare&1) == 1)
				{
					ht_insert(mem, mem_addr, r[R_RES]);
				}
				if(((rescare&2)>>1) == 1)
				{
					if(if3 == 0)
						irf.tags[op3] = r[R_RES];
					else
					{
						frf.tags[op3] = r[R_RES];
					}
				}
				if(((rescare&4)>>2) == 1)
				{
					pctag = r[R_PC];
				}
			}
		}