Exemplo n.º 1
0
void guess_enc( int level, int timeg, int ticksg)
{
	int		p1[P1SIZE];
	int		p2[P2SIZE];
	int		p3[P3SIZE];
	int*	saved_ptr;

	unsigned int  dt;
	unsigned int  v;
	unsigned char b;
	unsigned int  saved_t;
	int			  i,k;

	unsigned int size = sizes[ level ];
	int lw = PROCESS_TIME(size) - DELTA(size) ;
	unsigned int low_t;
	unsigned int up_t = PROCESS_TIME(size) + DELTA(size) ;

	low_t = lw <= 0 ? 0 : lw;

	low_t = ROUND( low_t );
	up_t =  ROUND( up_t ) + 10;

	delta_history[level] = (up_t-low_t);

//	printf("low %d up %d \n",low_t, up_t);

	for(dt=low_t;dt<=up_t;dt+=10)
	{
		saved_t = t;
		save_buffers(p1,p2,p3);		
		saved_ptr = ptr;
		

		t = t+dt;
		randomize(timeg, ticksg);
		//memcpy( rand_data[level+2], ptr, 4*10 );

		dt_history[level] = dt;
		ticks_history[ level ] = (ticksg == NO_TICKS_GLITCH ? 0 : 1);
		time_history[ level ] = (timeg == NO_TIME_GLITCH ? 0 : 1);

		for(i=0;i<10;i++)
		{
			if( *ptr < 0)
				initialize_P2();
			if(i==0) v=*ptr;
			rand_data[level+2][i]=*ptr;
			ptr++;
		}

		_asm{ 
			sar v,16h
		}
		b = (v&0x000000FF) ^ k3;
		if( b == seeds[level+2] )
		{		
			stat[level+2]++;

			if( level == MAX_LEVEL - 3)
			{
				/* candidate succeded */
				printf("----\n");
				printf("Succes for (time + ticks) = %X and PID = %d\n", init_time + init_ticks, pid);
				
				printf("dt sequence:   ");
				for(i=0;i<(MAX_LEVEL-2);i++)
					printf("%02d ",dt_history[i]);
				printf("\n");
				
				printf("delta:         ");
				for(i=0;i<(MAX_LEVEL-2);i++)
					printf("%02d ",delta_history[i]);
				printf("\n");

				printf("ticks glitches:");
				for(i=0;i<(MAX_LEVEL-2);i++)
					printf("%02d ",ticks_history[i]);
				printf("\n");

				printf("time glitches: ");
				for(i=0;i<(MAX_LEVEL-2);i++)
					printf("%02d ",time_history[i]);
				printf("\n");

				printf("random data for file 1:");
				for(k=0;k<5;k++)
				{
					for(i=0;i<10;i++)
					{
						v = rand_data[k][i];
						_asm{ 
							sar v,16h
						}
						b=v&0x000000FF;
						rand_suc[k][i]=b;
					}
				}
				for(i=0;i<10;i++)
					printf("%02X ",rand_suc[0][i]);
				printf("\n");



				success++;

				return;
			}

			/* propagate or generate glitches */

			if( timeg == NO_TIME_GLITCH )
			{
				if( ticksg == NO_TICKS_GLITCH )
					guess_enc( level+1, NO_TIME_GLITCH, NO_TICKS_GLITCH );
				guess_enc( level+1, NO_TIME_GLITCH, TICKS_GLITCH );
			}
			if( ticksg == NO_TICKS_GLITCH )
				guess_enc( level+1, TIME_GLITCH, NO_TICKS_GLITCH );
			guess_enc( level+1, TIME_GLITCH, TICKS_GLITCH );
			
		
		}	
		
		restore_buffers(p1,p2,p3);
		t = saved_t;
		ptr = saved_ptr;
	}
	return;
}
Exemplo n.º 2
0
int
run_io(void)
{
	pid_t		pid;
	int		rv;
	unsigned int	i, max_reads, max_writes;
	char		*src, *dst;
	char		path_buf[MAXPATHLEN];
	FILE		*iofile, *resfile;
	off_t		io_spot;
	size_t		rand_buf;
	int		reached_max = 0;
	time_t		tm;

	/* statistics */
	off_t		total = 0, read_total = 0, write_total = 0;
	off_t		write_block = 0, read_block = 0;

	switch (pid = fork()) {
	case -1:
		err_log(LOGERR | LOGFATAL, "could not fork");
		/* NOTREACHED */
		break;
	case 0:
		break;
	default:
		return (pid);
		/* NOTREACHED */
		break;
	}

	/* child */
	if (signal(SIGTERM, sigterm) == SIG_ERR)
		err_log(LOGERR | LOGFATAL,
		    "could not install TERM handler in process %i",
		    getpid());

	if (signal(SIGHUP, sighup) == SIG_ERR)
		err_log(LOGERR | LOGFATAL,
		    "could not install HUP handler in process %i",
		    getpid());

	if (signal(SIGALRM, sigalarm) == SIG_ERR)
		err_log(LOGERR | LOGFATAL,
		    "could not install ALARM handler in process %i",
		    getpid());

	if (timeout != -1)
		if (signal(SIGALRM, sigtimeout) == SIG_ERR)
			err_log(LOGERR | LOGFATAL,
			    "could not install TIMEOUT handler in process %i",
			    getpid());

	/* poor mans memory test */
	src = malloc(io_size);
	if (!src)
		err_log(LOGERR | LOGFATAL, "malloc failed in process %i",
		    getpid());

	fill_buffer(src, io_size, pattern);

	dst = malloc(io_size);
	if (!dst)
		err_log(LOGERR | LOGFATAL, "malloc failed in process %i",
		    getpid());

	fill_buffer(dst, io_size, pattern);

	if (memcmp(src, dst, io_size) != 0)
		err_log(LOGFATAL,
		    "source and destination buffer not the same");

	if (realpath(target_dir, path_buf) == NULL)
		err_log(LOGERR | LOGFATAL, "invalid destination path %s",
		    target_dir);

	rv = snprintf(target_dir, sizeof target_dir, "%s/%s_%i.io",
	    path_buf, __progname, getpid());
	if (rv == -1 || rv > sizeof target_dir)
		err_log(LOGERR | LOGFATAL,
		    "destination path name invalid or too long");

	iofile = fopen(target_dir, "w+");
	if (!iofile)
		err_log(LOGERR | LOGFATAL, "could not create io file");

	if (realpath(result_dir, path_buf) == NULL)
		err_log(LOGERR | LOGFATAL,
		    "invalid result path %s", result_dir);

	rv = snprintf(result_dir, sizeof result_dir, "%s/%s_%i.res",
	    path_buf, __progname, getpid());
	if (rv == -1 || rv > sizeof result_dir)
		err_log(LOGERR | LOGFATAL,
		    "result path name invalid or too long");

	resfile = fopen(result_dir, "w+");
	if (!resfile)
		err_log(LOGERR | LOGFATAL, "could not create res file");

	for (i = 0; i < 10; i++) {
		write_block = fwrite(src, io_size, 1, iofile);
		if (write_block != 1)
			err_log(LOGERR | LOGFATAL,
			    "could not write initial file data in %i",
			    getpid());

		total += write_block * io_size;
		write_total = total;
	}

	alarm(interval);

	max_reads = read_perc / 5;
	max_writes = (100 - read_perc) / 5;
	while (run) {
		if (print_stats) {
			print_stats = 0;
			time(&tm);
			fprintf(stderr,
			    "%.15s: total: %llu read: %llu write: %llu\n",
			    ctime(&tm) + 4, total, read_total , write_total);
			fflush(stderr);
		}

		if (update_res) {
			update_res = 0;
			time(&tm);
			fprintf(resfile,
			    "%.15s: total: %llu read: %llu write: %llu\n",
			    ctime(&tm) + 4, total, read_total , write_total);
			fflush(resfile);
			alarm(interval);
		}

		if (timeout != -1)
			alarm(timeout);

		/* reads */
		for (i = 0; i < max_reads; i++) {
			io_spot = get_file_size(target_dir) / io_size - 1;
			io_spot = (arc4random() % io_spot + 1) * io_size;
			rand_buf = 0;
			if (randomize)
				rand_buf = arc4random() % io_size;
			
			fseeko(iofile, io_spot, SEEK_SET);
			read_block = fread(dst, io_size - rand_buf, 1, iofile);
			if (read_block) {
				total += io_size - rand_buf;
				read_total += io_size - rand_buf;
			}	
			else
				err_log(LOGFATAL | LOGERR | LOGKILLALL,
				    "could not read from file in process %i",
				    getpid());

			if (!randomize)
				if (memcmp(src, dst, io_size) != 0) {
					save_buffers(src, dst, io_size);
					err_log(LOGFATAL | LOGKILLALL,
					    "data corruption in process %i;"
					    "buffers saved to /tmp",
					    getpid());
					}
		}

		/* writes */
		for (i = 0; i < max_writes; i++) {
			if (!reached_max)
				fseeko(iofile, 0, SEEK_END);
			else {
				io_spot = get_file_size(target_dir) /
				    io_size - 1;
				io_spot = (arc4random() % io_spot + 1) *
				    io_size;
				fseeko(iofile, io_spot, SEEK_SET);
			}

			rand_buf = 0;
			if (randomize)
				rand_buf = arc4random() % io_size;

			write_block = fwrite(src, io_size - rand_buf,
			    1, iofile);
			if (write_block) {
				total += io_size - rand_buf;
				write_total += io_size - rand_buf;
			}
			else
				err_log(LOGFATAL | LOGERR | LOGKILLALL,
				    "could not write to file in process %i",
				    getpid());
		}
		if (write_total >= file_size) {
			if (reached_max != 1)
				err_log(0, "file reached maximum size in "
				    "process %i\n", getpid());
			reached_max = 1;
		}
	}

	err_log(0, "%i exiting with total: %llu read: %llu write: %llu",
	    getpid(), total, read_total , write_total);

	free(src);
	free(dst);

	fclose(iofile);
	fclose(resfile);

	exit(0);

	/* NOTREACHED */
	return (0);
}
Exemplo n.º 3
0
void recover()
{
	int		p1[P1SIZE];
	int		p2[P2SIZE];
	int		p3[P3SIZE];

	int		q1[P1SIZE];
	int		q2[P2SIZE];
	int		q3[P3SIZE];

	unsigned char	b;
	unsigned int	v;
	unsigned int	i;
	unsigned int	highbits;

	unsigned int	s;
	unsigned int	oldn, n;

	int				done;

	t = 0;
	save_buffers(q1,q2,q3);	

	{
		/* fill bit 32-29 of the seed */
	//	seed = (highbits << 28) | (seed & 0x0FFFFFFF);

		oldn=0;
		restore_buffers(q1,q2,q3);	

		/**
		 SEEDS:
		 - time + ticks is a 27-bit to 32-bit seed  
		 - PID is a 10-bit seed ; pid = n*4 with n in  {0,1024}
		**/

		for(init_ticks= seed  & 0xFFFFF003; init_ticks< (seed&0xFFFFF003)+0x1000; init_ticks+=4)
		{
			pid = seed ^ init_ticks;

			stat[0]++;

			/**
			 ASSUMPTIONS
			 - we are still in the same 10ms frame
			 - no glitch occured 
			**/

			ptr = buff + 10;
			s = init_ticks % 0x2710;
			if( s < (0x64-10) )
			{
				ptr = ptr + s + 1;
			}
			else
			{
				s -= (0x64-10);
				n = (s/0x64)+1;
				
				if( n > oldn )
				{
					for(i=oldn;i<n;i++)
						initialize_P2();
					oldn = n;
				}

				ptr = buff + (s%0x64) + 1;
			}

			/**
			 OPTIMIZATION
			 - initialize_P1 has no effect on buff
			 - if *ptr != -1, delay initialize_P1 after the filter
			 - if the byte doesn't pass the filter, we spare initialize_P1
			 - if *ptr == -1 ; initialize_P2 must take place before initialize_P2
			**/
#define OPTIM
			
			
			
#ifdef OPTIM
			done = false;
			
			if( *ptr < 0)
			{
				save_buffers(p1,p2,p3);
				initialize_P1( (pid ^ *(ptr-1) ) & 0x0FFFFFFF );
				initialize_P2();
				memcpy( rand_data[1], ptr, 4*10 );
				v = *ptr;
				ptr+=10;

				done = true;
			}
			else 
				v = *ptr;
#else
			save_buffers(p1,p2,p3);
			done=true;
			initialize_P1( (pid ^ *(ptr-1) ) & 0x0FFFFFFF );
			
			for(i=0;i<10;i++)
			{
				if( *ptr < 0)
					initialize_P2();
				if(i==0) v=*ptr;
				ptr++;
			}	
#endif
			_asm{
				sar v,16h
			}
			b = (v&0x000000FF) ^ k3;

			if( (b == seeds[1]) )
			{	
			
				stat[1]++;
#ifdef OPTIM		
				if( done==false)
				{
					save_buffers(p1,p2,p3);
					initialize_P1( (pid ^ *(ptr-1) ) & 0x0FFFFFFF );
					

					for(i=0;i<10;i++)
					{
						if( *ptr < 0)
							initialize_P2();
						rand_data[1][i]=*ptr;
						ptr++;
					}
					//memcpy( rand_data[1], ptr, 4*10 );
					done=true;
				}
#endif
				guess_enc( 0, NO_TIME_GLITCH,	NO_TICKS_GLITCH );
				guess_enc( 0, NO_TIME_GLITCH,	TICKS_GLITCH );
				guess_enc( 0, TIME_GLITCH,		NO_TICKS_GLITCH );
				guess_enc( 0, TIME_GLITCH,		TICKS_GLITCH );

				
			}
			if(done==true) restore_buffers(p1,p2,p3);
		}
	}
}