Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] gettid() optimization #2024

Closed
vvb2060 opened this issue May 28, 2024 · 2 comments
Closed

[BUG] gettid() optimization #2024

vvb2060 opened this issue May 28, 2024 · 2 comments
Assignees
Labels

Comments

@vvb2060
Copy link

vvb2060 commented May 28, 2024

Description

#include <stdio.h>
#include <unistd.h>
#include <syscall.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
    printf("p getpid()=%d gettid()=%d\n", getpid(), gettid());
    int pid = fork();
    if (pid == 0) {
        printf("getpid()=%d gettid()=%d NR_getpid=%ld NR_gettid=%ld\n", getpid(), gettid(),
               syscall(__NR_getpid), syscall(__NR_gettid));
    } else if (pid > 0) {
        waitpid(pid, nullptr, 0);
    }
    return 0;
}

release build and exec

p getpid()=22023 gettid()=22023
getpid()=22024 gettid()=22023 NR_getpid=22024 NR_gettid=22024

decompile

__int64 sub_888()
{
  unsigned int v0; // w20
  unsigned int v1; // w19
  int v2; // w0
  unsigned int v3; // w20
  __int64 v4; // x21
  __int64 v5; // x0

  v0 = getpid();
  v1 = gettid();
  printf("p getpid()=%d gettid()=%d\n", v0, v1);
  v2 = fork();
  if ( v2 )
  {
    if ( v2 >= 1 )
      waitpid(v2, 0LL, 0);
  }
  else
  {
    v3 = getpid();
    v4 = syscall(172LL);
    v5 = syscall(178LL);
    printf("getpid()=%d gettid()=%d NR_getpid=%ld NR_gettid=%ld\n", v3, v1, v4, v5);
  }
  return 0LL;
}

Affected versions

r27

Canary version

No response

Host OS

Windows

Host OS version

Windows10

Affected ABIs

arm64-v8a

Build system

ndk-build

Other build system

No response

minSdkVersion

23

Device API level

34

@vvb2060 vvb2060 added the bug label May 28, 2024
@enh-google
Copy link
Collaborator

yeah, the __attribute__((__const__)) on gettid() is unsafe in any function that calls fork() or equivalent. fixed by https://android-review.googlesource.com/c/platform/bionic/+/3106497.

@enh-google enh-google self-assigned this May 28, 2024
@github-project-automation github-project-automation bot moved this to Unconfirmed in NDK r27 May 28, 2024
@DanAlbert
Copy link
Member

Since I still need to pull an updated toolchain for r27 RC 1, I'll do another sysroot update to grab this as well.

hubot pushed a commit to aosp-mirror/platform_bionic that referenced this issue May 29, 2024
Bug: android/ndk#2024
Change-Id: Ia264ce219fac99e8823e60c4a2e0607af8518056
@DanAlbert DanAlbert moved this from Unconfirmed to Needs prebuilt update in NDK r27 May 29, 2024
@github-project-automation github-project-automation bot moved this from Needs prebuilt update to Merged in NDK r27 Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Merged
Development

No branches or pull requests

3 participants