platform_system_core/libutils
Christopher Ferris 0e69160ebc Remove include/backtrace symlink.
This requires a few other changes to support building libutils
properly. It does appear the windows versions of libutils is
referencing CallStack code, but it doesn't seem to cause any problems.
However, I removed those references completely for the windows build.

Also removed a few extra spaces that seem to have accumulated in the
RefBase.cpp.

Bug: 170465278

Test: Builds and libutils unit tests pass.
Change-Id: Ibeee7791b13636b34bdd592c5420fd91620f752a
2020-10-20 12:46:14 -07:00
..
include/utils Revert "Add choice for changing sched policy when setting thread priority" 2020-09-27 11:21:11 +08:00
Android.bp Remove include/backtrace symlink. 2020-10-20 12:46:14 -07:00
BitSet_fuzz.cpp Add fuzzers for libutils classes 2020-05-15 10:30:16 -07:00
BitSet_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
CallStack.cpp Revert^2 "Prepare to fail in RefBase destructor if count is untouched" 2018-08-08 16:30:12 -07:00
CallStack_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
CleanSpec.mk
Errors.cpp statusToString: parenthesize strerror 2019-12-10 18:43:53 +00:00
FileMap.cpp Fail explicitly on length overflow. 2020-05-28 15:46:51 -07:00
FileMap_fuzz.cpp Add fuzzers for libutils classes 2020-05-15 10:30:16 -07:00
FileMap_test.cpp Fail explicitly on length overflow. 2020-05-28 15:46:51 -07:00
FuzzFormatTypes.h Adds license header 2020-10-02 11:08:15 -07:00
JenkinsHash.cpp
Looper.cpp Looper: missing cinttypes when debugging poll/callbacks 2019-05-11 20:53:47 +01:00
Looper_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
Looper_test.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
Looper_test_pipe.h Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
LruCache_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
LruCache_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
misc.cpp [libutils] Modernize codebase by replacing NULL with nullptr 2018-07-16 18:11:34 -07:00
MODULE_LICENSE_APACHE2
Mutex_test.cpp libutils: Fix thread safety annotations in Mutex 2019-03-29 12:21:57 -07:00
NativeHandle.cpp [libutils] Modernize codebase by replacing NULL with nullptr 2018-07-16 18:11:34 -07:00
NOTICE
OWNERS Add OWNERS. 2017-12-07 13:30:03 -08:00
Printer.cpp [libutils] Modernize codebase by replacing NULL with nullptr 2018-07-16 18:11:34 -07:00
Printer_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
ProcessCallStack.cpp [libutils] Modernize codebase by replacing NULL with nullptr 2018-07-16 18:11:34 -07:00
ProcessCallStack_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
README
RefBase.cpp Remove include/backtrace symlink. 2020-10-20 12:46:14 -07:00
RefBase_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
RefBase_test.cpp Revert "Revert "Fix wp and sp comparison bugs"" 2019-03-13 13:26:35 -07:00
SharedBuffer.cpp Reland "libutils: Introduce StaticString16"" 2019-09-05 13:19:14 -07:00
SharedBuffer.h Reland "libutils: Introduce StaticString16"" 2019-09-05 13:19:14 -07:00
SharedBuffer_test.cpp Fix SharedBuffer tests to pass under hwasan. 2020-10-19 16:51:58 -07:00
Singleton_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
Singleton_test.h Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
Singleton_test1.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
Singleton_test2.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
StopWatch.cpp Use -Werror in system/core 2017-11-01 11:32:55 -07:00
StopWatch_fuzz.cpp Add second batch of fuzzers for libutils 2020-08-07 15:36:39 -07:00
String8.cpp DO NOT MERGE - Merge RP1A.201005.006 2020-10-06 04:30:21 +00:00
String8_fuzz.cpp Fixes to String8 fuzzer 2020-09-25 16:28:49 -07:00
String8_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
String16.cpp String16::remove - avoid overflow am: 4048e49956 am: 107f18cb61 am: 9be245c3f0 am: 64fb5012b8 am: 4e27b6992b am: 26b81f59d2 am: 8367a666b5 2020-07-14 17:24:42 +00:00
String16_fuzz.cpp Add fuzzers for libutils classes 2020-05-15 10:30:16 -07:00
String16_test.cpp Reland "libutils: Introduce StaticString16"" 2019-09-05 13:19:14 -07:00
StrongPointer.cpp Add check to sp<> raw pointer constructor 2019-11-26 15:32:40 -08:00
StrongPointer_test.cpp libutils: introduce sp<T>::make 2020-02-20 17:21:27 -08:00
SystemClock.cpp Add uptimeNanos to SystemClock 2020-08-07 14:36:07 -07:00
SystemClock_test.cpp Add uptimeNanos to SystemClock 2020-08-07 14:36:07 -07:00
Threads.cpp Revert "Add choice for changing sched policy when setting thread priority" 2020-09-27 11:21:11 +08:00
Timers.cpp Make systemTime() abort on bad input. 2020-05-27 15:31:55 -07:00
Timers_test.cpp Make systemTime() abort on bad input. 2020-05-27 15:31:55 -07:00
Tokenizer.cpp Move system/core/ off NO_ERROR. 2018-10-08 11:15:52 -07:00
Trace.cpp
Unicode.cpp Update language to comply with Android's inclusive language guidance 2020-07-28 15:58:25 -07:00
Unicode_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
Vector_benchmark.cpp compare android::Vector and std::vector 2019-09-03 18:11:43 -07:00
Vector_fuzz.cpp Add fuzzers for libutils classes 2020-05-15 10:30:16 -07:00
Vector_test.cpp Clean up libcutils/libutils tests. 2019-02-11 13:15:56 -08:00
VectorImpl.cpp Move system/core/ off NO_ERROR. 2018-10-08 11:15:52 -07:00

