Commit graph

386 commits

Author SHA1 Message Date
Tom Cherry
a9a6d49ec0 liblog: use int32_t and uint32_t for new NDK APIs
As requested during the API review.

Bug: 150898477
Test: liblog and libbase unit tests
Merged-In: I0c1dd57f7499e432bb881e1da1beb55f1ff7de05
Change-Id: I0c1dd57f7499e432bb881e1da1beb55f1ff7de05
(cherry picked from commit f1a975bece)
2020-03-13 14:09:59 -07:00
Jiyong Park
5280b5c03e libbase uses liblog symbols via dlsym when it is built for APEX
libbase is a popular library that is used by many APEXes either directly
or transitively. It is being used by several Mainline modules that were
launched with Q, in which case everything in the APEX - including
libbase - shouldn't use new APIs that are added post Q, i.e. R.

libbase however is using a few new R symbols from liblog, and this is
preventing those Q-launching Mainline modules that are built in R source
tree from being installed to Q devices.

Fortunately, the dependencies to the new R symbols are guarded with a
flag; when the existence of the symbols are not guaranteed, it uses
dlsym. This change fixes the aforementioned problem by turning on the
flag also when libbase is built for an APEX.

Bug: 149569129
Test: TARGET_BUILD_APPS=com.android.media
vendor/google/build/build_mainline_modules.sh
adb install --staged out/dist/mainline_modules_arm64/com.android.media.apex
adb reboot
The APEX is installed and mediaextractor process doesn't crash

Change-Id: I44b5ec028850613cb45fc3e792f43cd8e87cfd00
2020-03-10 10:08:26 +09:00
Tom Cherry
b9a0e2ad60 base: access all new liblog symbols through liblog_functions
The original CL was missing these.

Bug: 149591522
Test: build
Merged-In: I99615b81cfe9166a00c812a8642ecf1244bf08b9
Change-Id: I99615b81cfe9166a00c812a8642ecf1244bf08b9
(cherry picked from commit d620ec7fb9)
2020-03-05 18:48:42 -08:00
Josh Gao
b7814e8c32 base: add CachedProperty.
Copy bionic's CachedProperty with some minor API tweaks, to allow for
efficient querying of properties that rarely change.

Bug: http://b/141959374
Test: treehugger
Change-Id: I4dfc3f527d30262b35e871d256cec69e69f2e1d7
(cherry picked from commit 891e6dad73)
2020-02-28 15:20:29 -08:00
Howard Ro
6c364f94ad Merge "Allow apexes to link against more libs" 2020-02-19 01:17:09 +00:00
Tom Cherry
d5f9d87da2 base: remove Errorf/ErrnoErrorf work around
This is no longer needed

Test: build
Change-Id: Ied9b26ff517906d662f90a31c98829843c1e9a9f
2020-02-14 12:43:11 -08:00
Jeffrey Huang
0c0b774a64 Allow apexes to link against more libs
Bug: 145923087
Test: m -j
Change-Id: I45e002ffbd16f13b71f32c80e8a2f826bfa9ec2e
2020-02-14 10:41:15 -08:00
Treehugger Robot
39e990d477 Merge "Convert system/core to Result::ok()" 2020-02-07 06:14:16 +00:00
Tom Cherry
91bbe9ef56 Merge "Check Errorf()/ErrnoErrorf() format string at compile time" 2020-02-06 19:14:15 +00:00
Bernie Innocenti
cecebbbacc Convert system/core to Result::ok()
No functionality changes, this is a mechanical cleanup.

Test: m
Test: cd system/core && atest
Change-Id: Ifdaa3ce1947ed578f656d5a446978726eb416c36
2020-02-06 17:04:27 +00:00
Bernie Innocenti
beffc028d6 Add macros to check for successful Result
These macros are meant to be used in tests:

  Result<File> maybe_a_file = OpenFile(...);
  EXPECT_OK(maybe_a_file);

On failure, the error is printed.

There's no equivalent EXPECT_NOT_OK() because it is a testing anti-pattern
which causes tests to pass even when the error changed as a result of a
regression. Use EPECT_EQ(result, Error(...)) instead.

