reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
//===-- sanitizer_linux_test.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Tests for sanitizer_linux.h
//
//===----------------------------------------------------------------------===//

#include "sanitizer_common/sanitizer_platform.h"
#if SANITIZER_LINUX

#include "sanitizer_common/sanitizer_linux.h"

#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_file.h"
#include "gtest/gtest.h"

#include <pthread.h>
#include <sched.h>
#include <stdlib.h>

#include <algorithm>
#include <vector>

namespace __sanitizer {

struct TidReporterArgument {
  TidReporterArgument() {
    pthread_mutex_init(&terminate_thread_mutex, NULL);
    pthread_mutex_init(&tid_reported_mutex, NULL);
    pthread_cond_init(&terminate_thread_cond, NULL);
    pthread_cond_init(&tid_reported_cond, NULL);
    terminate_thread = false;
  }

  ~TidReporterArgument() {
    pthread_mutex_destroy(&terminate_thread_mutex);
    pthread_mutex_destroy(&tid_reported_mutex);
    pthread_cond_destroy(&terminate_thread_cond);
    pthread_cond_destroy(&tid_reported_cond);
  }

  tid_t reported_tid;
  // For signaling to spawned threads that they should terminate.
  pthread_cond_t terminate_thread_cond;
  pthread_mutex_t terminate_thread_mutex;
  bool terminate_thread;
  // For signaling to main thread that a child thread has reported its tid.
  pthread_cond_t tid_reported_cond;
  pthread_mutex_t tid_reported_mutex;

 private:
  // Disallow evil constructors
  TidReporterArgument(const TidReporterArgument &);
  void operator=(const TidReporterArgument &);
};

class ThreadListerTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    pthread_t pthread_id;
    tid_t tid;
    for (uptr i = 0; i < kThreadCount; i++) {
      SpawnTidReporter(&pthread_id, &tid);
      pthread_ids_.push_back(pthread_id);
      tids_.push_back(tid);
    }
  }

  virtual void TearDown() {
    pthread_mutex_lock(&thread_arg.terminate_thread_mutex);
    thread_arg.terminate_thread = true;
    pthread_cond_broadcast(&thread_arg.terminate_thread_cond);
    pthread_mutex_unlock(&thread_arg.terminate_thread_mutex);
    for (uptr i = 0; i < pthread_ids_.size(); i++)
      pthread_join(pthread_ids_[i], NULL);
  }

  void SpawnTidReporter(pthread_t *pthread_id, tid_t *tid);

  static const uptr kThreadCount = 20;

  std::vector<pthread_t> pthread_ids_;
  std::vector<tid_t> tids_;

  TidReporterArgument thread_arg;
};

// Writes its TID once to reported_tid and waits until signaled to terminate.
void *TidReporterThread(void *argument) {
  TidReporterArgument *arg = reinterpret_cast<TidReporterArgument *>(argument);
  pthread_mutex_lock(&arg->tid_reported_mutex);
  arg->reported_tid = GetTid();
  pthread_cond_broadcast(&arg->tid_reported_cond);
  pthread_mutex_unlock(&arg->tid_reported_mutex);

  pthread_mutex_lock(&arg->terminate_thread_mutex);
  while (!arg->terminate_thread)
    pthread_cond_wait(&arg->terminate_thread_cond,
                      &arg->terminate_thread_mutex);
  pthread_mutex_unlock(&arg->terminate_thread_mutex);
  return NULL;
}

void ThreadListerTest::SpawnTidReporter(pthread_t *pthread_id, tid_t *tid) {
  pthread_mutex_lock(&thread_arg.tid_reported_mutex);
  thread_arg.reported_tid = -1;
  ASSERT_EQ(0, pthread_create(pthread_id, NULL,
                              TidReporterThread,
                              &thread_arg));
  while (thread_arg.reported_tid == (tid_t)(-1))
    pthread_cond_wait(&thread_arg.tid_reported_cond,
                      &thread_arg.tid_reported_mutex);
  pthread_mutex_unlock(&thread_arg.tid_reported_mutex);
  *tid = thread_arg.reported_tid;
}

static std::vector<tid_t> ReadTidsToVector(ThreadLister *thread_lister) {
  std::vector<tid_t> listed_tids;
  InternalMmapVector<tid_t> threads(128);
  EXPECT_TRUE(thread_lister->ListThreads(&threads));
  return std::vector<tid_t>(threads.begin(), threads.end());
}

static bool Includes(std::vector<tid_t> first, std::vector<tid_t> second) {
  std::sort(first.begin(), first.end());
  std::sort(second.begin(), second.end());
  return std::includes(first.begin(), first.end(),
                       second.begin(), second.end());
}

static bool HasElement(const std::vector<tid_t> &vector, tid_t element) {
  return std::find(vector.begin(), vector.end(), element) != vector.end();
}

// ThreadLister's output should include the current thread's TID and the TID of
// every thread we spawned.
TEST_F(ThreadListerTest, ThreadListerSeesAllSpawnedThreads) {
  tid_t self_tid = GetTid();
  ThreadLister thread_lister(getpid());
  std::vector<tid_t> listed_tids = ReadTidsToVector(&thread_lister);
  ASSERT_TRUE(HasElement(listed_tids, self_tid));
  ASSERT_TRUE(Includes(listed_tids, tids_));
}

TEST_F(ThreadListerTest, DoNotForgetThreads) {
  ThreadLister thread_lister(getpid());

  // Run the loop body twice, because ThreadLister might behave differently if
  // called on a freshly created object.
  for (uptr i = 0; i < 2; i++) {
    std::vector<tid_t> listed_tids = ReadTidsToVector(&thread_lister);
    ASSERT_TRUE(Includes(listed_tids, tids_));
  }
}

