common_runtime_test.cc revision bb9c6b1c55e9e2308b4f5892a398a8837231fdbd
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_runtime_test.h"
18
19#include <dirent.h>
20#include <dlfcn.h>
21#include <fcntl.h>
22#include <ScopedLocalRef.h>
23#include <stdlib.h>
24
25#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
26#include "base/macros.h"
27#include "base/logging.h"
28#include "base/stl_util.h"
29#include "base/stringprintf.h"
30#include "base/unix_file/fd_file.h"
31#include "class_linker.h"
32#include "compiler_callbacks.h"
33#include "dex_file.h"
34#include "gc_root-inl.h"
35#include "gc/heap.h"
36#include "gtest/gtest.h"
37#include "handle_scope-inl.h"
38#include "interpreter/unstarted_runtime.h"
39#include "jni_internal.h"
40#include "mirror/class_loader.h"
41#include "mem_map.h"
42#include "noop_compiler_callbacks.h"
43#include "os.h"
44#include "runtime-inl.h"
45#include "scoped_thread_state_change.h"
46#include "thread.h"
47#include "well_known_classes.h"
48
49int main(int argc, char **argv) {
50  // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
51  // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
52  // everything else. In case you want to see all messages, comment out the line.
53  setenv("ANDROID_LOG_TAGS", "*:e", 1);
54
55  art::InitLogging(argv);
56  LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
57  testing::InitGoogleTest(&argc, argv);
58  return RUN_ALL_TESTS();
59}
60
61namespace art {
62
63ScratchFile::ScratchFile() {
64  // ANDROID_DATA needs to be set
65  CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
66      "Are you subclassing RuntimeTest?";
67  filename_ = getenv("ANDROID_DATA");
68  filename_ += "/TmpFile-XXXXXX";
69  int fd = mkstemp(&filename_[0]);
70  CHECK_NE(-1, fd);
71  file_.reset(new File(fd, GetFilename(), true));
72}
73
74ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
75  filename_ = other.GetFilename();
76  filename_ += suffix;
77  int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
78  CHECK_NE(-1, fd);
79  file_.reset(new File(fd, GetFilename(), true));
80}
81
82ScratchFile::ScratchFile(File* file) {
83  CHECK(file != NULL);
84  filename_ = file->GetPath();
85  file_.reset(file);
86}
87
88ScratchFile::~ScratchFile() {
89  Unlink();
90}
91
92int ScratchFile::GetFd() const {
93  return file_->Fd();
94}
95
96void ScratchFile::Close() {
97  if (file_.get() != nullptr) {
98    if (file_->FlushCloseOrErase() != 0) {
99      PLOG(WARNING) << "Error closing scratch file.";
100    }
101  }
102}
103
104void ScratchFile::Unlink() {
105  if (!OS::FileExists(filename_.c_str())) {
106    return;
107  }
108  Close();
109  int unlink_result = unlink(filename_.c_str());
110  CHECK_EQ(0, unlink_result);
111}
112
113static bool unstarted_initialized_ = false;
114
115CommonRuntimeTest::CommonRuntimeTest() {}
116CommonRuntimeTest::~CommonRuntimeTest() {
117  // Ensure the dex files are cleaned up before the runtime.
118  loaded_dex_files_.clear();
119  runtime_.reset();
120}
121
122void CommonRuntimeTest::SetUpAndroidRoot() {
123  if (IsHost()) {
124    // $ANDROID_ROOT is set on the device, but not necessarily on the host.
125    // But it needs to be set so that icu4c can find its locale data.
126    const char* android_root_from_env = getenv("ANDROID_ROOT");
127    if (android_root_from_env == nullptr) {
128      // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
129      const char* android_host_out = getenv("ANDROID_HOST_OUT");
130      if (android_host_out != nullptr) {
131        setenv("ANDROID_ROOT", android_host_out, 1);
132      } else {
133        // Build it from ANDROID_BUILD_TOP or cwd
134        std::string root;
135        const char* android_build_top = getenv("ANDROID_BUILD_TOP");
136        if (android_build_top != nullptr) {
137          root += android_build_top;
138        } else {
139          // Not set by build server, so default to current directory
140          char* cwd = getcwd(nullptr, 0);
141          setenv("ANDROID_BUILD_TOP", cwd, 1);
142          root += cwd;
143          free(cwd);
144        }
145#if defined(__linux__)
146        root += "/out/host/linux-x86";
147#elif defined(__APPLE__)
148        root += "/out/host/darwin-x86";
149#else
150#error unsupported OS
151#endif
152        setenv("ANDROID_ROOT", root.c_str(), 1);
153      }
154    }
155    setenv("LD_LIBRARY_PATH", ":", 0);  // Required by java.lang.System.<clinit>.
156
157    // Not set by build server, so default
158    if (getenv("ANDROID_HOST_OUT") == nullptr) {
159      setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
160    }
161  }
162}
163
164void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
165  // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
166  if (IsHost()) {
167    const char* tmpdir = getenv("TMPDIR");
168    if (tmpdir != nullptr && tmpdir[0] != 0) {
169      android_data = tmpdir;
170    } else {
171      android_data = "/tmp";
172    }
173  } else {
174    android_data = "/data/dalvik-cache";
175  }
176  android_data += "/art-data-XXXXXX";
177  if (mkdtemp(&android_data[0]) == nullptr) {
178    PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
179  }
180  setenv("ANDROID_DATA", android_data.c_str(), 1);
181}
182
183void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
184  if (fail_on_error) {
185    ASSERT_EQ(rmdir(android_data.c_str()), 0);
186  } else {
187    rmdir(android_data.c_str());
188  }
189}
190
191std::string CommonRuntimeTest::GetCoreArtLocation() {
192  return GetCoreFileLocation("art");
193}
194
195std::string CommonRuntimeTest::GetCoreOatLocation() {
196  return GetCoreFileLocation("oat");
197}
198
199std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
200  std::vector<std::unique_ptr<const DexFile>> dex_files;
201  std::string error_msg;
202  MemMap::Init();
203  if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
204    LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
205    UNREACHABLE();
206  } else {
207    CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
208    return std::move(dex_files[0]);
209  }
210}
211
212void CommonRuntimeTest::SetUp() {
213  SetUpAndroidRoot();
214  SetUpAndroidData(android_data_);
215  dalvik_cache_.append(android_data_.c_str());
216  dalvik_cache_.append("/dalvik-cache");
217  int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
218  ASSERT_EQ(mkdir_result, 0);
219
220  std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
221  std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
222
223
224  RuntimeOptions options;
225  std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
226  options.push_back(std::make_pair(boot_class_path_string, nullptr));
227  options.push_back(std::make_pair("-Xcheck:jni", nullptr));
228  options.push_back(std::make_pair(min_heap_string, nullptr));
229  options.push_back(std::make_pair(max_heap_string, nullptr));
230
231  callbacks_.reset(new NoopCompilerCallbacks());
232
233  SetUpRuntimeOptions(&options);
234
235  // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them.
236  if (callbacks_.get() != nullptr) {
237    options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
238  }
239
240  PreRuntimeCreate();
241  if (!Runtime::Create(options, false)) {
242    LOG(FATAL) << "Failed to create runtime";
243    return;
244  }
245  PostRuntimeCreate();
246  runtime_.reset(Runtime::Current());
247  class_linker_ = runtime_->GetClassLinker();
248  class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
249
250  // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
251  // set up.
252  if (!unstarted_initialized_) {
253    interpreter::UnstartedRuntimeInitialize();
254    unstarted_initialized_ = true;
255  }
256
257  class_linker_->RunRootClinits();
258  boot_class_path_ = class_linker_->GetBootClassPath();
259  java_lang_dex_file_ = boot_class_path_[0];
260
261
262  // Runtime::Create acquired the mutator_lock_ that is normally given away when we
263  // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
264  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
265
266  // We're back in native, take the opportunity to initialize well known classes.
267  WellKnownClasses::Init(Thread::Current()->GetJniEnv());
268
269  // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
270  // pool is created by the runtime.
271  runtime_->GetHeap()->CreateThreadPool();
272  runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption before the test
273  // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
274  runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
275
276  // Get the boot class path from the runtime so it can be used in tests.
277  boot_class_path_ = class_linker_->GetBootClassPath();
278  ASSERT_FALSE(boot_class_path_.empty());
279  java_lang_dex_file_ = boot_class_path_[0];
280}
281
282void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
283  ASSERT_TRUE(dirpath != nullptr);
284  DIR* dir = opendir(dirpath);
285  ASSERT_TRUE(dir != nullptr);
286  dirent* e;
287  struct stat s;
288  while ((e = readdir(dir)) != nullptr) {
289    if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
290      continue;
291    }
292    std::string filename(dirpath);
293    filename.push_back('/');
294    filename.append(e->d_name);
295    int stat_result = lstat(filename.c_str(), &s);
296    ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
297    if (S_ISDIR(s.st_mode)) {
298      ClearDirectory(filename.c_str());
299      int rmdir_result = rmdir(filename.c_str());
300      ASSERT_EQ(0, rmdir_result) << filename;
301    } else {
302      int unlink_result = unlink(filename.c_str());
303      ASSERT_EQ(0, unlink_result) << filename;
304    }
305  }
306  closedir(dir);
307}
308
309void CommonRuntimeTest::TearDown() {
310  const char* android_data = getenv("ANDROID_DATA");
311  ASSERT_TRUE(android_data != nullptr);
312  ClearDirectory(dalvik_cache_.c_str());
313  int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
314  ASSERT_EQ(0, rmdir_cache_result);
315  TearDownAndroidData(android_data_, true);
316
317  // icu4c has a fixed 10-element array "gCommonICUDataArray".
318  // If we run > 10 tests, we fill that array and u_setCommonData fails.
319  // There's a function to clear the array, but it's not public...
320  typedef void (*IcuCleanupFn)();
321  void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
322  CHECK(sym != nullptr) << dlerror();
323  IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
324  (*icu_cleanup_fn)();
325
326  Runtime::Current()->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
327}
328
329std::string CommonRuntimeTest::GetLibCoreDexFileName() {
330  return GetDexFileName("core-libart");
331}
332
333std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
334  if (IsHost()) {
335    const char* host_dir = getenv("ANDROID_HOST_OUT");
336    CHECK(host_dir != nullptr);
337    return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
338  }
339  return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
340}
341
342std::string CommonRuntimeTest::GetTestAndroidRoot() {
343  if (IsHost()) {
344    const char* host_dir = getenv("ANDROID_HOST_OUT");
345    CHECK(host_dir != nullptr);
346    return host_dir;
347  }
348  return GetAndroidRoot();
349}
350
351// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
352#ifdef ART_TARGET
353#ifndef ART_TARGET_NATIVETEST_DIR
354#error "ART_TARGET_NATIVETEST_DIR not set."
355#endif
356// Wrap it as a string literal.
357#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
358#else
359#define ART_TARGET_NATIVETEST_DIR_STRING ""
360#endif
361
362std::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
363  CHECK(name != nullptr);
364  std::string filename;
365  if (IsHost()) {
366    filename += getenv("ANDROID_HOST_OUT");
367    filename += "/framework/";
368  } else {
369    filename += ART_TARGET_NATIVETEST_DIR_STRING;
370  }
371  filename += "art-gtest-";
372  filename += name;
373  filename += ".jar";
374  return filename;
375}
376
377std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
378  std::string filename = GetTestDexFileName(name);
379  std::string error_msg;
380  std::vector<std::unique_ptr<const DexFile>> dex_files;
381  bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
382  CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
383  for (auto& dex_file : dex_files) {
384    CHECK_EQ(PROT_READ, dex_file->GetPermissions());
385    CHECK(dex_file->IsReadOnly());
386  }
387  return dex_files;
388}
389
390std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
391  std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
392  EXPECT_EQ(1U, vector.size());
393  return std::move(vector[0]);
394}
395
396std::vector<const DexFile*> CommonRuntimeTest::GetDexFiles(jobject jclass_loader) {
397  std::vector<const DexFile*> ret;
398
399  ScopedObjectAccess soa(Thread::Current());
400
401  StackHandleScope<4> hs(Thread::Current());
402  Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
403      soa.Decode<mirror::ClassLoader*>(jclass_loader));
404
405  DCHECK_EQ(class_loader->GetClass(),
406            soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader));
407  DCHECK_EQ(class_loader->GetParent()->GetClass(),
408            soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader));
409
410  // The class loader is a PathClassLoader which inherits from BaseDexClassLoader.
411  // We need to get the DexPathList and loop through it.
412  Handle<mirror::ArtField> cookie_field =
413      hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie));
414  Handle<mirror::ArtField> dex_file_field =
415      hs.NewHandle(
416          soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile));
417  mirror::Object* dex_path_list =
418      soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)->
419      GetObject(class_loader.Get());
420  if (dex_path_list != nullptr && dex_file_field.Get() != nullptr &&
421      cookie_field.Get() != nullptr) {
422    // DexPathList has an array dexElements of Elements[] which each contain a dex file.
423    mirror::Object* dex_elements_obj =
424        soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
425        GetObject(dex_path_list);
426    // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
427    // at the mCookie which is a DexFile vector.
428    if (dex_elements_obj != nullptr) {
429      Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
430          hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
431      for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
432        mirror::Object* element = dex_elements->GetWithoutChecks(i);
433        if (element == nullptr) {
434          // Should never happen, fall back to java code to throw a NPE.
435          break;
436        }
437        mirror::Object* dex_file = dex_file_field->GetObject(element);
438        if (dex_file != nullptr) {
439          mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray();
440          DCHECK(long_array != nullptr);
441          int32_t long_array_size = long_array->GetLength();
442          for (int32_t j = 0; j < long_array_size; ++j) {
443            const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
444                long_array->GetWithoutChecks(j)));
445            if (cp_dex_file == nullptr) {
446              LOG(WARNING) << "Null DexFile";
447              continue;
448            }
449            ret.push_back(cp_dex_file);
450          }
451        }
452      }
453    }
454  }
455
456  return ret;
457}
458
459const DexFile* CommonRuntimeTest::GetFirstDexFile(jobject jclass_loader) {
460  std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
461  DCHECK(!tmp.empty());
462  const DexFile* ret = tmp[0];
463  DCHECK(ret != nullptr);
464  return ret;
465}
466
467jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
468  std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
469  std::vector<const DexFile*> class_path;
470  CHECK_NE(0U, dex_files.size());
471  for (auto& dex_file : dex_files) {
472    class_path.push_back(dex_file.get());
473    loaded_dex_files_.push_back(std::move(dex_file));
474  }
475
476  Thread* self = Thread::Current();
477  jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,                                                                                   class_path);
478  self->SetClassLoaderOverride(class_loader);
479  return class_loader;
480}
481
482std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
483  CHECK(suffix != nullptr);
484
485  std::string location;
486  if (IsHost()) {
487    const char* host_dir = getenv("ANDROID_HOST_OUT");
488    CHECK(host_dir != NULL);
489    location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
490  } else {
491    location = StringPrintf("/data/art-test/core.%s", suffix);
492  }
493
494  return location;
495}
496
497CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
498  vm_->SetCheckJniAbortHook(Hook, &actual_);
499}
500
501CheckJniAbortCatcher::~CheckJniAbortCatcher() {
502  vm_->SetCheckJniAbortHook(nullptr, nullptr);
503  EXPECT_TRUE(actual_.empty()) << actual_;
504}
505
506void CheckJniAbortCatcher::Check(const char* expected_text) {
507  EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
508      << "Expected to find: " << expected_text << "\n"
509      << "In the output   : " << actual_;
510  actual_.clear();
511}
512
513void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
514  // We use += because when we're hooking the aborts like this, multiple problems can be found.
515  *reinterpret_cast<std::string*>(data) += reason;
516}
517
518}  // namespace art
519
520namespace std {
521
522template <typename T>
523std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
524os << ::art::ToString(rhs);
525return os;
526}
527
528}  // namespace std
529