void JIT_CalculateLocalLayout(IRMethod* pMethod) { if (!pMethod->LocalsLayedOut) { IRLocalVariable* local = NULL; // Accounts for current IRMethod* in locals space uint32_t offset = 0; Log_WriteLine(LOGLEVEL__JIT_Layout, "Laying Out Locals of %s.%s.%s", pMethod->MethodDefinition->TypeDefinition->Namespace, pMethod->MethodDefinition->TypeDefinition->Name, pMethod->MethodDefinition->Name); for (uint32_t index = 0; index < pMethod->LocalVariableCount; ++index) { local = pMethod->LocalVariables[index]; //if (!local->VariableType) //{ // printf("Ignoring %u\n", (unsigned int)index); // continue; //} local->Size = JIT_GetStackSizeOfType(local->VariableType); offset += JIT_StackAlign(local->Size); local->Offset = offset; Log_WriteLine(LOGLEVEL__JIT_Layout, "Layout Local %u @ 0x%x, Size: 0x%x, Aligned: 0x%x, VarType: 0x%x, VarTypeName: %s", (unsigned int)index, (unsigned int)local->Offset, (unsigned int)local->Size, (unsigned int)JIT_StackAlign(local->Size), (unsigned int)local->VariableType, local->VariableType->TypeDefinition->Name); } pMethod->LocalsSize = offset; pMethod->LocalsLayedOut = TRUE; } }
IRType* IRType_Copy(IRType* pType) { IRType* type = (IRType*)calloc(1, sizeof(IRType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRType_Copy @ 0x%x, of 0x%x", (unsigned int)type, (unsigned int)pType); *type = *pType; return type; }
void IRAssembly_Destroy(IRAssembly* pAssembly) { Log_WriteLine(LOGLEVEL__Memory, "Memory: IRAssembly_Destroy @ 0x%x", (unsigned int)pAssembly); //AppDomain_RemoveAssembly(pAssembly); for (uint32_t index = 0; index < pAssembly->TypeCount; ++index) { if (pAssembly->Types[index]) { IRType_Destroy(pAssembly->Types[index]); } } for (uint32_t index = 0; index < pAssembly->FieldCount; ++index) { if (pAssembly->Fields[index]) { IRField_Destroy(pAssembly->Fields[index]); } } for (uint32_t index = 0; index < pAssembly->MethodCount; ++index) { if (pAssembly->Methods[index]) { IRMethod_Destroy(pAssembly->Methods[index]); } } free(pAssembly->StaticFields); free(pAssembly->Types); free(pAssembly->Fields); free(pAssembly->Methods); free(pAssembly); }
IRLocalVariable* IRLocalVariable_Create(IRMethod* pMethod, IRType* pType) { IRLocalVariable* localVariable = (IRLocalVariable*)calloc(1, sizeof(IRLocalVariable)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRLocalVariable_Create @ 0x%x", (unsigned int)localVariable); localVariable->VariableType = pType; return localVariable; }
IRLocalSSAData* IRLocalSSAData_Copy(IRLocalSSAData* pLocalSSAData) { IRLocalSSAData* data = (IRLocalSSAData*)calloc(1, sizeof(IRLocalSSAData)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRLocalSSAData_Copy @ 0x%x", (unsigned int)data); *data = *pLocalSSAData; return data; }
IRInstruction* IRInstruction_Copy(IRInstruction* pInstruction) { IRInstruction* instruction = (IRInstruction*)calloc(1, sizeof(IRInstruction)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRInstruction_Copy @ 0x%x", (unsigned int)instruction); *instruction = *pInstruction; return instruction; }
IRLocalSSAData* IRLocalSSAData_Create(IRLocalVariable* pDerivedLocalVariable) { IRLocalSSAData* data = (IRLocalSSAData*)calloc(1, sizeof(IRLocalSSAData)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRLocalSSAData_Create @ 0x%x", (unsigned int)data); data->Derived = pDerivedLocalVariable; return data; }
IRParameter* IRParameter_Copy(IRParameter* pParameter) { IRParameter* parameter = (IRParameter*)calloc(1, sizeof(IRParameter)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRParameter_Copy @ 0x%x", (unsigned int)parameter); *parameter = *pParameter; return parameter; }
IRParameter* IRParameter_Create(IRMethod* pMethod, IRType* pType) { IRParameter* parameter = (IRParameter*)calloc(1, sizeof(IRParameter)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRParameter_Create @ 0x%x", (unsigned int)parameter); parameter->Type = pType; return parameter; }
IRField* IRField_Copy(IRField* pField) { IRField* field = (IRField*)calloc(1, sizeof(IRField)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRField_Copy @ 0x%x, of 0x%x", (unsigned int)field, (unsigned int)pField); *field = *pField; return field; }
IRArrayType* IRArrayType_Create(IRType* pArrayType, IRType* pElementType) { IRArrayType* type = (IRArrayType*)calloc(1, sizeof(IRArrayType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRArrayType_Create @ 0x%x", (unsigned int)type); type->ArrayType = pArrayType; type->ElementType = pElementType; return type; }
IRInstruction* IRInstruction_Create(uint32_t pILLocation, IROpcode pOpcode) { IRInstruction* instruction = (IRInstruction*)calloc(1, sizeof(IRInstruction)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRInstruction_Create @ 0x%x", (unsigned int)instruction); instruction->ILLocation = pILLocation; instruction->Opcode = pOpcode; return instruction; }
IRPointerType* IRPointerType_Create(IRType* pPointerType, IRType* pTypePointedTo) { IRPointerType* type = (IRPointerType*)calloc(1, sizeof(IRPointerType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRPointerType_Create @ 0x%x", (unsigned int)type); type->PointerType = pPointerType; type->TypePointedTo = pTypePointedTo; return type; }
IRGenericType* IRGenericType_Create(IRType* pGenericType, IRType* pImplementationType) { IRGenericType* type = (IRGenericType*)calloc(1, sizeof(IRGenericType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRGenericType_Create @ 0x%x", (unsigned int)type); type->GenericType = pGenericType; type->ImplementationType = pImplementationType; return type; }
void Thread_Destroy(Thread* pThread) { ThreadScheduler_Remove(pThread); if (pThread->Domain) AppDomain_RemoveThread(pThread->Domain, pThread); Process_RemoveThread(pThread->Process, pThread); Log_WriteLine(LOGLEVEL__Memory, "Memory: Thread_Destroy @ 0x%x", (unsigned int)pThread); free(pThread->Stack); free(pThread); }
void JIT_CalculateFieldLayout(IRType* pType) { if (!pType->FieldsLayedOut) { IRField* field = NULL; uint32_t offset = 0; Log_WriteLine(LOGLEVEL__JIT_Layout, "Laying Out Fields of %s.%s", pType->TypeDefinition->Namespace, pType->TypeDefinition->Name); for (uint32_t index = 0; index < pType->FieldCount; ++index) { field = pType->Fields[index]; field->Size = JIT_GetStackSizeOfType(field->FieldType); field->Offset = offset; offset += field->Size; Log_WriteLine(LOGLEVEL__JIT_Layout, "Layout Field %u @ 0x%x, Size: 0x%x", (unsigned int)index, (unsigned int)field->Offset, (unsigned int)field->Size); } pType->FieldsLayedOut = TRUE; } }
void IRLocalVariable_Destroy(IRLocalVariable* pLocalVariable) { Log_WriteLine(LOGLEVEL__Memory, "Memory: IRLocalVariable_Destroy @ 0x%x", (unsigned int)pLocalVariable); if (pLocalVariable->SSAData) { IRLocalSSAData_Destroy(pLocalVariable->SSAData); } free(pLocalVariable); }
IRGenericMethod* IRGenericMethod_Create(IRType* pParentType, IRMethod* pGenericMethod, IRMethod* pImplementationMethod) { IRGenericMethod* method = (IRGenericMethod*)calloc(1, sizeof(IRGenericMethod)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRGenericMethod_Create @ 0x%x", (unsigned int)method); method->ParentType = pParentType; method->GenericMethod = pGenericMethod; method->ImplementationMethod = pImplementationMethod; return method; }
void JIT_CalculateStaticFieldLayout(IRAssembly* pAssembly) { IRField* field = NULL; uint32_t offset = 0; uint32_t totalSize = 0; Log_WriteLine(LOGLEVEL__JIT_Layout, "Laying Out Static Fields of %s", pAssembly->ParentFile->Filename); for (uint32_t index = 0; index < pAssembly->StaticFieldCount; ++index) { field = pAssembly->StaticFields[index]; //printf("Laying out static field: %s.%s.%s, %s %i\n", field->FieldType->GenericType->Parameters[0]->TypeDefinition->Namespace, field->FieldType->GenericType->Parameters[0]->TypeDefinition->Name, field->FieldDefinition->Name, field->FieldType->TypeDefinition->Name, (int)field->FieldType->IsGenericInstantiation); field->Size = JIT_GetStackSizeOfType(field->FieldType); field->Offset = offset; offset += field->Size; totalSize += field->Size; Log_WriteLine(LOGLEVEL__JIT_Layout, "Layout Static Field %u @ 0x%x, Size: 0x%x", (unsigned int)index, (unsigned int)field->Offset, (unsigned int)field->Size); } pAssembly->ParentDomain->StaticValues[pAssembly->AssemblyIndex] = realloc(pAssembly->ParentDomain->StaticValues[pAssembly->AssemblyIndex], totalSize); pAssembly->ParentDomain->StaticConstructorsCalled[pAssembly->AssemblyIndex] = (bool_t*)calloc(1, sizeof(bool_t) * pAssembly->TypeCount); }
IRType* IRAssembly_CreateGenericParameter(IRAssembly* pAssembly, bool_t pIsFromParentType, uint32_t pIndex) { IRType* type = (IRType*)calloc(1, sizeof(IRType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: Generic Parameter @ 0x%x", (unsigned int)type); type->IsGeneric = TRUE; type->IsGenericParameter = TRUE; type->IsGenericParameterFromParentType = pIsFromParentType; type->GenericParameterIndex = pIndex; //printf("IRAssembly_CreateGenericParameter Type->TypeDefinition = 0x%X\n", (unsigned int)type->TypeDefinition); return type; }
IRLocalVariable* IRLocalVariable_Copy(IRLocalVariable* pLocalVariable) { IRLocalVariable* localVariable = (IRLocalVariable*)calloc(1, sizeof(IRLocalVariable)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRLocalVariable_Copy @ 0x%x", (unsigned int)localVariable); *localVariable = *pLocalVariable; if (pLocalVariable->SSAData) { localVariable->SSAData = IRLocalSSAData_Copy(pLocalVariable->SSAData); //printf("Copied SSAData, 0x%X\n", (unsigned int)localVariable->SSAData); } return localVariable; }
void IRType_Destroy(IRType* pType) { Log_WriteLine(LOGLEVEL__Memory, "Memory: IRType_Destroy @ 0x%x", (unsigned int)pType); // TODO: Deal with copies not freeing their fields/methods, or having their own deep copies if (!pType->IsArrayType && !pType->IsPointerType) { //if (pType->NestedTypes) free(pType->NestedTypes); free(pType->Fields); free(pType->Methods); } if (pType->GenericType) free(pType->GenericType); free(pType); }
void JIT_CalculateParameterLayout(IRMethod* pMethod) { if (!pMethod->ParametersLayedOut) { IRParameter* parameter = NULL; uint32_t offset = 2 * gSizeOfPointerInBytes; Log_WriteLine(LOGLEVEL__JIT_Layout, "Laying Out Parameters of %s.%s.%s", pMethod->MethodDefinition->TypeDefinition->Namespace, pMethod->MethodDefinition->TypeDefinition->Name, pMethod->MethodDefinition->Name); /*if (pMethod->MethodDefinition->SignatureCache->HasThis && !pMethod->MethodDefinition->SignatureCache->ExplicitThis) { printf("It has a this param.\n"); }*/ for (uint32_t index = 0; index < pMethod->ParameterCount; ++index) { parameter = pMethod->Parameters[index]; parameter->Offset = offset; parameter->Size = JIT_GetStackSizeOfType(parameter->Type); offset += JIT_StackAlign(parameter->Size); Log_WriteLine(LOGLEVEL__JIT_Layout, "Layout Parameter %u @ 0x%x, Size: 0x%x, Aligned: 0x%x", (unsigned int)index, (unsigned int)parameter->Offset, (unsigned int)parameter->Size, (unsigned int)JIT_StackAlign(parameter->Size)); } pMethod->ParametersLayedOut = TRUE; } }
VOID Log_WriteWorker(_In_ PDEVICE_OBJECT DeviceObject, _In_opt_ PVOID Context, _In_ PIO_WORKITEM IoWorkItem) { UNREFERENCED_PARAMETER(DeviceObject); PLOG_COUNTED_STRING line = Context; ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL); Log_WriteLine(line); InterlockedDecrement(&LogScheduledPrints); IoFreeWorkItem(IoWorkItem); ExFreePoolWithTag(Context, LogAllocationTag); }
IRType* IRType_GenericDeepCopy(IRType* pType, IRAssembly* pAssembly) { IRType* type = (IRType*)calloc(1, sizeof(IRType)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRType_GenericDeepCopy @ 0x%x, of 0x%x", (unsigned int)type, (unsigned int)pType); *type = *pType; type->ParentAssembly = pAssembly; type->FieldsLayedOut = FALSE; if (pType->StackSizeCalculated || pType->SizeCalculated) { printf("Type = 0x%X\n", (unsigned int)pType); Panic("This should not be happening"); } return type; }
IRAssembly* IRAssembly_Create(AppDomain* pDomain, CLIFile *pFile) { IRAssembly* assembly = (IRAssembly*)calloc(1, sizeof(IRAssembly)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRAssembly_Create @ 0x%x", (unsigned int)assembly); assembly->ParentDomain = pDomain; assembly->ParentFile = pFile; assembly->MethodCount = pFile->MethodDefinitionCount; assembly->Methods = (IRMethod**)calloc(1, pFile->MethodDefinitionCount * sizeof(IRMethod*)); assembly->FieldCount = pFile->FieldCount; assembly->Fields = (IRField**)calloc(1, pFile->FieldCount * sizeof(IRField*)); assembly->TypeCount = pFile->TypeDefinitionCount; assembly->Types = (IRType**)calloc(1, pFile->TypeDefinitionCount * sizeof(IRType*)); for (uint32_t index = 1; index <= pFile->FieldCount; ++index) if ((pFile->Fields[index].Flags & (FieldAttributes_Static | FieldAttributes_Literal)) == FieldAttributes_Static) ++assembly->StaticFieldCount; assembly->StaticFields = (IRField**)calloc(1, assembly->StaticFieldCount * sizeof(IRField*)); AppDomain_AddAssembly(pDomain, assembly); return assembly; }
IRMethod* IRMethod_GenericDeepCopy(IRMethod* pMethod, IRAssembly* pAssembly) { IRMethod* method = (IRMethod*)calloc(1, sizeof(IRMethod)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRMethod_GenericDeepCopy @ 0x%x, of 0x%x", (unsigned int)method, (unsigned int)pMethod); *method = *pMethod; method->ParentAssembly = pAssembly; method->IRCodes = 0; method->Parameters = (IRParameter**)calloc(1, method->ParameterCount * sizeof(IRParameter*)); for (uint32_t index = 0; index < pMethod->ParameterCount; ++index) { method->Parameters[index] = IRParameter_Copy(pMethod->Parameters[index]); } if (method->IsGeneric) { method->GenericParameterCount = 0; } return method; }
void IRMethod_Destroy(IRMethod* pMethod) { Log_WriteLine(LOGLEVEL__Memory, "Memory: IRMethod_Destroy @ 0x%x", (unsigned int)pMethod); for (uint32_t index = 0; index < pMethod->LocalVariableCount; ++index) { if (pMethod->LocalVariables[index]) { IRLocalVariable_Destroy(pMethod->LocalVariables[index]); } } for (uint32_t index = 0; index < pMethod->ParameterCount; ++index) { if (pMethod->Parameters[index]) { IRParameter_Destroy(pMethod->Parameters[index]); } } if (pMethod->GenericMethod) IRGenericMethod_Destroy(pMethod->GenericMethod); free(pMethod->LocalVariables); free(pMethod->Parameters); free(pMethod); }
IRField* IRField_Create(IRType* pType, Field* pField) { IRField* field = (IRField*)calloc(1, sizeof(IRField)); Log_WriteLine(LOGLEVEL__Memory, "Memory: IRField_Create @ 0x%x", (unsigned int)field); field->FieldIndex = pField->TableIndex - 1; field->ParentAssembly = pType->ParentAssembly; field->FieldDefinition = pField; field->ParentType = pType; pType->ParentAssembly->Fields[field->FieldIndex] = field; if (!pField->SignatureCache) { pField->SignatureCache = FieldSignature_Expand(pField->Signature, pField->File); } field->FieldType = AppDomain_GetIRTypeFromSignatureType(field->ParentAssembly->ParentDomain, pField->File->Assembly, pField->SignatureCache->Type); if ((pField->Flags & (FieldAttributes_Static | FieldAttributes_Literal)) == FieldAttributes_Static) { field->IsStatic = TRUE; field->StaticFieldIndex = pType->ParentAssembly->StaticFieldIndex; pType->ParentAssembly->StaticFields[field->StaticFieldIndex] = field; ++pType->ParentAssembly->StaticFieldIndex; } return field; }
Thread* Thread_Create(Process* pProcess, size_t pEntryPoint, size_t pStackSize, uint8_t pPriority) { Thread* thread = (Thread*)calloc(1, sizeof(Thread)); thread->Process = pProcess; thread->EntryPoint = pEntryPoint; thread->Stack = (uint8_t*)calloc(1, pStackSize); _REENT_INIT_PTR(&thread->Reentrant); thread->Reentrant._stdin = stdin; thread->Reentrant._stdout = stdout; thread->Reentrant._stderr = stderr; thread->Reentrant.__sdidinit = TRUE; thread->Reentrant._new._reent._unused_rand = (uint32_t)thread; Log_WriteLine(LOGLEVEL__Memory, "Memory: Thread_Create @ 0x%x, Stack @ 0x%x", (unsigned int)thread, (unsigned int)thread->Stack); thread->StackSize = pStackSize; thread->Priority = pPriority; thread->SavedRegisterState.useresp = (uint32_t)(thread->Stack + thread->StackSize); thread->SavedRegisterState.eip = pEntryPoint; thread->SavedRegisterState.cs = 0x1B; thread->SavedRegisterState.ds = 0x23; thread->SavedRegisterState.ss = 0x23; thread->SavedRegisterState.eflags = 0x200; // Interrupts enabled, maybe need 0x202 ThreadScheduler_Add(thread); return thread; }