|
| 1 | +// Copyright 2025 The Dawn & Tint Authors |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +// list of conditions and the following disclaimer. |
| 8 | +// |
| 9 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// 3. Neither the name of the copyright holder nor the names of its |
| 14 | +// contributors may be used to endorse or promote products derived from |
| 15 | +// this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +#include "dawn/tests/unittests/wire/WireTest.h" |
| 29 | + |
| 30 | +#include "dawn/utils/WGPUHelpers.h" |
| 31 | +#include "dawn/wire/WireClient.h" |
| 32 | + |
| 33 | +namespace dawn::wire { |
| 34 | +namespace { |
| 35 | + |
| 36 | +enum class TestState { |
| 37 | + // Mix the two wires while both clients are connected. |
| 38 | + Alive, |
| 39 | + // Disconnect WireTwo before creating an object on it. |
| 40 | + DisconnectBefore, |
| 41 | + // Disconnect WireTwo after creating an object on it. |
| 42 | + DisconnectMid, |
| 43 | +}; |
| 44 | +std::ostream& operator<<(std::ostream& stream, TestState flavor) { |
| 45 | + switch (flavor) { |
| 46 | + case TestState::Alive: |
| 47 | + stream << "Alive"; |
| 48 | + break; |
| 49 | + case TestState::DisconnectBefore: |
| 50 | + stream << "DisconnectBefore"; |
| 51 | + break; |
| 52 | + case TestState::DisconnectMid: |
| 53 | + stream << "DisconnectMid"; |
| 54 | + break; |
| 55 | + } |
| 56 | + return stream; |
| 57 | +} |
| 58 | + |
| 59 | +// Two copies of WireTest to set up a second parallel wire. |
| 60 | +// Create two classes that inherit WireTest, so that the test can inherit both |
| 61 | +// of them, to set up two parallel wires to test with. |
| 62 | +class WireOne : public WireTest {}; |
| 63 | +class WireTwo : public WireTest {}; |
| 64 | + |
| 65 | +// Tests for intentional wire client CHECK crashes when trying to use objects |
| 66 | +// from the wrong wire which would have bogus IDs. (The crash is intended, but |
| 67 | +// in principle we shouldn't actually crash. See crbug.com/440387003.) |
| 68 | +class WireConfusionDeathTest : public WireOne, |
| 69 | + public WireTwo, |
| 70 | + public ::testing::WithParamInterface<TestState> { |
| 71 | + protected: |
| 72 | + void SetUp() override { |
| 73 | + WireOne::SetUp(); |
| 74 | + WireTwo::SetUp(); |
| 75 | + |
| 76 | + if (GetParam() == TestState::DisconnectBefore) { |
| 77 | + WireTwo::GetWireClient()->Disconnect(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void TearDown() override { |
| 82 | + WireTwo::TearDown(); |
| 83 | + WireOne::TearDown(); |
| 84 | + } |
| 85 | + |
| 86 | + template <typename Lambda> |
| 87 | + void MaybeDisconnectAndExpectDeath(bool expectDeath, Lambda lambda) { |
| 88 | + if (GetParam() == TestState::DisconnectMid) { |
| 89 | + WireTwo::GetWireClient()->Disconnect(); |
| 90 | + } |
| 91 | + if (expectDeath) { |
| 92 | +#if defined(DAWN_ENABLE_ASSERTS) |
| 93 | + EXPECT_DEATH(lambda(), "forClient == mClient"); |
| 94 | +#else |
| 95 | + EXPECT_DEATH(lambda(), ""); |
| 96 | +#endif |
| 97 | + } else { |
| 98 | + lambda(); |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +// Test calling queue.WriteBuffer using a buffer from another device. |
| 104 | +TEST_P(WireConfusionDeathTest, WriteBuffer) { |
| 105 | + wgpu::BufferDescriptor bufDesc{ |
| 106 | + .usage = wgpu::BufferUsage::CopyDst, |
| 107 | + .size = 4, |
| 108 | + }; |
| 109 | + wgpu::Buffer two_buf = WireTwo::device.CreateBuffer(&bufDesc); |
| 110 | + |
| 111 | + MaybeDisconnectAndExpectDeath(true, [&]() { |
| 112 | + WireOne::queue.WriteBuffer(two_buf, 0, nullptr, 0); // |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +// Test creating a bind group using a layout from an old device. |
| 117 | +TEST_P(WireConfusionDeathTest, NewBindGroupFromOldLayout) { |
| 118 | + wgpu::BindGroupLayout two_bgl = utils::MakeBindGroupLayout(WireTwo::device, {}); |
| 119 | + |
| 120 | + wgpu::BindGroupDescriptor bgDesc{.layout = two_bgl}; |
| 121 | + MaybeDisconnectAndExpectDeath(true, [&]() { |
| 122 | + WireOne::device.CreateBindGroup(&bgDesc); // |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +// Test creating a bind group on an old device using a layout from a new device. |
| 127 | +TEST_P(WireConfusionDeathTest, OldBindGroupFromNewLayout) { |
| 128 | + wgpu::BindGroupLayout one_bgl = utils::MakeBindGroupLayout(WireOne::device, {}); |
| 129 | + |
| 130 | + wgpu::BindGroupDescriptor bgDesc{.layout = one_bgl}; |
| 131 | + // Should not crash if wire two is already disconnected. |
| 132 | + MaybeDisconnectAndExpectDeath(GetParam() == TestState::Alive, |
| 133 | + [&]() { WireTwo::device.CreateBindGroup(&bgDesc); }); |
| 134 | +} |
| 135 | + |
| 136 | +INSTANTIATE_TEST_SUITE_P(, |
| 137 | + WireConfusionDeathTest, |
| 138 | + ::testing::Values(TestState::Alive, |
| 139 | + TestState::DisconnectBefore, |
| 140 | + TestState::DisconnectMid), |
| 141 | + testing::PrintToStringParamName()); |
| 142 | + |
| 143 | +} // anonymous namespace |
| 144 | +} // namespace dawn::wire |
0 commit comments