Top Banner
50.530 Exercise Solutions 2014 Fall Term
35

50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Dec 31, 2015

Download

Documents

Maurice Harmon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

50.530 Exercise Solutions

2014 Fall Term

Page 2: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 2: Exercise 1public static Boolean repOK(Stack mystack) {if (mystack.capacity() < 0) {return false;}

if (mystack.empty() != mystack.elements().hasMoreElements()) {return false;}

if (mystack.isEmpty() && mystack.size() != 0) {return false;}

//...

return true;}

Page 3: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 2: Exercise 2

• Valid test case:1. OrdSet newobj = new OrdSet(5);2. OrdSet newobj = new OrdSet(5); newobj. add(3);

• Redundant test case:OrdSet newobj = new OrdSet(5); newobj.toString();

• Illegal test case: OrdSet newobj = new OrdSet(-1);

Page 4: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 3: Exercise 1

DD+({1..8}, {}, 2)

DD+({1..8}, {}, 4)

try again

DD+({1..4,7,8}, {5,6}, 8)

test({1..4}) =? = test({5...8})

test({5,6}) = PASS

try again

test({5,6,8}) = FAIL

DD+({8}, {5,6}, 2)

found in {8}

return {8};

Page 5: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 3: Exercise 2

Use DDmin algorithm to minimize the input• Cut the input in a half and test on the first half• It fails and thus apply DDmin again and repeat.Once the minimum input is reached, identify the bug by tracing through the code based on the minimum test case.

Page 6: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 1

• Using Set Union: bug is at line 7 (since only line 7 is unique to the failed test case).

• Using Set Intersection: the result is {} and thus no predication.

Page 7: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 2

• Using Set Union: the result is {} and thus no predication.

• Using Set Intersection: the result is {} and thus no predication.

Page 8: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 3

• The nearest neighbor would be the second last test case and the predicate would be: line 7.

Page 9: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 4

• The nearest neighbor would be the first test case and the predicate would be: none.

Page 10: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 5

Page 11: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 5: Exercise 6

• To apply the method A/B/C/D, you need to instruct the program to monitor its execution (one for each block of code without any branches). • None of the methods would locate the bug (for instance, method E

would not work as the buggy line is not updating any variable) except perhaps method D (which depends on the test cases you generate).

Page 12: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 6: Exercise 1

• The slice of the assertion includes all the statements.

Page 13: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 1

• Answer 1: No, there is no race condition according to the happens-before approach.• Answer 2: Yes, there is a race condition on variable count (the first

and last line).

Page 14: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 2

• The lockset algorithm finds the race condition at line 1 since count is a shared variable and it is accessed without any lock held (so that the intersection is {}).

Page 15: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 3

• For variable count, after the first count++, it moves from state Virgin to state Exclusive• After the second count++, it moves from state Exclusive to Shared-

Modified state since it is accessed by a new thread with write privilege. • The checking then happens and since there is no lock held, a race

condition is reported.

Page 16: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 4

• The report would be there is no race.

Page 17: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 5

• Lockset-based algorithm• Race on x: line 10 and 1• Race on z: line 7 and 5

• Happens-before filtering• No race on Z since there is happens-before from unlock(L) to lock(L)

Page 18: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 8: Exercise 6

• Assume that thread 1 is scheduled first, after executing line 1, it is postponed; • Thread 2 is scheduled to run line 7 and then is blocked at line 8.• Thread 1 moves on and thus no race is reported.

Page 19: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 9: Exercise 1

(3*y + z)*y - z > 0

Page 20: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 9: Exercise 2

5 * y - z > 0

Page 21: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 9: Exercise 3

We get the following Hoare triple first.{K>0&&M>0&&k=K&&m=M}while(k!=m) {

if (k > m) {k = k-m;} else {m = m-k;}

}{k=GCD(K,M)}We discharge it using the loop invariant: GCD(K, M) = gcd(k,m)

