summaryrefslogtreecommitdiffstats
path: root/runtime/implicit_check_options.h
blob: a6595b88e0b0d0e0e37d9f481ae9e8778a42d67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_
#define ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_

#include "gc/heap.h"
#include "gc/space/image_space.h"
#include "instruction_set.h"
#include "runtime.h"

#include <string>

namespace art {

class ImplicitCheckOptions {
 public:
  static constexpr const char* kImplicitChecksOatHeaderKey = "implicit-checks";

  static std::string Serialize(bool explicit_null_checks, bool explicit_stack_overflow_checks,
                               bool explicit_suspend_checks) {
    char tmp[4];
    tmp[0] = explicit_null_checks ? 'N' : 'n';
    tmp[1] = explicit_stack_overflow_checks ? 'O' : 'o';
    tmp[2] = explicit_suspend_checks ? 'S' : 's';
    tmp[3] = 0;
    return std::string(tmp);
  }

  static bool Parse(const char* str, bool* explicit_null_checks,
                    bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) {
    if (str != nullptr && str[0] != 0 && str[1] != 0 && str[2] != 0 &&
        (str[0] == 'n' || str[0] == 'N') &&
        (str[1] == 'o' || str[1] == 'O') &&
        (str[2] == 's' || str[2] == 'S')) {
      *explicit_null_checks = str[0] == 'N';
      *explicit_stack_overflow_checks = str[1] == 'O';
      *explicit_suspend_checks = str[2] == 'S';
      return true;
    } else {
      return false;
    }
  }

  // Check whether the given flags are correct with respect to the current runtime and the given
  // executable flag.
  static bool CheckRuntimeSupport(bool executable, bool explicit_null_checks,
                                  bool explicit_stack_overflow_checks,
                                  bool explicit_suspend_checks, std::string* error_msg) {
    if (!executable) {
      // Not meant to be run, i.e., either we are compiling or dumping. Just accept.
      return true;
    }

    Runtime* runtime = Runtime::Current();
    // We really should have a runtime.
    DCHECK_NE(static_cast<Runtime*>(nullptr), runtime);

    if (runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
      // We are an interpret-only environment. Ignore the check value.
      return true;
    }

    if (runtime->ExplicitNullChecks() != explicit_null_checks ||
        runtime->ExplicitStackOverflowChecks() != explicit_stack_overflow_checks ||
        runtime->ExplicitSuspendChecks() != explicit_suspend_checks) {
      if (error_msg != nullptr) {
        // Create an error message.

        std::ostringstream os;
        os << "Explicit check options do not match runtime: ";
        os << runtime->ExplicitNullChecks() << " vs " << explicit_null_checks << " | ";
        os << runtime->ExplicitStackOverflowChecks() << " vs " << explicit_stack_overflow_checks
            << " | ";
        os << runtime->ExplicitSuspendChecks() << " vs " << explicit_suspend_checks;

        *error_msg = os.str();
      }

      // Currently we do not create correct images when pre-opting, so the emulator will fail with
      // this change. Once the change is in the tree, REMOVE.
      if (true) {
        // At least try to log it, though.
        if (error_msg != nullptr) {
          LOG(WARNING) << *error_msg;
        }
        return true;
      } else {
        return false;
      }
    }

    // Accepted.
    return true;
  }

  // Check (and override) the flags depending on current support in the ISA.
  // Right now will reset all flags to explicit except on ARM.
  static void CheckISASupport(InstructionSet isa, bool* explicit_null_checks,
                              bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) {
    switch (isa) {
      case kArm:
      case kThumb2:
        break;  // All checks implemented, leave as is.

      default:  // No checks implemented, reset all to explicit checks.
        *explicit_null_checks = true;
        *explicit_stack_overflow_checks = true;
        *explicit_suspend_checks = true;
    }
  }

  static bool CheckForCompiling(InstructionSet host, InstructionSet target,
                                bool* explicit_null_checks, bool* explicit_stack_overflow_checks,
                                bool* explicit_suspend_checks) {
    // Check the boot image settings.
    Runtime* runtime = Runtime::Current();
    if (runtime != nullptr) {
      gc::space::ImageSpace* ispace = runtime->GetHeap()->GetImageSpace();
      if (ispace != nullptr) {
        const OatFile* oat_file = ispace->GetOatFile();
        if (oat_file != nullptr) {
          const char* v = oat_file->GetOatHeader().GetStoreValueByKey(kImplicitChecksOatHeaderKey);
          if (!Parse(v, explicit_null_checks, explicit_stack_overflow_checks,
                     explicit_suspend_checks)) {
            LOG(FATAL) << "Should have been able to parse boot image implicit check values";
          }
          return true;
        }
      }
    }

    // Check the current runtime.
    bool cross_compiling = true;
    switch (host) {
      case kArm:
      case kThumb2:
        cross_compiling = target != kArm && target != kThumb2;
        break;
      default:
        cross_compiling = host != target;
        break;
    }
    if (!cross_compiling) {
      Runtime* runtime = Runtime::Current();
      *explicit_null_checks = runtime->ExplicitNullChecks();
      *explicit_stack_overflow_checks = runtime->ExplicitStackOverflowChecks();
      *explicit_suspend_checks = runtime->ExplicitSuspendChecks();
      return true;
    }

    // Give up.
    return false;
  }
};

}  // namespace art

#endif  // ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_