From 9a58d17049ff57b27ae4699ce62419ca3164e2ac Mon Sep 17 00:00:00 2001 From: mbalous Date: Fri, 4 Dec 2026 00:01:00 +0100 Subject: [PATCH 1/2] test(usb): serialize fake bus re-enumerations Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/test_usb_accessory.cpp | 32 ++++++++++++++++++++++++++++++++ tests/usb_fakes.hpp | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/tests/test_usb_accessory.cpp b/tests/test_usb_accessory.cpp index f5e3f6a..89c934c 100644 --- a/tests/test_usb_accessory.cpp +++ b/tests/test_usb_accessory.cpp @@ -2,6 +2,9 @@ // SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 // Android phones on a fake USB bus: which devices are asked, the accessory's endpoints, the switch requests, the // setting file. +#include +#include + #include "usb_accessory.hpp" #include "usb_fakes.hpp" @@ -68,6 +71,34 @@ void endpoints() { CHECK(!evo::readAccessoryEndpoints(bus.root.c_str(), "1-1", found)); } +void concurrentBusChanges() { + fake::Bus bus; + std::atomic start(false); + std::thread switching([&]() { + while (!start.load()) { + std::this_thread::yield(); + } + for (unsigned i = 0; i < 200; ++i) { + bus.accessory("1-1", 1, 3); + } + }); + std::thread unplugging([&]() { + while (!start.load()) { + std::this_thread::yield(); + } + for (unsigned i = 0; i < 200; ++i) { + bus.remove("1-1"); + bus.device("1-1", 1, 2, 0x04e8, 0x6860, 0, 6); + } + }); + start = true; + switching.join(); + unplugging.join(); + bus.accessory("1-1", 1, 3); + evo::AccessoryEndpoints found; + CHECK(evo::readAccessoryEndpoints(bus.root.c_str(), "1-1", found)); +} + void switchRequests() { fake::Phone phone; { @@ -145,6 +176,7 @@ int main() { portNames(); whoIsAsked(); endpoints(); + concurrentBusChanges(); switchRequests(); settingFile(); puts("PASS USB accessory: port names, devices asked and left alone, endpoints, switch requests, setting file"); diff --git a/tests/usb_fakes.hpp b/tests/usb_fakes.hpp index bea2853..998b260 100644 --- a/tests/usb_fakes.hpp +++ b/tests/usb_fakes.hpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -48,6 +49,8 @@ inline uint32_t nowMs() { // The kernel's list of USB devices, in a temporary folder. struct Bus { std::string root; + // Re-enumerations may nest operations, but must not delete another writer's directories. + std::recursive_mutex mu; Bus() { char path[] = "/tmp/entry-aad-usb-XXXXXX"; @@ -57,6 +60,7 @@ struct Bus { ~Bus() { nftw(root.c_str(), removeEntry, 8, FTW_DEPTH | FTW_PHYS); } void put(const std::string& folder, const char* name, const std::string& text) { + std::lock_guard lock(mu); const std::string path = root + "/" + folder; mkdir(path.c_str(), 0700); FILE* f = fopen((path + "/" + name).c_str(), "w"); @@ -71,6 +75,7 @@ struct Bus { } // Unplugged: the device and its interfaces go. void remove(const std::string& port) { + std::lock_guard lock(mu); std::vector gone; DIR* dir = opendir(root.c_str()); CHECK(dir != nullptr); @@ -88,6 +93,7 @@ struct Bus { // A device with one interface of that class. void device(const std::string& port, unsigned bus, unsigned address, unsigned vendor, unsigned product, unsigned deviceClass, unsigned interfaceClass) { + std::lock_guard lock(mu); remove(port); put(port, "busnum", number("%u", bus)); put(port, "devnum", number("%u", address)); @@ -99,6 +105,7 @@ struct Bus { } // A phone in accessory mode: interface 0 with its two bulk endpoints, as the kernel shows them. void accessory(const std::string& port, unsigned bus, unsigned address) { + std::lock_guard lock(mu); device(port, bus, address, 0x18d1, 0x2d01, 0, 0xff); const std::string interface = port + ":1.0"; put(interface, "bInterfaceSubClass", "ff"); From 906912789d8f922bd45aa99c6b9be87cb3c8570c Mon Sep 17 00:00:00 2001 From: mbalous Date: Fri, 4 Dec 2026 00:18:20 +0100 Subject: [PATCH 2/2] test(usb): synchronize replug after reset limit Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/test_usb_accessory.cpp | 16 ++++++++++++++ tests/test_usb_loop.cpp | 13 +++++++---- tests/usb_fakes.hpp | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/tests/test_usb_accessory.cpp b/tests/test_usb_accessory.cpp index 89c934c..003f91f 100644 --- a/tests/test_usb_accessory.cpp +++ b/tests/test_usb_accessory.cpp @@ -99,6 +99,21 @@ void concurrentBusChanges() { CHECK(evo::readAccessoryEndpoints(bus.root.c_str(), "1-1", found)); } +void oldDeviceCannotResetReplacement() { + fake::Bus bus; + fake::Phone oldPhone, replacement; + oldPhone.plug(bus, "1-1", true); + fake::Device oldDevice(oldPhone); + replacement.address = 40; + replacement.plug(bus, "1-1", true); + uint8_t protocol[2]; + CHECK(oldDevice.control(0xc0, 51, 0, 0, protocol, sizeof(protocol), 0) == -ENODEV); + CHECK(!oldDevice.claimInterface(0) && errno == ENODEV); + CHECK(!oldDevice.reset() && errno == ENODEV); + evo::UsbPhone found; + CHECK(evo::readUsbDevice(bus.root.c_str(), "1-1", found) && found.address == 40 && found.accessory); +} + void switchRequests() { fake::Phone phone; { @@ -177,6 +192,7 @@ int main() { whoIsAsked(); endpoints(); concurrentBusChanges(); + oldDeviceCannotResetReplacement(); switchRequests(); settingFile(); puts("PASS USB accessory: port names, devices asked and left alone, endpoints, switch requests, setting file"); diff --git a/tests/test_usb_loop.cpp b/tests/test_usb_loop.cpp index 9194bd3..a099a9d 100644 --- a/tests/test_usb_loop.cpp +++ b/tests/test_usb_loop.cpp @@ -34,10 +34,13 @@ evo::UsbDevice* openFake(unsigned bus, unsigned address) { struct Host : evo::UsbHost { std::atomic stop, busy; std::atomic fd; - std::atomic handed; - Host() : stop(false), busy(false), fd(-1), handed(0) {} + std::atomic handed, turns; + Host() : stop(false), busy(false), fd(-1), handed(0), turns(0) {} bool stopRequested() override { return stop; } - bool projectionBusy() override { return busy; } + bool projectionBusy() override { + ++turns; + return busy; + } void usbPhoneReady(int socket) override { fd = socket; busy = true; @@ -393,7 +396,9 @@ void quickReplugAfterResetLimit() { held.plug(bus, "1-1"); Run run(bus); CHECK(fake::within(10000, [&]() { return held.count(&fake::Phone::claims) == 40; })); - usleep(300000); // the last held interface reaches the reset limit + // The next turn records the reset limit; the turn after that confirms it completed. + const unsigned turns = run.host.turns; + CHECK(fake::within(4000, [&]() { return run.host.turns >= turns + 2; })); CHECK(held.count(&fake::Phone::resets) == 3 && run.host.handed == 0); const unsigned opens = held.count(&fake::Phone::opens); usleep(300000); diff --git a/tests/usb_fakes.hpp b/tests/usb_fakes.hpp index 998b260..df45c3f 100644 --- a/tests/usb_fakes.hpp +++ b/tests/usb_fakes.hpp @@ -73,6 +73,22 @@ struct Bus { snprintf(text, sizeof(text), format, value); return text; } + bool hasDevice(const std::string& port, unsigned bus, unsigned address) { + std::lock_guard lock(mu); + const std::string path = root + "/" + port; + FILE* busFile = fopen((path + "/busnum").c_str(), "r"); + FILE* addressFile = fopen((path + "/devnum").c_str(), "r"); + unsigned foundBus = 0, foundAddress = 0; + const bool found = busFile && addressFile && fscanf(busFile, "%u", &foundBus) == 1 && + fscanf(addressFile, "%u", &foundAddress) == 1; + if (busFile) { + fclose(busFile); + } + if (addressFile) { + fclose(addressFile); + } + return found && foundBus == bus && foundAddress == address; + } // Unplugged: the device and its interfaces go. void remove(const std::string& port) { std::lock_guard lock(mu); @@ -231,11 +247,16 @@ class Device : public evo::UsbDevice { public: explicit Device(Phone& phone) : phone_(phone) { Lock lock(phone_.mu); + address_ = phone_.address; ++phone_.opens; } int control(uint8_t type, uint8_t request, uint16_t, uint16_t index, uint8_t* data, uint16_t size, unsigned) override { Lock lock(phone_.mu); + std::unique_lock busLock = lockBus(); + if (!present()) { + return -ENODEV; + } if (request == 51) { ++phone_.protocolAsked; phone_.strings.clear(); // a new identification starts @@ -270,6 +291,11 @@ class Device : public evo::UsbDevice { } bool claimInterface(unsigned number) override { Lock lock(phone_.mu); + std::unique_lock busLock = lockBus(); + if (!present()) { + errno = ENODEV; + return false; + } CHECK(number == 0); ++phone_.claims; if (phone_.claimErrno != 0) { @@ -289,6 +315,11 @@ class Device : public evo::UsbDevice { } bool reset() override { Lock lock(phone_.mu); + std::unique_lock busLock = lockBus(); + if (!present()) { + errno = ENODEV; + return false; + } ++phone_.resets; phone_.strings.clear(); if (phone_.resets == phone_.resetGoneAt) { @@ -371,7 +402,18 @@ class Device : public evo::UsbDevice { short waitEvents() const override { return POLLIN; } private: + // Keep the presence check and operation atomic with fake unplug and re-enumeration. + std::unique_lock lockBus() { + return phone_.bus ? std::unique_lock(phone_.bus->mu) + : std::unique_lock(); + } + bool present() const { + return phone_.address == address_ && + (!phone_.bus || phone_.bus->hasDevice(phone_.port, phone_.busNumber, address_)); + } + Phone& phone_; + unsigned address_; std::vector pending_; std::set cancelled_; };