From: Mikhail Kurinnoi Date: Wed, 18 May 2022 14:57:07 +0000 (+0300) Subject: Fix Windows 32bit related issues. X-Git-Tag: submit/tizen/20220615.133502~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=21d84f2da69eb65fd2f69c60eb5fa41ed3d75603;p=sdk%2Ftools%2Fnetcoredbg.git Fix Windows 32bit related issues. --- diff --git a/fetchdeps.cmake b/fetchdeps.cmake index 14198d9..8ca6b7f 100644 --- a/fetchdeps.cmake +++ b/fetchdeps.cmake @@ -47,8 +47,13 @@ if ("${DOTNET_DIR}" STREQUAL "" AND (("${DBGSHIM_RUNTIME_DIR}" STREQUAL "") OR $ if (NOT "${retcode}" STREQUAL "0") message(FATAL_ERROR "Fatal error when downloading dotnet install script") endif() + if (CLR_CMAKE_PLATFORM_ARCH_I386) + set(NETSDKARCH "x86") + elseif() + set(NETSDKARCH "x64") + endif() execute_process( - COMMAND powershell -File "${CMAKE_CURRENT_BINARY_DIR}/dotnet-install.ps1" -Channel "${DOTNET_CHANNEL}" -InstallDir "${DOTNET_DIR}" -Architecture x64 -Verbose + COMMAND powershell -File "${CMAKE_CURRENT_BINARY_DIR}/dotnet-install.ps1" -Channel "${DOTNET_CHANNEL}" -InstallDir "${DOTNET_DIR}" -Architecture ${NETSDKARCH} -Verbose WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE retcode) if (NOT "${retcode}" STREQUAL "0") diff --git a/src/debugger/frames.cpp b/src/debugger/frames.cpp index 1b8dc97..744f4e7 100644 --- a/src/debugger/frames.cpp +++ b/src/debugger/frames.cpp @@ -32,7 +32,7 @@ static uint64_t GetSP(CONTEXT *context) return (uint64_t)(size_t)context->Sp; #elif defined(_TARGET_ARM64_) return (uint64_t)(size_t)context->Sp; -#elif +#else #error "Unsupported platform" #endif } diff --git a/src/debugger/manageddebugger.cpp b/src/debugger/manageddebugger.cpp index 0830fce..5e86bd6 100644 --- a/src/debugger/manageddebugger.cpp +++ b/src/debugger/manageddebugger.cpp @@ -1269,12 +1269,16 @@ static HRESULT ApplyMetadataAndILDeltas(Modules *pModules, const std::string &dl if (deltaILFileStream.is_open() && deltaMDFileStream.is_open()) { auto deltaILSize = deltaILFileStream.tellg(); - BYTE *deltaILMemBlock = new BYTE[deltaILSize]; + if (deltaILSize < 0) + return E_FAIL; + BYTE *deltaILMemBlock = new BYTE[(size_t)deltaILSize]; deltaILFileStream.seekg(0, std::ios::beg); deltaILFileStream.read((char*)deltaILMemBlock, deltaILSize); auto deltaMDSize = deltaMDFileStream.tellg(); - BYTE *deltaMDMemBlock = new BYTE[deltaMDSize]; + if (deltaMDSize < 0) + return E_FAIL; + BYTE *deltaMDMemBlock = new BYTE[(size_t)deltaMDSize]; deltaMDFileStream.seekg(0, std::ios::beg); deltaMDFileStream.read((char*)deltaMDMemBlock, deltaMDSize); diff --git a/src/main.cpp b/src/main.cpp index 502977e..31ec999 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -224,7 +224,11 @@ static HRESULT LaunchNewProcess(IDebugger *pDebugger, std::string &execFile, std return pDebugger->ConfigurationDone(); } -int main(int argc, char* argv[]) +int +#if defined(WIN32) && defined(_TARGET_X86_) + __cdecl +#endif + main(int argc, char* argv[]) { DWORD pidDebuggee = 0; diff --git a/src/managed/SymbolReader.cs b/src/managed/SymbolReader.cs index 8c962b7..6bfdc7e 100644 --- a/src/managed/SymbolReader.cs +++ b/src/managed/SymbolReader.cs @@ -512,7 +512,7 @@ namespace NetCoreDbg /// array of normal methods tokens /// pointer to memory with result /// "Ok" if information is available - internal static RetCode GetModuleMethodsRanges(IntPtr symbolReaderHandle, int constrNum, IntPtr constrTokens, int normalNum, IntPtr normalTokens, out IntPtr data) + internal static RetCode GetModuleMethodsRanges(IntPtr symbolReaderHandle, uint constrNum, IntPtr constrTokens, uint normalNum, IntPtr normalTokens, out IntPtr data) { Debug.Assert(symbolReaderHandle != IntPtr.Zero); data = IntPtr.Zero; diff --git a/src/managed/interop.cpp b/src/managed/interop.cpp index 971983f..25624ac 100644 --- a/src/managed/interop.cpp +++ b/src/managed/interop.cpp @@ -63,7 +63,7 @@ typedef RetCode (*GetHoistedLocalScopes)(PVOID, int32_t, PVOID*, int32_t*); typedef RetCode (*GetSequencePointByILOffsetDelegate)(PVOID, mdMethodDef, uint32_t, PVOID); typedef RetCode (*GetNextSequencePointByILOffsetDelegate)(PVOID, mdMethodDef, uint32_t, uint32_t*, int32_t*); typedef RetCode (*GetStepRangesFromIPDelegate)(PVOID, int32_t, mdMethodDef, uint32_t*, uint32_t*); -typedef RetCode (*GetModuleMethodsRangesDelegate)(PVOID, int32_t, PVOID, int32_t, PVOID, PVOID*); +typedef RetCode (*GetModuleMethodsRangesDelegate)(PVOID, uint32_t, PVOID, uint32_t, PVOID, PVOID*); typedef RetCode (*ResolveBreakPointsDelegate)(PVOID[], int32_t, PVOID, int32_t, int32_t, int32_t*, PVOID*); typedef RetCode (*GetAsyncMethodSteppingInfoDelegate)(PVOID, mdMethodDef, PVOID*, int32_t*, uint32_t*); typedef RetCode (*GetSourceDelegate)(PVOID, const WCHAR*, int32_t*, PVOID*); @@ -401,7 +401,7 @@ HRESULT CalculationDelegate(PVOID firstOp, int32_t firstType, PVOID secondOp, in return S_OK; } -HRESULT GetModuleMethodsRanges(PVOID pSymbolReaderHandle, int32_t constrTokensNum, PVOID constrTokens, int32_t normalTokensNum, PVOID normalTokens, PVOID *data) +HRESULT GetModuleMethodsRanges(PVOID pSymbolReaderHandle, uint32_t constrTokensNum, PVOID constrTokens, uint32_t normalTokensNum, PVOID normalTokens, PVOID *data) { std::unique_lock read_lock(CLRrwlock.reader); if (!getModuleMethodsRangesDelegate || !pSymbolReaderHandle || (constrTokensNum && !constrTokens) || (normalTokensNum && !normalTokens) || !data) diff --git a/src/managed/interop.h b/src/managed/interop.h index aee784d..879d449 100644 --- a/src/managed/interop.h +++ b/src/managed/interop.h @@ -93,7 +93,7 @@ namespace Interop WCHAR *localName, ULONG localNameLen, ULONG32 *pIlStart, ULONG32 *pIlEnd); HRESULT GetHoistedLocalScopes(PVOID pSymbolReaderHandle, mdMethodDef methodToken, PVOID *data, int32_t &hoistedLocalScopesCount); HRESULT GetStepRangesFromIP(PVOID pSymbolReaderHandle, ULONG32 ip, mdMethodDef MethodToken, ULONG32 *ilStartOffset, ULONG32 *ilEndOffset); - HRESULT GetModuleMethodsRanges(PVOID pSymbolReaderHandle, int32_t constrTokensNum, PVOID constrTokens, int32_t normalTokensNum, PVOID normalTokens, PVOID *data); + HRESULT GetModuleMethodsRanges(PVOID pSymbolReaderHandle, uint32_t constrTokensNum, PVOID constrTokens, uint32_t normalTokensNum, PVOID normalTokens, PVOID *data); HRESULT ResolveBreakPoints(PVOID pSymbolReaderHandles[], int32_t tokenNum, PVOID Tokens, int32_t sourceLine, int32_t nestedToken, int32_t &Count, PVOID *data); HRESULT GetAsyncMethodSteppingInfo(PVOID pSymbolReaderHandle, mdMethodDef methodToken, std::vector &AsyncAwaitInfo, ULONG32 *ilOffset); HRESULT GetSource(PVOID symbolReaderHandle, const std::string fileName, PVOID *data, int32_t *length); diff --git a/src/metadata/modules.cpp b/src/metadata/modules.cpp index 7a29b05..2388312 100644 --- a/src/metadata/modules.cpp +++ b/src/metadata/modules.cpp @@ -1053,14 +1053,15 @@ HRESULT Modules::FillSourcesCodeLinesForModule(ICorDebugModule *pModule, IMetaDa } pMDImport->CloseEnum(fEnum); - if (constrTokens.size() > std::numeric_limits::max() || normalTokens.size() > std::numeric_limits::max()) + if (sizeof(std::size_t) > sizeof(std::uint32_t) && + (constrTokens.size() > std::numeric_limits::max() || normalTokens.size() > std::numeric_limits::max())) { LOGE("Too big token arrays."); return E_FAIL; } PVOID data = nullptr; - IfFailRet(Interop::GetModuleMethodsRanges(pSymbolReaderHandle, (int32_t)constrTokens.size(), constrTokens.data(), (int32_t)normalTokens.size(), normalTokens.data(), &data)); + IfFailRet(Interop::GetModuleMethodsRanges(pSymbolReaderHandle, (uint32_t)constrTokens.size(), constrTokens.data(), (uint32_t)normalTokens.size(), normalTokens.data(), &data)); if (data == nullptr) return S_OK; @@ -1292,7 +1293,7 @@ HRESULT Modules::ResolveBreakpoint(/*in*/ CORDB_ADDRESS modAddress, /*in*/ std:: if (info_pair == m_modulesInfo.end()) return E_FAIL; // we must have it, since we loaded data from it - if (Tokens.size() > std::numeric_limits::max()) + if ((int32_t)Tokens.size() > std::numeric_limits::max()) { LOGE("Too big token arrays."); return E_FAIL; diff --git a/src/protocols/vscodeprotocol.cpp b/src/protocols/vscodeprotocol.cpp index 4a71713..277a9b8 100644 --- a/src/protocols/vscodeprotocol.cpp +++ b/src/protocols/vscodeprotocol.cpp @@ -335,7 +335,7 @@ void VSCodeProtocol::EmitOutputEvent(OutputCategory category, string_view output serialize_output(count, m_seqCounter, name, "", escaped_source); // compute total size of headers + text - size_t const total_size = count.size() + escaped_text.size(); + auto const total_size = count.size() + escaped_text.size(); // perform output cout << CONTENT_LENGTH << total_size << TWO_CRLF; diff --git a/src/utils/iosystem.h b/src/utils/iosystem.h index 9613d8c..acd826a 100644 --- a/src/utils/iosystem.h +++ b/src/utils/iosystem.h @@ -67,14 +67,23 @@ template struct IOSystemImpl constexpr static const size_t MaxIteratorSize = sizeof(void*) * 2; const Operations& ops; - mutable char data alignas(alignof(std::max_align_t)) [MaxIteratorSize]; + + // Note, we can't use `alignas(alignof(std::max_align_t))` here, since at least MSVC 32bit (VS2019) compiler can't + // generate proper code and ASAN detect "ERROR: AddressSanitizer: stack - buffer - underflow on address...". + union data_align_t + { + std::max_align_t align_field; + mutable char data[MaxIteratorSize]; + }; + data_align_t data_align_tmp; + char * const data; public: /// Iterator might be created from any other iterator or pointer type, /// for which `operator*` returns reference to `AsyncHandle`. template ()), AsyncHandle&>::value>::type> - AsyncHandleIterator(Iterator it) : ops(OpsImpl::ops) { new (data) Iterator(it); } + AsyncHandleIterator(Iterator it) : ops(OpsImpl::ops), data(data_align_tmp.data) { new (data) Iterator(it); } ~AsyncHandleIterator() { ops.destr(data); } diff --git a/src/utils/iosystem_win32.cpp b/src/utils/iosystem_win32.cpp index 861cb1e..d206c6c 100644 --- a/src/utils/iosystem_win32.cpp +++ b/src/utils/iosystem_win32.cpp @@ -517,7 +517,17 @@ Class::IOResult Class::close(const FileHandle& fh) Class::IOSystem::StdFiles Class::get_std_files() { using Handles = std::tuple; - /*thread_local*/ static alignas(alignof(Handles)) char mem[sizeof(Handles)]; // TODO + + // Note, we can't use `alignas(alignof(std::max_align_t))` here, since at least MSVC 32bit (VS2019) compiler can't + // generate proper code and ASAN detect "ERROR: AddressSanitizer: stack - buffer - underflow on address...". + union mem_align_t + { + std::max_align_t align_field; + char mem[sizeof(Handles)]; + }; + mem_align_t mem_align_tmp; + char * const mem = mem_align_tmp.mem; + Handles& handles = *new (mem) Handles { FileHandle(GetStdHandle(STD_INPUT_HANDLE)), FileHandle(GetStdHandle(STD_OUTPUT_HANDLE)), diff --git a/src/utils/streams.cpp b/src/utils/streams.cpp index 95f6185..ba4d18b 100644 --- a/src/utils/streams.cpp +++ b/src/utils/streams.cpp @@ -70,7 +70,7 @@ void InStreamBuf::compactify() { if (size_t(in_avail()) <= MaxMoveSize) // tail is not too big { - memmove(eback() + UngetChars, gptr(), in_avail()); + memmove(eback() + UngetChars, gptr(), size_t(in_avail())); setg(eback(), eback() + UngetChars, eback() + UngetChars + in_avail()); } } diff --git a/test-suite/MITestException/Program.cs b/test-suite/MITestException/Program.cs index 3265e5f..5a40c5c 100644 --- a/test-suite/MITestException/Program.cs +++ b/test-suite/MITestException/Program.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Runtime.InteropServices; using NetcoreDbgTest; using NetcoreDbgTest.MI; @@ -204,11 +205,15 @@ namespace MITestException Context.WasEntryPointHit(@"__FILE__:__LINE__"); }); + bool win32detect = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && IntPtr.Size == 4; Label.Breakpoint("STEP0"); + P p = new P(); Label.Breakpoint("STEP1"); Label.Checkpoint("test_steps", "try_catch", (Object context) => { Context Context = (Context)context; Context.StepOver(@"__FILE__:__LINE__"); + Context.WasStep(@"__FILE__:__LINE__", "STEP0"); + Context.StepOver(@"__FILE__:__LINE__"); Context.WasStep(@"__FILE__:__LINE__", "STEP1"); Context.StepOver(@"__FILE__:__LINE__"); Context.WasStep(@"__FILE__:__LINE__", "TRY1"); @@ -236,16 +241,29 @@ namespace MITestException Label.Checkpoint("try_catch", "finish", (Object context) => { Context Context = (Context)context; + + var resWin32Detect = Context.MIDebugger.Request(String.Format("-var-create - * \"win32detect\"")); + Assert.Equal(MIResultClass.Done, resWin32Detect.Class, @"__FILE__:__LINE__"); + Assert.Equal("bool", ((MIConst)resWin32Detect["type"]).CString, @"__FILE__:__LINE__"); + string win32detect = ((MIConst)resWin32Detect["value"]).CString; + // Check stack frames location var res = Context.MIDebugger.Request("-stack-list-frames"); Assert.Equal(MIResultClass.Done, res.Class, @"__FILE__:__LINE__"); var stack = (MIList)res["stack"]; - - Assert.Equal(3, stack.Count, @"__FILE__:__LINE__"); - Assert.True(Context.IsValidFrame((MIResult)stack[0], "CATCH4", 0), @"__FILE__:__LINE__"); - Assert.True(Context.IsValidFrame((MIResult)stack[1], "THROW1", 1), @"__FILE__:__LINE__"); - Assert.True(Context.IsValidFrame((MIResult)stack[2], "TRY3", 2), @"__FILE__:__LINE__"); + if (win32detect == "true") + { + Assert.Equal(1, stack.Count, @"__FILE__:__LINE__"); + Assert.True(Context.IsValidFrame((MIResult)stack[0], "CATCH4", 0), @"__FILE__:__LINE__"); + } + else + { + Assert.Equal(3, stack.Count, @"__FILE__:__LINE__"); + Assert.True(Context.IsValidFrame((MIResult)stack[0], "CATCH4", 0), @"__FILE__:__LINE__"); + Assert.True(Context.IsValidFrame((MIResult)stack[1], "THROW1", 1), @"__FILE__:__LINE__"); + Assert.True(Context.IsValidFrame((MIResult)stack[2], "TRY3", 2), @"__FILE__:__LINE__"); + } //Check local variables res = Context.MIDebugger.Request("-stack-list-variables"); diff --git a/third_party/linenoise-ng/src/linenoise.cpp b/third_party/linenoise-ng/src/linenoise.cpp index ad75190..71e91cd 100644 --- a/third_party/linenoise-ng/src/linenoise.cpp +++ b/third_party/linenoise-ng/src/linenoise.cpp @@ -3457,7 +3457,11 @@ void linenoisePrintKeyCodes(void) { disableRawMode(); } -static void WindowSizeChanged(int) { +static void +#if defined(WIN32) && defined(_TARGET_X86_) + __cdecl +#endif + WindowSizeChanged(int) { // do nothing here but setting this flag gotResize = true; #ifdef _WIN32 diff --git a/third_party/linenoise-ng/tst/example.c b/third_party/linenoise-ng/tst/example.c index 59792f8..69b377f 100644 --- a/third_party/linenoise-ng/tst/example.c +++ b/third_party/linenoise-ng/tst/example.c @@ -18,7 +18,11 @@ void completionHook (char const* prefix, linenoiseCompletions* lc) { } } -int main (int argc, char** argv) { +int +#if defined(WIN32) && defined(_TARGET_X86_) + __cdecl +#endif + main (int argc, char** argv) { linenoiseInstallWindowChangeHandler(); while(argc > 1) {