Skip to content
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

Prevent mutation on cast of passed-by-reference bitfield l-value #279

Merged
merged 4 commits into from
Jul 13, 2024
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
13 changes: 13 additions & 0 deletions src/libdredd/src/mutate_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@ void MutateVisitor::HandleExpr(clang::Expr* expr) {
return;
}

// Avoid mutation on cast when its underlying value is a bitfield
// l-value that is subsequently passed by reference. Any expression that is
// passed by reference is a child of MaterializeTemporaryExpr, which
// represents a prvalue temporary that is written into memory so that a
// reference can bind to it.
if (const auto* cast_expr = llvm::dyn_cast<clang::CastExpr>(expr)) {
if (cast_expr->getSubExpr()->refersToBitField() &&
GetFirstParentOfType<clang::MaterializeTemporaryExpr>(
*expr, compiler_instance_->getASTContext()) != nullptr) {
return;
}
}

if (optimise_mutations_) {
// If an expression is the direct child of a cast expression, do not mutate
// it unless the cast is an l-value to r-value cast. In an l-value to
Expand Down
10 changes: 10 additions & 0 deletions test/single_file/bitfield_reference_passing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
template<typename T> void bloop(T& x) { }

struct foo {
int b : 2;
};

int main() {
const foo d = foo();
bloop(d.b);
}
54 changes: 54 additions & 0 deletions test/single_file/bitfield_reference_passing.cc.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <cinttypes>
#include <cstddef>
#include <functional>
#include <string>


#ifdef _MSC_VER
#define thread_local __declspec(thread)
#elif __APPLE__
#define thread_local __thread
#endif

static thread_local bool __dredd_some_mutation_enabled = true;
static bool __dredd_enabled_mutation(int local_mutation_id) {
static thread_local bool initialized = false;
static thread_local uint64_t enabled_bitset[1];
if (!initialized) {
bool some_mutation_enabled = false;
const char* dredd_environment_variable = std::getenv("DREDD_ENABLED_MUTATION");
if (dredd_environment_variable != nullptr) {
std::string contents(dredd_environment_variable);
while (true) {
size_t pos = contents.find(",");
std::string token = (pos == std::string::npos ? contents : contents.substr(0, pos));
if (!token.empty()) {
int value = std::stoi(token);
int local_value = value - 0;
if (local_value >= 0 && local_value < 1) {
enabled_bitset[local_value / 64] |= (static_cast<uint64_t>(1) << (local_value % 64));
some_mutation_enabled = true;
}
}
if (pos == std::string::npos) {
break;
}
contents.erase(0, pos + 1);
}
}
initialized = true;
__dredd_some_mutation_enabled = some_mutation_enabled;
}
return (enabled_bitset[local_mutation_id / 64] & (static_cast<uint64_t>(1) << (local_mutation_id % 64))) != 0;
}

template<typename T> void bloop(T& x) { }

struct foo {
int b : 2;
};

int main() {
const foo d = foo();
if (!__dredd_enabled_mutation(0)) { bloop(d.b); }
}
54 changes: 54 additions & 0 deletions test/single_file/bitfield_reference_passing.cc.noopt.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <cinttypes>
#include <cstddef>
#include <functional>
#include <string>


#ifdef _MSC_VER
#define thread_local __declspec(thread)
#elif __APPLE__
#define thread_local __thread
#endif

static thread_local bool __dredd_some_mutation_enabled = true;
static bool __dredd_enabled_mutation(int local_mutation_id) {
static thread_local bool initialized = false;
static thread_local uint64_t enabled_bitset[1];
if (!initialized) {
bool some_mutation_enabled = false;
const char* dredd_environment_variable = std::getenv("DREDD_ENABLED_MUTATION");
if (dredd_environment_variable != nullptr) {
std::string contents(dredd_environment_variable);
while (true) {
size_t pos = contents.find(",");
std::string token = (pos == std::string::npos ? contents : contents.substr(0, pos));
if (!token.empty()) {
int value = std::stoi(token);
int local_value = value - 0;
if (local_value >= 0 && local_value < 1) {
enabled_bitset[local_value / 64] |= (static_cast<uint64_t>(1) << (local_value % 64));
some_mutation_enabled = true;
}
}
if (pos == std::string::npos) {
break;
}
contents.erase(0, pos + 1);
}
}
initialized = true;
__dredd_some_mutation_enabled = some_mutation_enabled;
}
return (enabled_bitset[local_mutation_id / 64] & (static_cast<uint64_t>(1) << (local_mutation_id % 64))) != 0;
}

template<typename T> void bloop(T& x) { }

struct foo {
int b : 2;
};

int main() {
const foo d = foo();
if (!__dredd_enabled_mutation(0)) { bloop(d.b); }
}
Loading