Exemplo n.º 1
0
void iostream::open_stream( void )
{	
	abytewriter* strwrt;
	unsigned char* buffer;
	int i;
	
	if ( mode == 0 ) {
		// read whole stream into memory buffer
		strwrt = new abytewriter( 0 );
		buffer = ( unsigned char* ) calloc( BUFFER_SIZE, sizeof( char ) );
		if ( buffer != NULL ) {
			while ( ( i = fread( buffer, sizeof( char ), BUFFER_SIZE, stdin ) ) > 0 )
				strwrt->write_n( buffer, i );
		}
		if ( strwrt->error ) {
			source = NULL;
			srcs   = 0;
		}
		else {
			source = strwrt->getptr();
			srcs   = strwrt->getpos();
		}
		delete ( strwrt );
		free( buffer );
		// free memory after done
		free_mem_sw = true;
	}
	
	// for writing: simply open new stream in mem writer
	// writing to stream will be done later
	open_mem();
}
Exemplo n.º 2
0
int main () {

   open_mem();
   reset_mem();

   // write data at the beginning of the file, ascii A,B,C,D
   write32(0,0x41424344);
   uint32_t data32 = read32(0);
   printf("@32 bit read - data: %08x\n",data32);

   // write data at the second place in the file, ascii H,I,J,K
   write32(1,0x48494a4b);
   data32 = read32(1);
   printf("@32 bit read - data: %08x\n",data32);

   write32(BLOCK_INST_BASE+CH_0,0x7778797a); // ascii w,x,y,z
   data32 = read32(BLOCK_INST_BASE+CH_0);
   printf("@32 bit read - data: %08x\n",data32);

   // write data to the last position, ascii h,j,k,l 
   write32(BLOCK_INST_BASE+CH_4,0x68696a6b);
   data32 = read32(BLOCK_INST_BASE+CH_4);
   printf("@32 bit read - data: %08x\n",data32);

   close_mem();

   return(0);
}
Exemplo n.º 3
0
iostream::iostream( void* src, int srctype, int srcsize, int iomode )
{
	// locally copy source, source type # and io mode #
	source = src;
	srct   = srctype;
	srcs   = srcsize;
	mode   = iomode;
	
	// don't free memory when reading - this will be useful if switching occurs
	free_mem_sw = false;
	
	// set binary mode for streams
	#if defined( _WIN32 )				
		setmode( fileno( stdin ), O_BINARY );
		setmode( fileno( stdout ), O_BINARY );
	#endif
	
	// open file/mem/stream
	switch ( srct )
	{
		case 0:
			open_file();
			break;
		
		case 1:
			open_mem();
			break;
		
		case 2:
			open_stream();
			break;
		
		default:			
			break;
	}
}
Exemplo n.º 4
0
Arquivo: os.c Projeto: sstefani/mtrace
void *mem_scan(struct task *task, struct mt_msg *cmd, void *payload, unsigned long *data_len)
{
	struct mt_scan_payload *mt_scan = payload;
	unsigned long mask = (unsigned long)mt_scan->mask;
	uint32_t ptr_size = mt_scan->ptr_size;
	void *blocks = mt_scan->data;
	unsigned long n = (cmd->payload_len - (blocks - payload)) / ptr_size;
	unsigned long map;
	struct map *maps;
	int h;
	unsigned long (*get_val)(void *data, unsigned long index);
	unsigned long start;
	unsigned long end;

	if (unlikely(options.verbose))
		fprintf(stderr, "+++ scan for memory leaks...\n");

	if (!n)
		return NULL;

	if (ptr_size == sizeof(uint32_t))
		get_val = get_val32;
	else
		get_val = get_val64;

	h = open_mem(task->pid);
	if (h == -1)
		return NULL;

	maps = get_writeable_mappings(task);

	for(map = 0; (start = maps[map].start) && (end = maps[map].end); ++map) {
		int do_peek = 0;

		while(start < end) {
			unsigned long i;
			char page_buf[PAGE_SIZE];

			if (!do_peek) {
				if (lseek(h, start, SEEK_SET) != (off_t)start || read(h, page_buf, sizeof(page_buf)) == -1)
					do_peek = 1;
			}

			if (do_peek) {
				if (copy_from_proc(task, ARCH_ADDR_T(start), page_buf, sizeof(page_buf)) != (int)sizeof(page_buf)) {
					fprintf(stderr, "ptrace (%s)\n", strerror(errno));
					break;
				}
			}

			for(i = 0; i < sizeof(page_buf) / ptr_size; ++i) {
				unsigned long found, addr;

				addr = get_val(page_buf, i);

				if (addr & mask)
					continue;

				found = find_block(get_val, blocks, n, addr);
				if (found != n) {
					if (!--n)
						goto finish;

					if (found != n)
						memmove(blocks + found * ptr_size, blocks + (found + 1) * ptr_size, (n - found) * ptr_size);
				}
			}

			start += sizeof(page_buf);
		}
	}

finish:
	close(h);

	*data_len = n * ptr_size;

	free(maps);

	return blocks;
}
Exemplo n.º 5
0
static void *mem_scan(mt_msg *cmd, void *payload, unsigned long *data_len)
{
	mt_scan_payload *mt_scan = payload;
	unsigned long mask = (unsigned long)mt_scan->mask;
	uint32_t ptr_size = mt_scan->ptr_size;
	void *blocks = mt_scan->data;
	unsigned long n = (cmd->payload_len - (blocks - payload)) / ptr_size;
	unsigned long map;
	struct map *maps;
	int h;
	unsigned long (*get_val)(void *data, unsigned long index);
	unsigned long start;
	unsigned long end;

	if (!n)
		return NULL;

	if (ptr_size == sizeof(uint32_t))
		get_val = get_val32;
	else
		get_val = get_val64;

	h = open_mem(cmd->pid);
	if (h == -1)
		return NULL;

	maps = get_writeable_mappings(cmd->pid);

	for(map = 0; (start = maps[map].start) && (end = maps[map].end); ++map) {
		int do_peek = 0;

		while(start < end) {
			unsigned long i;
			char page_buf[PAGE_SIZE];

			if (!do_peek) {
				if (lseek(h, start, SEEK_SET) != (off_t)start || read(h, page_buf, sizeof(page_buf)) == -1)
					do_peek = 1;
			}

			if (do_peek) {
				errno = 0;

				for(i = 0; i < sizeof(page_buf); i += sizeof(long)) {
					long val;

					val = ptrace(PTRACE_PEEKDATA, cmd->pid, start + i, 0);
					if (val == -1 && errno) {
						fprintf(stderr, "%s:%d ptrace PEEKDATA (%s)\n", __FUNCTION__, __LINE__, strerror(errno));
						break;
					}

					*(long *)&page_buf[i] = val;
				}
				if (i < sizeof(page_buf))
					break;
			}

			for(i = 0; i < sizeof(page_buf) / ptr_size; ++i) {
				unsigned long found, addr;

				addr = get_val(page_buf, i);

				if (addr & mask)
					continue;

				found = find_block(get_val, blocks, n, addr);
				if (found != n) {
					if (!--n)
						goto finish;

					if (found != n)
						memmove(blocks + found * ptr_size, blocks + (found + 1) * ptr_size, (n - found) * ptr_size);
				}
			}

			start += sizeof(page_buf);
		}
	}

finish:
	close(h);

	*data_len = n * ptr_size;

	free(maps);

	return blocks;
}