Title: Class 19: Synchronization
Date: 2014-04-08
Category: Classes
Tags: operating systems, kernel, kernel programming, synchronization, Dijkstra, Lamport
Author: David Evans

   <div class="todo"> 
   <center>
**[PS4 Assessment](https://docs.google.com/forms/d/1_MONt8V99KtucMRswC2L6yUtXlKYK9CY3wB9Ubf_MFY/viewform)** due after your demo (by Friday at the latest)  
**[Project Idea](https://docs.google.com/forms/d/1Sxg5aJuhEWPVpVzUzEBrTsChRsn2wtJogsd7ycSjcTk/viewform)** due by **11:59pm Thursday**.
   </center>
   </div>

<center>
<iframe src="http://www.slideshare.net/slideshow/embed_code/33284617" width="476" height="400" frameborder="2" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</center>

# PS4 Postmortem

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/4qpyGyEvNW0?list=PLvpsxlEF9cP3wPP3649CcsB8j82PFab-n" frameborder="2" allowfullscreen></iframe>
</center>

# Yesterday's SSL Bug

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/iA6AYtmJ4UI?list=PLvpsxlEF9cP3wPP3649CcsB8j82PFab-n" frameborder="2" allowfullscreen></iframe>
</center>

[SSL Heartbeat Bug](http://git.openssl.org/gitweb/?p=openssl.git;a=commitdiff;h=96db9023b881d7cd9f379b0c154650d6c108e9a3)  
[Diagnosis of the OpenSSL Heartbleed Bug](http://blog.existentialize.com/diagnosis-of-the-openssl-heartbleed-bug.html)

This is the [buggy version of the code](http://git.openssl.org/gitweb/?p=openssl.git;a=blob;f=ssl/d1_both.c;hb=34628967f1e65dc8f34e000f0f5518e21afbfc7b) (excerpted):
```C
int
dtls1_process_heartbeat(SSL *s)
         {
         unsigned char *p = &s->s3->rrec.data[0], *pl;
         unsigned short hbtype;
         unsigned int payload;
         unsigned int padding = 16; /* Use minimum padding */
 
         /* Read type and payload length first */
         hbtype = *p++;
         n2s(p, payload);
         pl = p;
 
         ...
 
         if (hbtype == TLS1_HB_REQUEST)
                 {
                 unsigned char *buffer, *bp;
                 int r;
 
                 /* Allocate memory for the response, size is 1 byte
                  * message type, plus 2 bytes payload length, plus
                  * payload, plus padding
                  */
                 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
                 bp = buffer;
 
                 /* Enter response type, length and copy payload */
                 *bp++ = TLS1_HB_RESPONSE;
                 s2n(payload, bp);
                 memcpy(bp, pl, payload);
                 bp += payload;
                 /* Random padding */
                 RAND_pseudo_bytes(bp, padding);
 
                 r = dtls1_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
 
                 if (r >= 0 && s->msg_callback)
                         s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
                                 buffer, 3 + payload + padding,
                                 s, s->msg_callback_arg);
 
                 OPENSSL_free(buffer);
 
                 if (r < 0)
                         return r;
                 }
         ...
``` 

How many serious security bugs remain in OpenSSL?  (Hint: if you answer 0 will most likely be proved wrong within a month or two!)
<div class="gap">

</div>

<center><a href="https://xkcd.com/1353/"><img src="http://imgs.xkcd.com/comics/heartbleed.png" width="600"></a></center>

# Projects

**Thursday, 11:59pm ** (10 April): [Project Idea](https://docs.google.com/forms/d/1Sxg5aJuhEWPVpVzUzEBrTsChRsn2wtJogsd7ycSjcTk/viewform)  
**16-21 April**: Project Reviews  
**24 and 29 April**: Project Presentations (in class)

Do something that is **fun** (for you to do, and others to see),
**relevant** (to the class), **technically interesting** (to you and
me), and **useful** (at least to you, hopefully to many).  You probably
can’t maximize all of these!  It is okay to sacrifice one or two of them
to increase others.  A good project should be strong on at least 2 of
these, which is much better than being mediocre of all four.

You may use [the projects from last semester for
inspiration](http://rust-class.org/0/pages/final-projects.html), but do
not feel limited by the kinds of projects students did last semester,
and you also have PS4 to build on.

# Synchronization

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/Tte2D4iKjbA?list=PLvpsxlEF9cP3wPP3649CcsB8j82PFab-n" frameborder="2" allowfullscreen></iframe>
</center>

E. W. Dijkstra, [_Solution to a Problem in Concurrent Program
Control_](|filename|./dijkstra.pdf), Communications of the ACM, 1965.
(1 page)

**Requirements**

1. Only one thread may be in the critical section at any time.
2. Each must eventually be able to enter its critical section.
3. Must be symmetrical (all run same program).
4. Cannot make any assumptions about speed of threads.

```python
   loop {         
         b[i] := false
L1:	 if k != i 
               c[i] := true
    	             if b[k]:
        	            k := i
                  goto L1
         else:
               c[i] := false
               for j in [1, ..., N]:
                    if j != i and not c[j]:
                         goto L1  
               critical section;   
               c[i] := true
               b[i] := true    
    }
```

How can you prove Dijkstra's solution is _safe_?
<div class="gap">

</div>

How can you prove Dijkstra's solution provides _liveness_?
<div class="gap">

</div>

How does Collab solve the mutual exclusion problem?  
<div class="gap">


</div>


Leslie Lamport, [_A New Solution of Dijkstra's Concurrent Programming
Problem_](|filename|./bakery.pdf), Communications of the ACM, 1972. (2 pages)

What are the advantages of Lamport's solution over Dijkstra's?
<div class="gap">

</div>

<div id="disqus_thread"></div>

<script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'rust-class'; // required: replace example with your forum shortname
	var disqus_url = 'http://www.rust-class.org/class-19-synchronization.html';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
