I took a look.
The C code is really “wild” and it would require a big effort to translate
it into ATS. Also, the ATS code translated from the C code would probably
scare people.
I propose a less demanding approach that may be much easier to gain
acceptance by C programmers.
First, let us see some code:
typedef region_proof = int
extern region_proof region_assert (void p, size_t n) ;
extern region_proof region_contain (int proof, void p, size_t n, void* p1,
size_t n1);
extern
void *memcpy_safe (region_proof, region_proof, void dst, void src, int n);
The idea is very simple:
region_assert is like a crypto hash function; it generates a hash value for
p and n
combined.
region_contain (pf, p, n, p1, n1) does three things:
- it checks that [pf] is the hash value for p and n;
- it checks that p <= p1 and p1+n1 <= p+n
- it generates a hash value for p1 and n1
memcpy_safe (pf_dst, pf_src, dst, src, n) does 3 three things
- it checks that pf_dst is a region proof for (dst, n)
- it checks that pf_src is a region proof for (src, n)
- it calls memcpy (dst, src, n)
Of course, proof construction and proof-checking can be readily
erased in production code.
The point of having such a mechanism is to allow C programmers
can at least start to think like ATS programmers. I have a hunch that this
may initiate a trend that can greatly enhance memory safety in C programming
and possibly lure people into learning ATS 
Cheer,
–HongweiOn Wednesday, April 9, 2014 8:41:41 PM UTC-4, Chris Double wrote:
I’ve created a branch of openssl with the bugfix reverted and some
infrastructure to implement code in ATS2:
https://github.com/doublec/openssl/tree/ats
This branch just replaces the dtls1_process_heartbeat function with an
ATS version. The ATS version is in ‘ssl/d1_both.dats’. It just a
copy/paste of the buggy C code wrapped in a %{ … %} block. The
ssl/Makefile uses patsopt to compile this to C and include it in the
openssl library. I comment out the old C function in ssl/d1_both.c.
To build:
$ git clone git://github.com/doublec/openssl
$ cd openssl
$ git checkout -b ats ats
$ ./config
$ make
$ make test
The next step would be to modify d1_both.dats to implement the
function in ATS and show some ATS features that would detect the bug.
Then implement the fix and show how this then compiles (hopefully!).
I’m hoping to get time to write a blog post about this to show some
ATS features. Any thoughts on the approach and ways to use ATS
welcome.
–
http://www.bluishcoder.co.nz