>From 1a3f40d4634ee71d7b426ffcd02b85aaab0e9a48 Mon Sep 17 00:00:00 2001 From: Changping Yu Date: Wed, 24 Jun 2020 17:17:25 +0800 Subject: [PATCH v4 1/2] kernel32/tests: Add test suite to modify the thread order of toolhelp. v4: add a strict judgment test to the order Some software incorrectly uses the way to get the main thread id. Coincidentally, the threads of wine and windows return differently. The list of threads returned by flipping can be consistent with windows. --- dlls/kernel32/tests/toolhelp.c | 100 +++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/dlls/kernel32/tests/toolhelp.c b/dlls/kernel32/tests/toolhelp.c index 9250f8dc54..36d0d6beab 100644 --- a/dlls/kernel32/tests/toolhelp.c +++ b/dlls/kernel32/tests/toolhelp.c @@ -41,6 +41,8 @@ static BOOL (WINAPI *pThread32Next)(HANDLE, LPTHREADENTRY32); /* 1 minute should be more than enough */ #define WAIT_TIME (60 * 1000) +/* Specify the number of simultaneous threads to test */ +#define NUM_THREADS 4 static DWORD WINAPI sub_thread(void* pmt) { @@ -150,6 +152,103 @@ static void test_process(DWORD curr_pid, DWORD sub_pcs_pid) ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n"); } +static DWORD WINAPI get_id_thread(void* curr_pid) +{ + HANDLE hSnapshot; + THREADENTRY32 te; + HANDLE ev, threads[NUM_THREADS]; + DWORD thread_ids[NUM_THREADS]; + DWORD thread_traversed[NUM_THREADS]; + DWORD tid, first_tid = 0; + BOOL found = FALSE; + int matched_idx = -1; + + ev = CreateEventW(NULL, FALSE, FALSE, NULL); + ok(ev != NULL, "Cannot create event\n"); + + for (int i = 0; i < NUM_THREADS; i++) + { + threads[i] = CreateThread(NULL, 0, sub_thread, ev, 0, &tid); + ok(threads[i] != NULL, "Cannot create thread\n"); + thread_ids[i] = tid; + } + + hSnapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + ok(hSnapshot != NULL, "Cannot create snapshot\n"); + + /* check that this current process is enumerated */ + te.dwSize = sizeof(te); + ok(pThread32First(hSnapshot, &te), "Thread cannot traverse\n"); + do + { + if (found) + { + if (te.th32OwnerProcessID != (DWORD)curr_pid) break; + + if (matched_idx >= 0) + { + thread_traversed[matched_idx++] = te.th32ThreadID; + if (matched_idx >= NUM_THREADS) break; + } + else if (thread_ids[0] == te.th32ThreadID){ + matched_idx = 0; + thread_traversed[matched_idx++] = te.th32ThreadID; + } + } + else if (te.th32OwnerProcessID == (DWORD)curr_pid) + { + found = TRUE; + first_tid = te.th32ThreadID; + } + } + while (pThread32Next(hSnapshot, &te)); + + ok(found, "couldn't find self and/or sub-process in process list\n"); + + /* Check if the thread order is strictly consistent */ + found = FALSE; + for (int i = 0; i < NUM_THREADS; i++) + { + if (thread_traversed[i] != thread_ids[i]) + { + found = TRUE; + break; + } + } + + todo_wine + ok(found==FALSE, "The thread order is not strictly consistent\n"); + + SetEvent(ev); + for (int i = 0; i < NUM_THREADS; i++) + CloseHandle(threads[i]); + CloseHandle(ev); + CloseHandle(hSnapshot); + + return first_tid; +} + +static void test_main_thread(DWORD curr_pid, DWORD main_tid) +{ + HANDLE thread; + DWORD tid, first_tid = 0; + int error; + + /* check that the main thread id is first one in this thread. */ + first_tid = get_id_thread((void *)curr_pid); + todo_wine + ok(first_tid == main_tid, "check main thread is error, main is %d, %d\n", main_tid, first_tid); + + /* check that the main thread id is first one in other thread. */ + thread = CreateThread(NULL, 0, get_id_thread, (void *)curr_pid, 0, &tid); + error = WaitForSingleObject(thread, 10000); + ok(error == WAIT_OBJECT_0, "Thread did not complete within timelimit\n"); + + ok(GetExitCodeThread(thread, &first_tid), "Could not retrieve ext code\n"); + todo_wine + ok(first_tid == main_tid, "check main thread is error, main is %d, %d\n", main_tid, first_tid); +} + static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid) { HANDLE hSnapshot; @@ -339,6 +438,7 @@ START_TEST(toolhelp) test_process(pid, info.dwProcessId); test_thread(pid, info.dwProcessId); + test_main_thread(pid, GetCurrentThreadId()); test_module(pid, curr_expected_modules, ARRAY_SIZE(curr_expected_modules)); test_module(info.dwProcessId, sub_expected_modules, ARRAY_SIZE(sub_expected_modules)); -- 2.27.0.windows.1