[clang] [llvm] [WinEH] Fix crash, object unwinding in the except block (PR #172287)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 16 03:33:34 PST 2025


GkvJwa wrote:


The cause of the crash was
```
  if (IsEHa) {
    const BasicBlock *EntryBB = &(Fn->getEntryBlock());
    calculateSEHStateForAsynchEH(EntryBB, -1, FuncInfo);
  }

->
calculateSEHStateForAsynchEH(The default state is -1

int State = WI->State;  // State = -1
->

However, if state cannot be reassigned, and the current Fn is seh_try_end

      else if (Fn && Fn->isIntrinsic() &&
               Fn->getIntrinsicID() == Intrinsic::seh_try_end)
        // end of current state, retrive new state from UnwindMap

It will be executed up to this point.
State = EHInfo.SEHUnwindMap[State].ToState; // State = -1

EHInfo.SEHUnwindMap[-1]
//  Therefore, I checked whether the value was greater than 0 before reading data from the vector.
```


class aa {
 public:
  static aa bb();

  ~aa();
};

void Foo(const int* p) {
  int d = 0;
  __try {
    d = *p;
  } __except (1) {
    ::aa::bb();
  }
}
```

> @GkvJwa `The calculateSEHStateNumbers function currently does NOT know about C++ EH constructs like the emitted seh_scope_begin. We simply skip over it in the state numbering which causes the issue.``__C_specific_handler`因此,LLC 的正确拒绝模式应该是包含 SEH(非 CXX)个性的函数`seh_scope_begin`。LLVM 目前无法正确处理这种情况。而且我认为它不应该尝试这样做。
> 
> 我认为 Clang 应该在前端捕获到这个问题,并生成类似的错误信息`C2712`。

It's okay, we can handle this simply first, and then improve SEH support later (I also encountered this problem during compilation. After reproducing it and creating a demo, I tried it with MSVC and found that it wasn't supported.)


https://github.com/llvm/llvm-project/pull/172287


More information about the cfe-commits mailing list