Commit Graph

1300 Commits

Author SHA1 Message Date
felkachang
78a8d373ca Dump the deobfuscate data using Protocol buffers
Using Google Protocol buffers to dump the obfuscating mapping
information of being used to deobfuscate items.
* shortened file path -> original file path
* resource id -> original resource name

This patch add a new option --save-obfuscating-map to deprecate
--resource-path-shortening-map. The option
--resource-path-shortening-map is kept until no one to use it.

Bug: 246489170
Bug: 228192695
     b/228192695#comment2
Test: make WITH_TIDY=1 aapt2
Test: atest aapt2_test

Change-Id: I29733c4dbae9f6dd2f0e9b2c87b0d2046662fc59
2022-12-01 13:53:31 +00:00
TreeHugger Robot
6757d436d9 Merge "Use lambda to refactor the obfuscating resource name." 2022-12-01 12:33:42 +00:00
felkachang
8ceb39c94f Use lambda to refactor the obfuscating resource name.
There are 2 copy codes for handling the obfuscating resources names
between serializing to pb format file and writing to apks. The
obfuscator also needs to dump resources names. It means there are
3 places to handle the obfuscating resources names.

So, using C++ lambda to apply the callback mechanism refactors the
codes.

Obfuscator
* Initial a Obfuscator according to Optimizer's options
* Add Obfuscator.IsEnabled() function.
    return true either shorten_resource_paths_ is true or
    collapse_key_stringpool_ is true.

Bug: 228192695

Test: atest aapt2_test idmap2_test

Change-Id: Idd2442beecf41e9392620ff801a36fd1285e06f9
2022-11-30 17:46:25 +00:00
TreeHugger Robot
33f36e06a8 Merge "Make StringPiece to be std::string_view alias" 2022-11-30 16:22:19 +00:00
Iurii Makhno
4dbae54d6e Merge "Fix compatibility between 'aapt2 dump' and 'aapt dump.'." 2022-11-30 15:32:38 +00:00
Iurii Makhno
0df4e6d05f Fix compatibility between 'aapt2 dump' and 'aapt dump.'.
1. Report permissions declared only under <manifest>.
2. If the same feature reported multiple times: the last declaration
   wins.

Bug: b/260704482
Test: manual runs
Change-Id: Ic9ff32573d2bf6550c3a01ed3019da83590eda2f
2022-11-30 13:31:16 +00:00
Yurii Zubrytskyi
a577514789 Make StringPiece to be std::string_view alias
Bug: 237583012
Test: build + boot + UTs

Change-Id: I849831f4466d3b9c7ec842b75256e7fcba77a0c0
2022-11-29 21:46:02 -08:00
Eric Miao
7a378b266f Merge "androidfw: Add support for 16-bit entry offsets" 2022-11-17 00:17:07 +00:00
Eric Miao
a485aa6f42 Merge "androidfw: Add support for compact resource entries" 2022-11-17 00:16:16 +00:00
Eric Miao
a1f2bce0e5 androidfw: Add support for 16-bit entry offsets
Bug: 237583012

Most offsets to the entries can be well encoded in 16-bit,
and given entries are 4-byte aligned, this gives us a range
of entry offsets from 0x00000 to 0xfffe * 4u, with 0xffffu
to represent ResTable_type::NO_ENTRY.

For now, 16-bit entry offset will be enabled only when:

 * all the entry offsets can be represented in 16-bit
 * --enable-compact-entries switch is turned on

Change-Id: I1c815c052aa5fba6eab2529434d31d7714c13694
2022-11-15 12:15:38 -08:00
Eric Miao
368cd19d03 androidfw: Add support for compact resource entries
Bug: 237583012

Given the large number of simple resources such as strings in
Android resources, their ResTable_entry and Res_value can be
encoded together in a compact way.  This allows a significant
saving in both storage and memory footprint.

The basic observations for simple resources are:

* ResTable_entry.size will always be sizeof(ResTable_entry)
  unless it's a complex entry

* ResTable_entry.key is unlikely to exceed 16-bit

* ResTable_entry.flags only uses 3 bits for now

* Res_value.size will always be sizeof(Res_value)

Given the above, we could well encode the information into
a compact/compatible structure.

  struct compact {
      uint16_t key;
      uint16_t flags;
      uint32_t data;
  };

The layout of this structure will allow maximum backward
compatibility. e.g. the flags will be at the same offset,
and a

  `dtohs((ResTable_entry *)entry->flags) & FLAG_COMPACT`

would tell if this entry is a compact one or not. For a
compact entry:

  struct compact *entry;

  entry_size  == sizeof(*entry)
  entry_key   == static_cast<uint32_t>(dtohs(entry->key))
  entry_flags == dtohs(entry->flags) & 0xff	// low 8-bit
  data_type   == dtohs(entry->flags) >> 8	// high 8-bit
  data_size   == sizeof(Res_value)
  data_value  == dtohl(entry->data)

