自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

記一次 .NET某上位機(jī)視覺(jué)程序卡死分析

開(kāi)發(fā) 前端
看到源碼之后太無(wú)語(yǔ)了,其實(shí)就是一個(gè)簡(jiǎn)單的 顏色賦值?,根據(jù)前面的探索styleManager1是由渲染線程創(chuàng)建的,所以主線程對(duì)它的賦值自然是得不到渲染線程的反饋。

一、背景

1. 講故事

前段時(shí)間有位朋友找到我,說(shuō)他的窗體程序在客戶這邊出現(xiàn)了卡死,讓我?guī)兔聪略趺椿厥??dump也生成了,既然有dump了那就上 windbg 分析吧。

二、WinDbg 分析

1. 為什么會(huì)卡死

窗體程序的卡死,入口門檻很低,后續(xù)往下分析就不一定了,不管怎么說(shuō)先用 !clrstack 看下主線程,輸出如下:

0:000> !clrstack
OS Thread Id: 0x3118 (0)
        Child SP               IP Call Site
000000c478afd1d8 00007ffc284e9a84 [HelperMethodFrame_1OBJ: 000000c478afd1d8] System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
000000c478afd300 00007ffbf2cc19ac System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 243]
000000c478afd330 00007ffbf2cc197f System.Threading.WaitHandle.WaitOne(Int32, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 194]
000000c478afd370 00007ffbf1421904 System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle)
000000c478afd3e0 00007ffbf0c8e2f4 System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
000000c478afd520 00007ffbf1425124 System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
000000c478afd590 00007ffb995d6fe8 DevComponents.DotNetBar.StyleManager.OnColorTintChanged(System.Drawing.Color, System.Drawing.Color)
000000c478afd5f0 00007ffb995d69ff DevComponents.DotNetBar.StyleManager.set_ColorTint(System.Drawing.Color)
000000c478afd680 00007ffb995d694c DevComponents.DotNetBar.StyleManager.set_ManagerColorTint(System.Drawing.Color)
...
000000c478afd6b0 00007ffb995d50f9 xxx.MarkInspectPadControl.InitializeComponent()

有經(jīng)驗(yàn)的朋友看到上面的卦象相信就知道咋事情了,即有工作線程創(chuàng)建了用戶控件導(dǎo)致的,而且這個(gè)控件貌似和 DevComponents 有關(guān),接下來(lái)的常規(guī)套路就是挖一下 WindowsFormsSynchronizationContext 對(duì)象看看到底是哪一個(gè)線程創(chuàng)建的,使用 !dso 即可。

0:000> !dso
OS Thread Id: 0x3118 (0)
RSP/REG          Object           Name
000000C478AFCF98 000002093b9143c0 System.Windows.Forms.WindowsFormsSynchronizationContext
...
0:000> !do poi(20939c91588)
Name:        System.Threading.Thread
MethodTable: 00007ffbf2769580
EEClass:     00007ffbf288c658
Size:        96(0x60) bytes
00007ffbf276aaf8  4001934       4c         System.Int32  1 instance                1 m_ManagedThreadId

按照劇本的話 WindowsFormsSynchronizationContext 應(yīng)該會(huì)有2個(gè),但這里只有1個(gè),這一個(gè)還是主線程的同步上下文,這就完?duì)僮恿?。。。完全不按照劇本走,這也是真實(shí)dump分析的復(fù)雜性,那到底是誰(shuí)創(chuàng)建的呢?天要絕人之路嗎?

2. 出路在哪里

所有東西的落地都在匯編里,而匯編又在方法里,所以突破口就是尋找線程棧中的方法,接下來(lái)到 System.Windows.Forms.Control.MarshaledInvoke 方法里看一看可有什么大貨,簡(jiǎn)化后如下:

