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

Commit 636011df authored by Łukasz Rymanowski's avatar Łukasz Rymanowski Committed by Łukasz Rymanowski (xWF)
Browse files

RemoteDevice: Fix for race condition on adding device properties

There could be a case where addDeviceProperties is called multiple times
for the same device which leads to situation of having more than one
DeviceProperties for single device.
This could lead to undefined behavior when reading properties e.g. bond
state etc.

It can happen on Bluetooth startup when there is many bonded devices.
Bug: 374909756
Bug: 375158716
Test: atest RemoteDevicesTest
Flag: com.android.bluetooth.flags.fix_add_device_properties

Change-Id: I6292bbe3f68794b0b79f21f2f37e584fbceaec3a
parent 114d45c0
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -290,10 +290,16 @@ public class RemoteDevices {
    @VisibleForTesting
    DeviceProperties addDeviceProperties(byte[] address) {
        synchronized (mDevices) {
            String key = Utils.getAddressStringFromByte(address);
            if (Flags.fixAddDeviceProperties() && mDevices.containsKey(key)) {
                debugLog("Properties for device " + key + " are already added");
                return mDevices.get(key);
            }

            DeviceProperties prop = new DeviceProperties();
            prop.setDevice(mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address)));
            prop.setAddress(address);
            String key = Utils.getAddressStringFromByte(address);

            DeviceProperties pv = mDevices.put(key, prop);

            if (pv == null) {
+16 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import android.os.TestLooperManager;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -25,6 +27,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.Utils;
import com.android.bluetooth.bas.BatteryService;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.hfp.HeadsetHalConstants;

import org.junit.After;
@@ -55,6 +58,7 @@ public class RemoteDevicesTest {

    private Context mTargetContext;
    private BluetoothManager mBluetoothManager;
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

@@ -760,6 +764,18 @@ public class RemoteDevicesTest {
        Assert.assertEquals(BLUETOOTH_CONNECT, mStringArgument.getValue());
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_ADD_DEVICE_PROPERTIES)
    public void testMultipleAddDeviceProperties() {
        // Verify that device property is null initially
        Assert.assertNull(mRemoteDevices.getDeviceProperties(mDevice1));
        DeviceProperties prop1 =
                mRemoteDevices.addDeviceProperties(Utils.getBytesFromAddress(TEST_BT_ADDR_1));
        DeviceProperties prop2 =
                mRemoteDevices.addDeviceProperties(Utils.getBytesFromAddress(TEST_BT_ADDR_1));
        Assert.assertEquals(prop2, prop1);
    }

    @Test
    public void testSetgetHfAudioPolicyForRemoteAg() {
        // Verify that device property is null initially