Commit graph

344 commits

Author SHA1 Message Date
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
Tomasz Wasilczyk
c3fc9607c2 Implement unique_fd.ok()
While I'm here, I'll fix how unique_fd disallows copy and assignment
constructors (detele instead of marking them private).

Bug: 135918744
Test: WiP change in master
Change-Id: Idefcc685943326c511f59d18790c1c4fa2e04989
2019-07-23 16:35:38 -07:00
Treehugger Robot
8987f061c4 Merge "Add nolint for implicit borrowed_fd constructors" 2019-07-16 04:11:20 +00:00
Tom Cherry
aa55ee6044 Add nolint for implicit borrowed_fd constructors
bpfloader uses both clang-tidy and this header, so the below lint
warnings are generated:

/work/aosp/system/core/base/include/android-base/unique_fd.h:261:18:
  warning: single-argument constructors must be marked explicit to
  avoid unintentional implicit conversions
  [google-explicit-constructor]
  /* implicit */ borrowed_fd(int fd) : fd_(fd) {}
                 ^
                 explicit

/work/aosp/system/core/base/include/android-base/unique_fd.h:263:18:
  warning: single-argument constructors must be marked explicit to
  avoid unintentional implicit conversions
  [google-explicit-constructor]
  /* implicit */ borrowed_fd(const unique_fd_impl<T>& ufd) : fd_(ufd.get()) {}
                 ^
                 explicit

Add NOLINT to quiet them.

Test: build without this lint warning
Change-Id: I5241938c33070b0fa39888289b8ca67d6d94ac73
2019-07-15 15:00:23 -07:00
Tom Cherry
820333dce4 Add noexcept to missing places in expected.h
These move and assignment operations are conditionally noexcept, so
add that to their definitions.  This is a performance issue as well as
correctness, since std containers will copy instead of move during
resize if these operations are not noexcept.

Test: build
Change-Id: I148f7eb3489e7f1dd68cc0fb0e555b56470e42da
2019-07-09 17:06:08 -07:00
Tom Cherry
a93e09f834 Merge "Don't implement our own remove_cvref_t." 2019-07-09 20:10:22 +00:00
Tom Cherry
a2928b52ff Don't implement our own remove_cvref_t.
clang-tidy flagged the anonymous namespace in the header as a warning,
but in any case, it'll be cleaner to implement this in terms of the
existing type traits.

Test: build
Change-Id: I189986d2a855c028e28dd9d62ab9da012feddc9b
2019-07-09 09:55:47 -07:00
Tom Cherry
247ffbf314 Fix a few clang-tidy issues and add NOLINT for others
android-base:
* Add NOLINT for expanding namespace std for std::string* ostream
  overload

libdm:
* Fix missing parentesis around macro parameters

init:
* Fix missing CLOEXEC usage and add NOLINT for the intended
  usages.
* Fix missing parentesis around macro parameters
* Fix erase() / remove_if() idiom
* Correctly specific unsigned char when intended
* 'namespace flags' should be signed, since 'flags' it signed for
  clone()
* Add clear to property restore vector<string> to empty after move
* Explicit comparison against 0 for strcmp

Test: build
Change-Id: I8c31dafda2c43ebc5aa50124cbbd6e23ed2c4101
2019-07-09 16:17:36 +00:00
Tom Cherry
13b1f34279 Add android::base::GetPids() function to return all pids
We loop over /proc to iterate through pids in a lot of code, so let's
consolidate this into a single function in libbase.

Test: new unit test
Change-Id: I908fab90b603546d0e3e8b8acdc8dadfc3552d62
2019-07-01 13:55:46 -07:00
Mohammad Samiul Islam
9f24c38fe3 Prevent definitions of expected.h from being included twice
Bug: 135683564
Test: Included expected.h from two different files. Builds succesfully
    with the fix. Does not build without the fix.

Change-Id: If1962a3b70f7580fa652a043aaf29bf592d1926c
2019-06-20 19:03:32 +01:00
Tom Cherry
b90dcc07d3 Create android-base/format.h that includes fmtlib headers
We're already including fmtlib statically in libbase, so let's also
expose its functionality directly.

