<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56171>56171</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            JIT-ed code crashed when throwing c++ exception on windows x64 debug build
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mengzhisy
      </td>
    </tr>
</table>

<pre>
    win64 Debug Build (Release Build is ok)
Consider the following code, Process crashed when run to `throw 42;`
If we use LLVMAddGlobalMapping instead of Engine::addGlobalMapping, the test passes, which is weird.
```
#include "MCJITTestAPICommon.h"
#include "llvm-c/Analysis.h"
#include "llvm-c/Core.h"
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Target.h"
#include "llvm-c/Transforms/PassManagerBuilder.h"
#include "llvm-c/Transforms/Scalar.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Host.h"
#include "gtest/gtest.h"

#include "llvm/ExecutionEngine/ExecutionEngine.h"

using namespace llvm;

namespace {
class MCJITCAPITest : public testing::Test {
protected:
    MCJITCAPITest() {
        InitializeNativeTarget();
        InitializeNativeTargetAsmPrinter();
    }

    void SetUp() override {
        Module = nullptr;
        Function = nullptr;
        Engine = nullptr;
        Error = nullptr;
    }

    void TearDown() override {
        if (Engine)
            LLVMDisposeExecutionEngine(Engine);
        else if (Module)
            LLVMDisposeModule(Module);
    }

    void buildMCJITOptions() {
        LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
        Options.OptLevel = 2;

        // Just ensure that this field still exists.
        Options.NoFramePointerElim = false;
    }

    void buildMCJITEngine() {
        ASSERT_EQ(
            0, LLVMCreateMCJITCompilerForModule(&Engine, Module, &Options,
                                                sizeof(Options), &Error));
    }

    LLVMModuleRef Module;
    LLVMValueRef Function;
    LLVMValueRef Function2;
    LLVMMCJITCompilerOptions Options;
    LLVMExecutionEngineRef Engine;
    char* Error;
};
}  // end anonymous namespace

static int localTestFunc() {
    throw 42;
}

TEST_F(MCJITCAPITest, addGlobalMapping) {
    Module = LLVMModuleCreateWithName("testModule");
    LLVMTypeRef FunctionType = LLVMFunctionType(LLVMInt32Type(), nullptr, 0, 0);
    LLVMValueRef MappedFn = LLVMAddFunction(Module, "mapped_fn", FunctionType);

    Function = LLVMAddFunction(Module, "test_fn", FunctionType);
    LLVMBasicBlockRef Entry = LLVMAppendBasicBlock(Function, "");
    LLVMBuilderRef Builder = LLVMCreateBuilder();
    LLVMPositionBuilderAtEnd(Builder, Entry);
    LLVMValueRef RetVal =
        LLVMBuildCall2(Builder, FunctionType, MappedFn, nullptr, 0, "");
    LLVMBuildRet(Builder, RetVal);
    LLVMDisposeBuilder(Builder);

    LLVMVerifyModule(Module, LLVMAbortProcessAction, &Error);
    LLVMDisposeMessage(Error);

    buildMCJITOptions();
    buildMCJITEngine();

//    If we use LLVMAddGlobalMapping instead of Engine::addGlobalMapping, the test passes, which is weird.
//    LLVMAddGlobalMapping(
//        Engine, MappedFn,
//        reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(&localTestFunc)));
    unwrap(Engine)->addGlobalMapping(
        unwrap<GlobalValue>(MappedFn), reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(&localTestFunc)));

    uint64_t raw = LLVMGetFunctionAddress(Engine, "test_fn");
    int (*usable)() = (int (*)())raw;

    int catched = 0;
    try {
        usable();
    } catch (int x) {
        catched = x;
    }
    EXPECT_EQ(catched, 42);
}

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFWFtz4jYU_jXmRbMMMbfwwAMxsM3OsqUJ3fYtI-wDqCskjySHsL--R_Idm5B9aT0esKVP536RvJXReXpiYjQgc9gme_KQMB4Rz79_Ag5UQzbANJE_PH_i9eZebxZIoVkEipgDkJ3kXCKJPQllBJ4fkLWSIWhNQkX1ASJyOoAgKhH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