Commit graph

32 commits

Author SHA1 Message Date
Kai Sky
862f049d17 Looper: Fixed compile error when enabling DEBUG_CALLBACKS
When enabling DEBUG_CALLBACKS , compiler complains
for the undeclared identifier 'fd'.
Fix by removing undeclared objects.

Change-Id: I751a9ef9d8843350105acd6a30645f897050ffa4
2023-11-29 05:41:01 +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
Treehugger Robot
e93c1abe4b Merge "Looper: Use sequence numbers in epoll_event to track requests" am: 663c692a2d am: 7dc38fe273 am: cf90434fe6 am: 6bf0ea23fd am: 32df321051
Original change: https://android-review.googlesource.com/c/platform/system/core/+/1795650

Change-Id: I1c830749b6c6999363c29c11f9cdb34749446b8d
2021-09-13 13:02:07 +00:00
Prabir Pradhan
729057ab8c Looper: Use sequence numbers in epoll_event to track requests
Previously, Looper internally kept track of the requests to add fds
using the fd value itself. It used an internal sequence number
associated with each request to add a callback to avoid a situation
where a callback is unexpectedly called. However, since it used the fd
value rather than the sequence number to register events into epoll,
there was still a way where unintended hangups could occur.

This exact sequence of events caused unintended behavior in Looper:
- An fd (FD) is added to the looper.
- Looper registers FD into epoll using its FD number.
- FD is closed.
- A hangup event arrives from epoll_wait while the Looper is polling.
Looper is waiting for the lock to process the callback for FD, because
it is blocked by:
- A new fd is created and added to the looper. Since the lowest number
fd is reused, this new fd has the same value as FD.
- The poll request for Looper is now unblocked, so it looks up the
callback associated with FD to process the hangup.
- Since FD is already associated with the new callback, the new callback
is called unintentionally.

This CL uses the sequence number to register fds into epoll. That way,
when we get a hangup from epoll that is associated with a sequence
number, there is no way an unexpected callback will called.

This CL also adds a test to verify this behavior. Due to the
nondeterministic nature of this multi-thread scenario, the test verifies
this scenario repeatedly. Without the fix in Looper, the test is flaky,
but should never fail after the fix.

Bug: 195020232
Bug: 189135695
Test: atest libutils_test --rerun-until-failure
Ignore-AOSP-First: Topic CL aosp/1799831 has a merge conflict with
internal master, resolved in ag/15613419.

Change-Id: Ib4edab7f2407adaef6a1708b29bc52634f25dbb6
2021-09-01 14:52:52 +00:00
Steven Moreland
a06e68cb7a Reland^2 "libutils: disable implicit ref base construction"
This reverts commit 2cb7593792.

Reason for revert: reland after fixing b/186468053
Bug: 184190315

Change-Id: I2a46b7a8148db444f9bd4b24746f64217cbba947
2021-04-27 00:09:23 +00:00
Steven Moreland
2cb7593792 Revert "Reland "libutils: disable implicit ref base construction""
This reverts commit 02c47655e7.

Reason for revert: broke another test b/186468053

Change-Id: I0ec82455ad203071e523092ae3cb5f0f1df6aac7
2021-04-27 00:07:55 +00:00
Steven Moreland
02c47655e7 Reland "libutils: disable implicit ref base construction"
This reverts commit 8e11f9e3f3.

Reason for revert: reland after fixing b/186028624
Bug: 184190315

Change-Id: Ia7339a46c3af7dd1a95bc82327cae5304f9f2e16
2021-04-26 17:19:17 +00:00
Steven Moreland
8e11f9e3f3 Revert "libutils: disable implicit ref base construction"
This reverts commit 9a1bf53c4b.

Reason for revert: b/186028624

Change-Id: Icb8b1623df2794222ce3fe34ed5f43506ccf2f68
2021-04-21 23:24:53 +00:00
Steven Moreland
9a1bf53c4b libutils: disable implicit ref base construction
Ignore-AOSP-First: b/184196278

Bug: 184190315
Test: libutils_test
Change-Id: If47f79a8b96ee75706817081950f9ca5c0d12731
2021-04-19 22:01:35 +00:00
Alessio Balsini
1406207e67 Looper: missing cinttypes when debugging poll/callbacks
When enabling DEBUG_POLL_AND_WAKE or DEBUG_CALLBACKS, compiler complains
for the undefined PRI* fprintf format constants.
Fix by including the missing cinttypes.

Test: m
Bug: none
Change-Id: I0a7b5b45bd274eefa9644cba491a8dc28f1992ff
2019-05-11 20:53:47 +01:00
Elliott Hughes
fd12160087 libutils: more detailed logging.
The question arose on the bug of whether this is a legit errno or just
a stale value.

Bug: http://b/129139241
Test: ran tests
Change-Id: I486176496028f04d4fcccdd0b29a3df6b13849c9
2019-04-01 12:24:19 -07:00
Nick Kralevich
0a8b4d1a8b use epoll_create1
epoll_create leaks file descriptors. Use epoll_create1(EPOLL_CLOEXEC)
instead.

Bug: 120983106
Test: compiles and boots
Change-Id: I7c135ee22aee61cc7895486fda904b1790c093d1
2018-12-17 09:39:24 -08:00
Josh Gao
2d08ae57d4 libutils: switch Looper's fds to unique_fd.
Switch Looper to using unique_fd for its owned file descriptors, to
benefit from fdsan.

Bug: http://b/111560345
Test: treehugger
Change-Id: I8efff7741ed19fd71f82f7e604b4f1c66fc5ea2b
2018-07-18 18:12:12 -07:00
Yi Kong
e1731a4f2e [libutils] Modernize codebase by replacing NULL with nullptr
Fixes -Wzero-as-null-pointer-constant warning.

Test: m
Bug: 68236239
Change-Id: I5e89ec8c42151875439d2656475a8739ab9cb7dc
2018-07-16 18:11:34 -07:00
Mathias Agopian
22dbf3947f clean-up libutils includes
moved Foo.h as first include of Foo.cpp, and
removed redundant includes.

Made NativeHandle non virtual.


Test: run & compile
Bug: n/a

Change-Id: I37fa746cd42c9ba23aba181f84cb6c619386406a
2017-03-03 14:47:47 -08:00
Mark Salyzyn
271a1a7cda resolve merge conflicts of a45b1d6f3 to master
Test: compile
Bug: 30465923
Change-Id: I648855539df3cfa176c6ecac19b6a562ba6feaf7
2017-01-11 11:41:38 -08:00
Mark Salyzyn
30f991f251 liblog: use log/log.h when utilizing ALOG macros
Test: compile
Bug: 30465923
Change-Id: Id6d76510819ebd88c3f5003d00d73a0dbe85e943
2017-01-11 09:31:15 -08:00
Mark Salyzyn
4bf372ae30 system/core Replace cutils/log.h with android/log.h am: 66ce3e08c5 am: eb189f147b am: 16f7786032
am: 0abff8fc61

Change-Id: I8c9d221094f0c27838e120aeb41fff276fbab4b2
2016-10-01 00:43:07 +00:00
Mark Salyzyn
66ce3e08c5 system/core Replace cutils/log.h with android/log.h
Should use android/log.h instead of cutils/log.h as a good example
to all others.  Adjust header order to comply with Android Coding
standards.

Test: Compile
Bug: 26552300
Bug: 31289077
Change-Id: I2c9cbbbd64d8dccf2d44356361d9742e4a9b9031
2016-09-30 12:47:05 -07:00
Colin Cross
fd49e46c39 Merge "Fix warnings in libutils headers" am: 65a1633ec3 am: 0f79b62bf6 am: 1446cbac58
am: 28cda0bf38

Change-Id: I1190211b67e0c8d02ed1eb09c6b25401687069dd
2016-09-23 01:17:43 +00:00
Colin Cross
17b5b82d64 Fix warnings in libutils headers
system/core/include is included in the global include path using
-isystem, which hides all warnings.  Fix warnings in libutils
headers in preparation for moving from -isystem to -I.

- Fix implicit cast from int64_t to long in Condition.h.  Remove
  the __LP64__ check and always compare against LONG_MAX before
  casting.
- Fix implicit cast from size_t to ssize_t in KeyedVector.h
- Fix -Wshadow-field-in-constructor warnings in Looper.h and RefBase.h
- Move destructors for MessageHandler and LooperCallback to Looper.cpp
  and ReferenceRenamer and VirtualLightRefBase to RefBase.cpp to prevent
  vtables in every compilation unit.
- Declare template variables in Singleton.h
- Fix old-style casts in StrongPointer.h and TypeHelpers.h
- Use template metaprogramming in TypeHelpers.h to avoid warnings on
  memmove on non-trivial types.
- Add an assignment operator to key_value_pair_t to complete
  rule-of-three
- Use memcpy instead of dereferencing a reinterpret_casted pointer to
  treat the bits of a float or double as int32_t or int64_t
- Escape unicode sequences inside doxygen comments between \code and
  \endcode
- Remove WIN32 ZD definition in Compat.h, %zd works fine with mingw
- Fix WIN32 printf warnings in Filemap.cpp
- Initialize mNullValue with 0 in LruCache.h, some of the tests use a
  non-pointer type for TValue.

Test: m -j native
Bug: 31492149
Change-Id: I385a05a3ca01258e44fe3b37ef77e4aaff547b26
2016-09-22 13:03:15 -07:00
John Reck
48c35dbe50 Make wake() error fatal
If wake() fails to write to mWakeEventFd, the looper is
screwed. This is really a fatal, unrecoverable error,
so treat it as such.

This is possibly an explaination for the unexplained
random ANRs

Change-Id: I5af3e013493b475d8e902d8ee6340f331e5d86c8
2016-08-23 09:39:39 -07:00
Nick Kralevich
c68c8862f9 more O_CLOEXEC
Change-Id: I330aef8d6f1fc35d6649995ef312954b84dff555
2015-12-18 20:57:25 -08:00
Elliott Hughes
5b8ff09578 resolved conflicts for merge of b40ebc54 to mnc-dev-plus-aosp
Change-Id: I9da05b46da2326ae21d164b137be57a9b5220f7b
2015-06-30 15:17:14 -07:00
Elliott Hughes
6ed68cc412 Consistently use strerror in libutils.
It's easier for people to debug, and side-steps the problem that errno
values differ between architectures.

Bug: http://b/17458391
Change-Id: I1db9b2cbb653839d3936b91e37e5cff02671318a
2015-06-30 10:41:15 -07:00
Jeff Brown
18a574f715 Work around spurious epoll notifications.
Sometimes it seems like we can get into a situation where we are
unable to remove an fd from the epoll set but it keeps sending us
events anyhow.  Defensively rebuild the epoll set in this case
to prevent us from spinning forever.

Bug: 21271428
Change-Id: I5607e565f2e12460d7113a1f62a70d38d334e271
2015-05-29 17:40:25 -07:00
Tim Kilbourn
8892ce6383 Use eventfd instead of pipe in Looper.
Bug: 19933224
Change-Id: I6611e918db960bbb0e74f8308e10bdf79510b899
2015-03-27 09:15:43 -07:00
Jeff Brown
e7d54f80cb Fix issues related to removing Looper callbacks after close.
When a file descriptor is closed before removing it from the
epoll set, it will normally be removed automatically from the
epoll set by the kernel.  However if there exists a duplicate
then the original file descriptor may remain in the set and
continue to receive events until all duplicates have been closed.

Unfortunately due to kernel limitations we need to rebuild the epoll
set from scratch because it may contain an old file handle that we are
now unable to remove since its file descriptor is no longer valid.
No such problem would have occurred if we were using the poll system
call instead, but that approach carries others disadvantages.

Bug: 19715279
Change-Id: If1ab8ebda0825755a416d513e888942a02ee3948
2015-03-12 19:44:49 -07:00
Jeff Brown
7a0310ef67 Fixed subtle bugs related to file descriptor recycling.
Added code to protect against situations that may occur when a
Looper callback has the side-effect of closing the file descriptor that
it is watching before it returns.  This code pattern is very
convenient for implementation but it does expose issues in how
the list of callbacks is maintained.  In particular, we
need to watch out for file descriptors which have been reused.

This change may resolve previously unexplained ANRs associated with
log messages such as: "Error modifying epoll events for fd 44, errno=2"

Bug: 10349083
Change-Id: I20eedf6ffbdeda382653ca0104962505194741b0
2015-03-11 15:00:36 -07:00
Jeff Brown
27e5721860 Rename Looper::isIdling() to isPolling() to resolve confusion.
The loop isn't technically idle at this time, it's just checking
whether any file descriptors have pending events.  However it's
still a good signal as to whether the loop is alive.

Bug: 19532373
Change-Id: I555c473e70ffd8a56e1b10aa60026eb674a16de9
2015-03-11 15:00:36 -07:00
Brian Carlstrom
1693d7e48f Make libutils Looper independent of frameworks/native
Change-Id: I1dacca10a3cd7601abc3451b69ed761bf71c38fe
2013-12-18 22:32:13 -08:00
Alex Ray
d98e07fdf9 move libs/utils to libutils
Change-Id: I6cf4268599460791414882f91eeb88a992fbd29d
2013-08-02 14:40:08 -07:00
Renamed from libs/utils/Looper.cpp (Browse further)