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