Пример #1
0
void tenkListChunks(PVOID heapHandle) {
	struct HPool			*heap;
	struct DestroyStruct	dStruct;
	struct HeapChunk		*curChunk;
	ULONG					i;

	heap = getHeap(&heapModel, heapHandle);
	dprintf("[T] Currently tracking %d chunks for heap 0x%08x\n", 
			heap->numChunks, heap->base);
	
	i = heap->inUseHead;
	while (i != NULLNODE) {
		if (CHUNK(i).inUse) {
			dprintf("\tAddress: 0x%08x\tSize: 0x%08x", CHUNK(i).addr, CHUNK(i).size);
			dprintf("\tFlags: 0x%08x\t%s\n\n", CHUNK(i).flags, 
					(CHUNK(i).free)?"FREE'D":"IN USE");
		}
		i = CHUNK(i).nextInUse;
	}

	if (heap->numChunks == 0) {
		dStruct.heapHandle	= heap->base;
		heapDestroy(&heapModel, &dStruct);
	}
}
Пример #2
0
// Usage: ./client <fileIn> <fileOut> <fileOut2> <heapSize> <rowCount> <rowLength>
int main (int argc, char* argv[]) {

	char* nameFileIn = argv[1];
	char* nameFileOut = argv[2];
	char* nameFileOut2 = argv[3];
	char** page;
	FILE* fpI,* fpO;
	int nr;
	heap_t* heap;
	strArray_t* save;

	int D = atoi(argv[4]), N = atoi(argv[5]), M = atoi(argv[6]);

	printf("input file: %s\noutput file: %s\noutput file 2: %s\n",
			nameFileIn, nameFileOut, nameFileOut2);
	printf("D: %d - N: %d - M: %d\n", D, N, M);

	heap = heapCreate(D);

	save = strArrayInit(heap, 0); /* inizializza a vuoto */

	page = (char**)heapAlloc(heap, N * sizeof(char*));
	fpI = fopen(nameFileIn, "r");
	fpO = fopen(nameFileOut, "w");
	if (page == NULL || fpI == NULL || fpO == NULL) {
		printf("Errore in allocazione memoria o apertura file\n");
		exit(1);
	}

	while ((nr = leggiPagina(heap, page, fpI, N)) > 0) {
		ordinaPagina(page, nr);
		scriviPagina(page, fpO, nr);
		filtraeLiberaPagina(heap, page, nr, save, M);
	} 
	
	fclose(fpI);
	fclose(fpO);
	heapFree(heap, page);

	fpO = fopen(nameFileOut2, "w");
	if (fpO == NULL) {
		printf("Errore in apertura file\n");
		exit(1);
	}

	strArraySort(save);
	strArrayPrint(save, fpO);
	fclose(fpO);

	strArrayFree(heap, save);

	heapDestroy(heap);

	return 0;

}
Пример #3
0
DWORD WINAPI tenkBackChannel(LPVOID lpvParam) {
	HANDLE		hPipe;
	TCHAR		buf[BUFSIZE+1];
	ULONG		bytesRead, bytesWritten;
	BOOL		fSuccess;
	ULONG64		funcAddr64;
	ULONG		*funcAddr, i;
	

	struct AllocateStruct	*aStruct = (struct AllocateStruct *)buf;
	struct ReallocateStruct	*rStruct = (struct ReallocateStruct *)buf;
	struct FreeStruct		*fStruct = (struct FreeStruct *)buf;
	struct CreateStruct		*cStruct = (struct CreateStruct *)buf;
	struct DestroyStruct	*dStruct = (struct DestroyStruct *)buf;
	struct CoalesceStruct	*cfbStruct = (struct CoalesceStruct *)buf;

	
	hPipe = (HANDLE) lpvParam;
	
	dprintf("[Byakugan] Waiting for named pipe connection...\n");

	for(;!ConnectNamedPipe(hPipe, NULL) == TRUE;) { dprintf("[B] waiting for connection...\n"); }

	dprintf("[Byakugan] Connected to back channel. :)\n");

	// Load addresses from symbols if possible for undoumented interfaces
	i = 0;
	while (undocdFunc[i] != NULL) {
		dprintf("[T] Sending address of %s\n", undocdFunc[i]);
		fSuccess = WriteFile(hPipe, &(undocdAddr[i]), sizeof(ULONG), &bytesWritten, NULL);
		if (!fSuccess || bytesWritten != sizeof(ULONG))
			dprintf("[T] Failed to send address of %s\n", undocdFunc[i]);
		i++;
	}
	//FlushFileBuffers(hPipe);
	dprintf("[T] Sent addresses of %d undocumented functions.\n", i);

	initializeHeapModel(&heapModel);

#undef THREEDHEAPFU_ENABLED //Place this in setup.bat

#if 0 //#ifdef THREEDHEAPFU_ENABLED
//Create a heap event proxy, play these back out to 3dheapfu
LPTSTR	lpszProxyPipename = TEXT("\\\\.\\pipe\\tenketsuProxy");
BOOL	fProxySuccess;
DWORD	dwProxyMode	= PIPE_READMODE_MESSAGE;
ULONG   bytesProxyWritten;
static BOOL	fDontAttemptProxyWrite = false;

HANDLE hProxyPipe = CreateFile(lpszProxyPipename,
								GENERIC_READ | GENERIC_WRITE,
								0,
								NULL,
								OPEN_EXISTING,
								0,
								NULL);

			if (hProxyPipe == INVALID_HANDLE_VALUE)
					dprintf("hProxyPipe == invalid handle\n");
			else
					dprintf("hProxyPipe == good\n");
			SetNamedPipeHandleState(hProxyPipe, &dwProxyMode, NULL, NULL); //?

#endif

	while (1) {
		fSuccess = ReadFile(	hPipe,
								buf,
								BUFSIZE*sizeof(TCHAR),
								&bytesRead,
								NULL);
		if (!fSuccess || bytesRead == 0) {
			dprintf("[Byakugan] ReadFile failed, or read 0 bytes.\n");
			continue;
		}
#if 0 //#ifdef THREEDHEAPFU_ENABLED
		//dprintf("jc: receieved an event of size %d. Forwarding on to ProxyPipe\n", bytesRead);
		//WriteFile(hPipe, &freeinfo, sizeof(struct FreeStruct), &bytesWritten, NULL);

		if (!fDontAttemptProxyWrite)
		{
			fProxySuccess = WriteFile(hProxyPipe, buf, bytesRead, &bytesProxyWritten, NULL); 
			if (bytesRead != bytesProxyWritten)
			{
				dprintf("Partial write to proxy on last event! ;(\n");
				dprintf("event size was %d. wrote %d\n", bytesRead, bytesProxyWritten);
				dprintf("Disabling message proxying until explicitly enabled.\n");
				fDontAttemptProxyWrite = true;
			}
		}

#endif
		switch ( *((BYTE *) buf) ) {
			case ALLOCATESTRUCT:
				//dprintf("[T] New Chunk @ 0x%08x\n", aStruct->ret);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\tSize: 0x%08x\n\n", 
				//		aStruct->heapHandle, aStruct->flags, aStruct->size);
				if (heapModel.state & MODEL) heapAllocate(&heapModel, aStruct);
				if (heapModel.state & LOG) logAllocate(&heapModel, aStruct);
				break;

			case REALLOCATESTRUCT:
				//dprintf("[T] Realloc'd Chunk @ 0x%08x\n", rStruct->ret);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\tSize: 0x%08x\n", 
				//		rStruct->heapHandle, rStruct->flags, rStruct->size);
				//if (rStruct->ret !=  (PVOID) rStruct->memoryPointer)
				//	dprintf("Replaces chunk @ 0x%08x\n", rStruct->memoryPointer);
				//dprintf("\n");
				if (heapModel.state & MODEL) heapReallocate(&heapModel, rStruct);
				if (heapModel.state & LOG) logReallocate(&heapModel, rStruct);
				break;

			case FREESTRUCT:
				//dprintf("[T] Free'd Chunk @ 0x%08x\n", fStruct->memoryPointer);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\n\n", fStruct->heapHandle, fStruct->flags);
				if (heapModel.state & MODEL) heapFree(&heapModel, fStruct);
				if (heapModel.state & LOG) logFree(&heapModel, fStruct);
				break;

			case CREATESTRUCT:
				dprintf("[T] New Heap: 0x%08x\n", cStruct->ret);
				dprintf("Base: 0x%08x\tReserve: 0x%08x\tFlags: 0x%08x\n",
						cStruct->base, cStruct->reserve, cStruct->flags);
				//dprintf("Commit: 0x%08x\tLock: 0x%08x\n\n", cStruct->commit, cStruct->lock);
				if (heapModel.state & MODEL) heapCreate(&heapModel, cStruct);
				break;

			case DESTROYSTRUCT:
				dprintf("[T] Heap Destroyed: 0x%08x\n\n", dStruct->heapHandle);
				if (heapModel.state & MODEL) heapDestroy(&heapModel, dStruct);
				break;
			
			case COALESCESTRUCT:
				//dprintf("[T] Free Block Consolidation (returned 0x%08x)\n", cfbStruct->ret);
				//dprintf("Heap: 0x%08x\tArg2: 0x%08x\tArg3: 0x%08x\tArg4: 0x%08x\n\n",
				//		cfbStruct->heapHandle, cfbStruct->arg2, cfbStruct->arg3, cfbStruct->arg4);
				if (heapModel.state & MODEL) heapCoalesce(&heapModel, cfbStruct);
				break;

			default:
				dprintf("[Byakugan] Tenketsu: Unrecognized data was returned.\n");
		}

	}


	return (0);
}
Пример #4
0
int main() {
    
    Heap* hh = heapNew( 1 );
    
    heapInsert( hh, "first", 1 );
    
    long pp;
    char* itemout = heapPop( hh, &pp );
    
    printf( "%s\n", itemout );
    printf( "size:%d\n", hh->size );
    
    heapInsert( hh, "one", 1 );
    heapInsert( hh, "ten", 10 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    heapInsert( hh, "ten", 10 );
    heapInsert( hh, "one", 1 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    heapInsert( hh, "384", 384 );
    heapInsert( hh, "887", 887 );
    heapInsert( hh, "778", 778 );

    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
    }
    
    int i;
    for(i=0; i<1000; i++) {
        char* payload = (char*)malloc(200*sizeof(char));
        long priority = rand()%1000+1;
        sprintf(payload, "%ld", priority);
        
        printf( "inserting '%s' with priority %ld\n", payload, priority );
        
        heapInsert( hh, payload, priority );
    }
    
    while( !heapEmpty( hh ) ) {
        itemout = heapPop( hh, &pp );
        printf( "%s\n", itemout );
        printf( "size:%d\n", hh->size );
        free(itemout);
    }
    
    heapDestroy( hh );
    
    return 1;
}