Skip to content

infinite recursive stack overflow #33

@nangua928

Description

@nangua928
#include <windows.h>
#include <thread>
#include <chrono>
#include <memory>

int main(int argc, wchar_t *argv[]) {
  for (int i = 0; i < 0x40; ++i)
    TlsAlloc();

  HMODULE h_vld = LoadLibraryA("vld.dll");
  typedef void(*vld_enable_t)(void);
  auto vld_enable = (vld_enable_t)::GetProcAddress(h_vld, "VLDGlobalEnable");
  vld_enable();
  std::thread([]() {std::make_shared<int>(); }).join();
  return 0;
}

When a memory allocation happens, VLD will store the information in its TLS slot to avoid contention and reduce performance impact. When VLD is assigned with a TLS slot >= 0x40 and a memory allocation happens in a thread that hasn't expand TLS, the access to TLS slot from VLD will trigger another TLS expansion.
However, the expansion will call RtlAllocateHeap to allocate memory and the record of this memory allocation will be saved into TLS slot by VLD, which will again trigger another TLS expansion… In this way, The program will enter infinite recursion.
Due to some restrictions, I can only use vld in dll in my program, which prevents me from initializing vld at the entry point at the beginning of the program. What should I do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions