This makes the required changes to the Scudo code to be able to work on
Android, both for ARM and AArch64. Note that Android is not enabled yet in
the cmake configs, it will be the object of a later commit.
The following constraints had to be taken into account:
- Android doesn't support thread_local, so things had to be changed to take this into account, using pthread_{set,get}specific as the alternative;
- Android can't necessarily afford a cache per thread, due to memory limitations (there are incentives to low memort devices). The current Android allocator, jemalloc, uses a fixed set of arenas, which is the solution I adopted as well, assigning them to threads in a round-robin fashion.
As a result, the thread specific data was moved into a context structure, that
can either be thread_local or global (in this case, allocated during
initialization and lasting the lifetime of the program). This accounts for the
new files: scudo_thread.h, scudo_thread_linux.cpp, scudo_thread_android.cpp.
The number of contexts for Android is modifiable as a define. This also means
that we must be able to tell if a thread cache requires locking now, as for
Android it can be shared between multiple threads. Hence the addition of a
mutex per context, that is locked only when dealing with non thread_local
scenarios.
Another change is the ability to bypass the Quarantine if the cache size is
set to 0. Android watches closely its PSS, and 1MB of Quarantine ends up in
about 6MB of extra PSS for Android 32-bit. So the Quarantine might not be an
option. With no quarantine, the old execution flow ended up with the Scudo
header being checked twice, once before Put, once in Recyle, without any added
value to it.
Other changes involve a bit of reordering of lines here and there, and some
variable renaming to make things a bit more explicit.
For this review, I would also appreciate potential optimization feedback, if
someone sees a way to do things better, that would be great! Thanks for reading
that long blob of text.
Does UNLIKELY really improve the generated code?