Commit graph

884 commits

Author SHA1 Message Date
Eric Miao
4a33c22c77 Merge "String8: fix infinite loop and segmentation fault in removeAll()" into main 2023-07-19 02:03:40 +00:00
Eric Miao
c6ce48ef19 String8: fix infinite loop and segmentation fault in removeAll()
Bug: 290835996
Test: libutils_fuzz_string8 for several minutes

String8::removeAll() has 2 serious problems:

1. When `other` is an empty string, `removeAll()` will loop infinitely
   due to below process:

   a) with `other` being empty string `""`, find() will call strstr()
      on an empty string, which always returns `mString`, and thus
      find() always return 0 in this case
   b) with find() returns 0 for empty string, the next while loop in
      String8::removeAll() will keep loop infinitely as `index` will
      always be 0

   This CL fixes this problem by returning true if `other` is an empty
   string (i.e. `strlen(other) == 0`), this follows the logic that an
   empty string will always be found and no actual remove needs to be
   done.

2. When `other` is a NULL string, strstr() has undefined behavior. See
   https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf.

   This undefined behavior on Android unfortunately causes immediate
   segmentation fault as the current `strstr` implementation in bionic
   libc doesn't check `needle` being NULL, and an access to a NULL
   location is performed to check if the `needle` string is an empty
   string, and thus causes segmentation fault.

   This CL gives an error message and aborts instead of having a
   segfault, and to keep some backward compatibility.

   This CL also adds test for String8::removeAll()

Change-Id: Ie2ccee6767efe0fed476db4ec6072717198279e9
2023-07-18 16:02:07 -07:00
Treehugger Robot
435b520bf0 Merge "Fix libutils_fuzz_string8 deadlock." into main 2023-07-18 21:49:06 +00:00
Steven Moreland
749becfa68 Fix libutils_fuzz_string8 deadlock.
Bug: 290835996
Test: libutils_fuzz_string8 for several minutes
Change-Id: I9b312dd968c380f4fa2a837d38121d0a7a7ac7b1
2023-07-18 21:04:07 +00:00
Eric Miao
cb199b4795 libutils: Improve performance of utf8_to_utf16/utf16_to_utf8
This CL improves the performance of below functions in helping with conversion
between utf8/utf16 with libutils:

  - utf8_to_utf16_length
  - utf8_to_utf16
  - utf16_to_utf8_length
  - utf16_to_utf

The basic idea is to keep the loop as tight as possible for the most
common cases, e.g. in UTF16-->UTF8 case, the most common case is
when the character is < 0x80 (ASCII), next is when it's < 0x0800 (
most Latin), and so on.

This version of implementation reduces the number of instructions
needed for every incoming utf-8 bytes in the original implementation
where:

  1) calculating how many bytes needed given a leading UTF-8 byte
     in utf8_codepoint_len(), it's a very clever way but involves
     multiple instructions to calculate regardless

  2) and an intermediate conversion to utf32, and then to utf16
     utf8_to_utf32_codepoint()

The end result is about ~1.5x throughput improvement.

Benchmark results on redfin (64bit) before the change:

utf8_to_utf16_length: bytes_per_second=307.556M/s
utf8_to_utf16:        bytes_per_second=246.664M/s
utf16_to_utf8_length: bytes_per_second=482.241M/s
utf16_to_utf8:        bytes_per_second=351.376M/s

After the change:

utf8_to_utf16_length: bytes_per_second=544.022M/s
utf8_to_utf16:        bytes_per_second=471.135M/s
utf16_to_utf8_length: bytes_per_second=685.381M/s
utf16_to_utf8:        bytes_per_second=580.004M/s

Ideas for future improvement could include alignment handling and loop
unrolling to increase throughput more.