Also create a small benchmark of the most common operations for
format() and StringPrintf(): int, float, and string arguments.  Note
that fmt::format is faster than StringPrintf() in all of these cases.

Test: build, run benchmark
Change-Id: I1e18f13673dd89ea936ea5c51418fad723495b08
2019-06-14 14:55:27 -07:00
Jiyong Park
20524ed6a2 Add Errorf and ErrnoErrorf
String formatting for error message can be done by using Errorf and
ErrnoErrorf. e.g. `return Errorf("{} failures", num_failure);`

The string format follows that of fmtlib, not printf.

Bug: 132145659
Test: libbase_test
Change-Id: I65f45d5f12ed23e2c14d6eb442278f467cc42e83
2019-06-14 14:29:36 +09:00
Jooyung Han
df7d32753e fix: passing ResultError to Error thru operator<<
It is typical to pass error to callers like following;

    if (!result) {
      return Error() << result.error();
    }

To transfer errno(or ResultError#code()), Error defines a specialization
operator<<(const ResultError&).

This change fixes so that ResultError is properly handled

Bug: 132145659
Test: atest libbase_test
Change-Id: Ib35457da2d4b923d8e652c54ac510a75546cf918
2019-06-11 13:38:01 +09:00
Tom Cherry
91e6f888d7 Remove Result<Success> in favor of Result<void>
Result<void> is the correct way to do this.

Bug: 132145659
Test: libbase and init unit tests
Change-Id: I9c2e16cee6657a1895f215016df41a8b290383c7
2019-06-10 12:53:15 -07:00
Tom Cherry
383e1418ff base: Add MmapFile as a synonym for MappedFile
I was looking for 'MmapFile' earlier and couldn't find it.  After
being pointed to it, we thought that adding this synonym would help
others trying to find this file.

Test: n/a
Change-Id: I7fbfd8e090f21183a5de3603a76f884562b99c4a
2019-06-07 13:05:22 -07:00
Treehugger Robot
bb1b83e325 Merge "Add expected<void,E>" 2019-06-07 07:17:47 +00:00
Treehugger Robot
2ba59c5b81 Merge "Move result.h from init to libbase" 2019-06-06 23:27:29 +00:00
Jiyong Park
b2a85c4333 Add expected<void,E>
Result<void> could be used instead of Result<Success>.

Bug: 132145659
Test: libbase_test
Change-Id: I7f079a94d06c5ecb8b0303ced981f0995253dc40
2019-06-07 08:10:32 +09:00
Jiyong Park
8fd64c8af1 Move result.h from init to libbase
The Result, Error, ErrnoError are quite generic. Moving them from init
to libbase so that they can be used from other places.

Bug: 132145659
Test: libbase_test
Change-Id: Id774a587f74380fadd7a0fc88c0aa892c3d9a489
2019-06-06 08:58:55 +09:00
Treehugger Robot
44cabca1ef Merge "base: fix out of date documentation with PropertySet()" 2019-06-05 21:24:23 +00:00
Tom Cherry
ef2469e547 base: fix out of date documentation with PropertySet()
As of Android-P, __system_property_set() is synchronous and only will
return '0' if the property was successfully set.

Test: n/a
Change-Id: Icbc0a6d8be1ae683565f378c534ea8e5c5f25357
2019-06-05 10:31:33 -07:00
Treehugger Robot
41f690ba2a Merge "Fix leak of mapped zip central directories" 2019-06-04 12:36:52 +00:00
Jiyong Park
5c06e6e17b Minor fixes on android::base::expected
This is a follow-up change of 7d89fb164b.

- Some of the missing conditions for SFNAIE were added
- Fixed indentation
- The assignment operator became "= default"

Bug: 132145659
Test: libbase_test

Change-Id: Ib5232a6e5e1d3df67e185d6e8c03374105c1ce94
2019-05-29 08:37:32 +09:00
Ryan Mitchell
ce1c49da6f Fix leak of mapped zip central directories
Memory mapping the central directory of specific APKs caused memory
mappings to build up over time because they were never unmapped
correctly. This is because MappedFile is not calling munmap with the
size of the data after aligning the mmap offset to a page boundary.

Bug: 133463863
Test: install APKs and verify that the entire mapped CD is unmaped
Test: ran aapt2 as daemon and confirmed that mapped CD is unmapped
Change-Id: I26bda27c83615d1a166d92e43a2327909c453b89
2019-05-28 22:59:53 +00:00
Jiyong Park
7d89fb164b Add android::base::expected
android::base::expected is an Android implementation of the
std::expected proposal.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0323r7.html

For usage, refer to the expected.h header file and
expected_test.cpp

Bug: 132145659
Test: libbase_test

Change-Id: I65d3a1ecf8654d9858989755dfd0065c81f7b209
2019-05-22 06:39:27 +09:00
Josh Gao
fe1d1e815f Merge "Revert "Revert "Add a way to turn off unique_fd's operator int.""" 2019-05-09 23:09:38 +00:00
Josh Gao
27241a7a96 Revert "Revert "Add a way to turn off unique_fd's operator int.""
The original commit broke aosp-master-with-phones, because of
vendor libraries the depended on the int versions of libbase functions.

This patch reverts the revert, and also adds ABI-compatibility shims for
the replaced functions.

This reverts commit 2c58e1924a.

Bug: http://b/131312539
Test: treehugger
Test: forrest run of aosp-master-with-phones
Change-Id: I75cc84ec8d963e20862f7662e8e2f409471f41cc
2019-05-06 16:18:02 -07:00
dimitry
a808b1150a Enable native_bridge_support
Enable native bridge support for libbase, liblog,
libziparchive and libpropertyinfoparser.

This makes it possible to use them in binaries for translated
architectures.

Bug: http://b/77159578
Test: make
Change-Id: If67ce92288b17a052ea1e79a268e284f7d941439
2019-05-06 14:05:05 +02:00
Elliott Hughes
b4dc7be6c5 libbase: add ConsumePrefix/ConsumeSuffix.
adb was already using ConsumePrefix, and now we have another would-be
user in cutils. (There appears to be one place in adb that should use
ConsumeSuffix, so I'm assuming we'll want that sooner or later.)

I've kept these inline because adb and google3's versions both were, and
I'm easily led.

Test: treehugger
Change-Id: I29d99032f6f6ccbfaefece59725db8afb02a4c87
2019-05-03 12:49:31 -07:00
Ian Kasprzak
2c58e1924a Revert "Add a way to turn off unique_fd's operator int."
This reverts commit 93d63c010a.

Reason for revert: b/131312539 - All aosp-master-with-phones phones failing boot / health check.

Change-Id: Id4239f93484b7d6b8d9bcd355a59cbd40d9766e2
2019-04-25 15:59:28 +00:00
Josh Gao
93d63c010a Add a way to turn off unique_fd's operator int.
unique_fd's implicit conversion to int has led to tons of problems (see
all of the overloads for close, fdopen, fdopendir, etc.). Add a switch
that can turn it off, and reduce the ridiculous amount of work to fix up
callers by introducing a borrowed_fd type that can be constructed from
either int or unique_fd.

