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