Skip to content

tsd_state_set coredump while create a new thread #2804

Open

Description

@MrqiangBig

I found a cretain tsd_state_ste coredump scene at debug mode(while assert not work in release mod) ,the code like this:
`pthread_key_t key[5];

static void test_exit(void *arg)
{
uint32_t num = *(uint32_t *)arg;

(*(uint32_t *)arg)++;
pthread_setspecific(key[num + 1], arg);

}

static void test_exit1(void *arg)
{
uint32_t num = *(uint32_t *)arg;

(*(uint32_t *)arg)++;
pthread_setspecific(key[num + 1], arg);

}
static void test_exit2(void *arg)
{
uint32_t num = *(uint32_t *)arg;

(*(uint32_t *)arg)++;
pthread_setspecific(key[num + 1], arg);

}
static void test_exit3(void *arg)
{
uint32_t num = *(uint32_t *)arg;

printf("test_exit333 %u\n", num); /*tigger malloc*/

(*(uint32_t *)arg)++;
pthread_setspecific(key[num + 1], arg);

}
static void test_exit4(void *arg)
{
uint32_t num = *(uint32_t *)arg;

(*(uint32_t *)arg)++;

}
static void *testcase1(void *arg)
{
int i;

uint32_t num = *(uint32_t *)arg;
pthread_setspecific(key[num], arg);
return NULL;

}
static void *testcase2(void *arg)
{
int i;

void *p = malloc(1024ull * 1024 * 1024 * 90);
if (NULL != p) {
    printf("%p\n", p);
}
return NULL;

}

int main(int argc, char *argv[])
{
pthread_t trd;
uint32_t num = 0;

pthread_key_create(&key[4], test_exit4);
pthread_key_create(&key[3], test_exit3);
pthread_key_create(&key[2], test_exit2);
pthread_key_create(&key[1], test_exit1);
pthread_key_create(&key[0], test_exit);

pthread_create(&trd, NULL, testcase1, &num);
pthread_join(trd, NULL);
pthread_create(&trd, NULL, testcase2, &num);
pthread_join(trd, NULL);
return 0;

}
`

The result is

Image

The reason is that while thread exit,the nptl_deallocate_tsd will do PTHREAD_DESTRUCTOR_ITERATIONS(4) cycles; we trigger malloc at cycle 4,tsd tls will add to list,but will not cleanup due to the above mechanism。

When this thread exit done,we create a new thread again, the new thread will use the stackaddr due to nptl thread cache. While last thread tls not cleanup,it is in list now,so at this time, tsd add assert.

But I hava no idea to slove this problem, does anyone else can slove this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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