From 63c642141306666f5eec213cad62cadc8aaccfe7 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Fri, 15 May 2020 10:33:56 -0700 Subject: [PATCH] Fixed issues using object flags on on-debug default alloc --- BeefLibs/corlib/src/Runtime.bf | 2 +- .../minlib/src/System/Diagnostics/Debug.bf | 3 ++- IDE/mintest/minlib/src/System/Runtime.bf | 2 +- IDE/src/Compiler/BfCompiler.bf | 7 +++--- IDE/src/IDEApp.bf | 5 ++--- IDE/src/Workspace.bf | 12 ++++++++++ IDEHelper/Compiler/BfIRCodeGen.cpp | 9 ++++++-- IDEHelper/Compiler/BfModule.cpp | 22 ++++++++++++++++++- 8 files changed, 50 insertions(+), 12 deletions(-) diff --git a/BeefLibs/corlib/src/Runtime.bf b/BeefLibs/corlib/src/Runtime.bf index 58c77f5e..79b7c0b6 100644 --- a/BeefLibs/corlib/src/Runtime.bf +++ b/BeefLibs/corlib/src/Runtime.bf @@ -1,4 +1,4 @@ -#if BF_ENABLE_REALTIME_LEAK_CHECK || BF_DEBUG_ALLOC +#if BF_ENABLE_OBJECT_DEBUG_FLAGS || BF_DEBUG_ALLOC #define BF_DBG_RUNTIME #endif diff --git a/IDE/mintest/minlib/src/System/Diagnostics/Debug.bf b/IDE/mintest/minlib/src/System/Diagnostics/Debug.bf index a9a69cf5..813592f4 100644 --- a/IDE/mintest/minlib/src/System/Diagnostics/Debug.bf +++ b/IDE/mintest/minlib/src/System/Diagnostics/Debug.bf @@ -24,8 +24,9 @@ namespace System.Diagnostics #if !DEBUG [CallingConvention(.Cdecl), SkipCall] -#endif +#else [CallingConvention(.Cdecl)] +#endif static extern void Write(char8* str, int strLen); public static void WriteLine(StringView line) diff --git a/IDE/mintest/minlib/src/System/Runtime.bf b/IDE/mintest/minlib/src/System/Runtime.bf index 8d4b8eea..ca3b7771 100644 --- a/IDE/mintest/minlib/src/System/Runtime.bf +++ b/IDE/mintest/minlib/src/System/Runtime.bf @@ -1,4 +1,4 @@ -#if BF_ENABLE_REALTIME_LEAK_CHECK || BF_DEBUG_ALLOC +#if BF_ENABLE_OBJECT_DEBUG_FLAGS || BF_DEBUG_ALLOC #define BF_DBG_RUNTIME #endif diff --git a/IDE/src/Compiler/BfCompiler.bf b/IDE/src/Compiler/BfCompiler.bf index d2f5af96..9a028e49 100644 --- a/IDE/src/Compiler/BfCompiler.bf +++ b/IDE/src/Compiler/BfCompiler.bf @@ -589,9 +589,10 @@ namespace IDE.Compiler SetOpt(options.mEmitDynamicCastCheck, .EmitDynamicCastCheck); SetOpt(enableObjectDebugFlags, .EnableObjectDebugFlags); SetOpt(emitObjectAccessCheck, .EmitObjectAccessCheck); -#if BF_PLATFORM_WINDOWS - SetOpt(options.mEnableRealtimeLeakCheck, .EnableRealtimeLeakCheck); -#endif + + if (options.LeakCheckingEnabled) + SetOpt(options.mEnableRealtimeLeakCheck, .EnableRealtimeLeakCheck); + SetOpt(options.mEnableSideStack, .EnableSideStack); #if !CLI SetOpt(options.mAllowHotSwapping, .EnableHotSwapping); diff --git a/IDE/src/IDEApp.bf b/IDE/src/IDEApp.bf index 9da6e015..e1657c4d 100644 --- a/IDE/src/IDEApp.bf +++ b/IDE/src/IDEApp.bf @@ -7180,13 +7180,12 @@ namespace IDE // Only supported on Windows at the moment bool hasLeakCheck = false; -#if BF_PLATFORM_WINDOWS - if (workspaceOptions.mEnableRealtimeLeakCheck && workspaceOptions.mEnableObjectDebugFlags) + if (workspaceOptions.LeakCheckingEnabled) { hasLeakCheck = true; macroList.Add("BF_ENABLE_REALTIME_LEAK_CHECK"); } -#endif + if ((workspaceOptions.mAllocType == .Debug) || (hasLeakCheck)) macroList.Add("BF_DEBUG_ALLOC"); diff --git a/IDE/src/Workspace.bf b/IDE/src/Workspace.bf index e249190b..9566ae2c 100644 --- a/IDE/src/Workspace.bf +++ b/IDE/src/Workspace.bf @@ -245,6 +245,18 @@ namespace IDE delete configSel; } + public bool LeakCheckingEnabled + { + get + { +#if BF_PLATFORM_WINDOWS + return mEnableRealtimeLeakCheck && mEnableObjectDebugFlags && (mAllocType == .Debug); +#else + return false; +#endif + } + } + public bool IsTestProject(Project project) { return ((mBuildKind == .Test) && diff --git a/IDEHelper/Compiler/BfIRCodeGen.cpp b/IDEHelper/Compiler/BfIRCodeGen.cpp index a15c53c1..3c165566 100644 --- a/IDEHelper/Compiler/BfIRCodeGen.cpp +++ b/IDEHelper/Compiler/BfIRCodeGen.cpp @@ -1939,11 +1939,16 @@ void BfIRCodeGen::HandleNextCmd() } break; case BfIRCmd_CreateFunction: - { + { CMD_PARAM(llvm::FunctionType*, type); BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read(); CMD_PARAM(String, name); - SetResult(curId, llvm::Function::Create(type, LLVMMapLinkageType(linkageType), name.c_str(), mLLVMModule)); + + auto func = mLLVMModule->getFunction(name.c_str()); + if ((func == NULL) || (func->getFunctionType() != type)) + func = llvm::Function::Create(type, LLVMMapLinkageType(linkageType), name.c_str(), mLLVMModule); + + SetResult(curId, func); } break; case BfIRCmd_EnsureFunctionPatchable: diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index f05aaacd..02f7af63 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -8120,10 +8120,20 @@ BfIRValue BfModule::AllocFromType(BfType* type, const BfAllocTarget& allocTarget } } + bool wasAllocated = false; if (!result) { if (hasCustomAllocator) result = AllocBytes(allocTarget.mRefNode, allocTarget, typeInstance, sizeValue, GetConstValue(typeInstance->mInstAlign), (BfAllocFlags)(BfAllocFlags_ZeroMemory | BfAllocFlags_NoDefaultToMalloc)); + else if ((mCompiler->mOptions.mObjectHasDebugFlags) && (!mCompiler->mOptions.mDebugAlloc)) + { + SizedArray llvmArgs; + llvmArgs.push_back(sizeValue); + auto irFunc = GetBuiltInFunc(BfBuiltInFuncType_Malloc); + result = mBfIRBuilder->CreateCall(irFunc, llvmArgs); + result = mBfIRBuilder->CreateBitCast(result, mBfIRBuilder->MapTypeInstPtr(typeInstance)); + wasAllocated = true; + } } if (result) @@ -8139,6 +8149,16 @@ BfIRValue BfModule::AllocFromType(BfType* type, const BfAllocTarget& allocTarget (isResultInitialized ? "Dbg_ObjectCreatedEx" : "Dbg_ObjectAllocatedEx") : (isResultInitialized ? "Dbg_ObjectCreated" : "Dbg_ObjectAllocated")); mBfIRBuilder->CreateCall(objectCreatedMethod.mFunc, llvmArgs); + + if (wasAllocated) + { + auto byteType = GetPrimitiveType(BfTypeCode_UInt8); + auto bytePtrType = CreatePointerType(byteType); + auto flagsPtr = mBfIRBuilder->CreateBitCast(result, mBfIRBuilder->MapType(bytePtrType)); + auto flagsVal = mBfIRBuilder->CreateLoad(flagsPtr); + auto modifiedFlagsVal = mBfIRBuilder->CreateOr(flagsVal, mBfIRBuilder->CreateConst(BfTypeCode_UInt8, /*BF_OBJECTFLAG_ALLOCATED*/4)); + mBfIRBuilder->CreateStore(modifiedFlagsVal, flagsPtr); + } } else { @@ -8170,7 +8190,7 @@ BfIRValue BfModule::AllocFromType(BfType* type, const BfAllocTarget& allocTarget else { SizedArray llvmArgs; - llvmArgs.push_back(sizeValue); + llvmArgs.push_back(sizeValue); BfIRFunction irFunc; if (mCompiler->mOptions.mDebugAlloc) {