Test: cd system/core && atest
Test: m
Change-Id: Ie26f90d3c62620e7b1f10013829ba43ef5364fe1
2020-02-06 15:27:26 +09:00
Bernie Innocenti
4adfde41ea Add android::base::expected::ok()
This is an alias for has_value() which is meant to be more concise, yet
more explicit than the conversions to bool.

Leave operator bool() in place for now: due to the large number of
users, removal of operator bool() needs to be broken up into incremental
stages.

Test: cd system/core && atest
Test: m
Change-Id: Ib1eb00f47d3c0f2229bb176b62687463b834f280
2020-02-06 15:27:26 +09:00
Tom Cherry
f8e6bf6d81 Check Errorf()/ErrnoErrorf() format string at compile time
fmtlib provides compile time checking of format strings that we're not
currently using.  This change makes Errorf() and ErrnoErrorf() into
macros such that we can take advantage of this capability.

Test: build successfully normally
Test: fail the build if using an invalid format string
Change-Id: Icb8ba8cb973bbd1fa4755a62e7598bdbb0113757
2020-02-05 07:57:14 -08:00
Bernie Innocenti
1cc76dfe84 Delete dangerous comparison operators from base::expected
These operators were included because they're present in the draft
standard proposal of std::expected, but they were deemed to lead to
bugs, particularly when T is implicitly convertible to bool.

Change-Id: Ib149decf1f230198f358dc1ae0eaed71961363f6
Test: m
2020-02-04 12:55:23 +00:00
Tom Cherry
42ee2e4f8f libbase: Have LogdLogger use LOGGER_ENTRY_MAX_PAYLOAD for its buffer
LogdLogger has its own buffer for adding the file and line number to
FATAL messages, but it is much lower than LOGGER_ENTRY_MAX_PAYLOAD and
that causes problems now that more logs are piped through this logger,
so increase the limit to maximum.

Also, in the case that the file and line number are not added, simply
pass the buffer through to liblog, since there is no reason to copy to
a separate buffer.

Bug: 148678509
Test: build, unit tests
Change-Id: I064aa34641e784dca6c529c51cb192069821d64a
2020-01-31 20:01:47 +00:00
Tom Cherry
ff464b1288 liblog: always restore errno in logging functions
Some recent changes can have these logging functions potentially set
errno.  This change places android::base::ErrnoRestorer at the entry
point of the public functions where we want to guarantee errno is
restored to ensure this will not happen again.

Test: build
Change-Id: Iab4170ab16b9c7301474a509ee42d38b370b91a4
2020-01-27 13:50:44 -08:00
Tom Cherry
2ec6a53a46 liblog: use default tag for loggability checks if no tag is provided
Bug: 116329414
Bug: 119867234
Test: new unit tests
Change-Id: I92a3f4f95e5f482f6fe20f17ed83c8ed367b06dc
2020-01-27 09:38:54 -08:00
Maciej Żenczykowski
041f8f5054 add NOLINTNEXTLINE(google-explicit-constructor)
Test: builds without warnings
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Iac7c63816dd7bf2536c0d465ef1e845330be505b
2020-01-26 20:15:07 -08:00
Yifan Hong
d66e442c21 Merge changes from topic "ramdisk"
* changes:
  Add e2fsck to fs_config
  Make ramdisk_available.
2020-01-24 22:39:35 +00:00
Tom Cherry
69ee5dde99 Move default tag from libbase to liblog
Bug: 119867234
Test: log tags look right, libbase/liblog unit tests
Change-Id: I3670c3fdce3d0238a23a53bba2877ffed1291f9c
2020-01-23 08:39:31 -08:00
Yifan Hong
7dd4553991 Make ramdisk_available.
Test: pass
Bug: 147347110
Change-Id: I9787e386e552393efc5beed5aac577be9ce1a3c5
2020-01-22 16:47:14 -08:00
Elliott Hughes
7becf26301 Merge "Make OSTREAM_STRING_POINTER_USAGE_WARNING build even with GCC" 2020-01-22 01:06:05 +00:00
Tom Cherry
0391a879f7 Move minimum log priority from libbase to liblog
See the previous commit moving SetLogger and SetAborter to liblog for
motivation.