Android Utility Function Library
================================


If you need a feature that is native to Linux but not present on other
platforms, construct a platform-dependent implementation that shares
the Linux interface.  That way the actual device runs as "light" as
possible.

If that isn't feasible, create a system-independent interface and hide
the details.

The ultimate goal is *not* to create a super-duper platform abstraction
layer.  The goal is to provide an optimized solution for Linux with
reasonable implementations for other platforms.



Resource overlay
================


Introduction
------------

Overlay packages are special .apk files which provide no code but
additional resource values (and possibly new configurations) for
resources in other packages. When an application requests resources,
the system will return values from either the application's original
package or any associated overlay package. Any redirection is completely
transparent to the calling application.

Resource values have the following precedence table, listed in
descending precedence.

 * overlay package, matching config (eg res/values-en-land)

 * original package, matching config

 * overlay package, no config (eg res/values)

 * original package, no config

During compilation, overlay packages are differentiated from regular
packages by passing the -o flag to aapt.


Background
----------

This section provides generic background material on resources in
Android.


How resources are bundled in .apk files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Android .apk files are .zip files, usually housing .dex code,
certificates and resources, though packages containing resources but
no code are possible. Resources can be divided into the following
categories; a `configuration' indicates a set of phone language, display
density, network operator, etc.

 * assets: uncompressed, raw files packaged as part of an .apk and
           explicitly referenced by filename. These files are
           independent of configuration.

 * res/drawable: bitmap or xml graphics. Each file may have different
                 values depending on configuration.

 * res/values: integers, strings, etc. Each resource may have different
               values depending on configuration.

Resource meta information and information proper is stored in a binary
format in a named file resources.arsc, bundled as part of the .apk.

Resource IDs and lookup
~~~~~~~~~~~~~~~~~~~~~~~
During compilation, the aapt tool gathers application resources and
generates a resources.arsc file. Each resource name is assigned an
integer ID 0xppttiii (translated to a symbolic name via R.java), where

 * pp: corresponds to the package namespace (details below).

 * tt: corresponds to the resource type (string, int, etc). Every
       resource of the same type within the same package has the same
       tt value, but depending on available types, the actual numerical
       value may be different between packages.

 * iiii: sequential number, assigned in the order resources are found.

Resource values are specified paired with a set of configuration
constraints (the default being the empty set), eg res/values-sv-port
which imposes restrictions on language (Swedish) and display orientation
(portrait). During lookup, every constraint set is matched against the
current configuration, and the value corresponding to the best matching
constraint set is returned (ResourceTypes.{h,cpp}).

Parsing of resources.arsc is handled by ResourceTypes.cpp; this utility
is governed by AssetManager.cpp, which tracks loaded resources per
process.

Assets are looked up by path and filename in AssetManager.cpp. The path
to resources in res/drawable are located by ResourceTypes.cpp and then
handled like assets by AssetManager.cpp. Other resources are handled
solely by ResourceTypes.cpp.

Package ID as namespace
~~~~~~~~~~~~~~~~~~~~~~~
The pp part of a resource ID defines a namespace. Android currently
defines two namespaces:

 * 0x01: system resources (pre-installed in framework-res.apk)

 * 0x7f: application resources (bundled in the application .apk)

ResourceTypes.cpp supports package IDs between 0x01 and 0x7f
(inclusive); values outside this range are invalid.

Each running (Dalvik) process is assigned a unique instance of
AssetManager, which in turn keeps a forest structure of loaded
resource.arsc files. Normally, this forest is structured as follows,
where mPackageMap is the internal vector employed in ResourceTypes.cpp.

mPackageMap[0x00] -> system package
mPackageMap[0x01] -> NULL
mPackageMap[0x02] -> NULL
...
mPackageMap[0x7f - 2] -> NULL
mPackageMap[0x7f - 1] -> application package



The resource overlay extension
------------------------------

The resource overlay mechanism aims to (partly) shadow and extend
existing resources with new values for defined and new configurations.
Technically, this is achieved by adding resource-only packages (called
overlay packages) to existing resource namespaces, like so:

mPackageMap[0x00] -> system package -> system overlay package
mPackageMap[0x01] -> NULL
mPackageMap[0x02] -> NULL
...
mPackageMap[0x7f - 2] -> NULL
mPackageMap[0x7f - 1] -> application package -> overlay 1 -> overlay 2

The use of overlay resources is completely transparent to
applications; no additional resource identifiers are introduced, only
configuration/value pairs. Any number of overlay packages may be loaded
at a time; overlay packages are agnostic to what they target -- both
system and application resources are fair game.

The package targeted by an overlay package is called the target or
original package.

Resource overlay operates on symbolic resources names. Hence, to
override the string/str1 resources in a package, the overlay package
would include a resource also named string/str1. The end user does not
have to worry about the numeric resources IDs assigned by aapt, as this
is resolved automatically by the system.

As of this writing, the use of resource overlay has not been fully
explored. Until it has, only OEMs are trusted to use resource overlay.
For this reason, overlay packages must reside in /system/overlay.


Resource ID mapping
~~~~~~~~~~~~~~~~~~~
Resource identifiers must be coherent within the same namespace (ie
PackageGroup in ResourceTypes.cpp). Calling applications will refer to
resources using the IDs defined in the original package, but there is no
guarantee aapt has assigned the same ID to the corresponding resource in
an overlay package. To translate between the two, a resource ID mapping
{original ID -> overlay ID} is created during package installation
(PackageManagerService.java) and used during resource lookup. The
mapping is stored in /data/resource-cache, with a @idmap file name
suffix.

The idmap file format is documented in a separate section, below.


Package management
~~~~~~~~~~~~~~~~~~
Packages are managed by the PackageManagerService. Addition and removal
of packages are monitored via the inotify framework, exposed via
android.os.FileObserver.

During initialization of a Dalvik process, ActivityThread.java requests
the process' AssetManager (by proxy, via AssetManager.java and JNI)
to load a list of packages. This list includes overlay packages, if
present.

When a target package or a corresponding overlay package is installed,
the target package's process is stopped and a new idmap is generated.
This is similar to how applications are stopped when their packages are
upgraded.


Creating overlay packages
-------------------------

Overlay packages should contain no code, define (some) resources with
the same type and name as in the original package, and be compiled with
the -o flag passed to aapt.

The aapt -o flag instructs aapt to create an overlay package.
Technically, this means the package will be assigned package id 0x00.

There are no restrictions on overlay packages names, though the naming
convention <original.package.name>.overlay.<name> is recommended.


Example overlay package
~~~~~~~~~~~~~~~~~~~~~~~

To overlay the resource bool/b in package com.foo.bar, to be applied
when the display is in landscape mode, create a new package with
no source code and a single .xml file under res/values-land, with
an entry for bool/b. Compile with aapt -o and place the results in
/system/overlay by adding the following to Android.mk:

LOCAL_AAPT_FLAGS := -o com.foo.bar
LOCAL_MODULE_PATH := $(TARGET_OUT)/overlay


The ID map (idmap) file format
------------------------------

The idmap format is designed for lookup performance. However, leading
and trailing undefined overlay values are discarded to reduce the memory
footprint.


idmap grammar
~~~~~~~~~~~~~
All atoms (names in square brackets) are uint32_t integers. The
idmap-magic constant spells "idmp" in ASCII. Offsets are given relative
to the data_header, not to the beginning of the file.

map          := header data
header       := idmap-magic <crc32-original-pkg> <crc32-overlay-pkg>
idmap-magic  := <0x706d6469>
data         := data_header type_block+
data_header  := <m> header_block{m}
header_block := <0> | <type_block_offset>
type_block   := <n> <id_offset> entry{n}
entry        := <resource_id_in_target_package>


idmap example
~~~~~~~~~~~~~
Given a pair of target and overlay packages with CRC sums 0x216a8fe2
and 0x6b9beaec, each defining the following resources

Name          Target package  Overlay package
string/str0   0x7f010000      -
string/str1   0x7f010001      0x7f010000
string/str2   0x7f010002      -
string/str3   0x7f010003      0x7f010001
string/str4   0x7f010004      -
bool/bool0    0x7f020000      -
integer/int0  0x7f030000      0x7f020000
integer/int1  0x7f030001      -

the corresponding resource map is

0x706d6469 0x216a8fe2 0x6b9beaec 0x00000003 \
0x00000004 0x00000000 0x00000009 0x00000003 \
0x00000001 0x7f010000 0x00000000 0x7f010001 \
0x00000001 0x00000000 0x7f020000

or, formatted differently

0x706d6469  # magic: all idmap files begin with this constant
0x216a8fe2  # CRC32 of the resources.arsc file in the original package
0x6b9beaec  # CRC32 of the resources.arsc file in the overlay package
0x00000003  # header; three types (string, bool, integer) in the target package
0x00000004  #   header_block for type 0 (string) is located at offset 4
0x00000000  #   no bool type exists in overlay package -> no header_block
0x00000009  #   header_block for type 2 (integer) is located at offset 9
0x00000003  # header_block for string; overlay IDs span 3 elements
0x00000001  #   the first string in target package is entry 1 == offset
0x7f010000  #   target 0x7f01001 -> overlay 0x7f010000
0x00000000  #   str2 not defined in overlay package
0x7f010001  #   target 0x7f010003 -> overlay 0x7f010001
0x00000001  # header_block for integer; overlay IDs span 1 element
0x00000000  #   offset == 0
0x7f020000  #   target 0x7f030000 -> overlay 0x7f020000