common_runtime_test.cc 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Ian Rogers's avatar
Ian Rogers committed
17 18 19 20 21 22
#include "common_runtime_test.h"

#include <dirent.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <ScopedLocalRef.h>
23
#include <stdlib.h>
Ian Rogers's avatar
Ian Rogers committed
24 25

#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
Andreas Gampe's avatar
Andreas Gampe committed
26
#include "base/macros.h"
27
#include "base/logging.h"
Ian Rogers's avatar
Ian Rogers committed
28 29 30 31 32 33
#include "base/stl_util.h"
#include "base/stringprintf.h"
#include "base/unix_file/fd_file.h"
#include "class_linker.h"
#include "compiler_callbacks.h"
#include "dex_file.h"
34
#include "gc_root-inl.h"
Ian Rogers's avatar
Ian Rogers committed
35
#include "gc/heap.h"
36
#include "gtest/gtest.h"
Andreas Gampe's avatar
Andreas Gampe committed
37
#include "interpreter/unstarted_runtime.h"
Ian Rogers's avatar
Ian Rogers committed
38 39
#include "jni_internal.h"
#include "mirror/class_loader.h"
40
#include "mem_map.h"
Ian Rogers's avatar
Ian Rogers committed
41 42 43 44 45 46
#include "noop_compiler_callbacks.h"
#include "os.h"
#include "runtime-inl.h"
#include "scoped_thread_state_change.h"
#include "thread.h"
#include "well_known_classes.h"
47 48

int main(int argc, char **argv) {
49 50 51
  // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
  // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
  // everything else. In case you want to see all messages, comment out the line.
52
  setenv("ANDROID_LOG_TAGS", "*:e", 1);
53

54
  art::InitLogging(argv);
Ian Rogers's avatar
Ian Rogers committed
55
  LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
56 57 58
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
Ian Rogers's avatar
Ian Rogers committed
59 60 61 62 63 64 65 66 67 68 69

namespace art {

ScratchFile::ScratchFile() {
  // ANDROID_DATA needs to be set
  CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
      "Are you subclassing RuntimeTest?";
  filename_ = getenv("ANDROID_DATA");
  filename_ += "/TmpFile-XXXXXX";
  int fd = mkstemp(&filename_[0]);
  CHECK_NE(-1, fd);
70
  file_.reset(new File(fd, GetFilename(), true));
Ian Rogers's avatar
Ian Rogers committed
71 72 73 74 75 76 77
}

ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
  filename_ = other.GetFilename();
  filename_ += suffix;
  int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
  CHECK_NE(-1, fd);
78
  file_.reset(new File(fd, GetFilename(), true));
Ian Rogers's avatar
Ian Rogers committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

ScratchFile::ScratchFile(File* file) {
  CHECK(file != NULL);
  filename_ = file->GetPath();
  file_.reset(file);
}

ScratchFile::~ScratchFile() {
  Unlink();
}

int ScratchFile::GetFd() const {
  return file_->Fd();
}

95
void ScratchFile::Close() {
96 97 98 99 100
  if (file_.get() != nullptr) {
    if (file_->FlushCloseOrErase() != 0) {
      PLOG(WARNING) << "Error closing scratch file.";
    }
  }
101 102 103 104 105 106 107
}

void ScratchFile::Unlink() {
  if (!OS::FileExists(filename_.c_str())) {
    return;
  }
  Close();
Ian Rogers's avatar
Ian Rogers committed
108 109 110 111
  int unlink_result = unlink(filename_.c_str());
  CHECK_EQ(0, unlink_result);
}

Andreas Gampe's avatar
Andreas Gampe committed
112 113
static bool unstarted_initialized_ = false;

Ian Rogers's avatar
Ian Rogers committed
114
CommonRuntimeTest::CommonRuntimeTest() {}
115 116 117 118 119
CommonRuntimeTest::~CommonRuntimeTest() {
  // Ensure the dex files are cleaned up before the runtime.
  loaded_dex_files_.clear();
  runtime_.reset();
}
Ian Rogers's avatar
Ian Rogers committed
120

121
void CommonRuntimeTest::SetUpAndroidRoot() {
Ian Rogers's avatar
Ian Rogers committed
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
  if (IsHost()) {
    // $ANDROID_ROOT is set on the device, but not necessarily on the host.
    // But it needs to be set so that icu4c can find its locale data.
    const char* android_root_from_env = getenv("ANDROID_ROOT");
    if (android_root_from_env == nullptr) {
      // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
      const char* android_host_out = getenv("ANDROID_HOST_OUT");
      if (android_host_out != nullptr) {
        setenv("ANDROID_ROOT", android_host_out, 1);
      } else {
        // Build it from ANDROID_BUILD_TOP or cwd
        std::string root;
        const char* android_build_top = getenv("ANDROID_BUILD_TOP");
        if (android_build_top != nullptr) {
          root += android_build_top;
        } else {
          // Not set by build server, so default to current directory
          char* cwd = getcwd(nullptr, 0);
          setenv("ANDROID_BUILD_TOP", cwd, 1);
          root += cwd;
          free(cwd);
        }
#if defined(__linux__)
        root += "/out/host/linux-x86";
#elif defined(__APPLE__)
        root += "/out/host/darwin-x86";
#else
#error unsupported OS
#endif
        setenv("ANDROID_ROOT", root.c_str(), 1);
      }
    }
    setenv("LD_LIBRARY_PATH", ":", 0);  // Required by java.lang.System.<clinit>.

    // Not set by build server, so default
    if (getenv("ANDROID_HOST_OUT") == nullptr) {
      setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
    }
  }
161
}
Ian Rogers's avatar
Ian Rogers committed
162

163
void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
Ian Rogers's avatar
Ian Rogers committed
164
  // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
165 166 167 168 169 170 171 172 173 174 175
  if (IsHost()) {
    const char* tmpdir = getenv("TMPDIR");
    if (tmpdir != nullptr && tmpdir[0] != 0) {
      android_data = tmpdir;
    } else {
      android_data = "/tmp";
    }
  } else {
    android_data = "/data/dalvik-cache";
  }
  android_data += "/art-data-XXXXXX";
Ian Rogers's avatar
Ian Rogers committed
176 177 178 179 180 181
  if (mkdtemp(&android_data[0]) == nullptr) {
    PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
  }
  setenv("ANDROID_DATA", android_data.c_str(), 1);
}

182 183 184 185 186 187 188 189
void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
  if (fail_on_error) {
    ASSERT_EQ(rmdir(android_data.c_str()), 0);
  } else {
    rmdir(android_data.c_str());
  }
}