This creates more harmony between the two mechanisms in libbase and
liblog for checking loggability.
Currently:
1) libbase filters all messages based on its minimum log priority. For
   example, if minimum log priority in libbase remained at its
   default, but a tag was specifically opted into DEBUG logs via
   log.tag.<tag>, libbase would not print this log.
2) liblog ignores libbase's minimum log priority.  For example if a
   process called SetMinimumLogPriority(WARNING) but used a library
   that logged via liblog's ALOGI macro, that log would still be
   printed even though the process intends on filtering out those INFO
   messages.

With this change:
1) If both a minimum log priority and a priority through log.tag.<tag>
   are set, then the lower of the two values is used.
2) If only one or the other is set, then that value is used.  This
   fixes the two issues described above.
3) If neither of these values are set, then the default of using INFO
   is unchanged.

Bug: 116329414
Bug: 119867234
Test: libbase and liblog minimum log priority tests
Change-Id: Icb49b30b9d93bf797470e23730ae9e537931bb6c
2020-01-21 12:14:43 -08:00
Tom Cherry
349b0c43ad Move SetLogger and SetAborter from libbase to liblog
libbase is copied into each APEX module which requires it, meaning
that there may be multiple instances of libbase running within a
single process with their own copy of libbase's globals.  This means
that SetLogger() and SetAborter() will only impact logs from the
instance of libbase that calls it.  This change moves this state to
liblog, since it will only ever have one instance in a single
process.

One major side-effect here is that now both ALOGE style and LOG(...)
style logs will be handled through the same logger function.  For
example, a logger specified through libbase's SetLogger() will now see
logs sent to liblog through ALOGE().  This is intended behavior.

A second side-effect is that libbase's stderr logger is used for all
host logging now.  It's simply a better logging default than the
fake_log_device logger in liblog currently and makes ALOGE and
LOG(...) logs on host follow the same format.

Bug: 119867234
Test: libbase and liblog unit tests; logging works
Change-Id: Ib52cbfb4e43749e50910ed19a993dffae19ace86
2020-01-21 11:05:24 -08:00
Davide Pallotti
2acd25a33d Make OSTREAM_STRING_POINTER_USAGE_WARNING build even with GCC
This is for projects built with GCC that import parts of Android that,
despite not having Android-specific dependencies, still end up
depending on logging.h.
Also removes outdated notes.

Change-Id: I5a47b302bcaeeb935592d8fc7ad2fe5068d226c3
2020-01-21 10:54:36 +01:00
Tom Cherry
f633b62b63 Merge "Require liblog for libbase in all configurations" 2020-01-15 23:20:07 +00:00
Peter Collingbourne
9ef741ccac Merge "Add unique_fd::operator{==,!=} overloads that take a unique_fd." 2020-01-15 01:24:01 +00:00
Peter Collingbourne
cc9566b6df Add unique_fd::operator{==,!=} overloads that take a unique_fd.
With C++20, any use of the existing overloads with two unique_fd
arguments is likely to become ambiguous:
http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20200113/301880.html

Newer versions of clang complain about this ambiguity. Fix the error by
adding overloads that take two unique_fds.

Bug: 145916209
Change-Id: I18a292827d8841b6d24f948682123ab54dc7aaca
2020-01-13 18:44:11 -08:00
Tom Cherry
d044eaa4e7 base: remove LOG_TO(), PLOG_TO() and LOG_STREAM_TO() macros
There were only two users of these and both of them were better off
setting up a default logger.

These macros are not particularly useful as it's not useful for a
single program to write to both the MAIN and SYSTEM logs.  In fact,
the opposite of these macros would be more beneficial: having more
programs write to only the MAIN or only the SYSTEM buffer, so getting
rid of these macros removes a temptation for bad behavior.

Users that absolutely need to do this behavior can still use the
liblog macros or functions, but that should be an extreme edge case,
such as the few programs that write to the CRASH buffer and does not
need to exist in libbase.

Bug: 119867234
Test: build
Change-Id: I23369c3b48ed636b617220cab47f77fdd5559763
2020-01-13 09:29:15 -08:00
Tom Cherry
992163021d Require liblog for libbase in all configurations
liblog will soon be required for all of libbase's logging.  This
change proactively requires liblog in all configurations instead of
just Android.

