This is an archive of the discontinued LLVM Phabricator instance.

Avoid getpid call after kill
AcceptedPublic

Authored by captain5050 on Sep 30 2020, 2:28 PM.

Details

Reviewers
dvyukov
Summary

kill maybe used to synchronize with ptrace where PTRACE_SYSCALL will run to the next syscall. By having a getpid call after the kill this may cause certain issues with unit tests running with ptrace and tsan. Record the result of the getpid comparison so that a second getpid call isn't necessary after the real kill call.
Do similar for pthread_kill.

Diff Detail

Event Timeline

captain5050 requested review of this revision.Sep 30 2020, 2:28 PM
captain5050 created this revision.

We are querying own PID, I don't understand how it can be wrong. We definitely did not kill itself if we are still running :)
Please add a test that exposes the problem. Will also help to understand what scenario we are fixing.

Thanks Dmitry, the problem isn't that the getpid is wrong per se, the scenario is:

  • ptraced program self stops with a SIGSTOP either by raise, kill, ..
  • ptracing program attaches... and issues PTRACE_SYSCALL to resume the ptraced program until the next syscall,
  • normally this would be some expected syscall, with tsan it will always be the getpid here.

As we'd like tests to pass with tsan, then we either teach the code to ignore getpid when running with tsan or avoid the interception using assembly - both of which are somewhat ugly workarounds.
While adding a test is attractive the harnessing of a parent/child process plus variants for OS and architecture, mean test code would dwarf what is a somewhat straightforward change.

dvyukov accepted this revision.Oct 1 2020, 11:39 PM

Thanks Dmitry, the problem isn't that the getpid is wrong per se, the scenario is:

  • ptraced program self stops with a SIGSTOP either by raise, kill, ..
  • ptracing program attaches... and issues PTRACE_SYSCALL to resume the ptraced program until the next syscall,
  • normally this would be some expected syscall, with tsan it will always be the getpid here.

As we'd like tests to pass with tsan, then we either teach the code to ignore getpid when running with tsan or avoid the interception using assembly - both of which are somewhat ugly workarounds.
While adding a test is attractive the harnessing of a parent/child process plus variants for OS and architecture, mean test code would dwarf what is a somewhat straightforward change.

I see.
A test for this would help to keep this property in future.

This revision is now accepted and ready to land.Oct 1 2020, 11:39 PM