190 191 192 193 194 195 196
std::string CommonRuntimeTest::GetCoreArtLocation() {
  return GetCoreFileLocation("art");
}

std::string CommonRuntimeTest::GetCoreOatLocation() {
  return GetCoreFileLocation("oat");
}
197

198 199
std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
  std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogers's avatar
Ian Rogers committed
200
  std::string error_msg;
201
  MemMap::Init();
Ian Rogers's avatar
Ian Rogers committed
202 203
  if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
    LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
204
    UNREACHABLE();
Ian Rogers's avatar
Ian Rogers committed
205 206
  } else {
    CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
207
    return std::move(dex_files[0]);
Ian Rogers's avatar
Ian Rogers committed
208 209 210 211
  }
}

void CommonRuntimeTest::SetUp() {
212 213
  SetUpAndroidRoot();
  SetUpAndroidData(android_data_);
Ian Rogers's avatar
Ian Rogers committed
214 215 216 217 218 219 220 221 222 223 224
  dalvik_cache_.append(android_data_.c_str());
  dalvik_cache_.append("/dalvik-cache");
  int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
  ASSERT_EQ(mkdir_result, 0);

  std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
  std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));

  callbacks_.reset(new NoopCompilerCallbacks());

  RuntimeOptions options;
225 226
  std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
  options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogers's avatar
Ian Rogers committed
227
  options.push_back(std::make_pair("-Xcheck:jni", nullptr));
228 229
  options.push_back(std::make_pair(min_heap_string, nullptr));
  options.push_back(std::make_pair(max_heap_string, nullptr));
Ian Rogers's avatar
Ian Rogers committed
230 231
  options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
  SetUpRuntimeOptions(&options);
