@@ -787,6 +787,40 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
787787 event->when = nsecs_t (iev.time .tv_sec ) * 1000000000LL
788788 + nsecs_t (iev.time .tv_usec ) * 1000LL ;
789789 ALOGV (" event time %lld, now %lld" , event->when , now);
790+
791+ // Bug 7291243: Add a guard in case the kernel generates timestamps
792+ // that appear to be far into the future because they were generated
793+ // using the wrong clock source.
794+ //
795+ // This can happen because when the input device is initially opened
796+ // it has a default clock source of CLOCK_REALTIME. Any input events
797+ // enqueued right after the device is opened will have timestamps
798+ // generated using CLOCK_REALTIME. We later set the clock source
799+ // to CLOCK_MONOTONIC but it is already too late.
800+ //
801+ // Invalid input event timestamps can result in ANRs, crashes and
802+ // and other issues that are hard to track down. We must not let them
803+ // propagate through the system.
804+ //
805+ // Log a warning so that we notice the problem and recover gracefully.
806+ if (event->when >= now + 10 * 1000000000LL ) {
807+ // Double-check. Time may have moved on.
808+ nsecs_t time = systemTime (SYSTEM_TIME_MONOTONIC);
809+ if (event->when > time) {
810+ ALOGW (" An input event from %s has a timestamp that appears to "
811+ " have been generated using the wrong clock source "
812+ " (expected CLOCK_MONOTONIC): "
813+ " event time %lld, current time %lld, call time %lld. "
814+ " Using current time instead." ,
815+ device->path .string (), event->when , time, now);
816+ event->when = time;
817+ } else {
818+ ALOGV (" Event time is ok but failed the fast path and required "
819+ " an extra call to systemTime: "
820+ " event time %lld, current time %lld, call time %lld." ,
821+ event->when , time, now);
822+ }
823+ }
790824#else
791825 event->when = now;
792826#endif
0 commit comments