Skip to content

Commit

Permalink
use DONT_USE_PR_GET_TID_ADDRESS to test tid offset
Browse files Browse the repository at this point in the history
scan in pthread

Signed-off-by: lfbzhm <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed May 5, 2024
1 parent 060d7b8 commit 950ff28
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ task:
chown -R rootless.rootless /home/rootless
# set PATH
echo 'export PATH=/usr/local/go/bin:/usr/local/bin:$PATH' >> /root/.bashrc
# set CGO_CFLAGS
echo 'export CGO_CFLAGS="-DDONT_USE_PR_GET_TID_ADDRESS=1"' >> /root/.bashrc
# Setup ssh localhost for terminal emulation (script -e did not work)
ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""
cat /root/.ssh/id_ed25519.pub >> /root/.ssh/authorized_keys
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
race: ["-race", ""]
criu: ["", "criu-dev"]
dmz: ["", "runc_nodmz"]
tidscan: ["", "-DDONT_USE_PR_GET_TID_ADDRESS=1"]
exclude:
# Disable most of criu-dev jobs, as they are expensive
# (need to compile criu) and don't add much value/coverage.
Expand Down Expand Up @@ -151,6 +152,7 @@ jobs:
- name: build
env:
EXTRA_BUILDTAGS: ${{ matrix.dmz }}
CGO_CFLAGS: ${{ matrix.tidscan }}
run: sudo -E PATH="$PATH" make EXTRA_FLAGS="${{ matrix.race }}" all

- name: install bats
Expand All @@ -162,6 +164,7 @@ jobs:
if: matrix.rootless != 'rootless'
env:
EXTRA_BUILDTAGS: ${{ matrix.dmz }}
CGO_CFLAGS: ${{ matrix.tidscan }}
run: sudo -E PATH="$PATH" -- make TESTFLAGS="${{ matrix.race }}" localunittest

- name: add rootless user
Expand Down
9 changes: 7 additions & 2 deletions libcontainer/nsenter/nsexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ pid_t gettid(void)
# define RUNC_TID_KLUDGE 0
#endif
#if RUNC_TID_KLUDGE
# if !defined(DONT_USE_PR_GET_TID_ADDRESS)
# define DONT_USE_PR_GET_TID_ADDRESS 0
# endif
# define TID_OFFSET_SCAN_MAX 1024
static int tid_offset = 0;

Expand Down Expand Up @@ -392,15 +395,18 @@ static pid_t *find_tls_tid_address(void)
.tid = gettid(),
};

# if !DONT_USE_PR_GET_TID_ADDRESS
if (!prctl(PR_GET_TID_ADDRESS, &tid_addr))
/*
* Make sure the address actually contains the current TID. musl uses a
* different pointer with CLONE_CHILD_CLEARTID, so PR_GET_TID_ADDRESS
* succeeding doesn't mean the address is the one we want.
*/
if (tid_addr && *tid_addr == main_tid.tid) {
goto got_tid_addr;
return tid_addr;
}
# endif
write_log(DEBUG, "let's try using tid scan to find out the tid address in struct thread");

/*
* If we cannot use PR_GET_TID_ADDRESS to get &PTHREAD_SELF->tid, we
Expand Down Expand Up @@ -448,7 +454,6 @@ static pid_t *find_tls_tid_address(void)
tid_addr = (pid_t *) (main_tid.handle + tid_offset);
}

got_tid_addr:
return tid_addr;
}
#endif /* RUNC_TID_KLUDGE */
Expand Down

0 comments on commit 950ff28

Please sign in to comment.