Can a posix thread modify another thread's stack variable within the same program ?


  • 0
    M

    Is it possible for one thread to corrupt the stack of another thread by directly modifying the stack address ? How are thread local variables implemented to provide access safety ?


  • 0
    M
    This post is deleted!

  • 0
    J

    of course.
    But the child threads stack is different from main thread.
    Child Threads' statck consists of the VMA got from mmap. And the it's protected by mprotect(NONE) when segmentfault
    main thread's stack is protected by kernel when segmentfault.


  • 0

    Nope, all threads share a common heap, however, each thread has a private stack, and a red zone is appended for each stack


  • 0
    R

    Since each thread has individual stack space, it is not possible to modify another thread's stack variable. Each stack is protected by a boundary and it also stores the start address, current stack pointer and few other information-variables location at the edge. which protects the another thread's stackoverflow modifying stack variables.


  • 0
    S

    Yes, a thread can modify the stack contents of another thread if they are in the same process. Stack is simply a region of memory in the process's address space and all threads of that process have same access rights to all its memory regions.
    For TLS, the compiler places all TLS variables in .tdata section. For each thread, the .tdata is copied to a new memory location. When threads switch, the .tdata is also switched.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.