Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
fix: #17, Remove PsGetThreadCreateTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroKaku committed Nov 1, 2022
1 parent 370a301 commit 208f1c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
30 changes: 19 additions & 11 deletions src/crt/vcruntime/per_thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
struct __vcrt_ptd_km : public __vcrt_ptd
{
void* tid;
long long create_time;
long long uid;
};

static NPAGED_LOOKASIDE_LIST __vcrt_startup_ptd_pools;
static RTL_AVL_TABLE __vcrt_startup_ptd_table;
static KSPIN_LOCK __vcrt_startup_ptd_table_lock;

static long long __get_thread_uid(_In_ PETHREAD thread)
{
CLIENT_ID id = PsGetThreadClientId(thread);
size_t high = reinterpret_cast<size_t>(id.UniqueProcess) >> 2;
size_t low = reinterpret_cast<size_t>(id.UniqueThread ) >> 2;
return (static_cast<long long>(high & ~uint32_t(0)) << 32) | (static_cast<long long>(low & ~uint32_t(0)));
}

RTL_GENERIC_COMPARE_RESULTS NTAPI __vcrt_ptd_table_compare(
_In_ RTL_AVL_TABLE* /*table*/,
_In_ PVOID first,
Expand Down Expand Up @@ -66,10 +74,10 @@ static __vcrt_ptd* __cdecl store_and_initialize_ptd(__vcrt_ptd* const ptd)
}

// reuse outdated ptd.
if (PsGetThreadCreateTime(PsGetCurrentThread()) != static_cast<__vcrt_ptd_km*>(new_ptd)->create_time)
if (__get_thread_uid(PsGetCurrentThread()) != static_cast<__vcrt_ptd_km*>(new_ptd)->uid)
{
inserted = true;
RtlSecureZeroMemory(new_ptd, sizeof __vcrt_ptd); // not reset pid/create_time
RtlSecureZeroMemory(new_ptd, sizeof __vcrt_ptd); // not reset pid/uid
}

if (inserted)
Expand All @@ -95,8 +103,8 @@ extern "C" bool __cdecl __vcrt_initialize_ptd()
&__vcrt_ptd_table_allocate, &__vcrt_ptd_table_free, &__vcrt_startup_ptd_pools);

__vcrt_ptd_km ptd{};
ptd.tid = PsGetCurrentThreadId();
ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
ptd.tid = PsGetCurrentThreadId();
ptd.uid = __get_thread_uid(PsGetCurrentThread());