232

233
  PreRuntimeCreate();
Ian Rogers's avatar
Ian Rogers committed
234 235 236 237
  if (!Runtime::Create(options, false)) {
    LOG(FATAL) << "Failed to create runtime";
    return;
  }
238
  PostRuntimeCreate();
Ian Rogers's avatar
Ian Rogers committed
239 240 241
  runtime_.reset(Runtime::Current());
  class_linker_ = runtime_->GetClassLinker();
  class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
242 243 244

  // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
  // set up.
Andreas Gampe's avatar
Andreas Gampe committed
245 246 247 248
  if (!unstarted_initialized_) {
    interpreter::UnstartedRuntimeInitialize();
    unstarted_initialized_ = true;
  }
249

Ian Rogers's avatar
Ian Rogers committed
250
  class_linker_->RunRootClinits();
251 252 253
  boot_class_path_ = class_linker_->GetBootClassPath();
  java_lang_dex_file_ = boot_class_path_[0];

Ian Rogers's avatar
Ian Rogers committed
254 255 256 257 258 259 260 261 262 263 264 265

  // Runtime::Create acquired the mutator_lock_ that is normally given away when we
  // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
  Thread::Current()->TransitionFromRunnableToSuspended(kNative);

  // We're back in native, take the opportunity to initialize well known classes.
  WellKnownClasses::Init(Thread::Current()->GetJniEnv());

  // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
  // pool is created by the runtime.
  runtime_->GetHeap()->CreateThreadPool();
  runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption before the test
266 267
  // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
  runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
268 269 270 271 272

  // Get the boot class path from the runtime so it can be used in tests.
  boot_class_path_ = class_linker_->GetBootClassPath();
  ASSERT_FALSE(boot_class_path_.empty());
  java_lang_dex_file_ = boot_class_path_[0];
Ian Rogers's avatar
Ian Rogers committed
273 274
}

275 276 277
void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
  ASSERT_TRUE(dirpath != nullptr);
  DIR* dir = opendir(dirpath);
Ian Rogers's avatar
Ian Rogers committed
278 279
  ASSERT_TRUE(dir != nullptr);
  dirent* e;
280
  struct stat s;
Ian Rogers's avatar
Ian Rogers committed
281 282 283 284
  while ((e = readdir(dir)) != nullptr) {
    if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
      continue;
    }
285
    std::string filename(dirpath);
Ian Rogers's avatar
Ian Rogers committed
286 287
    filename.push_back('/');
    filename.append(e->d_name);
288 289 290 291 292 293 294 295 296 297
    int stat_result = lstat(filename.c_str(), &s);
    ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
    if (S_ISDIR(s.st_mode)) {
      ClearDirectory(filename.c_str());
      int rmdir_result = rmdir(filename.c_str());
      ASSERT_EQ(0, rmdir_result) << filename;
    } else {
      int unlink_result = unlink(filename.c_str());
      ASSERT_EQ(0, unlink_result) << filename;
    }
Ian Rogers's avatar
Ian Rogers committed
298 299
  }
  closedir(dir);
300 301 302 303 304 305
}

void CommonRuntimeTest::TearDown() {
  const char* android_data = getenv("ANDROID_DATA");
  ASSERT_TRUE(android_data != nullptr);
  ClearDirectory(dalvik_cache_.c_str());
Ian Rogers's avatar
Ian Rogers committed
306 307
  int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
  ASSERT_EQ(0, rmdir_cache_result);
308
  TearDownAndroidData(android_data_, true);
Ian Rogers's avatar
Ian Rogers committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

  // icu4c has a fixed 10-element array "gCommonICUDataArray".
  // If we run > 10 tests, we fill that array and u_setCommonData fails.
  // There's a function to clear the array, but it's not public...
  typedef void (*IcuCleanupFn)();
  void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
  CHECK(sym != nullptr) << dlerror();
  IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
  (*icu_cleanup_fn)();

  Runtime::Current()->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
}

std::string CommonRuntimeTest::GetLibCoreDexFileName() {
  return GetDexFileName("core-libart");
}

