1 /*
2  * Copyright (C) 2023 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 <android/api-level.h>
18 #include <android-base/properties.h>
19 #include <gtest/gtest.h>
20 #include <vintf/VintfObject.h>
21 
22 using android::vintf::VintfObject;
23 using android::vintf::RuntimeInfo;
24 
get_config(const std::map<std::string,std::string> & configs,const std::string & key)25 std::optional<std::string> get_config(
26     const std::map<std::string, std::string>& configs, const std::string& key) {
27   auto it = configs.find(key);
28   if (it == configs.end()) {
29     return std::nullopt;
30   }
31   return it->second;
32 }
33 
34 // Returns true if the device has the specified feature.
deviceSupportsFeature(const char * feature)35 static bool deviceSupportsFeature(const char* feature) {
36   bool device_supports_feature = false;
37   FILE* p = popen("pm list features", "re");
38   if (p) {
39     char* line = NULL;
40     size_t len = 0;
41     while (getline(&line, &len, p) > 0) {
42       if (strstr(line, feature)) {
43         device_supports_feature = true;
44         break;
45       }
46     }
47     if (line) {
48       free(line);
49       line = NULL;
50     }
51     pclose(p);
52   }
53   return device_supports_feature;
54 }
55 
isTV()56 static bool isTV() {
57   return deviceSupportsFeature("android.software.leanback");
58 }
59 
60 /*
61  * Tests that the kernel in use is meant to run on CPUs that support a
62  * 64-bit Instruction Set Architecture (ISA).
63  */
TEST(KernelISATest,KernelUses64BitISA)64 TEST(KernelISATest, KernelUses64BitISA) {
65   /*
66    * Exclude VSR-3.12 from Android TV
67    */
68 
69   if (isTV()) GTEST_SKIP() << "Exempt from TV devices";
70   /*
71    * ro.vendor.api_level is the VSR API level, which is calculated
72    * as:
73    *
74    * vendor.api_level = min(ro.product.first_api_level, ro.board.[first_]api_level)
75    *
76    * If ro.board.api_level is defined, it is used for the comparison instead
77    * of ro.board.first_api_level.
78    */
79   int vendor_api_level = android::base::GetIntProperty("ro.vendor.api_level",
80                                                        -1);
81 
82   /*
83    * Ensure that we run this test for devices launching with Android 14+, but not
84    * devices that are upgrading to Android 14+.
85    */
86   if (vendor_api_level < __ANDROID_API_U__)
87     GTEST_SKIP() << "Exempt from KernelUses64BitISATest: ro.vendor.api_level ("
88                  << vendor_api_level << ") < " << __ANDROID_API_U__;
89 
90   std::shared_ptr<const RuntimeInfo> runtime_info = VintfObject::GetRuntimeInfo();
91   ASSERT_NE(nullptr, runtime_info);
92 
93   const auto& configs = runtime_info->kernelConfigs();
94   ASSERT_EQ(get_config(configs, "CONFIG_64BIT"), "y") << "VSR-3.12: Devices "
95                                                       << "launching with Android 14+ "
96                                                       << "must support 64-bit ABIs and "
97                                                       << "thus must use a 64-bit kernel.";
98 }
99