This CL also fixes issues below:

  1. utf16_to_utf8_length() should return 0 when the source string has
     length of 0, the original code returns -1 as below:

    ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
    {
        if (src == nullptr || src_len == 0) {
            return -1;
        }
	...

  2. utf8_to_utf16() should check whether input string is valid.

Change-Id: I546138a7a8050681a524eabce9864219fc44f48e
2023-07-12 13:23:07 -07:00
Treehugger Robot
f5d4edaa31 Merge "libutils: rewrite Vector fuzzer" 2023-07-01 01:28:13 +00:00
Steven Moreland
379d5c83d5 libutils: rewrite Vector fuzzer
It could never have gotten much coverage.

Bug: 288741501
Test: libutils_fuzz_vector (2,000,000 iterations)
                           (~60k-100k iterations/s)
Change-Id: I6f442642b5a3246dd08784f735db5aad5fd4d398
2023-07-01 00:28:48 +00:00
Tomasz Wasilczyk
aab4105ef6 Drop const assignment operator.
Also, silence cert-oop54-cpp - self-assignment is already handled in
VectorImpl class.

Bug: 289151149
Test: it builds
Change-Id: I8be7714ed53d1515df7cfdf6de6f3c90b3e5cc76
2023-06-28 11:21:47 -07:00
Fabien Sanglard
2aba0a2283 Fix LruCache, allow std:string caching
The default initalization for mNullValue uses 0 which is in the case of
a std::string TValue will invoke the contructor with undefined behavior
parameter.

Using an empty uniform initialization {} addresses the problem.

Test: Already tested in lrucache_test.cpp
Bug: 257127748
Change-Id: I37420ce8a16c99f3014538a0208d7e113870b1c7
2023-06-15 00:37:52 +00:00
Hao Chen
b3e1993ee5 Add the Missing Header
`stderror` is used in this file and it is defined in `string.h/cstring`, so
the header file needs to be directly included.

Test: build
Bug: 285204695
Change-Id: Idf34126626ad0e9bb397df3d5da50439bf18381d
2023-06-05 13:49:17 -07:00
Hans Boehm
3e8ad4d546 Halve iteration count for some RefBase tests
The RacingDestructors test was occasionally timing out, by non-huge
amounts. Halve the number of iterations for long-running tests here.

Bug: 284964396
Test: Treehugger
Change-Id: If639ce98fbeb783431f07006dece4cd69f9f8b50
2023-05-31 10:19:54 -07:00
zijunzhao
6634c1741e Fix the missing std
Bug: b/239662094
Test: enable Wunqualified-std-cast-call locally and run m to build
Change-Id: I203af8dbbbf1d889b9fc5efd5b62fa165ee09dc2
2023-03-01 23:30:29 +00:00
Zhi Dou
ff9e640e43 Replace "apex_inherit" min_sdk_version
Replace "apex_inherit" min_sdk_version to a conditional setting. If
environment veriable KEEP_APEX_INHERIT is set, using "apex_inherit" as
the min_sdk_version, otherwise set the number to "29". For more detail
please refer
https://docs.google.com/document/d/1R2vZw0cQa-haAMgFyQ682uSq9aGBNQrzMHKIsU17-XY/edit?usp=sharing&resourcekey=0-gUbs463r9LCKs7vdP_Xkmg

Test: build APEX uses this library, and presubmit
Bug: 254634795
Change-Id: If7acfce5fb2e1cb1cc7208a8c57b1e1cd1499c11
Merged-In: Ie6984128e6b84ba73de3f4c08eca5560657c5ca2
2022-12-20 16:05:54 +00:00
Hsin-Yi Chen
20d45cc9ce Merge "Add an ABI dump directory for libutils" 2022-12-09 01:57:43 +00:00
Hsin-Yi Chen
3de02bd0d5 Add an ABI dump directory for libutils
The ABI dumps add visibility for API changes in a commonly used library.

Test: development/vndk/tools/header-checker/utils/create_reference_dumps.py \
      -libs libutils -products aosp_arm64 \
      -ref-dump-dir system/core/libutils/abi-dumps
Test: m libutils.vendor
Bug: 227282691
Change-Id: I498c62853562a8fc6702bdd622603b6f7d516274
2022-12-08 11:06:22 +08:00
Eric Miao
c2527073c2 libutils: Add more tests for Unicode
This CL added additional tests for converting between utf16 and utf8,
specifically tests that:

  1. check utf16_to_utf8_length() returns 0 if input is an
     empty UTF16 string

  2. check utf16_to_utf8_length() returns 1 if input is a
     single ASCII character UTF16 string

  3. check utf16_to_utf8_length() returns 3 if input is a
     single UTF-16 character between U+0800 - U+FFFF

  4. check utf16_to_utf8_length() returns 4 if input has
     a surrogate pair

  5. check unpaired UTF-16 surrogate is handled correctly
     (skipped)

  6. check utf16_to_utf8_length(0 handles invalid surrogate
     case correctly, by skipping the first but handling the
     rest correctly

  7. check a normal string with a mix of 1/2/3/4-byte UTF8
     characters is correctly converted by utf16_to_utf8()

  8. check conversion from invalid utf8 sequence with invalid
     leading byte and/or invalid trailing byte(s) should still
     work and not crash

Change-Id: If68e514af0e84ddebf5900b2e140e76ba4f44553
2022-12-06 15:14:27 -08:00
Biswapriyo Nath
890f064990 libutils: Fix missing definition of shared_ptr and unique_ptr
This includes memory header for shared_ptr and unique_ptr templates.
Fixes the following errors:

RefBase.h:803:1: error: ‘shared_ptr’ does not name a type
RefBase.h:810:1: error: ‘unique_ptr’ does not name a type

Change-Id: I6a7a67333c9ef05250c5a3c6199d7fac288f946b
2022-11-25 12:15:53 +05:30
Biswapriyo Nath
9e25bf0136 libutils: Include limits for std::numeric_limits::max
This fixes the following error:
core/libutils/String8.cpp:316:67: error: incomplete type 'std::numeric_limits<long long unsigned int>' used in nested name specifier
  316 |         if (static_cast<size_t>(n) > std::numeric_limits<size_t>::max() - 1 ||
      |                                                                   ^~~

Change-Id: I80cdd2442e2798c37e066e3fdeee7dd5ac34b669
2022-10-21 10:26:08 +05:30
Steven Moreland
b7412c8cd6 libutils: RefBase DEBUG_REF love
Done here:
- path saved to a useable location on host
- path always printed (for visibility for new users)
- open(.., 666) - That's '01204' not '0644' oops

Future considerations:
- make defines constexpr instead of ifdef
- copy malloc hook design to avoid needing to recompile code
- make libutilscallstack default on host
- run libutils tests, not just test compile debug mode
- code incorrectly prints ref 'doesn't exist'
  seems it's gotten out of sync, but still good enough
  to use stacktraces

Bug: 244325464
Test: manual
Change-Id: I732e5b8aec3cd946ef3559a2a814caf693846cc0
2022-10-10 16:58:57 +00:00
Steven Moreland
377adea81c libutils: DEBUG_* modes compile forever
I've seen these fixed before, so we compile the debug modes
now.

Bug: 244325464
Test: 'm libutils_test_compile'
Change-Id: I4271909e81893ad448bc46b8a3a567a84c40f8a3
2022-10-08 05:13:47 +00:00
Elliott Hughes
b795d6fa4b Fix the build with a newer LLVM.
Unify all our "noinline" variants to the current most common one, not
least because the new [[noinline]] syntax is fussier about where it goes.

Test: treehugger
Change-Id: Icfcb75c9d687f0f05c19f66ee778fd8962519436
2022-09-14 20:16:25 +00:00
Christopher Ferris
15fee82247 Fix thread unwind in CallStack.
The CallStack unwind of a different thread was broken since it
wasn't properly setting the tid value.

Fix this problem and add new unit tests to verify the behavior.

Bug: 246405269

Test: New unit tests pass.
Test: Ran unit tests for 1000 operations to verify not flaky.
Change-Id: I00342e6cdcdb4bcb68f29734dadee6c987c98040
2022-09-12 18:37:22 -07:00
Ryan Prichard
4913ca88e5 Remove unnecessary std::unary_function base classes
The function objects work equally well without them, and the base
classes were wrong for both types:
 * HashForEntry: returns size_t but declared to return hash_t
   (uint32_t)
 * EqualityForHashedEntries: returns bool and takes two parameters but
   declared to return hash_t and take one parameter

std::unary_function was deprecated in C++11 and removed in C++17.
Upstream libc++ now removes the type for new-enough C++ dialects.

Bug: http://b/175635923
Test: treehugger
Change-Id: I2ff15c5da6a4e4f71df08c243f8af2f11d8d2b0d
2022-08-26 20:24:57 -07:00
Steven Moreland
643d08e706 Merge "RefBase: test for stack check" am: 5daa3bb90e
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2169132

Change-Id: Ib7b35af8469fdc563244f2dde4a4e0ee91e7f35a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-07-29 18:31:32 +00:00
Steven Moreland
5daa3bb90e Merge "RefBase: test for stack check" 2022-07-29 18:10:03 +00:00
Steven Moreland
cd4ef87efd RefBase: test for stack check
Surprised this isn't breaking anything, so wanted to
make sure it worked.

Bug: 232557259
Test: libutils_test
Change-Id: Iaec47d644c02dc190e397c6f84dcfab4cc76f566
2022-07-29 00:54:57 +00:00
Steven Moreland
c55c68fe33 Merge "libutils: disallow extending lifetime on stack" am: ea25b4ba5b
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2166445

Change-Id: I48eb86eb8426b1e5c7c92f957173b2eb63870864
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-07-28 23:45:34 +00:00
Steven Moreland
ea25b4ba5b Merge "libutils: disallow extending lifetime on stack" 2022-07-28 23:17:35 +00:00
Steven Moreland
de52706513 Merge "libutils: RefBase always disallow on stack" am: 51e98b8378
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2166102

Change-Id: Ia8b3205fbb6e36b3c85a378df4c63db4f32759f5
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-07-28 16:29:49 +00:00
Steven Moreland
51e98b8378 Merge "libutils: RefBase always disallow on stack" 2022-07-28 16:11:10 +00:00
Steven Moreland
414bab91eb Merge "libutils: RefBase: extra check for double own" am: b0fe01da56
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2166103

Change-Id: Ie252e8a229d8151aad82347f6b216dbed752d86b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-07-26 22:27:26 +00:00
Steven Moreland
d086fe5109 libutils: disallow extending lifetime on stack
Bug: 232557259
Test: libutils_test
Change-Id: Iacf45b9f295a48904606ced35994ba35566bfcc3
2022-07-26 22:11:13 +00:00
Steven Moreland
c340a08b1b libutils: RefBase always disallow on stack
Before, we only did this in sp<> constructors, but we can always make
this check during the initial incStrong.

Since prebuilts call into the existing report race function declared
in StrongPointer.h, we still call this function from RefBase.cpp.

Bug: 232557259
Test: libutils_test
Change-Id: I4080b1869b83ecf655fc9c182b6de768a6358adf
2022-07-26 22:10:51 +00:00
Steven Moreland
483a2def8d libutils: RefBase: extra check for double own
Bug: 232557259
Test: libutils_test
Change-Id: Ibd400750e7973600ec3fa493838a3b52cafe3add
2022-07-26 17:59:14 +00:00
Xin Li
ebe6598a1c Merge tm-dev-plus-aosp-without-vendor@8763363
Bug: 236760014
Merged-In: Ia927c19f544536f78c20ccef5830bd1be0cebf9b
Change-Id: I0267b9eaad470a56db68f3a0b99abfc41192c6d7
2022-06-28 21:23:43 +00:00
Steven Moreland
9d8af6af65 RefBase: disallow make_shared, make_unique
It's risky to mix multiple-ownership types. Taken from SharedRefBase.

Bug: 232557259
Test: N/A
Change-Id: Ic0dbd6d11e44fa9db87c4f9b1776d4989cbf9f56
2022-06-22 21:54:43 +00:00
Hsin-Yi Chen
d82bd5a626 RESTRICT AUTOMERGE Ignore weak symbol difference in libutils ABI check am: c7d9320ca4
Original change: https://googleplex-android-review.googlesource.com/c/platform/system/core/+/18603830

Change-Id: Ia15131ac6bb49823df032e39a8ee81e5d332addb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-05-27 03:18:38 +00:00
Hsin-Yi Chen
c7d9320ca4 RESTRICT AUTOMERGE Ignore weak symbol difference in libutils ABI check
The diff_flags make the ABI checker ignore the weak symbol difference
caused by PGO.

Ignore-AOSP-First: This patch relaxes the ABI check for T only.
                   b/232982219 tracks the long-term solution.
Test: make libutils.vendor
Bug: 230076879
Change-Id: I8ec2c0f5a540263b4e8a0a4f9cf26f9c297593b5
2022-05-26 15:20:06 +08:00
Treehugger Robot
e7da3eb805 Merge "Doc Thread requirement." am: c545516e2c am: 40ce64b6db
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2094349

Change-Id: I95cebb43fcd70803fe0bdece39d57546053264cb
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-05-12 03:21:16 +00:00
Treehugger Robot
c545516e2c Merge "Doc Thread requirement." 2022-05-12 01:53:32 +00:00
Christopher Ferris
39819bbc94 Merge "Use the new AndroidUnwinder object." am: af4db6749a am: ce1c33332c
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2095264

Change-Id: Icc76b39136bce395106c070aeb338d4e6922a28e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-05-12 00:32:48 +00:00
Steven Moreland
90722d254e Doc Thread requirement.
Fixes: 230893354
Test: N/A
Change-Id: I426184840c7bf61fe3369388cfc6db184470acf0
2022-05-11 23:13:17 +00:00
Christopher Ferris
ab63124cd9 Use the new AndroidUnwinder object.
Replaces libbacktrace in CallStack. There is one small behavioral
change, the BuildId data is added to the unwinds.

Bug: 120606663

Test: All unit tests pass.
Test: Run the fuzzer for over an hour without any crashes.
Change-Id: Ic8a4247c515ce0d3cdc4d2cc15167d1948b15fa5
2022-05-10 17:19:12 -07:00
Elliott Hughes
5affe3efe2 Merge "libutils: clearer abort on overflow." am: 4ba0e62970 am: 2c623bc650
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2078005

Change-Id: I150014b6afda9aff751383c5b5b6ff2934ec7400
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-04-28 01:15:58 +00:00
Elliott Hughes
b10bf63c93 libutils: clearer abort on overflow.
Let's turn a bug into a feature... Since this code is built with intsan,
anyone who caused overflow here will have had an abort, so we know
no-one actually needs the BAD_INDEX return that was presumably the
original author's intent. So let's just mandate that, since it's a lot
harder to ignore an abort than it is to ignore an error return.

Bug: http://b/179044558
Test: treehugger
Change-Id: I08f1018f9da1e09de885699138b7543d55bb2a36
(cherry picked from commit a5f2e4d421)
Merged-In: I08f1018f9da1e09de885699138b7543d55bb2a36
2022-04-28 00:25:25 +00:00
Elliott Hughes
a5f2e4d421 libutils: clearer abort on overflow.
Let's turn a bug into a feature... Since this code is built with intsan,
anyone who caused overflow here will have had an abort, so we know
no-one actually needs the BAD_INDEX return that was presumably the
original author's intent. So let's just mandate that, since it's a lot
harder to ignore an abort than it is to ignore an error return.

Bug: http://b/179044558
Test: treehugger
Change-Id: I08f1018f9da1e09de885699138b7543d55bb2a36
2022-04-27 14:29:44 -07:00
Steven Moreland
6a1358236e Merge "Mark libutilscallstack min sdk version." am: f5ca027819 am: d94c71f4fb am: 83441e640e
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2061788

Change-Id: I0675d8f86306e73c977b8eed9adb513d902ce684
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-04-12 18:54:48 +00:00
Steven Moreland
6c509ca3c3 Mark libutilscallstack min sdk version.
Mark this as 29 for easy inclusion in APEXes when debugging. Make
sure we don't introduce calls to things here which would prevent
this from being used on old platforms.

Fixes: 228561718
Test: build
Change-Id: I2574455dbfe681117e4afcf6eef3546be51393fa
2022-04-11 22:30:07 +00:00
Florian Mayer
2b393f3664 Merge "Make callback outlive LruCache." am: 6948bbf62c am: 9e67f9302c am: 877cd36104
Original change: https://android-review.googlesource.com/c/platform/system/core/+/2052189

Change-Id: Ibe676c9758cb914098ea775e05d8f468c0a564e2
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
2022-04-01 01:20:50 +00:00
Florian Mayer
e0240d3f61 Make callback outlive LruCache.
The callback can be called by the destructor of LruCache, so it needs to be destructed last.

Bug: 227635615
Change-Id: I7d965a2000c1ec32d9c9e88d25ab0c1ba3e9c739
2022-03-31 20:21:28 +00:00