Skip to content

Restore returns fix #294

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 2 commits into from
Nov 7, 2016
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
2 changes: 1 addition & 1 deletion regression/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DIRS = ansi-c cbmc cpp
DIRS = ansi-c cbmc cpp goto-instrument

test:
$(foreach var,$(DIRS), $(MAKE) -C $(var) test || exit 1;)
32 changes: 32 additions & 0 deletions regression/goto-instrument/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

default: tests.log

test:
@if ! ../test.pl -c ../chain.sh ; then \
../failed-tests-printer.pl ; \
exit 1; \
fi

tests.log:
@if ! ../test.pl -c ../chain.sh ; then \
../failed-tests-printer.pl ; \
exit 1; \
fi

show:
@for dir in *; do \
if [ -d "$$dir" ]; then \
vim -o "$$dir/*.c" "$$dir/*.out"; \
fi; \
done;

clean:
@for dir in *; do \
rm -f tests.log; \
if [ -d "$$dir" ]; then \
cd "$$dir"; \
rm -f *.out *.gb; \
cd ..; \
fi \
done

13 changes: 13 additions & 0 deletions regression/goto-instrument/chain.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

SRC=../../../src

GC=$SRC/goto-cc/goto-cc
GI=$SRC/goto-instrument/goto-instrument

OPTS=$1
NAME=${2%.c}

$GC $NAME.c -o $NAME.gb
$GI $OPTS $NAME.gb

13 changes: 13 additions & 0 deletions regression/goto-instrument/restore-returns1/ret.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

int foo()
{
int x;
return x;
}

int main()
{
foo();
return 0;
}

7 changes: 7 additions & 0 deletions regression/goto-instrument/restore-returns1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
ret.c
"--escape-analysis --dump-c"
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
19 changes: 19 additions & 0 deletions regression/goto-instrument/restore-returns2/ret.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

int foo()
{
int x;
int y;
if (x)
{
return 0;
}
int z;
return 1;
}

int main()
{
foo();
return 0;
}

7 changes: 7 additions & 0 deletions regression/goto-instrument/restore-returns2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
ret.c
"--escape-analysis --dump-c"
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
33 changes: 18 additions & 15 deletions src/goto-programs/remove_returns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,29 @@ bool remove_returnst::restore_returns(
// replace "fkt#return_value=x;" by "return x;"
code_returnt return_code(assign.rhs());

// now turn the `return' into `assignment'
i_it->type=RETURN;
i_it->code=return_code;
// the assignment might be a goto target
i_it->make_skip();
i_it++;

// remove the subsequent goto (and possibly dead)
goto_programt::instructionst::iterator next=i_it;
++next;
assert(next!=goto_program.instructions.end());
while(!i_it->is_goto() && !i_it->is_end_function())
{
assert(i_it->is_dead());
i_it++;
}

if(next->is_dead())
if(i_it->is_goto())
{
assert(to_code_dead(next->code).symbol()==
return_code.return_value());
next=goto_program.instructions.erase(next);
assert(next!=goto_program.instructions.end());
goto_programt::const_targett target=i_it->get_target();
assert(target->is_end_function());
}
else
{
assert(i_it->is_end_function());
i_it=goto_program.instructions.insert(i_it, *i_it);
}

assert(next->is_goto());
// i_it remains valid
goto_program.instructions.erase(next);
i_it->make_return();
i_it->code=return_code;
}
}

Expand Down