std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
  if (IsHost()) {
    const char* host_dir = getenv("ANDROID_HOST_OUT");
    CHECK(host_dir != nullptr);
    return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
  }
  return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
}

std::string CommonRuntimeTest::GetTestAndroidRoot() {
  if (IsHost()) {
    const char* host_dir = getenv("ANDROID_HOST_OUT");
    CHECK(host_dir != nullptr);
    return host_dir;
  }
  return GetAndroidRoot();
}

Andreas Gampe's avatar
Andreas Gampe committed
344 345 346 347 348 349 350 351 352 353 354
// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
#ifdef ART_TARGET
#ifndef ART_TARGET_NATIVETEST_DIR
#error "ART_TARGET_NATIVETEST_DIR not set."
#endif
// Wrap it as a string literal.
#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
#else
#define ART_TARGET_NATIVETEST_DIR_STRING ""
#endif

355
std::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
Ian Rogers's avatar
Ian Rogers committed
356 357 358 359 360 361
  CHECK(name != nullptr);
  std::string filename;
  if (IsHost()) {
    filename += getenv("ANDROID_HOST_OUT");
    filename += "/framework/";
  } else {
Andreas Gampe's avatar
Andreas Gampe committed
362
    filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogers's avatar
Ian Rogers committed
363 364 365 366
  }
  filename += "art-gtest-";
  filename += name;
  filename += ".jar";
367 368 369 370 371
  return filename;
}

std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
  std::string filename = GetTestDexFileName(name);
Ian Rogers's avatar
Ian Rogers committed
372
  std::string error_msg;
373
  std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogers's avatar
Ian Rogers committed
374 375
  bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
  CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
376
  for (auto& dex_file : dex_files) {
Ian Rogers's avatar
Ian Rogers committed
377 378 379 380 381 382
    CHECK_EQ(PROT_READ, dex_file->GetPermissions());
    CHECK(dex_file->IsReadOnly());
  }
  return dex_files;
}

383 384
std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
  std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogers's avatar
Ian Rogers committed
385
  EXPECT_EQ(1U, vector.size());
386
  return std::move(vector[0]);
Ian Rogers's avatar
Ian Rogers committed
387 388 389
}

jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
390 391
  std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
  std::vector<const DexFile*> class_path;
Ian Rogers's avatar
Ian Rogers committed
392
  CHECK_NE(0U, dex_files.size());
393 394
  for (auto& dex_file : dex_files) {
    class_path.push_back(dex_file.get());
Ian Rogers's avatar
Ian Rogers committed
395
    class_linker_->RegisterDexFile(*dex_file);
396
    loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogers's avatar
Ian Rogers committed
397
  }
398 399 400 401 402 403
  Thread* self = Thread::Current();
  JNIEnvExt* env = self->GetJniEnv();
  ScopedLocalRef<jobject> class_loader_local(env,
      env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
  jobject class_loader = env->NewGlobalRef(class_loader_local.get());
  self->SetClassLoaderOverride(class_loader_local.get());
404
  Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path);
Ian Rogers's avatar
Ian Rogers committed
405 406 407
  return class_loader;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
  CHECK(suffix != nullptr);

  std::string location;
  if (IsHost()) {
    const char* host_dir = getenv("ANDROID_HOST_OUT");
    CHECK(host_dir != NULL);
    location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
  } else {
    location = StringPrintf("/data/art-test/core.%s", suffix);
  }

  return location;
}

Ian Rogers's avatar
Ian Rogers committed
423
CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
424
  vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogers's avatar
Ian Rogers committed
425 426 427
}

CheckJniAbortCatcher::~CheckJniAbortCatcher() {
428
  vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogers's avatar
Ian Rogers committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  EXPECT_TRUE(actual_.empty()) << actual_;
}

void CheckJniAbortCatcher::Check(const char* expected_text) {
  EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
      << "Expected to find: " << expected_text << "\n"
      << "In the output   : " << actual_;
  actual_.clear();
}

void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
  // We use += because when we're hooking the aborts like this, multiple problems can be found.
  *reinterpret_cast<std::string*>(data) += reason;
}

}  // namespace art

namespace std {

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
os << ::art::ToString(rhs);
return os;
}

}  // namespace std