Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_ |
| 18 | #define ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_ |
| 19 | |
| 20 | #include "gc/heap.h" |
| 21 | #include "gc/space/image_space.h" |
| 22 | #include "instruction_set.h" |
| 23 | #include "runtime.h" |
| 24 | |
| 25 | #include <string> |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class ImplicitCheckOptions { |
| 30 | public: |
| 31 | static constexpr const char* kImplicitChecksOatHeaderKey = "implicit-checks"; |
| 32 | |
| 33 | static std::string Serialize(bool explicit_null_checks, bool explicit_stack_overflow_checks, |
| 34 | bool explicit_suspend_checks) { |
| 35 | char tmp[4]; |
| 36 | tmp[0] = explicit_null_checks ? 'N' : 'n'; |
| 37 | tmp[1] = explicit_stack_overflow_checks ? 'O' : 'o'; |
| 38 | tmp[2] = explicit_suspend_checks ? 'S' : 's'; |
| 39 | tmp[3] = 0; |
| 40 | return std::string(tmp); |
| 41 | } |
| 42 | |
| 43 | static bool Parse(const char* str, bool* explicit_null_checks, |
| 44 | bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) { |
| 45 | if (str != nullptr && str[0] != 0 && str[1] != 0 && str[2] != 0 && |
| 46 | (str[0] == 'n' || str[0] == 'N') && |
| 47 | (str[1] == 'o' || str[1] == 'O') && |
| 48 | (str[2] == 's' || str[2] == 'S')) { |
| 49 | *explicit_null_checks = str[0] == 'N'; |
| 50 | *explicit_stack_overflow_checks = str[1] == 'O'; |
| 51 | *explicit_suspend_checks = str[2] == 'S'; |
| 52 | return true; |
| 53 | } else { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Check whether the given flags are correct with respect to the current runtime and the given |
| 59 | // executable flag. |
| 60 | static bool CheckRuntimeSupport(bool executable, bool explicit_null_checks, |
| 61 | bool explicit_stack_overflow_checks, |
| 62 | bool explicit_suspend_checks, std::string* error_msg) { |
| 63 | if (!executable) { |
| 64 | // Not meant to be run, i.e., either we are compiling or dumping. Just accept. |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | Runtime* runtime = Runtime::Current(); |
| 69 | // We really should have a runtime. |
| 70 | DCHECK_NE(static_cast<Runtime*>(nullptr), runtime); |
| 71 | |
| 72 | if (runtime->GetInstrumentation()->IsForcedInterpretOnly()) { |
| 73 | // We are an interpret-only environment. Ignore the check value. |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | if (runtime->ExplicitNullChecks() != explicit_null_checks || |
| 78 | runtime->ExplicitStackOverflowChecks() != explicit_stack_overflow_checks || |
| 79 | runtime->ExplicitSuspendChecks() != explicit_suspend_checks) { |
| 80 | if (error_msg != nullptr) { |
| 81 | // Create an error message. |
| 82 | |
| 83 | std::ostringstream os; |
| 84 | os << "Explicit check options do not match runtime: "; |
| 85 | os << runtime->ExplicitNullChecks() << " vs " << explicit_null_checks << " | "; |
| 86 | os << runtime->ExplicitStackOverflowChecks() << " vs " << explicit_stack_overflow_checks |
| 87 | << " | "; |
| 88 | os << runtime->ExplicitSuspendChecks() << " vs " << explicit_suspend_checks; |
| 89 | |
| 90 | *error_msg = os.str(); |
| 91 | } |
| 92 | |
| 93 | // Currently we do not create correct images when pre-opting, so the emulator will fail with |
| 94 | // this change. Once the change is in the tree, REMOVE. |
| 95 | if (true) { |
| 96 | // At least try to log it, though. |
| 97 | if (error_msg != nullptr) { |
| 98 | LOG(WARNING) << *error_msg; |
| 99 | } |
| 100 | return true; |
| 101 | } else { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Accepted. |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // Check (and override) the flags depending on current support in the ISA. |
| 111 | // Right now will reset all flags to explicit except on ARM. |
| 112 | static void CheckISASupport(InstructionSet isa, bool* explicit_null_checks, |
| 113 | bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) { |
| 114 | switch (isa) { |
| 115 | case kArm: |
| 116 | case kThumb2: |
| 117 | break; // All checks implemented, leave as is. |
| 118 | |
| 119 | default: // No checks implemented, reset all to explicit checks. |
| 120 | *explicit_null_checks = true; |
| 121 | *explicit_stack_overflow_checks = true; |
| 122 | *explicit_suspend_checks = true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static bool CheckForCompiling(InstructionSet host, InstructionSet target, |
| 127 | bool* explicit_null_checks, bool* explicit_stack_overflow_checks, |
| 128 | bool* explicit_suspend_checks) { |
| 129 | // Check the boot image settings. |
| 130 | Runtime* runtime = Runtime::Current(); |
| 131 | if (runtime != nullptr) { |
| 132 | gc::space::ImageSpace* ispace = runtime->GetHeap()->GetImageSpace(); |
| 133 | if (ispace != nullptr) { |
| 134 | const OatFile* oat_file = ispace->GetOatFile(); |
| 135 | if (oat_file != nullptr) { |
| 136 | const char* v = oat_file->GetOatHeader().GetStoreValueByKey(kImplicitChecksOatHeaderKey); |
| 137 | if (!Parse(v, explicit_null_checks, explicit_stack_overflow_checks, |
| 138 | explicit_suspend_checks)) { |
| 139 | LOG(FATAL) << "Should have been able to parse boot image implicit check values"; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Check the current runtime. |
| 147 | bool cross_compiling = true; |
| 148 | switch (host) { |
| 149 | case kArm: |
| 150 | case kThumb2: |
| 151 | cross_compiling = target != kArm && target != kThumb2; |
| 152 | break; |
| 153 | default: |
| 154 | cross_compiling = host != target; |
| 155 | break; |
| 156 | } |
| 157 | if (!cross_compiling) { |
| 158 | Runtime* runtime = Runtime::Current(); |
| 159 | *explicit_null_checks = runtime->ExplicitNullChecks(); |
| 160 | *explicit_stack_overflow_checks = runtime->ExplicitStackOverflowChecks(); |
| 161 | *explicit_suspend_checks = runtime->ExplicitSuspendChecks(); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | // Give up. |
| 166 | return false; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | } // namespace art |
| 171 | |
| 172 | #endif // ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_ |