private object MarshaledInvoke(Control caller, Delegate method, object[] args, bool synchronous)
{
    bool flag = false;
    if (SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(this, Handle), out var _) == SafeNativeMethods.GetCurrentThreadId() && synchronous)
    {
        flag = true;
    }
    ThreadMethodEntry threadMethodEntry = new ThreadMethodEntry(caller, this, method, args, synchronous, executionContext);
    lock (threadCallbackList)
    {
        if (threadCallbackMessage == 0)
        {
            threadCallbackMessage = SafeNativeMethods.RegisterWindowMessage(Application.WindowMessagesVersion + "_ThreadCallbackMessage");
        }
        threadCallbackList.Enqueue(threadMethodEntry);
    }
    if (flag)
    {
        InvokeMarshaledCallbacks();
    }
    else
    {
        UnsafeNativeMethods.PostMessage(new HandleRef(this, Handle), threadCallbackMessage, IntPtr.Zero, IntPtr.Zero);
    }
    if (synchronous)
    {
        if (!threadMethodEntry.IsCompleted)
        {
            WaitForWaitHandle(threadMethodEntry.AsyncWaitHandle);
        }
        return threadMethodEntry.retVal;
    }
    return threadMethodEntry;
}

從卦中的代碼來(lái)看,這個(gè) SafeNativeMethods.GetWindowThreadProcessId 方法是關(guān)鍵,它可以拿到這個(gè)窗口創(chuàng)建的processid和threadid,接下來(lái)觀察下簡(jiǎn)化后的匯編代碼。

