google / mozc

Mozc - a Japanese Input Method Editor designed for multi-platform
Other
2.46k stars 365 forks source link

[Windows] SearchHost.exe may freeze caused by FileUtil::CreateDirectory #1076

Open hiroyuki-komatsu opened 1 month ago

hiroyuki-komatsu commented 1 month ago

There are some reports that SearchHost.exe freezes with Mozc built with BRANDING=GoogleJapaneseInput.

Through bisect investigations, I figured out 68e99df9805875b1a5b9906652aa967678ac59bf is the change of this issue. It modified FileUtil::CreateDirectory.

We should update FileUtil::CreateDirectory again to address the problem.

As a tricky point, this issue is reported only on BRANDING=GoogleJapaneseInput and channel_dev=0.

References:

yukawa commented 1 month ago

While I still cannot reproduce the issue locally, based on the information I got my current hypothesis is that broadFileSystemAccess capability given to SearchHost.exe is playing an interesting role.

When Mozc TIP DLL calls CreateFileW API to open config1.db, the process itself does not have sufficient permission to open it. Then broadFileSystemAccess capability takes place and Windows.Storage.OneCore.dll attempts brokered file access after initializing the thread with RoInitialize() when not yet initialized. If this happens in a certain situation, TSF runtime gets confused and may start re-initializing Mozc TIP.

  1. TSF calls back into Mozc TIP
  2. Mozc TIP calls CreateFileW()
  3. The system invokes RoInitialize() before returning from CreateFileW()
  4. TSF calls back into Mozc TIP before returning from RoInitialize()
  5. Now Mozc TIP is handling a reentrant callback.

The on-going issue looks to be a deadlock due to non-reentrant lock.

  1. TSF calls back into Mozc TIP
  2. Mozc TIP acquires a non-reentrant lock.
  3. Mozc TIP calls CreateFileW()
  4. The system invokes RoInitialize() before returning from CreateFileW()
  5. TSF calls back into Mozc TIP before returning from RoInitialize()
  6. Now Mozc TIP is handling a reentrant callback.
  7. Mozc TIP tries to acquire the same non-reentrant lock, which causes a dead lock.

To confirm that SearchHost.exe has broadFileSystemAccess capability, you can find the following SID in its process.