// If new threads have spawned during ThreadLister object's lifetime, calling
// relisting should cause ThreadLister to recognize their existence.
TEST_F(ThreadListerTest, NewThreads) {
  ThreadLister thread_lister(getpid());
  std::vector<tid_t> threads_before_extra = ReadTidsToVector(&thread_lister);

  pthread_t extra_pthread_id;
  tid_t extra_tid;
  SpawnTidReporter(&extra_pthread_id, &extra_tid);
  // Register the new thread so it gets terminated in TearDown().
  pthread_ids_.push_back(extra_pthread_id);

  // It would be very bizarre if the new TID had been listed before we even
  // spawned that thread, but it would also cause a false success in this test,
  // so better check for that.
  ASSERT_FALSE(HasElement(threads_before_extra, extra_tid));

  std::vector<tid_t> threads_after_extra = ReadTidsToVector(&thread_lister);
  ASSERT_TRUE(HasElement(threads_after_extra, extra_tid));
}

TEST(SanitizerCommon, SetEnvTest) {
  const char kEnvName[] = "ENV_FOO";
  SetEnv(kEnvName, "value");
  EXPECT_STREQ("value", getenv(kEnvName));
  unsetenv(kEnvName);
  EXPECT_EQ(0, getenv(kEnvName));
}

#if (defined(__x86_64__) || defined(__i386__)) && !SANITIZER_ANDROID
void *thread_self_offset_test_func(void *arg) {
  bool result =
      *(uptr *)((char *)ThreadSelf() + ThreadSelfOffset()) == ThreadSelf();
  return (void *)result;
}

TEST(SanitizerLinux, ThreadSelfOffset) {
  EXPECT_TRUE((bool)thread_self_offset_test_func(0));
  pthread_t tid;
  void *result;
  ASSERT_EQ(0, pthread_create(&tid, 0, thread_self_offset_test_func, 0));
  ASSERT_EQ(0, pthread_join(tid, &result));
  EXPECT_TRUE((bool)result);
}

// libpthread puts the thread descriptor at the end of stack space.
void *thread_descriptor_size_test_func(void *arg) {
  uptr descr_addr = ThreadSelf();
  pthread_attr_t attr;
  pthread_getattr_np(pthread_self(), &attr);
  void *stackaddr;
  size_t stacksize;
  pthread_attr_getstack(&attr, &stackaddr, &stacksize);
  return (void *)((uptr)stackaddr + stacksize - descr_addr);
}

TEST(SanitizerLinux, ThreadDescriptorSize) {
  pthread_t tid;
  void *result;
  ASSERT_EQ(0, pthread_create(&tid, 0, thread_descriptor_size_test_func, 0));
  ASSERT_EQ(0, pthread_join(tid, &result));
  EXPECT_EQ((uptr)result, ThreadDescriptorSize());
}
#endif

TEST(SanitizerCommon, LibraryNameIs) {
  EXPECT_FALSE(LibraryNameIs("", ""));

  char full_name[256];
  const char *paths[] = { "", "/", "/path/to/" };
  const char *suffixes[] = { "", "-linux", ".1.2", "-linux.1.2" };
  const char *base_names[] = { "lib", "lib.0", "lib-i386" };
  const char *wrong_names[] = { "", "lib.9", "lib-x86_64" };
  for (uptr i = 0; i < ARRAY_SIZE(paths); i++)
    for (uptr j = 0; j < ARRAY_SIZE(suffixes); j++) {
      for (uptr k = 0; k < ARRAY_SIZE(base_names); k++) {
        internal_snprintf(full_name, ARRAY_SIZE(full_name), "%s%s%s.so",
                          paths[i], base_names[k], suffixes[j]);
        EXPECT_TRUE(LibraryNameIs(full_name, base_names[k]))
            << "Full name " << full_name
            << " doesn't match base name " << base_names[k];
        for (uptr m = 0; m < ARRAY_SIZE(wrong_names); m++)
          EXPECT_FALSE(LibraryNameIs(full_name, wrong_names[m]))
            << "Full name " << full_name
            << " matches base name " << wrong_names[m];
      }
    }
}

#if defined(__mips64)
// Effectively, this is a test for ThreadDescriptorSize() which is used to
// compute ThreadSelf().
TEST(SanitizerLinux, ThreadSelfTest) {
  ASSERT_EQ(pthread_self(), ThreadSelf());
}
#endif

TEST(SanitizerCommon, StartSubprocessTest) {
  int pipe_fds[2];
  ASSERT_EQ(0, pipe(pipe_fds));
#if SANITIZER_ANDROID
  const char *shell = "/system/bin/sh";
#else
  const char *shell = "/bin/sh";
#endif
  const char *argv[] = {shell, "-c", "echo -n 'hello'", (char *)NULL};
  int pid = StartSubprocess(shell, argv,
                            /* stdin */ kInvalidFd, /* stdout */ pipe_fds[1]);
  ASSERT_GT(pid, 0);

  // wait for process to finish.
  while (IsProcessRunning(pid)) {
  }
  ASSERT_FALSE(IsProcessRunning(pid));

  char buffer[256];
  {
    char *ptr = buffer;
    uptr bytes_read;
    while (ReadFromFile(pipe_fds[0], ptr, 256, &bytes_read)) {
      if (!bytes_read) {
        break;
      }
      ptr += bytes_read;
    }
    ASSERT_EQ(5, ptr - buffer);
    *ptr = 0;
  }
  ASSERT_EQ(0, strcmp(buffer, "hello")) << "Buffer: " << buffer;
  internal_close(pipe_fds[0]);
}

}  // namespace __sanitizer

#endif  // SANITIZER_LINUX