1 /*
2  * Copyright (C) 2015 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 #include "compiler_options.h"
18 
19 #include <fstream>
20 #include <string_view>
21 
22 #include "android-base/stringprintf.h"
23 
24 #include "arch/instruction_set.h"
25 #include "arch/instruction_set_features.h"
26 #include "art_method-inl.h"
27 #include "base/runtime_debug.h"
28 #include "base/variant_map.h"
29 #include "class_linker.h"
30 #include "cmdline_parser.h"
31 #include "compiler_options_map-inl.h"
32 #include "dex/dex_file-inl.h"
33 #include "runtime.h"
34 #include "scoped_thread_state_change-inl.h"
35 #include "simple_compiler_options_map.h"
36 
37 namespace art HIDDEN {
38 
CompilerOptions()39 CompilerOptions::CompilerOptions()
40     : compiler_filter_(CompilerFilter::kDefaultCompilerFilter),
41       huge_method_threshold_(kDefaultHugeMethodThreshold),
42       inline_max_code_units_(kUnsetInlineMaxCodeUnits),
43       instruction_set_(kRuntimeISA == InstructionSet::kArm ? InstructionSet::kThumb2 : kRuntimeISA),
44       instruction_set_features_(nullptr),
45       no_inline_from_(),
46       dex_files_for_oat_file_(),
47       image_classes_(),
48       compiler_type_(CompilerType::kAotCompiler),
49       image_type_(ImageType::kNone),
50       multi_image_(false),
51       compile_art_test_(false),
52       emit_read_barrier_(false),
53       baseline_(false),
54       debuggable_(false),
55       generate_debug_info_(kDefaultGenerateDebugInfo),
56       generate_mini_debug_info_(kDefaultGenerateMiniDebugInfo),
57       generate_build_id_(false),
58       implicit_null_checks_(false),
59       implicit_so_checks_(true),
60       implicit_suspend_checks_(false),
61       compile_pic_(false),
62       dump_timings_(false),
63       dump_pass_timings_(false),
64       dump_stats_(false),
65       profile_branches_(false),
66       profile_compilation_info_(nullptr),
67       verbose_methods_(),
68       abort_on_hard_verifier_failure_(false),
69       abort_on_soft_verifier_failure_(false),
70       init_failure_output_(nullptr),
71       dump_cfg_file_name_(""),
72       dump_cfg_append_(false),
73       force_determinism_(false),
74       check_linkage_conditions_(false),
75       crash_on_linkage_violation_(false),
76       deduplicate_code_(true),
77       count_hotness_in_compiled_code_(false),
78       resolve_startup_const_strings_(false),
79       initialize_app_image_classes_(false),
80       check_profiled_methods_(ProfileMethodsCheck::kNone),
81       max_image_block_size_(std::numeric_limits<uint32_t>::max()),
82       passes_to_run_(nullptr) {
83 }
84 
~CompilerOptions()85 CompilerOptions::~CompilerOptions() {
86   // Everything done by member destructors.
87   // The definitions of classes forward-declared in the header have now been #included.
88 }
89 
90 namespace {
91 
92 bool kEmitRuntimeReadBarrierChecks = kIsDebugBuild &&
93     RegisterRuntimeDebugFlag(&kEmitRuntimeReadBarrierChecks);
94 
95 }  // namespace
96 
EmitRunTimeChecksInDebugMode() const97 bool CompilerOptions::EmitRunTimeChecksInDebugMode() const {
98   // Run-time checks (e.g. Marking Register checks) are only emitted in slow-debug mode.
99   return kEmitRuntimeReadBarrierChecks;
100 }
101 
ParseDumpInitFailures(const std::string & option,std::string * error_msg)102 bool CompilerOptions::ParseDumpInitFailures(const std::string& option, std::string* error_msg) {
103   init_failure_output_.reset(new std::ofstream(option));
104   if (init_failure_output_.get() == nullptr) {
105     *error_msg = "Failed to construct std::ofstream";
106     return false;
107   } else if (init_failure_output_->fail()) {
108     *error_msg = android::base::StringPrintf(
109         "Failed to open %s for writing the initialization failures.", option.c_str());
110     init_failure_output_.reset();
111     return false;
112   }
113   return true;
114 }
115 
ParseCompilerOptions(const std::vector<std::string> & options,bool ignore_unrecognized,std::string * error_msg)116 bool CompilerOptions::ParseCompilerOptions(const std::vector<std::string>& options,
117                                            bool ignore_unrecognized,
118                                            std::string* error_msg) {
119   auto parser = CreateSimpleParser(ignore_unrecognized);
120   CmdlineResult parse_result = parser.Parse(options);
121   if (!parse_result.IsSuccess()) {
122     *error_msg = parse_result.GetMessage();
123     return false;
124   }
125 
126   SimpleParseArgumentMap args = parser.ReleaseArgumentsMap();
127   return ReadCompilerOptions(args, this, error_msg);
128 }
129 
IsImageClass(const char * descriptor) const130 bool CompilerOptions::IsImageClass(const char* descriptor) const {
131   // Historical note: We used to hold the set indirectly and there was a distinction between an
132   // empty set and a null, null meaning to include all classes. However, the distinction has been
133   // removed; if we don't have a profile, we treat it as an empty set of classes. b/77340429
134   return image_classes_.find(std::string_view(descriptor)) != image_classes_.end();
135 }
136 
IsPreloadedClass(std::string_view pretty_descriptor) const137 bool CompilerOptions::IsPreloadedClass(std::string_view pretty_descriptor) const {
138   return preloaded_classes_.find(pretty_descriptor) != preloaded_classes_.end();
139 }
140 
ShouldCompileWithClinitCheck(ArtMethod * method) const141 bool CompilerOptions::ShouldCompileWithClinitCheck(ArtMethod* method) const {
142   if (method != nullptr &&
143       Runtime::Current()->IsAotCompiler() &&
144       method->IsStatic() &&
145       !method->IsConstructor() &&
146       // Compiled code for native methods never do a clinit check, so we may put the resolution
147       // trampoline for native methods. This means that it's possible post zygote fork for the
148       // entry to be dirtied. We could resolve this by either:
149       // - Make these methods use the generic JNI entrypoint, but that's not
150       //   desirable for a method that is in the profile.
151       // - Ensure the declaring class of such native methods are always in the
152       //   preloaded-classes list.
153       // - Emit the clinit check in the compiled code of native methods.
154       !method->IsNative()) {
155     ScopedObjectAccess soa(Thread::Current());
156     ObjPtr<mirror::Class> cls = method->GetDeclaringClass<kWithoutReadBarrier>();
157     return cls->IsInBootImageAndNotInPreloadedClasses();
158   }
159   return false;
160 }
161 
162 }  // namespace art
163