0:000> !U /d 00007ffbf0c8e2f4
preJIT generated code
System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
Begin 00007ffbf0c8dec0, size 4e9
00007ffb`f0c8dec0 55              push    rbp
00007ffb`f0c8dec1 4157            push    r15
00007ffb`f0c8dec3 4156            push    r14
00007ffb`f0c8dec5 4155            push    r13
00007ffb`f0c8dec7 4154            push    r12
00007ffb`f0c8dec9 57              push    rdi
00007ffb`f0c8deca 56              push    rsi
00007ffb`f0c8decb 53              push    rbx
00007ffb`f0c8decc 4881ecf8000000  sub     rsp,0F8h
00007ffb`f0c8ded3 488dac2430010000 lea     rbp,[rsp+130h]
...
00007ffb`f0c8dff0 488d55b0        lea     rdx,[rbp-50h]
00007ffb`f0c8dff4 ff151e1eddff    call    qword ptr [System_Windows_Forms_ni+0x8fe18 (00007ffb`f0a5fe18)] (System.Windows.Forms.SafeNativeMethods.GetWindowThreadProcessId(System.Runtime.InteropServices.HandleRef, Int32 ByRef), mdToken: 00000000060033c4)
00007ffb`f0c8dffa 448bf0          mov     r14d,eax

根據(jù)卦中的匯編以及x64調(diào)用協(xié)定,lea rdx,[rbp-50h] 就是我們的 processid,同時(shí) mov r14d,eax 中的 r14d 就是我們的 threadid,突破口已找到,接下來(lái)就是深挖了。

3. 如何挖出進(jìn)程ID和線程ID

有一點(diǎn)要知道 000000c478afd520 和 MarshaledInvoke 方法的 rsp 隔了一個(gè) 0x8,同時(shí)方法中影響 rsp 的 push 和 sub 都要計(jì)算進(jìn)去,這里就不贅述了,具體可以參考文章:https://www.cnblogs.com/huangxincheng/p/17250240.html 簡(jiǎn)單計(jì)算后如下:

0:000> ? 000000c478afd520-0x8-(0n8*0n8)-0xF8+0x130
Evaluate expression: 843838379280 = 000000c4`78afd510
0:000> dp 000000c4`78afd510-0x50 L1
000000c4`78afd4c0  00000000`000029dc

0:000> r r14
r14=000000c478afcf14
0:000> dp 000000c478afcf14 L1
000000c4`78afcf14  00000000`00000080

從卦中可以看到 processid=29dc ,threadid=0x80,這東西是何方神圣呢,我們用 ~ 來(lái)找它的真身吧。

0:000> ~
...
  18  Id: 29dc.80 Suspend: 0 Teb: 000000c4`7890d000 Unfrozen
...

0:018> k
 # Child-SP          RetAddr               Call Site
00 000000c4`7a2ffcc8 00007ffc`28028ba3     ntdll!NtWaitForSingleObject+0x14
01 000000c4`7a2ffcd0 00007ffb`fa651cf8     KERNELBASE!WaitForSingleObjectEx+0x93
02 000000c4`7a2ffd70 00007ffb`fa652a51     wpfgfx_v0400!CPartitionManager::GetWork+0x17b
03 000000c4`7a2ffdc0 00007ffb`fa67a2fb     wpfgfx_v0400!CPartitionThread::Run+0x21
04 000000c4`7a2ffdf0 00007ffc`2a037bd4     wpfgfx_v0400!CPartitionThread::ThreadMain+0x2b
05 000000c4`7a2ffe20 00007ffc`2a76ced1     kernel32!BaseThreadInitThunk+0x14
06 000000c4`7a2ffe50 00000000`00000000     ntdll!RtlUserThreadStart+0x21

現(xiàn)在有點(diǎn)傻傻分不清了,怎么 winform 里還有 wpf 的渲染線程,有可能是 DevComponents 這種第三方控件在底層引入的吧。到這里路子又被堵死了,接下來(lái)該往哪里走呢?三步一回頭,繼續(xù)看主線程上的方法代碼吧。

4. 在源碼中尋找答案

雖然在兩條路上的突圍都失敗了,但可以明顯的看到離真相真的越來(lái)越近,也收獲到了大量的作戰(zhàn)信息,通過(guò)上面的 set_ManagerColorTint 方法的反編譯,參考如下:

private void InitializeComponent()
{
    this.styleManager1.ManagerColorTint = System.Drawing.Color.Black;
}

[Description("Indicates color current style is tinted with.")]
[Category("Appearance")]
public Color ManagerColorTint
{
    get
    {
        return ColorTint;
    }
    set
    {
        ColorTint = value;
    }
}

看到源碼之后太無(wú)語(yǔ)了,其實(shí)就是一個(gè)簡(jiǎn)單的 顏色賦值,根據(jù)前面的探索styleManager1是由渲染線程創(chuàng)建的,所以主線程對(duì)它的賦值自然是得不到渲染線程的反饋。

那這個(gè)問(wèn)題該怎么辦呢?大概是如下兩種吧。

  1. 重點(diǎn)關(guān)注 styleManager1 控件,用排除法觀察程序運(yùn)行狀況。
  2. 看文檔是否用了錯(cuò)誤的方式使用 styleManager1 控件。

三:總結(jié)

這次生產(chǎn)事故還是挺有意思的,為什么 WinForm 中可以存在 CPartitionThread 渲染線程,最后還禍在其身,給我?guī)装倮齞ump分析之旅中添加了一筆色彩!

責(zé)任編輯:武曉燕 來(lái)源: 一線碼農(nóng)聊技術(shù)
相關(guān)推薦

2024-07-12 11:20:34

.NET崩潰視覺(jué)程序

2024-06-06 10:51:15

自動(dòng)化系統(tǒng)推測(cè)

2024-12-27 13:31:18

.NETdump調(diào)試

2024-05-20 09:39:02

.NETurl線程池

2022-10-13 18:40:05

.NETOA后端

2023-09-27 07:23:10

.NET監(jiān)控軟件

2024-05-28 10:18:30

WPF程序數(shù)據(jù)

2024-07-01 13:00:24

.NET網(wǎng)絡(luò)邊緣計(jì)算

2022-01-17 21:28:36

管理系統(tǒng).NET

2023-05-15 11:15:50

.NET門診語(yǔ)句

2024-11-29 10:06:59

2022-10-09 10:47:37

NET視覺(jué)軟件

2022-10-25 14:17:01

.NET代碼程序

2023-06-26 00:12:46

2024-03-28 12:56:36

2023-04-06 10:52:18

2023-03-26 20:24:50

ERP網(wǎng)站系統(tǒng)

2023-07-06 10:11:38

.NET模式dump

2024-03-26 00:44:53

.NETCIM系統(tǒng)

2024-05-31 12:56:06

.NET代碼方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)