To allow minimum code change and backward compatibility,
we change 'struct ResTable_entry' to 'union ResTable_entry',
with an anonymous structure inside that's fully backward
compatible. Thus, any existing reference such as:

  ResTable_entry *entry = ...
  if (dtohs(entry->flags) & ResTable_entry::FLAG_COMPLEX) ...

would still work.

However, special care needs to be taken after an entry is
obtained, and when value needs to be extracted.

A compact entry will not encode a complex value, and hence
complex entries/values are handled the same way.

Change-Id: I15d97a4f5e85fab28c075496f7f0cf6b1fcd73e3
2022-11-15 12:12:33 -08:00
Rico Wind
9db035899e Merge "Add support for DynamicRefTable when serializing/deserializing proto" 2022-11-12 13:51:24 +00:00
Iurii Makhno
a7ba6b6813 Do not validate localeConfig if it is referenced as external symbol.
Bug: b/258816606
Test: Link_test
Change-Id: I5dd4aa75c02ceebad26d025a99ba302ec673abf8
2022-11-11 18:32:13 +00:00
Rico Wind
e70bbb1ecf Add support for DynamicRefTable when serializing/deserializing proto
Test: make aapt2_tests
Bug: 239390219
Bug: 237783271
Change-Id: I768f9167117c3a85c4dcabf2ece8a31e277c1e98
2022-11-11 09:37:54 +01:00
Brandon Liu
a35bf78caa Fixing edge cases that @string references passed in path parts.
Bug: b/241114745
Test: Added and verified affected atests pass
Change-Id: Ia7048222bf91aa3ce02e090789e9af05bbed97e1
2022-11-03 21:17:58 +00:00
Brandon Liu
3cff2550ca Force path parts in intent filter with deeplinks to have leading slash.
Bug: 241114745
Test: Added and verified affected atests pass
Change-Id: I1aa5148b1b0722e9fde33be6cbb6277ebfb6e10d
2022-10-25 17:50:32 +00:00
Iurii Makhno
f3ee72ce56 Merge "Expose flags for collapse resource name to 'convert' command." 2022-10-18 19:18:14 +00:00
Iurii Makhno
054e43317e Expose flags for collapse resource name to 'convert' command.
To achieve this ParseResourceConfig is extracted to Utils.cpp and tests
are moved from Optimize_test.cpp to Util_test.cpp.

Bug: b/249793372
Test: Util_test, Convert_test
Change-Id: I5a0458e3834d5ea62c96013abc14527285e895e0
2022-10-18 17:12:59 +00:00
Brandon Liu
07e272d2e4 Sparse encoding fully drop config.sdkVersion check
Bug: 197642721
Test: Updated and verified affected atests pass
Change-Id: I0e5b7086e665340cd2995c11af8b901de85d321f
2022-10-12 01:43:05 +00:00
Jeremy Meyer
7726cdd61c Merge "Allow passing in a category override to aapt2" 2022-10-07 17:57:16 +00:00
Iurii Makhno
0370f2aaa5 Merge changes I938a3425,Ica98a25a
* changes:
  Add deduplicate_entry_values mode to TableFlattener.
  Intoduce ResEntryWriter unit.
2022-10-05 16:30:50 +00:00
Iurii Makhno
8b33e7adb8 Add deduplicate_entry_values mode to TableFlattener.
This CL introduces DeduplicateItemsResEntryWriter which stores unique
pairs of entry and value and deduplicates a new pair based of this data
before writing it into the buffer.

Bug: b/249793372
Test: ResEntriesWriter_test, TableFlattener_test
Change-Id: I938a3425a87bfad9853e59193680de2050a95bd2
2022-10-05 12:31:24 +00:00
Iurii Makhno
cf844a3e8c Intoduce ResEntryWriter unit.
ResEntryWriter is responsible to write pairs of entry+value into binary
buffers and allows easy customization how and where entries are written.

This is no-op refactoring and all existing tests should pass.

Bug: b/249793372
Test: Existing tests
Change-Id: Ica98a25aeb49cc96cad28547c462b36316a8adb6
2022-10-05 00:04:31 +00:00
Stephen Hines
eb39a51194 Fix stack-use-after-scope for a std::string
`android::util::Utf16ToUtf8()` actually returns a `std::string` due to
the actual conversion to Utf8. `ParseResourceNamedType()` operates on a
`StringPiece` of `converted` (the `std::string` returned from that
call), and stashes it away for later use. Of course, by the time we're
using the `StringPiece` in `parsed_type`, `converted` has already gone
out of scope and is invalid to access.

Bug: http://b/250827883
Test: ./art/test/testrunner/run_build_test_target.py -j80 art-asan

