Skip to content

Commit

Permalink
Merge pull request #74 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Fixed bug in RCE
  • Loading branch information
AjayBrahmakshatriya authored May 20, 2024
2 parents 173b884 + 97cf734 commit 1a8f476
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/builder/operator_overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ typename return_type_helper<T>::type operator&(const T &a) {

// Prefix increment
template <typename T>
typename return_type_helper<T>::type operator++(T &a) {
typename return_type_helper<typename std::remove_reference<T>::type>::type operator++(T &&a) {
return (a = a + 1);
}
// Postfix increment
template <typename T>
typename return_type_helper<T>::type operator++(T &a, int) {
typename return_type_helper<typename std::remove_reference<T>::type>::type operator++(T &&a, int) {
return ((a = a + 1) - 1);
}
// Prefix decrement
template <typename T>
typename return_type_helper<T>::type operator--(T &a) {
typename return_type_helper<typename std::remove_reference<T>::type>::type operator--(T &&a) {
return (a = a - 1);
}
// Postfix increment
template <typename T>
typename return_type_helper<T>::type operator--(T &a, int) {
typename return_type_helper<typename std::remove_reference<T>::type>::type operator--(T &&a, int) {
return ((a = a - 1) + 1);
}

Expand Down
1 change: 1 addition & 0 deletions samples/outputs.var_names/sample42
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ void bar (void) {
y_0[0] = y_0[0] - x_2;
y_0[x_2] = y_0[x_2] / 2;
}
(y_0[0] = y_0[0] + 1) - 1;
}

6 changes: 6 additions & 0 deletions samples/outputs.var_names/sample57
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
void bar (void) {
int x_0 = 0;
int* y_1 = (&(x_0));
(y_1[0] = y_1[0] + 1) - 1;
}

1 change: 1 addition & 0 deletions samples/outputs/sample42
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ void bar (void) {
var0[0] = var0[0] - var2;
var0[var2] = var0[var2] / 2;
}
(var0[0] = var0[0] + 1) - 1;
}

6 changes: 6 additions & 0 deletions samples/outputs/sample57
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
void bar (void) {
int var0 = 0;
int* var1 = (&(var0));
(var1[0] = var1[0] + 1) - 1;
}

2 changes: 2 additions & 0 deletions samples/sample42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ static void bar(void) {
*y -= x;
y[x] /= 2;
}

(*y)++;
}

int main(int argc, char *argv[]) {
Expand Down
31 changes: 31 additions & 0 deletions samples/sample57.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Include the headers
#include "blocks/c_code_generator.h"
#include "builder/static_var.h"
#include "builder/dyn_var.h"
#include "blocks/rce.h"
#include <iostream>

// Include the BuildIt types
using builder::dyn_var;
using builder::static_var;

static void bar(void) {
// Insert code to stage here

dyn_var<int> x = 0;
dyn_var<int*> y = &x;

(*y)++;


}

int main(int argc, char* argv[]) {
builder::builder_context context;
auto ast = context.extract_function_ast(bar, "bar");
block::eliminate_redundant_vars(ast);
block::c_code_generator::generate_code(ast, std::cout, 0);
return 0;
}


10 changes: 10 additions & 0 deletions src/blocks/rce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class var_use_counter : public block_visitor {
if (std::find(assigned_vars.begin(), assigned_vars.end(), v) == assigned_vars.end())
assigned_vars.push_back(v);
}
virtual void visit(addr_of_expr::Ptr e) override {
e->expr1->accept(this);
if (!isa<var_expr>(e->expr1))
return;
var_expr::Ptr ve = to<var_expr>(e->expr1);
var::Ptr v = ve->var1;
// Variables that have their addresses taken can potentially be assigned and hence should not be copy eliminated
if (std::find(assigned_vars.begin(), assigned_vars.end(), v) == assigned_vars.end())
assigned_vars.push_back(v);
}
};

class check_side_effects : public block_visitor {
Expand Down

0 comments on commit 1a8f476

Please sign in to comment.