Tomáš Petříček and Vít Šefl
petricek@d3s.mff.cuni.cz | sefl@ksvi.mff.cuni.cz
@tomasp.net | https://tomasp.net







$ cat test.c
#include <stdio.h>
int main(void) {
char msg[] = "Hello world";
char buf[4];
buf[4] = 'J';
printf("%s\n", msg);
return 0;
}
No undefined behavior!

$ cat test.c
#include <stdio.h>
int main(void) {
char msg[] = "Hello world";
char buf[4];
buf[4] = 'J';
printf("%s\n", msg);
return 0;
}
$ gcc test.c && ./test
Jello world
No undefined behavior!
Writing to a value out of array bounds
C does not specify how arrays are allocated



\(\boxed{\Gamma \vdash e : \tau}\)
Given a typing context \(\Gamma\), the
type of the expression \(e\) is \(\tau\).


Arguments of correct types
References
are available
No mismatched return values


Primitive types only
Contexts \(\Gamma\) are finite partial maps from location names \(\mathbb{L}\) to location types \(T_{loc}\)
Location types are
only ints (for now)


Typing rules for primitives, operators and if
Assumptions are other judgements or side-conditions


Look at the syntactic structure, choose a rule to apply
Repeat for all sub-expressions until done
Conditional expression can be of any type!


Store access requires \(l\) is in \(\Gamma\)
No construct modifies \(\Gamma\) (now)


Example #1: Arguments match and \(l_1\) is in the context
Example #2: Accessing undefined location \(l_1\)

skip like the semantics


If \(\Gamma \vdash e : \tau\) and \(dom(\Gamma)\subseteq dom(s)\) then either \(e\) is a
value or there exist \(e', s'\) such that \(\langle e, s \rangle \rightarrow \langle e', s'\rangle\).
If \(\Gamma \vdash e : \tau\) and \(dom(\Gamma) \subseteq dom(s)\) and
\(\langle e, s \rangle \rightarrow \langle e', s'\rangle\)
then \(\Gamma \vdash e' : \tau\)
and \(dom(\Gamma) \subseteq dom(s')\).

If \(\Gamma \vdash e : \tau\) and \(dom(\Gamma)\subseteq dom(s)\) and
\(\langle e, s \rangle \rightarrow^{*} \langle e', s'\rangle\)
then either
\(e'\) is a value or there exist \(e'', s''\) such
that \(\langle e', s' \rangle \rightarrow \langle e'', s''\rangle\).