Change-Id: Iea71a5cc84b7dfa96e7dcb549435f8394770a4df
2022-10-04 03:08:39 -07:00
Jeremy Meyer
6b05b7d95f Allow passing in a category override to aapt2
Bug: 240322840
Test: unit test
Change-Id: Ia605ad8ad0267b02d67da15cf65775f69aef59e5
2022-09-30 22:24:25 +00:00
Yurii Zubrytskyi
1fc44ebb01 Clean up some resources code
More moves and fewer allocations

Bug: 237583012
Test: unit tests
Change-Id: I5cf43c8af0743c0e4d96808f1e55ceb4f02d7021
2022-09-29 14:28:51 -07:00
felkachang
4bdd3ac44c Rename ResourcePathShortener to Obfuscator
ResourcePathShortener is a part of Obfuscator. Obfuscator will not
only shorten the path but also handle obfuscating resource names.

Format C++ code by clang-format.

Bug: 228192695

Test: atest aapt2_test idmap2_test
Change-Id: I27b4b1af44061e3cd8a6717ffd46b8a187cdc41d
2022-09-26 12:29:08 +08:00
Donald Chai
c000962349 Define a unified protobuf message for "aapt2 optimize" metadata.
This helps solve three problems:

* obfuscation maps are now documented.
* obfuscation maps will not require hand-rolled parsers.
* no additional plumbing will be required for future
  optimizations---plumbing individual mapping files in Bazel for
  ProGuard, aapt2, etc. has been a pain.

Bug: 153201806
Test: make aapt2
Change-Id: Ie3829834527e8875d768ae28242d3aeca28c6db2
2022-09-26 11:52:55 +08:00
Iurii Makhno
a0fadc4bba Merge "Expose '--force-sparse-encoding' flag for 'optimize' and 'convert' commands." 2022-09-23 11:40:45 +00:00
Iurii Makhno
da06e4d920 Expose '--force-sparse-encoding' flag for 'optimize' and 'convert' commands.
This allows to use sparse encoding for APKs that do not specify minSdk
version inside. This is common for split APKs generated by bundletool
when Android App Bundle (AAB) is split into multiple APKs.

Bug: b/223394605
Test: TableFlattener_test
Change-Id: I2a99a611f855cffbcea20018d869e1eb54d1b3d5
2022-09-22 23:44:50 +00:00
Iurii Makhno
513f52f970 Merge "Use the latest 'uses-sdk' tag to detect target SDK version." 2022-09-07 00:48:04 +00:00
Iurii Makhno
3bffbb95e4 Merge "Expose default ('--_--') locale in proto format." 2022-09-06 22:41:29 +00:00
TreeHugger Robot
6cf4234405 Merge "Fix aapt2 dump xmltree not escape text nodes" 2022-08-31 03:42:07 +00:00
Brandon Liu
2dc0edb044 aapt2 sparse encoding bugfix
aapt2 disable sparse encoding when sdk < 32

Bug: 197642721
Test: Updated and verified affected atests pass
Change-Id: I839978207eb30f958f5d695cf8dea7f0dad1312f

aapt2 enable sparse encoding when sdk is not set.

Bug: 197642721
Test: Added new unit test and verified all tests pass
Change-Id: Ib58941d98eafe8e543d50054c9d36a51d7977c71
2022-08-29 19:28:33 +00:00
felkachang
a1da277369 Fix aapt2 dump xmltree not escape text nodes
The text of CDATA in XML document should be escaped before the CDATA
text is printed.

Dump xmltree command use XmlPrinter inheriting from xml:ConstVisitor.
To fix issue, it only needs to modify
XmlPrinter::Visit(const xml::Text).

Bug: 140373430
Test: aapt2 dump xmltree --file res/xml/test.xml app_debug.apk

Change-Id: I4f43120b4fbc2ea7136fb51fdeb24e254b94c60f
2022-08-29 17:08:22 +00:00
Treehugger Robot
a014667eab Merge "aapt2: Fix deprecated proto API usage." am: 2992680274 am: aeaa62c5d0 am: d682efe214 am: 2138ab2378 am: c7f844c610
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2176683

Change-Id: I497146869af89d911c0dc03d2646c0e20db5e276
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-08-26 16:11:18 +00:00
Treehugger Robot
c7f844c610 Merge "aapt2: Fix deprecated proto API usage." am: 2992680274 am: aeaa62c5d0 am: d682efe214 am: 2138ab2378
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2176683

