message_pump_android: Do not instrument callbacks

When orderfile instrumentation is enabled, every function gets
instrumented with an additional call to the hook
__cyg_profile_func_enter_bare() somewhere in the prologue. We define
this hook to (among other things) pull __builtin_return_address(0) and
check that it is within the address range of .text.

We observed repeatedly on the orderfile bot that on instrumented builds
the __builtin_return_address(0) returns incorrect results for
instrumented functions DelayedLooperCallback() and
NonDelayedLooperCallback(). These are somewhat special calls, as they
are made from /system/lib64/libutils.so using an indirect call.

My main hypothesis is that the ABI may not be fully respected with these
cross-library calls (and when aggressive PGO optimizations are applied).
The issue needs a more thorough investigation.

For now, apply a speculative pseudofix: exclude these functions from
instrumentation. If it does not fix the issue on the orderfile bot, the
hypothesis above would be rejected.

Bug: 330761384
Change-Id: Ib997d972f8210d9ef621dcc629ceba791b5fcaef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5385791
Commit-Queue: Egor Pasko <[email protected]>
Reviewed-by: Michael Thiessen <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1276980}
diff --git a/base/message_loop/message_pump_android.cc b/base/message_loop/message_pump_android.cc
index 7610675..18ef103 100644
--- a/base/message_loop/message_pump_android.cc
+++ b/base/message_loop/message_pump_android.cc
@@ -30,13 +30,22 @@
 
 // https://crbug.com/873588. The stack may not be aligned when the ALooper calls
 // into our code due to the inconsistent ABI on older Android OS versions.
+//
+// https://crbug.com/330761384#comment3. Calls from libutils.so into
+// NonDelayedLooperCallback() and DelayedLooperCallback() confuse aarch64 builds
+// with orderfile instrumentation causing incorrect value in
+// __builtin_return_address(0). Disable instrumentation for them. TODO(pasko):
+// Add these symbols to the orderfile manually or fix the builtin.
 #if defined(ARCH_CPU_X86)
-#define STACK_ALIGN __attribute__((force_align_arg_pointer))
+#define NO_INSTRUMENT_STACK_ALIGN \
+  __attribute__((force_align_arg_pointer, no_instrument_function))
 #else
-#define STACK_ALIGN
+#define NO_INSTRUMENT_STACK_ALIGN __attribute__((no_instrument_function))
 #endif
 
-STACK_ALIGN int NonDelayedLooperCallback(int fd, int events, void* data) {
+NO_INSTRUMENT_STACK_ALIGN int NonDelayedLooperCallback(int fd,
+                                                       int events,
+                                                       void* data) {
   if (events & ALOOPER_EVENT_HANGUP)
     return 0;
 
@@ -46,7 +55,9 @@
   return 1;  // continue listening for events
 }
 
-STACK_ALIGN int DelayedLooperCallback(int fd, int events, void* data) {
+NO_INSTRUMENT_STACK_ALIGN int DelayedLooperCallback(int fd,
+                                                    int events,
+                                                    void* data) {
   if (events & ALOOPER_EVENT_HANGUP)
     return 0;