[TLS] Kept x86 TCB self consistent with pthread_self() (#4112)

This commit is contained in:
Yang Liu authored and GitHub committed 2026-07-24 20:51:14 +02:00
1 parent f4ee1c1b53
commit 478972a34b
3 files changed
+21 -5

No files matched your search

+8 -5
View File
@@ -2,6 +2,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
@@ -51,11 +52,11 @@ uint32_t my_set_thread_area_32(x64emu_t* emu, thread_area_32_t* td)
int isempty = 0;
// first, check if the "user_desc", here td, is "empty"
if(td->read_exec_only==1 && td->seg_not_present==1)
if( !td->base_addr
if( !td->base_addr
&& !td->limit
&& !td->seg_32bit
&& !td->contents
&& !td->limit_in_pages
&& !td->seg_32bit
&& !td->contents
&& !td->limit_in_pages
&& !td->useable)
isempty = 1;
int idx = td->entry_number;
@@ -297,7 +298,9 @@ static tlsdatasize_t* setupTLSData(box64context_t* context)
memcpy((void*)((uintptr_t)ptr+0x28), context->canary, sizeof(void*)); // put canary in place
uintptr_t tlsptr = (uintptr_t)ptr;
memcpy((void*)((uintptr_t)ptr+0x0), &tlsptr, sizeof(void*));
memcpy((void*)((uintptr_t)ptr+0x10), &tlsptr, sizeof(void*)); // set tcb and self same address
// Wrapped pthread symbols expose native pthread_t handles, so keep the fast self slot consistent.
uintptr_t pthread_self_value = (uintptr_t)pthread_self();
memcpy((void*)((uintptr_t)ptr+0x10), &pthread_self_value, sizeof(void*));
uintptr_t dtp = (uintptr_t)ptr+POS_TLS;
memcpy((void*)(tlsptr+sizeof(void*)), &dtp, sizeof(void*));
if(dtssize) {
BIN
View File
Binary file not shown.
+13
View File
@@ -1,5 +1,6 @@
#define _MULTI_THREADED
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
@@ -40,11 +41,23 @@ typedef struct {
int data2;
} threadparm_t;
static uintptr_t fast_pthread_self(void)
{
uintptr_t self;
__asm__ volatile("movq %%fs:0x10, %0" : "=r"(self));
return self;
}
void *thread_run(void *parm)
{
int rc;
threadparm_t *gData;
if (fast_pthread_self() != (uintptr_t)pthread_self()) {
fprintf(stderr, "TLS self pointer does not match pthread_self()\n");
exit(1);
}
pthread_mutex_lock(mutex_ptr);
if (pthread_self()==thread[0]) {
printf("Thread 1: Entered (%d/%d)\n", TLS_data1, TLS_data2);