Skip to content

Commit

Permalink
Fix data race on mTestIndex. (#7494)
Browse files Browse the repository at this point in the history
We could end up sending a message and getting a response to it before
we ever incremented mTestIndex (if our call into NextTest() was on a
thread other than the message thread).  If that happened, we would end
up running some subtest twice, and then later whenever we
incrememented mTestIndex would end up skipping some subtest.

Fixes #7493
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 31, 2021
1 parent a0cf78f commit 3689594
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
26 changes: 16 additions & 10 deletions examples/chip-tool/commands/tests/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class TestCluster : public TestCommand
{
public:
TestCluster() : TestCommand("TestCluster") {}
TestCluster() : TestCommand("TestCluster"), mTestIndex(0) {}

/////////// TestCommand Interface /////////
CHIP_ERROR NextTest() override
Expand All @@ -37,7 +37,11 @@ class TestCluster : public TestCommand
SetCommandExitStatus(true);
}

switch (mTestIndex)
// Ensure we increment mTestIndex before we start running the relevant
// command. That way if we lose the timeslice after we send the message
// but before our function call returns, we won't end up with an
// incorrect mTestIndex value observed when we get the response.
switch (mTestIndex++)
{
case 0:
err = TestSendClusterTestClusterCommandTest_0();
Expand All @@ -55,7 +59,6 @@ class TestCluster : public TestCommand
err = TestSendClusterTestClusterCommandReadAttribute_4();
break;
}
mTestIndex++;

if (CHIP_NO_ERROR != err)
{
Expand All @@ -67,8 +70,8 @@ class TestCluster : public TestCommand
}

private:
uint16_t mTestIndex = 0;
uint16_t mTestCount = 5;
std::atomic_uint16_t mTestIndex;
const uint16_t mTestCount = 5;

//
// Tests methods
Expand Down Expand Up @@ -437,7 +440,7 @@ class TestCluster : public TestCommand
class OnOffCluster : public TestCommand
{
public:
OnOffCluster() : TestCommand("OnOffCluster") {}
OnOffCluster() : TestCommand("OnOffCluster"), mTestIndex(0) {}

/////////// TestCommand Interface /////////
CHIP_ERROR NextTest() override
Expand All @@ -450,7 +453,11 @@ class OnOffCluster : public TestCommand
SetCommandExitStatus(true);
}

switch (mTestIndex)
// Ensure we increment mTestIndex before we start running the relevant
// command. That way if we lose the timeslice after we send the message
// but before our function call returns, we won't end up with an
// incorrect mTestIndex value observed when we get the response.
switch (mTestIndex++)
{
case 0:
err = TestSendClusterOnOffCommandReadAttribute_0();
Expand All @@ -468,7 +475,6 @@ class OnOffCluster : public TestCommand
err = TestSendClusterOnOffCommandReadAttribute_4();
break;
}
mTestIndex++;

if (CHIP_NO_ERROR != err)
{
Expand All @@ -480,8 +486,8 @@ class OnOffCluster : public TestCommand
}

private:
uint16_t mTestIndex = 0;
uint16_t mTestCount = 5;
std::atomic_uint16_t mTestIndex;
const uint16_t mTestCount = 5;

//
// Tests methods
Expand Down
13 changes: 8 additions & 5 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class {{asCamelCased filename false}}: public TestCommand
{
public:
{{asCamelCased filename false}}(): TestCommand("{{filename}}") {}
{{asCamelCased filename false}}(): TestCommand("{{filename}}"), mTestIndex(0) {}

/////////// TestCommand Interface /////////
CHIP_ERROR NextTest() override
Expand All @@ -15,15 +15,18 @@ class {{asCamelCased filename false}}: public TestCommand
SetCommandExitStatus(true);
}

switch (mTestIndex)
// Ensure we increment mTestIndex before we start running the relevant
// command. That way if we lose the timeslice after we send the message
// but before our function call returns, we won't end up with an
// incorrect mTestIndex value observed when we get the response.
switch (mTestIndex++)
{
{{#chip_tests_items}}
case {{index}}:
err = TestSendCluster{{asCamelCased cluster false}}Command{{asCamelCased command false}}_{{index}}();
break;
{{/chip_tests_items}}
}
mTestIndex++;

if (CHIP_NO_ERROR != err)
{
Expand All @@ -36,8 +39,8 @@ class {{asCamelCased filename false}}: public TestCommand


private:
uint16_t mTestIndex = 0;
uint16_t mTestCount = {{totalTests}};
std::atomic_uint16_t mTestIndex;
const uint16_t mTestCount = {{totalTests}};

//
// Tests methods
Expand Down

0 comments on commit 3689594

Please sign in to comment.