mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-18 11:13:57 +00:00
Fix static TLS for WOW64
This fix resolves shell32.dll TLS problems. Also it uses correct structure and field names
This commit is contained in:
@@ -214,15 +214,17 @@ namespace syscalls
|
||||
|
||||
if (info_class == ProcessTlsInformation)
|
||||
{
|
||||
constexpr auto thread_data_offset = offsetof(PROCESS_TLS_INFO, ThreadData);
|
||||
if (process_information_length < thread_data_offset)
|
||||
if (process_information_length < sizeof(PROCESS_TLS_INFORMATION) ||
|
||||
(process_information_length - (sizeof(PROCESS_TLS_INFORMATION) - sizeof(THREAD_TLS_INFORMATION))) %
|
||||
sizeof(THREAD_TLS_INFORMATION))
|
||||
{
|
||||
return STATUS_BUFFER_OVERFLOW;
|
||||
return STATUS_INFO_LENGTH_MISMATCH;
|
||||
}
|
||||
|
||||
const emulator_object<THREAD_TLS_INFO> data{c.emu, process_information + thread_data_offset};
|
||||
constexpr auto thread_data_offset = offsetof(PROCESS_TLS_INFORMATION, ThreadData);
|
||||
const emulator_object<THREAD_TLS_INFORMATION> data{c.emu, process_information + thread_data_offset};
|
||||
|
||||
PROCESS_TLS_INFO tls_info{};
|
||||
PROCESS_TLS_INFORMATION tls_info{};
|
||||
c.emu.read_memory(process_information, &tls_info, thread_data_offset);
|
||||
|
||||
for (uint32_t i = 0; i < tls_info.ThreadDataCount; ++i)
|
||||
@@ -242,38 +244,78 @@ namespace syscalls
|
||||
|
||||
entry.Flags = 2;
|
||||
|
||||
thread_iterator->second.teb64->access([&](TEB64& teb) {
|
||||
const auto is_wow64 = c.win_emu.process.is_wow64_process;
|
||||
const auto& thread = thread_iterator->second;
|
||||
|
||||
thread.teb64->access([&](TEB64& teb) {
|
||||
entry.ThreadId = teb.ClientId.UniqueThread;
|
||||
|
||||
const auto tls_vector = teb.ThreadLocalStoragePointer;
|
||||
constexpr auto ptr_size = sizeof(EmulatorTraits<Emu64>::PVOID);
|
||||
uint64_t tls_vector = teb.ThreadLocalStoragePointer;
|
||||
const auto ptr_size = is_wow64 ? sizeof(EmulatorTraits<Emu32>::PVOID) : sizeof(EmulatorTraits<Emu64>::PVOID);
|
||||
|
||||
if (is_wow64)
|
||||
{
|
||||
if (!thread.teb32.has_value())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
thread.teb32->access([&tls_vector](const TEB32& teb32) { tls_vector = teb32.ThreadLocalStoragePointer; });
|
||||
}
|
||||
|
||||
if (!tls_vector)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (tls_info.TlsRequest == ProcessTlsReplaceIndex)
|
||||
if (tls_info.OperationType == ProcessTlsReplaceIndex)
|
||||
{
|
||||
const auto tls_entry_ptr = tls_vector + (tls_info.TlsIndex * ptr_size);
|
||||
uint64_t old_entry{};
|
||||
|
||||
const auto old_entry = c.emu.read_memory<EmulatorTraits<Emu64>::PVOID>(tls_entry_ptr);
|
||||
c.emu.write_memory<EmulatorTraits<Emu64>::PVOID>(tls_entry_ptr, entry.TlsModulePointer);
|
||||
|
||||
entry.TlsModulePointer = old_entry;
|
||||
}
|
||||
else if (tls_info.TlsRequest == ProcessTlsReplaceVector)
|
||||
{
|
||||
const auto new_tls_vector = entry.TlsVector;
|
||||
|
||||
for (uint32_t index = 0; index < tls_info.TlsVectorLength; ++index)
|
||||
if (is_wow64)
|
||||
{
|
||||
const auto old_entry = c.emu.read_memory<uint64_t>(tls_vector + index * ptr_size);
|
||||
c.emu.write_memory(new_tls_vector + index * ptr_size, old_entry);
|
||||
old_entry = c.emu.read_memory<EmulatorTraits<Emu32>::PVOID>(tls_entry_ptr);
|
||||
c.emu.write_memory<EmulatorTraits<Emu32>::PVOID>(tls_entry_ptr, static_cast<uint32_t>(entry.NewTlsData));
|
||||
}
|
||||
else
|
||||
{
|
||||
old_entry = c.emu.read_memory<EmulatorTraits<Emu64>::PVOID>(tls_entry_ptr);
|
||||
c.emu.write_memory<EmulatorTraits<Emu64>::PVOID>(tls_entry_ptr, entry.NewTlsData);
|
||||
}
|
||||
|
||||
teb.ThreadLocalStoragePointer = new_tls_vector;
|
||||
entry.TlsVector = tls_vector;
|
||||
entry.OldTlsData = old_entry;
|
||||
}
|
||||
else if (tls_info.OperationType == ProcessTlsReplaceVector)
|
||||
{
|
||||
const auto new_tls_vector = entry.NewTlsData;
|
||||
|
||||
for (uint32_t index = 0; index < tls_info.PreviousCount; ++index)
|
||||
{
|
||||
if (is_wow64)
|
||||
{
|
||||
const auto old_entry = c.emu.read_memory<uint32_t>(tls_vector + (index * ptr_size));
|
||||
c.emu.write_memory(new_tls_vector + (index * ptr_size), old_entry);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto old_entry = c.emu.read_memory<uint64_t>(tls_vector + (index * ptr_size));
|
||||
c.emu.write_memory(new_tls_vector + (index * ptr_size), old_entry);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_wow64)
|
||||
{
|
||||
thread.teb32->access([&new_tls_vector](TEB32& teb32) {
|
||||
teb32.ThreadLocalStoragePointer = static_cast<uint32_t>(new_tls_vector);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
teb.ThreadLocalStoragePointer = new_tls_vector;
|
||||
}
|
||||
|
||||
entry.OldTlsData = tls_vector;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user