Test: treehugger
Change-Id: If77cf5cbcaddacdaec5919a15b3520fb68f51a62
2019-04-24 12:59:42 -07:00
Bernie Innocenti
7cb72c96b4 Disallow operator!() on unique_fd
This catches a common mistake where client code checks for errors using
the common idiom that works for std::iostream and other file-like
classes:

  unique_fd fd = open(...);
  if (!fd) {
  }

Test: atest libbase_test
Test: m droid
Change-Id: I9629a7795537ecb3b57be9c741c06f80967e4cc2
2019-03-28 15:56:07 +09:00
Yifan Hong
f959fffc1c Merge changes from topic "lpdumpd"
* changes:
  libbase: realpath is wrapped with TEMP_FAILURE_RETRY
  liblp: Replace open with GetControlFileOrOpen
  init: expand prop in 'file'
  libcutils: android_get_control_file uses realpath.
2019-03-26 20:35:36 +00:00
Yifan Hong
5f4cb8a240 libbase: realpath is wrapped with TEMP_FAILURE_RETRY
Although man page for realpath doesn't say so, the bionic
implementation of realpath may exit with error code
EINTR. In such cases, retry.

Test: boots (sanity)
Change-Id: Ic5feb7152374a81bd2f475ad74c4bc8c3afb3a20
2019-03-22 14:26:25 -07:00
Yurii Zubrytskyi
59d876c7a2 [base] Convert Starts/Ends/Equals string functions to string_view
Test: UTs

