Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 58c1e6d0 authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: protect MonotonicCounter from overflow

Use __builtin_sub_overflow.

Bug: 214726263
Test: atest test_monotonic_counter
Test: Run OboeTester with Legacy output for 13 hours.
Change-Id: Ie3cbf155c3059f50c17a32361c203265d63dc31b
parent b4f42a9b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -557,7 +557,7 @@ int64_t AudioStreamRecord::getFramesWritten() {
        case AAUDIO_STREAM_STATE_STARTED:
            result = mAudioRecord->getPosition(&position);
            if (result == OK) {
                mFramesWritten.update32(position);
                mFramesWritten.update32((int32_t)position);
            }
            break;
        case AAUDIO_STREAM_STATE_STOPPING:
+1 −1
Original line number Diff line number Diff line
@@ -508,7 +508,7 @@ int64_t AudioStreamTrack::getFramesRead() {
    case AAUDIO_STREAM_STATE_PAUSED:
        result = mAudioTrack->getPosition(&position);
        if (result == OK) {
            mFramesRead.update32(position);
            mFramesRead.update32((int32_t)position);
        }
        break;
    default:
+8 −3
Original line number Diff line number Diff line
@@ -41,7 +41,12 @@ public:
    }

    /**
     * advance the current value to match the counter
     * Advance the current value to match the counter.
     *
     * Note that it will take several million years for the 64-bit
     * counters to wrap around.
     * So we do not use __builtin_sub_overflow.
     * We want to know if overflow happens because of a bug.
     */
    void catchUpTo(int64_t counter) {
        if ((counter - mCounter64) > 0) {
@@ -74,7 +79,8 @@ public:
     * @return current value of the 64-bit counter
     */
    int64_t update32(int32_t counter32) {
        int32_t delta = counter32 - mCounter32;
        int32_t delta;
        __builtin_sub_overflow(counter32, mCounter32, &delta);
        // protect against the mCounter64 going backwards
        if (delta > 0) {
            mCounter64 += delta;
@@ -108,5 +114,4 @@ private:
    int32_t mCounter32 = 0;
};


#endif //UTILITY_MONOTONIC_COUNTER_H