Bug: 119867234
Test: build
Change-Id: I696162fbebc78d4ef23c6032412101ac51d397a4
2020-01-09 10:43:26 -08:00
Elliott Hughes
fba2a1a1ec Fix SEGV in libziparchive with malformed zip file.
d77c99ebc3 changed MappedFile to return a
bogus zero-length mapping on failure rather than nullptr. None of the
calling code was changed, though, and it seems like doing so would be a
bad idea. Revert that part of the change.

Add missing tests, and tidy up some of the logging. Also remove
single-use or obfuscatory constants from the tests.

The new "empty.zip" was created by using zip(1) to create a zip file
with one entry, then using `zip -d` to remove it.

The new "zero-size-cd.zip" was created by using zip(1) to create a zip
file containing a single empty file, and then hex editing the two byte
"size of the central directory" field in the "end of central directory
record" structure at the end of the file. (This is equivalent to, but
much smaller than, the example zip file provided by the bug reporter.)

Bug: http://b/145925341
Test: treehugger
Change-Id: Iff64673bce7dae886ccbc9dd6c2bbe18de19f9d2
2019-12-17 08:39:09 -08:00
Josh Gao
81e316b3a2 base: avoid evaluating macro argument multiple times.
Previously, in the regex test helpers, we would evaluate the haystack
expression again to generate the error message, which leads to
nonsensical errors if the expression returns a different value on the
second call (e.g. functions like dlerror which return null on subsequent
calls).

Test: bionic-unit-tests with a failure
Test: treehugger
Change-Id: I2126cefeb45e26638194af8a82d0f2a9d7196edf
2019-12-12 14:25:45 -08:00
Josh Gao
9e6ac64871 Merge "base: steal Chromium's NoDestructor." 2019-12-06 23:03:39 +00:00
Josh Gao
efc9a63bde base: steal Chromium's NoDestructor.
Pillage from Chromium a wrapper type that skips destruction of its
wrapped type, to avoid problems with premature destruction of
variables with static lifetime.

Test: libbase_test on host
Change-Id: I7d4541f7b59f467b232d5c4f8250dc1ea45e28fa
2019-12-06 13:23:05 -08:00
Daniel Colascione
9e3cbb64ee Expose ParseBool from libbase
Also change the properties implementation to call the new API.  We use
this ParseBool API in the new SystemProperties implementation, with
which we want the libbase property API to be consistent.

Test: included
Change-Id: I89cb3eb4e1203a6bb0da41914dad720e44c00303
2019-11-19 13:31:50 -08:00
Elliott Hughes
908e0dfda5 Add absl-like StringReplace.
Needed for cli-test.

