Skip to content

[clang][dataflow] Merge RecordValues with different locations correctly. #65319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,19 @@ static Value *mergeDistinctValues(QualType Type, Value &Val1,

Value *MergedVal = nullptr;
if (auto *RecordVal1 = dyn_cast<RecordValue>(&Val1)) {
[[maybe_unused]] auto *RecordVal2 = cast<RecordValue>(&Val2);

// Values to be merged are always associated with the same location in
// `LocToVal`. The location stored in `RecordVal` should therefore also
// be the same.
assert(&RecordVal1->getLoc() == &RecordVal2->getLoc());

// `RecordVal1` and `RecordVal2` may have different properties associated
// with them. Create a new `RecordValue` without any properties so that we
// soundly approximate both values. If a particular analysis needs to merge
// properties, it should do so in `DataflowAnalysis::merge()`.
MergedVal = &MergedEnv.create<RecordValue>(RecordVal1->getLoc());
auto *RecordVal2 = cast<RecordValue>(&Val2);

if (&RecordVal1->getLoc() == &RecordVal2->getLoc())
// `RecordVal1` and `RecordVal2` may have different properties associated
// with them. Create a new `RecordValue` with the same location but
// without any properties so that we soundly approximate both values. If a
// particular analysis needs to merge properties, it should do so in
// `DataflowAnalysis::merge()`.
MergedVal = &MergedEnv.create<RecordValue>(RecordVal1->getLoc());
else
// If the locations for the two records are different, need to create a
// completely new value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that how cryptic when this could happen, it might be helpful to have a brief comment that both could happen depending on a subtle order of CFG and therefore how merge happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly what you mean...

Whether the locations are the same or not doesn't really depend on the order in which we process CFG blocks. Instead, locations are guaranteed to be the same if the RecordValue is stored in ExprToLoc, and they are usually (but not always) different if the RecordValue is stored in ExprToVal.

The order in which we process CFG blocks affects whether we actually see different RecordValues or not when the merge happens -- which makes merges difficult to trigger reliably from an integration test (example). But I'm not sure how relevant that is to the situation here?

Anyway, my hope is that all of this will go away relatively soon because we're now working on eliminating the RecordStorageLocation from RecordValue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, I wanted to see if some comment may help but it's a bit irrelevant here. Also given that it's going away I'm good with it, thanks!

MergedVal = MergedEnv.createValue(Type);
} else {
MergedVal = MergedEnv.createValue(Type);
}
Expand Down
69 changes: 69 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,75 @@ TEST_F(EnvironmentTest, CreateValueRecursiveType) {
EXPECT_THAT(PV, NotNull());
}

TEST_F(EnvironmentTest, JoinRecords) {
using namespace ast_matchers;

std::string Code = R"cc(
struct S {};
// Need to use the type somewhere so that the `QualType` gets created;
S s;
)cc";

auto Unit =
tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
auto &Context = Unit->getASTContext();

ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);

auto Results =
match(qualType(hasDeclaration(recordDecl(hasName("S")))).bind("SType"),
Context);
const QualType *TyPtr = selectFirst<QualType>("SType", Results);
ASSERT_THAT(TyPtr, NotNull());
QualType Ty = *TyPtr;
ASSERT_FALSE(Ty.isNull());

auto *ConstructExpr = CXXConstructExpr::CreateEmpty(Context, 0);
ConstructExpr->setType(Ty);
ConstructExpr->setValueKind(VK_PRValue);

// Two different `RecordValue`s with the same location are joined into a
// third `RecordValue` with that same location.
{
Environment Env1(DAContext);
auto &Val1 = *cast<RecordValue>(Env1.createValue(Ty));
RecordStorageLocation &Loc = Val1.getLoc();
Env1.setValue(*ConstructExpr, Val1);

Environment Env2(DAContext);
auto &Val2 = Env2.create<RecordValue>(Loc);
Env2.setValue(Loc, Val2);
Env2.setValue(*ConstructExpr, Val2);

Environment::ValueModel Model;
Environment EnvJoined = Environment::join(Env1, Env2, Model);
auto *JoinedVal = cast<RecordValue>(EnvJoined.getValue(*ConstructExpr));
EXPECT_NE(JoinedVal, &Val1);
EXPECT_NE(JoinedVal, &Val2);
EXPECT_EQ(&JoinedVal->getLoc(), &Loc);
}

// Two different `RecordValue`s with different locations are joined into a
// third `RecordValue` with a location different from the other two.
{
Environment Env1(DAContext);
auto &Val1 = *cast<RecordValue>(Env1.createValue(Ty));
Env1.setValue(*ConstructExpr, Val1);

Environment Env2(DAContext);
auto &Val2 = *cast<RecordValue>(Env2.createValue(Ty));
Env2.setValue(*ConstructExpr, Val2);

Environment::ValueModel Model;
Environment EnvJoined = Environment::join(Env1, Env2, Model);
auto *JoinedVal = cast<RecordValue>(EnvJoined.getValue(*ConstructExpr));
EXPECT_NE(JoinedVal, &Val1);
EXPECT_NE(JoinedVal, &Val2);
EXPECT_NE(&JoinedVal->getLoc(), &Val1.getLoc());
EXPECT_NE(&JoinedVal->getLoc(), &Val2.getLoc());
}
}

TEST_F(EnvironmentTest, InitGlobalVarsFun) {
using namespace ast_matchers;

Expand Down