Change-Id: I1d362eba5c9632c8bab73f9bd4f526a55b47ac61
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-08-25 22:56:16 +00:00
Treehugger Robot
2992680274 Merge "aapt2: Fix deprecated proto API usage." 2022-08-25 21:22:48 +00:00
Iurii Makhno
cfc559b189 Expose default ('--_--') locale in proto format.
Bug: b/243145989
Test: Dump_test
Change-Id: Ie56aabb0f9b4b923d560470db7b426ab6ee34bd2
2022-08-25 15:57:52 +00:00
Iurii Makhno
05fd929d3e Use the latest 'uses-sdk' tag to detect target SDK version.
At the moment aapt2 dumps information from the latest <uses-sdk> element
but uses all of them to detect target SDK which affects implied
permissions. Use only the latest one for detecting targetSdk as well.

Bug: b/243145989
Test: Dump_test
Change-Id: I70f828f1845ab070fa84c5297af12df24e7068dc
2022-08-24 19:00:49 +00:00
Iurii Makhno
499ecd35d3 Fix achitectures and 'camera' component extraction in 'dump badging'.
Logic is synchronised with 'aapt':
 * use 'android.media.*' intents for 'camera' component detection;
 * use only 'lib/' under root to parse supported archs.

Bug: b/243145989
Test: Dump_test.cpp
Change-Id: I90fbe802a97a4382e3021b344d0c5e9708563389
2022-08-24 11:42:19 +00:00
felkachang
8947def237 Prevent overlayable resources from being obfuscated
Once the build process finds one overlayable resource entry is not in
the list of collapse name exemption, the build process outputs the
resource name automatically and gives a warning message to notify the
developers.

Bug: 228192695

Test: atest aapt2_test
Change-Id: I989b75343d803b059c902baa088590fce7237211
2022-08-11 17:54:56 +08:00
TreeHugger Robot
c5f64f47ad Merge changes from topic "b/232475788-frameworks/base" into tm-qpr-dev am: 317201974a am: b446de7503
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19457538

Change-Id: I0b05e29bd38798ee0a15303757adc61f378d370e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-08-08 15:43:06 +00:00
TreeHugger Robot
b446de7503 Merge changes from topic "b/232475788-frameworks/base" into tm-qpr-dev am: 317201974a
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19457538

Change-Id: Ia79695069f20fe1a66db965780cf7d8532325db6
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-08-08 15:31:09 +00:00
Krzysztof Kosiński
92d263378a aapt2: Fix deprecated proto API usage.
Bug: 203713560
Test: presubmit
Change-Id: Ie8bb2c010cff1c2e861918496290d1ef6dd6daef
2022-08-06 08:24:47 +00:00
Mårten Kongstad
b0502bb700 Scaffolding: reject installs based on device fingerprint
Add the scaffolding for teaching the package parser to reject a package
if its manifest specifies a set of prefixes, and the device fingerprint
does not start with one of the prefixes.

Bug: b/232475788
Test: m
Change-Id: I9f3f3d3ad96332d2d65fbf6f58c7ce35992bcd7e
2022-08-01 18:03:59 +00:00
Josh Hou
ec67cb4990 Verify the locale format within a localeConfig file
Leverage the existing mechanism for validating the locale format to verify whether the locale format of the compiled localeConfig file follows the BCP47 reqular expressions

Bug: 208943132
Test: 1. atest aapt2_tests
      2. Build the app bundle with the local aapt2 in the Android Studio
         a. Make sure the app can be built pass with AGP 7.3.0-alpha06.
	 b. Make sure the app can be built pass by using the local aapt2 with gradle: set the android.aapt2FromMavenOverride=<path> variable in gradle.properties.
	 c. Revove the property above in gradle.properties then apply AGP 7.3.0-alpha07. Make sure the appt2 shows error(AAPT: error: failed to load XML file: No such file or directory) when building the app.
	 d. Add the locale aapt2 with gradle: set the android.aapt2FromMavenOverride=<path> variable in gradle.properties. Make sure the app can be built pass.

Change-Id: Ifa4b9123d36f9c2d450fdf16e1f52963962d7d81
2022-06-10 23:51:55 +08:00
Yurii Zubrytskyi
34627f9cc1 Revert "Verify the locale format within a localeConfig file"
This reverts commit d3bd4526ee.

Reason for revert: b/226200249
Bug: 226200249

Change-Id: I9a98ac22bf3152bbdb84e9e004c33f03ef246a92
2022-06-02 18:38:15 -07:00
Jeremy Meyer
56f36e8f2f Move StringPool to libandroidfw
Test: verified affected tests pass
Bug: 232940948
Change-Id: I22089893d7e5013f759c39ce190bec07fa6435db
2022-05-31 23:43:03 +00:00
Jackal Guo
546f3f7526 Merge "Revert "Escape single quotes in aapt2"" into tm-dev am: d63cdb916d am: d16011d658
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18134683

Change-Id: I48606f0c79a69e64dc12f1fcf70663ba8897accc
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-05-03 16:39:46 +00:00