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