Test: treehugger
Change-Id: Ib1fd01ef7f3e54e5778cc548dd789b5fcfcb7bd9
2019-11-18 16:02:31 -08:00
Elliott Hughes
10dc4ca3cd libbase: add a README.md covering the most frequent question.
Test: N/A
Change-Id: Iebea3e74bb7d59778fa0d2342e51bb4ffc5450a3
2019-11-06 09:38:21 -08:00
Elliott Hughes
2d70aad034 Remove Mac/Windows quick_exit() implementation.
This was added with the intention of using it in adb, but then the
change that would have used it
(https://android-review.googlesource.com/c/platform/system/core/+/273824)
was abandoned.

Remove the corresponding (never used) implementation.

Bug: http://b/31468413
Test: treehugger
Change-Id: I42322d079c175b7c6fbd12940e5bc022bd9ebd1c
2019-10-29 13:30:43 -07:00
Alex Buynytskyy
0c2819fc9d Addressing comments in 1130236.
Use Unicode versions of mktemp function.
Correctly handle unset TMP variable in tests.

Test: atest libbase_test
Change-Id: I5009962d33b224c4d5f39102e03daeb0837f1525
2019-10-02 16:48:22 -07:00
Alex Buynytskyy
43f0b0c4ba Using WideChar->UTF8 versions of Windows API to obtain temp folder.
This will correctly handle non-ascii chars in paths.
+Windows specific tests for non-unicode chars and root of disk as a temp
folder.

Test: atest libbase_test

Change-Id: Ief3ee26df93e122250441bfe41f0440fe62bfadc
2019-10-02 02:22:23 +00:00
Bowgo Tsai
41d8a576ec <android-base/endian.h>: fix interoperability issue
<winsock2.h> has defined htonl(), ntohl(), htons() and ntohs() with
different return type that cannot be replaced by those macro definitions
in <android-base/endian.h>.

Includes <winsock2.h> first to prevent them from being replaced.
Then defines the macro later so we don't need to call into DLL
when using those functions.

Bug: 139639521
Test: m libbase_test && wine out/host/windows-x86/nativetest64/libbase_test/libbase_test64.exe
Test: m checkbuild
Change-Id: I672f23a31c6800df10e04d36695d446bca4c91e9
2019-09-18 13:06:45 +08:00
Paul Crowley
b3236ebab6 Remove -D_FILE_OFFSET_BITS=64 on host builds
This prevents us from using glibc's <fts.h> because of
https://sourceware.org/bugzilla/show_bug.cgi?id=11460,
and we now only support 64-bit hosts.

Bug: 139544611
Test: Treehugger
Change-Id: Iaacc9ea17473201bf3d701e82081264921062d5e
2019-09-12 00:27:38 +00:00
Daniel Colascione
7c706baedb Improve codegen slightly when doing FD validity checks
See https://gcc.godbolt.org/z/hGUZIk

Test: inspection
Bug: 140506797
Change-Id: I6fe87b16e18c93b7fc3ca28edbbc68c245e7e5e5
2019-09-04 13:46:50 -07:00
Haibo Huang
f4c3471d22 Include chrono.h
Upstream merged time.h into chrono.h

Test: build
Change-Id: Iadbdfa4902ddd2dd38ee1dba51ef59def54130e4
2019-08-27 20:38:14 +00:00
Yurii Zubrytskyi
5f0e5e0ecc Merge "[base] Make MappedFile work with OS file handles" 2019-08-15 16:50:31 +00:00
Yurii Zubrytskyi
d77c99ebc3 [base] Make MappedFile work with OS file handles
adb uses its own implementation of Windows HANDLE->int mapping,
and it doesn't play well with _get_osfhandle() function used in
MappedFile::FromFd().

This CL adds another function that accepts raw handle, so adb
can pass it directly

+ make constant functions 'const'
+ make the MappedFile movable, as nothing prevents it from being
  one

Test: libbase_test

Change-Id: Ifde4a4094b910e9c7b431126ecf3fef5aa3bb4a6
2019-08-14 10:04:32 -07:00
Colin Cross
888c1b9de5 Fix more endian.h issues
__BIONIC__ is defined in sys/cdefs.h, __GLIBC__ is defined in
features.h (which is included from sys/cdefs.h).  If sys/cdefs.h
was not included before android-base/endian.h it was always falling
back to the Windows definitions.

mingw defines LITTLE_ENDIAN, BIG_ENDIAN and BYTE_ORDER in
sys/params.h, use those definitions to avoid conflicts.

glibc uses different names for letoh*, add compatibily #defines.

Test: m checkbuild
Change-Id: I0709a964cc8f20dd9fa4f03064cc67d97ae6c525
2019-08-13 15:27:00 -07:00
Elliott Hughes
f86ea02de6 Use as much of the macOS endian support as we can.
Turns out that although there's no <endian.h> or <sys/endian.h>, there
are <machine/endian.h> and <sys/_endian.h>, and they're included by
other system headers such as <dirent.h>.

Reuse the contents of <sys/_endian.h> here for better interop.

Bug: http://b/139203733
Test: treehugger
Change-Id: Ic0e9bfa1a5b56d05e9e542839d237b6ceae4aa8c
2019-08-12 12:11:42 -07:00
Anatol Pomazau
fd41f9a579 Add header that defines strerror()
It helps to fix a compilation issue with host libc:

core/base/errors_unix.cpp:25:10: error: use of undeclared identifier 'strerror'
  return strerror(error_code);
         ^

Change-Id: I71d4221cc1bfa6f0cc764b40b6b28159994b8e04
2019-08-08 23:31:36 +00:00
Tomasz Wasilczyk
c9ec95272c Merge "Implement unique_fd.ok()" 2019-07-24 13:51:43 +00:00