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 #include "instruction_set_features.h"
18 
19 #include <gtest/gtest.h>
20 
21 #ifdef ART_TARGET_ANDROID
22 #include <android-base/properties.h>
23 #endif
24 
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 
28 namespace art {
29 
30 using android::base::StringPrintf;
31 
32 #ifdef ART_TARGET_ANDROID
33 #if defined(__aarch64__)
TEST(InstructionSetFeaturesTest,DISABLED_FeaturesFromSystemPropertyVariant)34 TEST(InstructionSetFeaturesTest, DISABLED_FeaturesFromSystemPropertyVariant) {
35   LOG(WARNING) << "Test disabled due to no CPP define for A53 erratum 835769";
36 #else
37 TEST(InstructionSetFeaturesTest, FeaturesFromSystemPropertyVariant) {
38 #endif
39   // Take the default set of instruction features from the build.
40   std::unique_ptr<const InstructionSetFeatures> instruction_set_features(
41       InstructionSetFeatures::FromCppDefines());
42 
43   // Read the variant property.
44   std::string key = StringPrintf("dalvik.vm.isa.%s.variant", GetInstructionSetString(kRuntimeISA));
45   std::string dex2oat_isa_variant = android::base::GetProperty(key, "");
46   if (!dex2oat_isa_variant.empty()) {
47     // Use features from property to build InstructionSetFeatures and check against build's
48     // features.
49     std::string error_msg;
50     std::unique_ptr<const InstructionSetFeatures> property_features(
51         InstructionSetFeatures::FromVariant(kRuntimeISA, dex2oat_isa_variant, &error_msg));
52     ASSERT_TRUE(property_features.get() != nullptr) << error_msg;
53 
54     EXPECT_TRUE(property_features->HasAtLeast(instruction_set_features.get()))
55       << "System property features: " << *property_features.get()
56       << "\nFeatures from build: " << *instruction_set_features.get();
57   }
58 }
59 
60 #if defined(__aarch64__)
61 TEST(InstructionSetFeaturesTest, DISABLED_FeaturesFromSystemPropertyString) {
62   LOG(WARNING) << "Test disabled due to no CPP define for A53 erratum 835769";
63 #else
64 TEST(InstructionSetFeaturesTest, FeaturesFromSystemPropertyString) {
65 #endif
66   // Take the default set of instruction features from the build.
67   std::unique_ptr<const InstructionSetFeatures> instruction_set_features(
68       InstructionSetFeatures::FromCppDefines());
69 
70   // Read the variant property.
71   std::string variant_key = StringPrintf("dalvik.vm.isa.%s.variant",
72                                          GetInstructionSetString(kRuntimeISA));
73   std::string dex2oat_isa_variant = android::base::GetProperty(variant_key, "");
74   if (!dex2oat_isa_variant.empty()) {
75     // Read the features property.
76     std::string features_key = StringPrintf("dalvik.vm.isa.%s.features",
77                                             GetInstructionSetString(kRuntimeISA));
78     std::string dex2oat_isa_features = android::base::GetProperty(features_key, "");
79     if (!dex2oat_isa_features.empty()) {
80       // Use features from property to build InstructionSetFeatures and check against build's
81       // features.
82       std::string error_msg;
83       std::unique_ptr<const InstructionSetFeatures> base_features(
84           InstructionSetFeatures::FromVariant(kRuntimeISA, dex2oat_isa_variant, &error_msg));
85       ASSERT_TRUE(base_features.get() != nullptr) << error_msg;
86 
87       std::unique_ptr<const InstructionSetFeatures> property_features(
88           base_features->AddFeaturesFromString(dex2oat_isa_features, &error_msg));
89       ASSERT_TRUE(property_features.get() != nullptr) << error_msg;
90 
91       EXPECT_TRUE(property_features->HasAtLeast(instruction_set_features.get()))
92       << "System property features: " << *property_features.get()
93       << "\nFeatures from build: " << *instruction_set_features.get();
94     }
95   }
96 }
97 
98 #if defined(__arm__)
99 TEST(InstructionSetFeaturesTest, DISABLED_FeaturesFromCpuInfo) {
100   LOG(WARNING) << "Test disabled due to buggy ARM kernels";
101 #else
102 TEST(InstructionSetFeaturesTest, FeaturesFromCpuInfo) {
103 #endif
104   // Take the default set of instruction features from the build.
105   std::unique_ptr<const InstructionSetFeatures> instruction_set_features(
106       InstructionSetFeatures::FromCppDefines());
107 
108   // Check we get the same instruction set features using /proc/cpuinfo.
109   std::unique_ptr<const InstructionSetFeatures> cpuinfo_features(
110       InstructionSetFeatures::FromCpuInfo());
111   EXPECT_TRUE(cpuinfo_features->HasAtLeast(instruction_set_features.get()))
112       << "CPU Info features: " << *cpuinfo_features.get()
113       << "\nFeatures from build: " << *instruction_set_features.get();
114 }
115 #endif
116 
117 #ifndef ART_TARGET_ANDROID
118 TEST(InstructionSetFeaturesTest, HostFeaturesFromCppDefines) {
119   std::string error_msg;
120   std::unique_ptr<const InstructionSetFeatures> default_features(
121       InstructionSetFeatures::FromVariant(kRuntimeISA, "default", &error_msg));
122   ASSERT_TRUE(error_msg.empty());
123 
124   std::unique_ptr<const InstructionSetFeatures> cpp_features(
125       InstructionSetFeatures::FromCppDefines());
126   EXPECT_TRUE(cpp_features->HasAtLeast(default_features.get()))
127       << "Default variant features: " << *default_features.get()
128       << "\nFeatures from build: " << *cpp_features.get();
129 }
130 #endif
131 
132 #if defined(__arm__)
133 TEST(InstructionSetFeaturesTest, DISABLED_FeaturesFromHwcap) {
134   LOG(WARNING) << "Test disabled due to buggy ARM kernels";
135 #else
136 TEST(InstructionSetFeaturesTest, FeaturesFromHwcap) {
137 #endif
138   // Take the default set of instruction features from the build.
139   std::unique_ptr<const InstructionSetFeatures> instruction_set_features(
140       InstructionSetFeatures::FromCppDefines());
141 
142   // Check we get the same instruction set features using AT_HWCAP.
143   std::unique_ptr<const InstructionSetFeatures> hwcap_features(
144       InstructionSetFeatures::FromHwcap());
145   EXPECT_TRUE(hwcap_features->HasAtLeast(instruction_set_features.get()))
146       << "Hwcap features: " << *hwcap_features.get()
147       << "\nFeatures from build: " << *instruction_set_features.get();
148 }
149 
150 TEST(InstructionSetFeaturesTest, FeaturesFromAssembly) {
151   // Take the default set of instruction features from the build.
152   std::unique_ptr<const InstructionSetFeatures> instruction_set_features(
153       InstructionSetFeatures::FromCppDefines());
154 
155   // Check we get the same instruction set features using assembly tests.
156   std::unique_ptr<const InstructionSetFeatures> assembly_features(
157       InstructionSetFeatures::FromAssembly());
158   EXPECT_TRUE(assembly_features->HasAtLeast(instruction_set_features.get()))
159       << "Assembly features: " << *assembly_features.get()
160       << "\nFeatures from build: " << *instruction_set_features.get();
161 }
162 
163 }  // namespace art
164