Add tls parameter and flag:CLONE_SETTLS cover for clone and clone3 syscall
PATCH v4 1/1 Add tls parameter and flag:CLONE_SETTLS cover for clone and clone3 syscall
tls parameter and related flag:CLONE_SETTLS are missed in the testing, so add them into existed test case
Signed-off-by chunfuwen <[email protected]>
---
Changes in v4:
- remove riscv and loongarch definition
Changes in v3:
- fix missing head file for x86
Changes in v2:
- create separate files for clone and clone3
--- testcases/kernel/syscalls/clone/clone10.c | 167 ++++++++++++++++++++ testcases/kernel/syscalls/clone3/clone304.c | 167 ++++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 testcases/kernel/syscalls/clone/clone10.c create mode 100644 testcases/kernel/syscalls/clone3/clone304.c
testcases/kernel/syscalls/clone/clone10.c new file +167 -0
@@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Red Hat Inc. All Rights Reserved. +* Author: Chunfu Wen <[email protected]> +*/ + +/*\ +* Add tls parameter and flag:CLONE_SETTLS cover for clone +*/ + +#define _GNU_SOURCE +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <sched.h> +#include <sys/wait.h> +#if defined(__i386__) +#include <asm/ldt.h> +#endif + +#include "tst_test.h" +#include "clone_platform.h" +#include "lapi/syscalls.h" +#include "lapi/futex.h" + +#define TLS_SIZE 4096 +#define TLS_ALIGN 16 + +static pid_t ptid, ctid; +static void *tls_ptr; +static void *child_stack; +static struct user_desc *tls_desc; + +/* TLS variable to validate in child */ +static __threadint tls_test_var =12345 ; + +static int test_flags[] = { +CLONE_SETTLS , +CLONE_PARENT |CLONE_SETTLS , +CLONE_CHILD_SETTID |CLONE_SETTLS , +CLONE_PARENT_SETTID |CLONE_VM |CLONE_SETTLS , +CLONE_THREAD |CLONE_SIGHAND |CLONE_VM |CLONE_CHILD_CLEARTID |CLONE_SETTLS |SIGCHLD , +CLONE_VM |CLONE_FS |CLONE_FILES |CLONE_VFORK |CLONE_SIGHAND |CLONE_SETTLS , +}; + +static void init_tls (void ) +{ +#if defined(__x86_64__) +/* x86-64: tls_ptr is the new %fs base address */ + tls_ptr =aligned_alloc (TLS_ALIGN ,TLS_SIZE ); +if (!tls_ptr) +tst_brk (TBROK |TERRNO ,"aligned_alloc for TLS failed" ); +memset (tls_ptr,0 ,TLS_SIZE ); + +#elif defined(__i386__) +/* x86 32-bit: TLS is a struct user_desc */ + tls_desc =SAFE_MALLOC (sizeof (*tls_desc)); +memset (tls_desc,0 ,sizeof (*tls_desc)); + tls_desc->entry_number =-1 ; + tls_desc->base_addr = (unsigned long )SAFE_MALLOC (TLS_SIZE ); + tls_desc->limit =TLS_SIZE ; + tls_desc->seg_32bit =1 ; + tls_desc->contents =0 ; + tls_desc->read_exec_only =0 ; + tls_desc->limit_in_pages =0 ; + tls_desc->seg_not_present =0 ; + tls_desc->useable =1 ; + tls_ptr = tls_desc; +#elif defined(__aarch64__) || defined(__powerpc64__) || defined(__s390x__) +/* +* Other architectures: detect if dedicated TLS register is available. +* You don't manually touch TPIDR_EL0. +* The kernel automatically writes it into the TPIDR_EL0 register for the new thread after clone() succeeds. +*/ + tls_ptr =aligned_alloc (TLS_ALIGN ,TLS_SIZE ); +if (!tls_ptr) +tst_brk (TBROK |TERRNO ,"aligned_alloc for TLS failed" ); +memset (tls_ptr,0 ,TLS_SIZE ); +#else +tst_brk (TCONF ,"This architecture does not support TLS" ); +#endif +} + +static void free_tls (void ) +{ +#if defined(__x86_64__) +free (tls_ptr); + +#elif defined(__i386__) +if (tls_desc) { +free ((void *)(uintptr_t )tls_desc->base_addr); +free (tls_desc); + } + +#elif defined(__aarch64__) || defined(__powerpc64__) || defined(__s390x__) +free (tls_ptr); +#endif +} + +static void setup (void ) +{ + child_stack =SAFE_MALLOC (CHILD_STACK_SIZE ); +init_tls (); +} + +static void cleanup (void ) +{ +free (child_stack); +free_tls (); +} + +static int check_child(void *arg LTP_ATTRIBUTE_UNUSED) +{ +if (tls_test_var ==12345 ) +tst_res (TPASS ,"Child TLS variable has expected value" ); +else +tst_res (TFAIL ,"Child TLS variable corrupted or not set" ); + +tst_syscall (__NR_exit,0 ); +return 0 ; +} + +static long clone_child (int flags) +{ +TEST (ltp_clone7 (flags, check_child,NULL ,CHILD_STACK_SIZE , + child_stack, &ptid, tls_ptr, &ctid)); + +if (TST_RET ==-1 &&TTERRNO ==ENOSYS ) +tst_brk (TCONF ,"clone parameters doesn't match" ); + +if (TST_RET ==-1 ) +tst_brk (TBROK |TTERRNO ,"clone() failed" ); + +return TST_RET ; +} + +static void verify_tls (unsigned int i) +{ +pid_t child; + ctid =-1 ; +struct timespec timeout = {5 /* sec */ ,0 }; + child =SAFE_FORK (); +if (!child) { +tst_syscall (__NR_getpid); +clone_child (test_flags[i]); +if (test_flags[i] &CLONE_THREAD ) { +if (syscall (SYS_futex, &ctid,FUTEX_WAIT ,-1 , &timeout)) { +if (errno !=EWOULDBLOCK || ctid ==-1 ) { +tst_res (TFAIL |TERRNO , +"futex failed, ctid: %d" , ctid); +exit (0 ); + } + } + } +exit (0 ); + } +sleep (1 ); +tst_reap_children (); +} + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (test_flags), + .test = verify_tls, + .setup = setup, + .cleanup = cleanup, + .forks_child =1 +};
testcases/kernel/syscalls/clone3/clone304.c new file +167 -0
167 lines not on this page · raw message
Signature · 5 lines
2.43.5 -- Mailing list info: https://lists.linux.it/listinfo/ltp