Change-Id: I3751656d44e7a814488169c53d3e8af0da44f262
2019-03-21 16:29:46 +00:00
Pirama Arumuga Nainar
5d7f84144d Support zero-length mapped files
Bug: http://b/119818070 "app crashes when reading asset of zero length"

Add support for zero-length mappings for the Windows code path as well.

Test: ran libbase_test on Windows under wine.
Change-Id: Iccb65fa800c636444100c9369f41e36d24a53a99
2019-03-07 16:12:55 -08:00
Josh Gao
d3387380f7 Hopefully fix more darwin build breakage.
Silence narrowing warnings by manually casting to a smaller type,
convert static_asserts to CHECKs, and switch from using MSG_CMSG_CLOEXEC
to manually setting FD_CLOEXEC.

Test: mma
Change-Id: I8aa02804179d754de860279fe34773800b937ab6
2019-02-19 14:37:02 -08:00
Josh Gao
b57dc7a670 Fix darwin build break.
The <malloc.h> include was vestigial from a previous revision.

Test: mma on linux
Change-Id: Iaf8351bec23c2792cefca0e5c9bd42664488f5c4
2019-02-19 13:17:23 -08:00
Josh Gao
8954d0bc98 Merge changes from topic "base_cmsg"
* changes:
  adb: switch to base::{Send,Receive}FileDescriptors.
  debuggerd: switch to base::{Send,Receive}FileDescriptors.
  base: add helpers for sending/receiving file descriptors.
2019-02-19 20:53:01 +00:00
Josh Gao
14f9500a35 base: add helpers for sending/receiving file descriptors.
Almost all of the uses of cmsg(3) in our source tree are wrong in at
least one of the following ways:

  - not aligning the cmsg buffer
  - leaking fds if more fds are received than expected
  - blindly dereferencing CMSG_DATA without checking the header
  - using CMSG_SPACE instead of CMSG_LEN for .cmsg_len
  - using CMSG_LEN instead of CMSG_SPACE for .msg_controllen
  - using a length specified in number of fds instead of bytes

Implement wrapper functions that implement this dumpster fire of an
API correctly.

Bug: http://b/122047630
Test: libbase_test32
Change-Id: I6ac34d67bbbf1bfa9727ab598248fc178ea19df9
2019-02-13 13:21:54 -08:00
Josh Gao
982f7bd345 base: add ScopedLockAssertion.
This is a useful helper for anyone that's using thread safety
annotations alongside std::condition_variable, extract it from adb and
move it to libbase.

Test: mma
Change-Id: Ic51e2f2a0e6a16628034b29d8ff32bf2155afccd
2019-02-12 16:32:35 -08:00
Elliott Hughes
eb0ef145fd MappedFile and FileMap should support zero-length mappings.
Bug: http://b/119818070 "app crashes when reading asset of zero length"
Test: ran tests
Change-Id: Idd2ad6f6e72c8e445aff78a460fac96dea41c950
2019-02-06 14:29:35 -08:00
Josh Gao
92ee52cc38 base: don't overwrite errno in unique_fd::~unique_fd.
unique_fd's destructor potentially mangling errno makes it difficult to
use correctly in code that sets errno (or, in reality, it makes it so
that errno values get randomly stomped upon if close actually sets
errno, because no one accounts for this case).

Preserve errno ourselves to avoid this.

Test: treehugger
Change-Id: Ib06e6f65866d86fff4032b2311021eaf9226a1af
2019-01-30 15:18:56 -08:00