if (!store_and_initialize_ptd(&ptd))
{
Expand Down Expand Up @@ -138,8 +146,8 @@ extern "C" __vcrt_ptd* __cdecl __vcrt_getptd_noexit()
__vcrt_ptd* existing_ptd = nullptr;

__vcrt_ptd_km ptd{};
ptd.tid = PsGetCurrentThreadId();
ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
ptd.tid = PsGetCurrentThreadId();
ptd.uid = __get_thread_uid(PsGetCurrentThread());

KLOCK_QUEUE_HANDLE lock_state{};
KeAcquireInStackQueuedSpinLock(&__vcrt_startup_ptd_table_lock, &lock_state);
Expand All @@ -166,8 +174,8 @@ extern "C" __vcrt_ptd* __cdecl __vcrt_getptd_noinit()
__vcrt_ptd* existing_ptd = nullptr;

__vcrt_ptd_km ptd{};
ptd.tid = PsGetCurrentThreadId();
ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
ptd.tid = PsGetCurrentThreadId();
ptd.uid = __get_thread_uid(PsGetCurrentThread());

KLOCK_QUEUE_HANDLE lock_state{};
KeAcquireInStackQueuedSpinLock(&__vcrt_startup_ptd_table_lock, &lock_state);
Expand Down Expand Up @@ -234,8 +242,8 @@ extern "C" void __cdecl __vcrt_freeptd(_Inout_opt_ __vcrt_ptd* const ptd)
// data if one does not already exist.

__vcrt_ptd_km current_ptd{};
current_ptd.tid = PsGetCurrentThreadId();
current_ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
current_ptd.tid = PsGetCurrentThreadId();
current_ptd.uid = __get_thread_uid(PsGetCurrentThread());


__vcrt_ptd* const block_to_free = (ptd == nullptr)
Expand Down
26 changes: 17 additions & 9 deletions src/ucrt/internal/per_thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
struct __acrt_ptd_km : public __acrt_ptd
{
void* tid;
long long create_time;
long long uid;
};

static NPAGED_LOOKASIDE_LIST __acrt_startup_ptd_pools;
static RTL_AVL_TABLE __acrt_startup_ptd_table;
static KSPIN_LOCK __acrt_startup_ptd_table_lock;

static long long __get_thread_uid(_In_ PETHREAD thread)
{
CLIENT_ID id = PsGetThreadClientId(thread);
size_t high = reinterpret_cast<size_t>(id.UniqueProcess) >> 2;
size_t low = reinterpret_cast<size_t>(id.UniqueThread ) >> 2;
return (static_cast<long long>(high & ~uint32_t(0)) << 32) | (static_cast<long long>(low & ~uint32_t(0)));
}

RTL_GENERIC_COMPARE_RESULTS NTAPI __acrt_ptd_table_compare(
_In_ RTL_AVL_TABLE* /*table*/,
_In_ PVOID first,
Expand Down Expand Up @@ -69,10 +77,10 @@ static __acrt_ptd* __cdecl store_and_initialize_ptd(__acrt_ptd* const ptd)
}

// reuse outdated ptd.
if (PsGetThreadCreateTime(PsGetCurrentThread()) != static_cast<__acrt_ptd_km*>(new_ptd)->create_time)
if (__get_thread_uid(PsGetCurrentThread()) != static_cast<__acrt_ptd_km*>(new_ptd)->uid)
{
inserted = true;
RtlSecureZeroMemory(new_ptd, sizeof __acrt_ptd); // not reset pid/create_time
RtlSecureZeroMemory(new_ptd, sizeof __acrt_ptd); // not reset pid/uid
}

return new_ptd;
Expand All @@ -91,8 +99,8 @@ extern "C" bool __cdecl __acrt_initialize_ptd()
&__acrt_ptd_table_allocate, &__acrt_ptd_table_free, &__acrt_startup_ptd_pools);

__acrt_ptd_km ptd{};
ptd.tid = PsGetCurrentThreadId();
ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
ptd.tid = PsGetCurrentThreadId();
ptd.uid = __get_thread_uid(PsGetCurrentThread());

if (!store_and_initialize_ptd(&ptd))
{
Expand Down Expand Up @@ -123,8 +131,8 @@ extern "C" __acrt_ptd* __cdecl __acrt_getptd_noexit()
__acrt_ptd* existing_ptd = nullptr;

__acrt_ptd_km ptd{};
ptd.tid = PsGetCurrentThreadId();
ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
ptd.tid = PsGetCurrentThreadId();
ptd.uid = __get_thread_uid(PsGetCurrentThread());

KLOCK_QUEUE_HANDLE lock_state{};
KeAcquireInStackQueuedSpinLock(&__acrt_startup_ptd_table_lock, &lock_state);
Expand Down Expand Up @@ -159,8 +167,8 @@ extern "C" __acrt_ptd* __cdecl __acrt_getptd()
extern "C" void __cdecl __acrt_freeptd()
{
__acrt_ptd_km current_ptd{};
current_ptd.tid = PsGetCurrentThreadId();
current_ptd.create_time = PsGetThreadCreateTime(PsGetCurrentThread());
current_ptd.tid = PsGetCurrentThreadId();
current_ptd.uid = __get_thread_uid(PsGetCurrentThread());


__acrt_ptd* const block_to_free = &current_ptd;
Expand Down

0 comments on commit 208f1c9

Please sign in to comment.