| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/devices/serial/drivers/virtio-console/console.h" | |
| #include <lib/fake-bti/bti.h> | |
| #include <lib/virtio/backends/fake.h> | |
| #include <algorithm> | |
| #include <memory> | |
| #include <vector> | |
| #include <zxtest/zxtest.h> | |
| #include "src/devices/testing/mock-ddk/mock-device.h" | |
| namespace { | |
| constexpr uint16_t kQueueCount = 2; | |
| constexpr uint16_t kRingSize = 32; | |
| class FakeConsoleBackend : public virtio::FakeBackend { | |
| public: | |
| explicit FakeConsoleBackend(zx::unowned_bti fake_bti) | |
| : virtio::FakeBackend({{0, kRingSize}, {1, kRingSize}}), fake_bti_(fake_bti) {} | |
| uint64_t ReadFeatures() override { return VIRTIO_F_VERSION_1; } | |
| zx_status_t SetRing(uint16_t index, uint16_t count, zx_paddr_t pa_desc, zx_paddr_t pa_avail, | |
| zx_paddr_t pa_used) override { | |
| FakeBackend::SetRing(index, count, pa_desc, pa_avail, pa_used); | |
| if (index < kQueueCount) { | |
| size_t vmo_count = 0; | |
| ZX_ASSERT(fake_bti_get_pinned_vmos(fake_bti_->get(), nullptr, 0, &vmo_count) == ZX_OK); | |
| ZX_ASSERT(vmo_count > 0u); | |
| std::vector<fake_bti_pinned_vmo_info_t> vmos(vmo_count); | |
| ZX_ASSERT(fake_bti_get_pinned_vmos(fake_bti_->get(), vmos.data(), vmos.size(), &vmo_count) == | |
| ZX_OK); | |
| const size_t returned = std::min(vmo_count, vmos.size()); | |
| std::vector<zx::vmo> owned_vmos(returned); | |
| for (size_t i = 0; i < returned; ++i) { | |
| owned_vmos[i].reset(vmos[i].vmo); | |
| } | |
| ZX_ASSERT(returned > 0u); | |
| queues_[index].ring_vmo = std::move(owned_vmos[returned - 1]); | |
| queues_[index].ring_vmo_offset = vmos[returned - 1].offset; | |
| queues_[index].desc_offset = pa_desc - FAKE_BTI_PHYS_ADDR; | |
| queues_[index].used_offset = pa_used - FAKE_BTI_PHYS_ADDR; | |
| } | |
| return ZX_OK; | |
| } | |
| void MutateDesc(uint16_t queue_index, uint16_t desc_id, uint16_t flags, uint16_t next) { | |
| vring_desc desc{}; | |
| const uint64_t addr = queues_[queue_index].ring_vmo_offset + queues_[queue_index].desc_offset + | |
| desc_id * sizeof(vring_desc); | |
| ASSERT_OK(queues_[queue_index].ring_vmo.read(&desc, addr, sizeof(desc))); | |
| desc.flags = flags; | |
| desc.next = next; | |
| ASSERT_OK(queues_[queue_index].ring_vmo.write(&desc, addr, sizeof(desc))); | |
| } | |
| void PushUsed(uint16_t queue_index, uint32_t id, uint32_t len) { | |
| union __PACKED Used { | |
| vring_used head; | |
| struct __PACKED { | |
| uint8_t header[sizeof(vring_used)]; | |
| vring_used_elem elements[kRingSize]; | |
| }; | |
| } used{}; | |
| const uint64_t addr = queues_[queue_index].ring_vmo_offset + queues_[queue_index].used_offset; | |
| ASSERT_OK(queues_[queue_index].ring_vmo.read(&used, addr, sizeof(used))); | |
| used.elements[used.head.idx++ & (kRingSize - 1)] = {.id = id, .len = len}; | |
| ASSERT_OK(queues_[queue_index].ring_vmo.write(&used, addr, sizeof(used))); | |
| } | |
| private: | |
| struct QueueInfo { | |
| zx::vmo ring_vmo; | |
| uint64_t ring_vmo_offset = 0; | |
| size_t desc_offset = 0; | |
| size_t used_offset = 0; | |
| }; | |
| zx::unowned_bti fake_bti_; | |
| QueueInfo queues_[kQueueCount]; | |
| }; | |
| TEST(ConsoleTest, IrqRingUpdateHandlesMalformedChainsAndDuplicateDescriptors) { | |
| auto parent = MockDevice::FakeRootParent(); | |
| zx::bti bti; | |
| ASSERT_OK(fake_bti_create(bti.reset_and_get_address())); | |
| auto backend = std::make_unique<FakeConsoleBackend>(bti.borrow()); | |
| FakeConsoleBackend* backend_ptr = backend.get(); | |
| auto device = | |
| std::make_unique<virtio::ConsoleDevice>(parent.get(), std::move(bti), std::move(backend)); | |
| ASSERT_OK(device->Init()); | |
| virtio::ConsoleDevice* dev_ptr = device.release(); | |
| // Receive queue (index 0): | |
| // - Descriptor 0 has an out-of-bounds next index (kRingSize). | |
| // - Descriptor 1 self-loops (next == 1). | |
| // - Descriptor 2 is completed twice; the second entry must not re-add it via InContainer(). | |
| backend_ptr->MutateDesc(0, 0, VRING_DESC_F_WRITE | VRING_DESC_F_NEXT, kRingSize); | |
| backend_ptr->MutateDesc(0, 1, VRING_DESC_F_WRITE | VRING_DESC_F_NEXT, 1); | |
| backend_ptr->PushUsed(0, 0, 8); | |
| backend_ptr->PushUsed(0, 1, 8); | |
| backend_ptr->PushUsed(0, 2, 8); | |
| backend_ptr->PushUsed(0, 2, 8); | |
| // Transmit queue (index 1): spurious entry on unallocated descriptor 0 must be skipped cleanly. | |
| backend_ptr->PushUsed(1, 0, 0); | |
| dev_ptr->IrqRingUpdate(); | |
| dev_ptr->DdkAsyncRemove(); | |
| mock_ddk::ReleaseFlaggedDevices(parent.get()); | |
| } | |
| } // namespace |