c++ - 作为 win32 服务运行时 OpenSSL SSL_CTX_new 崩溃

标签 c++ multithreading winapi ssl openssl

我遇到了一个奇怪的 OpenSSL 问题:当我的应用程序作为 win32 服务(本地服务/网络服务或系统服务)运行时,SSL_CTX_new() 函数似乎崩溃了。

我的代码是这样的:

int main() {
   SSL_library_init();

   const int nLocks = CRYPTO_num_locks();

   ossl_mtx_pool = new mutexT* [nLocks]; // simple wrapper class over mutexes
   for(int i = 0; i < nLocks; ++i)
      ossl_mtx_pool[i] = new mutexT;

   CRYPTO_THREADID_set_callback(ossl_threadid_function); // returns win32 thread id
   CRYPTO_set_locking_callback(ossl_locking_fun); // simple function that calls mutexT::lock/unlock)

   OpenSSL_add_all_algorithms();
   SSL_load_error_strings();
   ERR_load_BIO_strings();

   // some other app initialization here...

   // run win32 service thread OR just a seaparate thread
}

// called later on in the context of a different thread
int myclient()
{
   myCtx *ctx = new connContextT;
   const SSL_METHOD *mm = SSLv23_client_method();
   if(mm == 0) {
      // print some error whcih doesn't show up
   }   

   printf("I'm Here!\n"); //< this print shows up       

   ctx->sslCtx = SSL_CTX_new(mm);
   // crash >HERE< before being able to print anything else

   // some code to connect to server
}

真正奇怪的是,如果我将这个应用程序作为普通的 win32 应用程序运行(即不调用 win32 服务函数,但仍在单独的线程中运行 myclient()),一切都会完美无缺。

我在 win7 上使用 mingw32 gcc 4.7.1 进行编译,并且静态链接 openssl(无 DLL)。

任何有助于理解问题的帮助将不胜感激。 谢谢你!

最佳答案

经过更多调试后,我在 ossl_threadid_function 中发现了一个非常愚蠢的错误 (!!!) 显然返回错误的 ID 会导致 openssl 中的堆栈损坏,这就是我未能生成任何有用的回溯的原因。

令人惊讶的是,将应用程序作为正常进程而不是服务运行并没有造成任何问题,甚至像 Application Verifier 指出的运行时工具也没有。显然,这与服务实例至少多一个线程这一事实有关......

对此深感抱歉,感谢大家的宝贵时间。 问候。

关于c++ - 作为 win32 服务运行时 OpenSSL SSL_CTX_new 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18933144/

相关文章:

c++ - 在头文件中添加头文件

java - Java EE 7-ManagedThreadFactory可能的限制

c# - 将数据与线程 : How do you do that? 相关联

java - JDBC 不需要的语句关闭

.net - 获取cmd.exe的当前工作目录

c++ - 如何在 Visual Studio Code 中使用 `pkg-config gtkmm-3.0 --cflags --libs`

c++ - "optimized out"值是否应该是随机的?

C++ 变量增量停止程序

c - 写文件和写控制台

C++获取程序文件目录,附加额外路径并执行