Page 22: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 9: Exercise 4

First row from left to right: yes, yes, noSecond row from left to right: yes, yes, yes

Page 23: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 9: Exercise 5

For the inner loop, the ranking function is x-y and termination can be proved by establishing the following Hoare triple.

{x-y=V&&y<x&&y>0} y = 2*y {x-y < V} For the outer loop, the ranking function is x and termination can be proved by establishing the following Hoare triple

{x>=0&&x=V} y := 1;while (y < x) { y = 2*y; }x = x – 1;{x < V}

which can be proved using a loop invariant (i.e., x is unchanged) for the inner loop.

Page 24: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 10: Exercise 1

(Monday or Wednesday or Thursday)and(!Wednesday)and(!Friday)and(!Tuesday and !Thursday)

SAT solving would lead to a solution: Monday = true; the rest = false;

Page 25: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 10: Exercise 2

The assertion can be demonstrated possible by symbolically executing the following path. !a && b && !a && c && y = 1 && z = 2 && !(x+y+z!=3)The solution is: a = false; b = true; c = true

Page 26: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 10: Exercise 3

Example: x+y<=4

Page 27: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 10: Exercise 4

Symbolic execution the left most path, apply interpolantion and get y>=1 at node 3. Skip the last alternative; backtrack …

2

3 3

4 4 4

2

3 3

4 4 4

1x = x+1

[y>=1]x = x+2

[y<1]x = x+4

*

y<1

y>=1

4 4

[y>=1]x = x+2 y<1

[y<1]x = x+4 [y<1]x = x+4 [y<1]x = x+4y>=1 y>=1 y>=1

Page 28: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 10: Exercise 5

The only exception that could rise at array[x]=2 when x < 0. The following proves that if that statement is never reached, x >= 0. Base case: assume input is false, the following is true: {x>=0}rec(){x>=0} which is basically {x>=0}x=x+1{x>=0} (*).Induction step: assume that {x>=0}rec(){x>=0}.

{x>=0}rec(){x>=0} implies {x>=0}rec(); rec();{x>=0} (**).{x>=0} method-body {x>=0} since (*) and (**){x>=0} rec() {x>=0}

Page 29: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 11: Exercise 1

We can prove that the error is not occurring using a Hoare triple as below: {x=0.1&&y=0}loop-body{y>=0}, which can be proved using the following loop invariant: y>=0.

Page 30: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 11: Exercise 2

Sample: y=1, orig(x) > 0, x > y, return = *x, return = orig(*x) + y

Page 31: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 11: Exercise 3

The same tree (the proof) will be built.

L0

L1

L2

L3

new!=old

L4L5

new=old+1

L1

L6 L2

L5

L1subsumed by new!=old

Page 32: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 12: Exercise 1

Page 33: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 12: Exercise 2

1: if (*) { 3: got_lock = 0;4: if (*) {5: lock();6: got_lock ++;7: }8: if (got_lock) {9: unlock();10: }11: if (*) {goto 3;}12: }14: lock();15: old = new;16: if (*) {17: unlock();18: new ++;19: }20: if (new != old) {goto 14;}21: unlock ();

1: if (*) { 3: skip;4: if (*) {5: locked=true6: skip;7: }8: if (*) {9: locked=false;10: }11: if (*) {goto 3;}12: }14: locked=true;15: skip;16: if (*) {17: locked=false;18: skip;19: }20: if (*) {goto 14;}21: locked=true;

Page 34: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 12: Exercise 3

r

gy

Page 35: 50.530 Exercise Solutions 2014 Fall Term. Week 2: Exercise 1 public static Boolean repOK(Stack mystack) { if (mystack.capacity() < 0) { return false;

Week 12: Exercise 4

2

3

4

5

6

7

8

{!locked, ne}

{locked, ne}

{locked, !ne}

{!locked, !ne}

{!locked, ne}

{!locked, ne} 8{locked, !ne}

{locked, !ne}