otapreopt_parameters.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. ** Copyright 2016, 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. #include "otapreopt_parameters.h"
  17. #include <cstring>
  18. #include <android-base/logging.h>
  19. #include "dexopt.h"
  20. #include "installd_constants.h"
  21. #include "otapreopt_utils.h"
  22. #ifndef LOG_TAG
  23. #define LOG_TAG "otapreopt"
  24. #endif
  25. namespace android {
  26. namespace installd {
  27. static bool ParseBool(const char* in) {
  28. if (strcmp(in, "true") == 0) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. static const char* ParseNull(const char* arg) {
  34. return (strcmp(arg, "!") == 0) ? nullptr : arg;
  35. }
  36. static bool ParseUInt(const char* in, uint32_t* out) {
  37. char* end;
  38. long long int result = strtoll(in, &end, 0);
  39. if (in == end || *end != '\0') {
  40. return false;
  41. }
  42. if (result < std::numeric_limits<uint32_t>::min() ||
  43. std::numeric_limits<uint32_t>::max() < result) {
  44. return false;
  45. }
  46. *out = static_cast<uint32_t>(result);
  47. return true;
  48. }
  49. bool OTAPreoptParameters::ReadArguments(int argc, const char** argv) {
  50. // Expected command line:
  51. // target-slot [version] dexopt {DEXOPT_PARAMETERS}
  52. const char* target_slot_arg = argv[1];
  53. if (target_slot_arg == nullptr) {
  54. LOG(ERROR) << "Missing parameters";
  55. return false;
  56. }
  57. // Sanitize value. Only allow (a-zA-Z0-9_)+.
  58. target_slot = target_slot_arg;
  59. if (!ValidateTargetSlotSuffix(target_slot)) {
  60. LOG(ERROR) << "Target slot suffix not legal: " << target_slot;
  61. return false;
  62. }
  63. // Check for version or "dexopt" next.
  64. if (argv[2] == nullptr) {
  65. LOG(ERROR) << "Missing parameters";
  66. return false;
  67. }
  68. if (std::string("dexopt").compare(argv[2]) == 0) {
  69. // This is version 1 (N) or pre-versioning version 2.
  70. constexpr int kV2ArgCount = 1 // "otapreopt"
  71. + 1 // slot
  72. + 1 // "dexopt"
  73. + 1 // apk_path
  74. + 1 // uid
  75. + 1 // pkg
  76. + 1 // isa
  77. + 1 // dexopt_needed
  78. + 1 // oat_dir
  79. + 1 // dexopt_flags
  80. + 1 // filter
  81. + 1 // volume
  82. + 1 // libs
  83. + 1; // seinfo
  84. if (argc == kV2ArgCount) {
  85. return ReadArgumentsPostV1(2, argv, false);
  86. } else {
  87. return ReadArgumentsV1(argv);
  88. }
  89. }
  90. uint32_t version;
  91. if (!ParseUInt(argv[2], &version)) {
  92. LOG(ERROR) << "Could not parse version: " << argv[2];
  93. return false;
  94. }
  95. return ReadArgumentsPostV1(version, argv, true);
  96. }
  97. static int ReplaceMask(int input, int old_mask, int new_mask) {
  98. return (input & old_mask) != 0 ? new_mask : 0;
  99. }
  100. void OTAPreoptParameters::SetDefaultsForPostV1Arguments() {
  101. // Set se_info to null. It is only relevant for secondary dex files, which we won't
  102. // receive from a v1 A side.
  103. se_info = nullptr;
  104. // Set downgrade to false. It is only relevant when downgrading compiler
  105. // filter, which is not the case during ota.
  106. downgrade = false;
  107. // Set target_sdk_version to 0, ie the platform SDK version. This is
  108. // conservative and may force some classes to verify at runtime.
  109. target_sdk_version = 0;
  110. // Set the profile name to the primary apk profile.
  111. profile_name = "primary.prof";
  112. // By default we don't have a dex metadata file.
  113. dex_metadata_path = nullptr;
  114. // The compilation reason is ab-ota (match the system property pm.dexopt.ab-ota)
  115. compilation_reason = "ab-ota";
  116. // Flag is enabled by default for A/B otas.
  117. dexopt_flags = DEXOPT_GENERATE_COMPACT_DEX;
  118. }
  119. bool OTAPreoptParameters::ReadArgumentsV1(const char** argv) {
  120. // Check for "dexopt".
  121. if (argv[2] == nullptr) {
  122. LOG(ERROR) << "Missing parameters";
  123. return false;
  124. }
  125. if (std::string("dexopt").compare(argv[2]) != 0) {
  126. LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[2];
  127. return false;
  128. }
  129. SetDefaultsForPostV1Arguments();
  130. size_t param_index = 0;
  131. for (;; ++param_index) {
  132. const char* param = argv[3 + param_index];
  133. if (param == nullptr) {
  134. break;
  135. }
  136. switch (param_index) {
  137. case 0:
  138. apk_path = param;
  139. break;
  140. case 1:
  141. uid = atoi(param);
  142. break;
  143. case 2:
  144. pkgName = param;
  145. break;
  146. case 3:
  147. instruction_set = param;
  148. break;
  149. case 4: {
  150. // Version 1 had:
  151. // DEXOPT_DEX2OAT_NEEDED = 1
  152. // DEXOPT_PATCHOAT_NEEDED = 2
  153. // DEXOPT_SELF_PATCHOAT_NEEDED = 3
  154. // We will simply use DEX2OAT_FROM_SCRATCH.
  155. dexopt_needed = DEX2OAT_FROM_SCRATCH;
  156. break;
  157. }
  158. case 5:
  159. oat_dir = param;
  160. break;
  161. case 6: {
  162. // Version 1 had:
  163. constexpr int OLD_DEXOPT_PUBLIC = 1 << 1;
  164. // Note: DEXOPT_SAFEMODE has been removed.
  165. // constexpr int OLD_DEXOPT_SAFEMODE = 1 << 2;
  166. constexpr int OLD_DEXOPT_DEBUGGABLE = 1 << 3;
  167. constexpr int OLD_DEXOPT_BOOTCOMPLETE = 1 << 4;
  168. constexpr int OLD_DEXOPT_PROFILE_GUIDED = 1 << 5;
  169. constexpr int OLD_DEXOPT_OTA = 1 << 6;
  170. static_assert(DEXOPT_GENERATE_COMPACT_DEX > OLD_DEXOPT_OTA, "must not overlap");
  171. int input = atoi(param);
  172. dexopt_flags |=
  173. ReplaceMask(input, OLD_DEXOPT_PUBLIC, DEXOPT_PUBLIC) |
  174. ReplaceMask(input, OLD_DEXOPT_DEBUGGABLE, DEXOPT_DEBUGGABLE) |
  175. ReplaceMask(input, OLD_DEXOPT_BOOTCOMPLETE, DEXOPT_BOOTCOMPLETE) |
  176. ReplaceMask(input, OLD_DEXOPT_PROFILE_GUIDED, DEXOPT_PROFILE_GUIDED) |
  177. ReplaceMask(input, OLD_DEXOPT_OTA, 0);
  178. break;
  179. }
  180. case 7:
  181. compiler_filter = param;
  182. break;
  183. case 8:
  184. volume_uuid = ParseNull(param);
  185. break;
  186. case 9:
  187. shared_libraries = ParseNull(param);
  188. break;
  189. default:
  190. LOG(ERROR) << "Too many arguments, got " << param;
  191. return false;
  192. }
  193. }
  194. if (param_index != 10) {
  195. LOG(ERROR) << "Not enough parameters";
  196. return false;
  197. }
  198. return true;
  199. }
  200. bool OTAPreoptParameters::ReadArgumentsPostV1(uint32_t version, const char** argv, bool versioned) {
  201. size_t num_args_expected = 0;
  202. switch (version) {
  203. case 2: num_args_expected = 11; break;
  204. case 3: num_args_expected = 12; break;
  205. case 4: num_args_expected = 13; break;
  206. case 5: num_args_expected = 14; break;
  207. case 6: num_args_expected = 15; break;
  208. case 7:
  209. // Version 8 adds a new dexopt flag: DEXOPT_GENERATE_COMPACT_DEX
  210. case 8: num_args_expected = 16; break;
  211. // Version 9 adds a new dexopt flag: DEXOPT_GENERATE_APP_IMAGE
  212. case 9: num_args_expected = 16; break;
  213. // Version 10 is a compatibility bump.
  214. case 10: num_args_expected = 16; break;
  215. default:
  216. LOG(ERROR) << "Don't know how to read arguments for version " << version;
  217. return false;
  218. }
  219. size_t dexopt_index = versioned ? 3 : 2;
  220. // Check for "dexopt".
  221. if (argv[dexopt_index] == nullptr) {
  222. LOG(ERROR) << "Missing parameters";
  223. return false;
  224. }
  225. if (std::string("dexopt").compare(argv[dexopt_index]) != 0) {
  226. LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[dexopt_index];
  227. return false;
  228. }
  229. // Validate the number of arguments.
  230. size_t num_args_actual = 0;
  231. while (argv[dexopt_index + 1 + num_args_actual] != nullptr) {
  232. num_args_actual++;
  233. }
  234. if (num_args_actual != num_args_expected) {
  235. LOG(ERROR) << "Invalid number of arguments. expected="
  236. << num_args_expected << " actual=" << num_args_actual;
  237. return false;
  238. }
  239. // The number of arguments is OK.
  240. // Configure the default values for the parameters that were added after V1.
  241. // The default values will be overwritten in case they are passed as arguments.
  242. SetDefaultsForPostV1Arguments();
  243. for (size_t param_index = 0; param_index < num_args_actual; ++param_index) {
  244. const char* param = argv[dexopt_index + 1 + param_index];
  245. switch (param_index) {
  246. case 0:
  247. apk_path = param;
  248. break;
  249. case 1:
  250. uid = atoi(param);
  251. break;
  252. case 2:
  253. pkgName = param;
  254. break;
  255. case 3:
  256. instruction_set = param;
  257. break;
  258. case 4:
  259. dexopt_needed = atoi(param);
  260. break;
  261. case 5:
  262. oat_dir = param;
  263. break;
  264. case 6:
  265. dexopt_flags = atoi(param);
  266. // Add CompactDex generation flag for versions less than 8 since it wasn't passed
  267. // from the package manager. Only conditionally set the flag here so that it can
  268. // be fully controlled by the package manager.
  269. dexopt_flags |= (version < 8) ? DEXOPT_GENERATE_COMPACT_DEX : 0u;
  270. break;
  271. case 7:
  272. compiler_filter = param;
  273. break;
  274. case 8:
  275. volume_uuid = ParseNull(param);
  276. break;
  277. case 9:
  278. shared_libraries = ParseNull(param);
  279. break;
  280. case 10:
  281. se_info = ParseNull(param);
  282. break;
  283. case 11:
  284. downgrade = ParseBool(param);
  285. break;
  286. case 12:
  287. target_sdk_version = atoi(param);
  288. break;
  289. case 13:
  290. profile_name = ParseNull(param);
  291. break;
  292. case 14:
  293. dex_metadata_path = ParseNull(param);
  294. break;
  295. case 15:
  296. compilation_reason = ParseNull(param);
  297. break;
  298. default:
  299. LOG(FATAL) << "Should not get here. Did you call ReadArguments "
  300. << "with the right expectation? index=" << param_index
  301. << " num_args=" << num_args_actual;
  302. return false;
  303. }
  304. }
  305. if (version < 10) {
  306. // Do not accept '&' as shared libraries from versions prior to 10. These may lead
  307. // to runtime crashes. The server side of version 10+ should send the correct
  308. // context in almost all cases (e.g., only for actual shared packages).
  309. if (shared_libraries != nullptr && std::string("&") == shared_libraries) {
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. } // namespace installd
  316. } // namespace android