Lecture 2: Systems Programming
Lecture 2 is an introduction to systems programming. It discusses what
is meant by systems programming, what is the state of the art, and how
changes in the environment are driving changes in systems and the way
we write systems programs. It concludes with a discussion of how systems
programming might evolve, to set the scene for the remainder of the
course.
Part 1: Systems Programming
The 1st part of this lecture reviews what is systems programming.
It talks about the tendency of systems programs to be constrained
by memory performance, the need to conform to external data
representations. It highlights how I/O performance is frequently
a bottleneck. And it discusses the need to efficient management
of shared state. Background reading is highlighted that considers
these issues in more depth.
00:00:00.000
In this lecture, I’d like to introduce
00:00:02.967
the concept of systems programming, and explain
00:00:04.933
what makes it a uniquely challenging problem.
00:00:08.000
I’ll start be explaining what is systems
00:00:10.624
programming, and what makes it different to
00:00:13.148
applications programming.
00:00:13.869
I’ll briefly discuss the state of the
00:00:16.493
art in systems programming languages and operating
00:00:19.017
systems. Then, I’ll move on to highlight
00:00:21.541
some challenges and limitations that affect these
00:00:24.065
state of the art systems.
00:00:25.868
Finally, I’ll conclude by discussing some possible
00:00:28.492
next steps in systems programming, to set
00:00:31.015
the scene for the remainder of the
00:00:33.539
course.
00:00:35.000
So, what are systems programs?
00:00:37.394
They’re the programs that form the infrastructure.
00:00:40.706
They comprise the operating systems, device drivers,
00:00:44.018
network protocol stacks, and services on which
00:00:47.229
the rest of the computing ecosystem is
00:00:50.441
built.
00:00:51.000
Systems programs tend to be characterised by
00:00:54.173
strict performance constraints.
00:00:55.489
They must effectively manage memory, and achieve
00:00:58.662
high performance while matching externally-specified constraints on
00:01:01.735
the data representation. Authors of device drivers
00:01:04.807
and network protocol implementations don’t have the
00:01:07.880
luxury of choosing their own data representation:
00:01:10.952
they must match externally specified formats exactly.
00:01:14.025
And they must do so while achieving
00:01:17.097
high performance.
00:01:17.975
This rules out surprisingly many programming languages
00:01:21.148
and environments, since they don’t provide such
00:01:24.220
control. It’s difficult to write high-performance systems
00:01:27.293
programs in Java or Python, for example,
00:01:30.365
since they don’t give you control over
00:01:33.438
how data is laid out in memory.
00:01:36.511
Languages like C, C++, and Rust do
00:01:39.583
give such control.
00:01:41.000
Systems programs need to manage I/O efficiently.
00:01:43.936
In part, this is a continuation of
00:01:46.872
the previous point about data layout.
00:01:49.303
If the internal and external representations of
00:01:52.138
data differ, then the format conversion hurts
00:01:54.974
performance.
00:01:56.129
But it’s also that I/O performance of
00:01:59.065
systems programs is fundamental. If a single
00:02:01.901
application is slow, then it can be
00:02:04.737
replaced with minimal impact on the rest
00:02:07.573
of the system. If the underlying operating
00:02:10.409
system is slow, the entire system is
00:02:13.245
affected.
00:02:13.000
Systems programs also need to effectively manage
00:02:16.725
shared state.
00:02:17.761
Modern hardware is increasingly multicore, and modern
00:02:21.487
applications are concurrent. Operating systems resources,
00:02:24.594
device drivers, network protocols, and other libraries
00:02:28.219
are always being accessed by multiple processes,
00:02:31.845
and in multiple contexts, at once.
00:02:34.952
Race conditions, deadlocks, and poor performance at
00:02:38.578
this level affects everything running on the
00:02:42.203
system. It’s critical to making effective use
00:02:45.828
of the available resources.
00:02:49.000
Memory management for systems programs must be
00:02:52.237
predictable.
00:02:53.435
There must be predictable bounds on memory
00:02:56.672
usage. The memory needed by the system
00:02:59.808
should scale with the tasks it’s performing,
00:03:02.945
and overheads should be small compared to
00:03:06.082
the data. It should be clear how
00:03:09.219
much memory is used, and when that
00:03:12.356
memory is used, so that the system
00:03:15.492
as a whole can be understood.
00:03:18.181
This is especially important as real-time applications
00:03:21.418
become more common, since unpredictability in memory
00:03:24.555
management can disrupt application timing performance.
00:03:27.243
Consistent and bounded memory usage is beneficial
00:03:30.480
overall though, especially as relatively memory constrained
00:03:33.617
devices, such as IoT devices, wearables,
00:03:36.306
and smartphones, proliferate.
00:03:37.000
Data representation and locality matters for performance.
00:03:40.144
The diagram shows the effects of object
00:03:43.288
sizes and their spacing, known as the
00:03:46.332
stride distance, on read performance for a
00:03:49.375
typical modern system.
00:03:50.680
We see that as the size of
00:03:53.824
the objects being read increases, the read
00:03:56.868
throughput drops. Similarly, as the stride increases,
00:03:59.912
that is, as objects are spaced further
00:04:02.955
apart in memory, throughput drops. This drop
00:04:05.999
in throughput can be dramatic: the worst
00:04:09.043
case shows several orders of magnitude performance
00:04:12.087
loss compared to the peak.
00:04:14.261
The cause is the CPU cache.
00:04:16.970
Modern processors can access small objects,
00:04:19.579
located close to each other in memory,
00:04:22.623
extremely quickly. Fetching larger objects into the
00:04:25.667
cache is slower. And fetching widely,
00:04:28.276
or irregularly, spaced objects from memory is
00:04:31.320
slower still.
00:04:32.190
Effective systems programs arrange data in memory
00:04:35.334
such that related objects are co-located,
00:04:37.943
to achieve high performance. This requires a
00:04:40.986
programming language that gives precise control over
00:04:44.030
data layout.
00:04:45.000
Similarly, device drivers and network protocol implementation
00:04:48.160
must confirm to fixed external layouts.
00:04:50.784
Whether matching the structure of a hardware
00:04:53.844
control register, or the format of a
00:04:56.905
standard network protocol data unit, such systems
00:04:59.965
must have exact bit-level control over the
00:05:03.026
data representation.
00:05:04.000
The key feature of systems programming languages
00:05:07.049
is that they provide such control over
00:05:09.998
data layout and memory management. This introduces
00:05:12.947
complexity – pointers in C are undoubtably
00:05:15.896
harder to learn than objects in Java,
00:05:18.845
for example – but it’s needed if
00:05:21.794
performance is to be maintained.
00:05:25.000
The second constraint on systems programs tends
00:05:27.956
to be I/O performance.
00:05:29.588
The chart shows how the transmission rate
00:05:32.544
of Ethernet has increased over time,
00:05:34.991
from the initial 1Mbps data rate,
00:05:37.439
to 40 Gbps parts, and with 100
00:05:40.295
Gbps Ethernet becoming available. It’s clear that
00:05:43.151
the transmission rate of Ethernet, in bits
00:05:46.007
per second, is still growing at something
00:05:48.862
close to an exponential rate.
00:05:50.902
While this has been happening, the maximum
00:05:53.858
Ethernet packet size, the MTU, has remained
00:05:56.714
constant at 1500 bytes. The result is
00:05:59.570
that the number of packets sent per
00:06:02.426
second has increased exponentially.
00:06:04.057
Wireless link speeds follow a similar curve.
00:06:07.013
This is becoming problematic, because single core
00:06:09.969
CPU performance peaked in the mid-2000s.
00:06:12.417
For over a decade now, the number
00:06:15.273
of network packets that must be processed
00:06:18.129
per second has been increasing, while performance
00:06:20.984
of the processors that must handle those
00:06:23.840
packets has improved little.
00:06:25.472
The result is that packets must be
00:06:28.428
processed more efficiently than ever before.
00:06:30.876
The system has less time to spend
00:06:33.732
processing each packet, since it must process
00:06:36.587
more packets with approximately the same amount
00:06:39.443
of resources.
00:06:40.259
To make matters worse, while this has
00:06:43.215
been happening, we’ve also seen the move
00:06:46.071
to encrypt virtually all of the data
00:06:48.927
passing over the network, to improve security,
00:06:51.782
and the increasing use of streaming and
00:06:54.638
interactive video. Both of these increase the
00:06:57.494
amount of work that must be performed
00:07:00.350
for each packet.
00:07:01.574
The combination of more work per packet,
00:07:04.530
more packets per second, and processors that
00:07:07.385
are not getting much faster, make writing
00:07:10.241
network device drivers, protocol stack implementations,
00:07:12.689
and networked applications, increasingly difficult.
00:07:14.729
Similar trends exist for solid state disk
00:07:17.685
performance.
00:07:18.843
The I/O stack, network and disk,
00:07:21.391
is fast becoming a critical bottleneck in
00:07:24.246
overall system performance.
00:07:25.470
Systems software to support I/O has to
00:07:28.426
be highly efficient.
00:07:30.000
The final constraint on systems programs is
00:07:33.208
management of shared state.
00:07:34.984
Systems programs are responsible for coordinating use
00:07:38.192
of shared resources between the operating system
00:07:41.300
and the applications running on the system.
00:07:44.408
They manage the internal state of the
00:07:47.516
operating system kernel, the file systems,
00:07:50.180
network code, device drivers, etc. All this
00:07:53.288
is accessed concurrently by multiple threads,
00:07:55.952
as different applications access operating system resources.
00:07:59.060
Applications programs can be written in a
00:08:02.268
way that ignores concurrency, and uses only
00:08:05.376
a single thread of execution, if the
00:08:08.484
application programmer so chooses. The systems programs
00:08:11.592
that support those applications don’t have that
00:08:14.700
option. They must support multiple applications running
00:08:17.808
at once, on multicore hardware.
00:08:20.028
Concurrency and parallelism are pervasive concerns for
00:08:23.236
systems programs, and cannot be avoided.
00:08:27.000
What is common to all these concerns
00:08:30.289
is that performance of the systems programs
00:08:33.478
comprising the infrastructure fundamentally affects the overall
00:08:36.667
system performance.
00:08:37.578
Inefficiencies in systems programs will reduce performance,
00:08:40.867
and increase power consumption of the entire
00:08:44.056
system, irrespective of the application it’s running.
00:08:47.244
This means that systems components are often
00:08:50.433
the bottleneck, simply because they’re the basis
00:08:53.622
on which higher-layer components depend.
00:08:57.000
These trends push systems programming languages to
00:08:59.952
offer low-level control.
00:09:01.174
To achieve the necessary performance and efficiency,
00:09:04.125
it’s essential that the program be able
00:09:06.977
to control the layout of data in
00:09:09.828
memory, to manage when, and in what
00:09:12.680
order, data is accessed, and to have
00:09:15.531
precise control of when, and how,
00:09:17.975
data is shared.
00:09:19.197
This is why languages, such as C
00:09:22.149
and C++, that give such low-level control,
00:09:25.000
remain popular for systems programming.
00:09:27.037
A C programmer can, if they choose,
00:09:29.989
control precisely the layout of data in
00:09:32.840
memory, and they have the flexibility to
00:09:35.692
choose exactly when and how data is
00:09:38.543
shared between threads. They don’t have to
00:09:41.395
exercise that control, but it’s available if
00:09:44.246
necessary.
00:09:45.404
Providing this control is a hallmark of
00:09:48.355
systems programming languages.
00:09:49.577
It pushes the language, and it’s runtime,
00:09:52.529
to be more transparent and predictable in
00:09:55.380
how it compiles to machine instructions,
00:09:57.824
and about the costs of various operations,
00:10:00.676
and to favour simpler, more predictable,
00:10:03.120
runtime environments.
00:10:03.935
Languages such as Java, Python, and Go,
00:10:06.886
that do not provide such transparency and
00:10:09.738
control, are not well suited to such
00:10:12.589
low-level systems programming.
00:10:13.811
The paper listed on the slide,
00:10:16.356
“Programming language challenges in systems codes:
00:10:18.800
why systems programmers still use C,
00:10:21.244
and what to do about it” by
00:10:24.095
Jonathan Shapiro, explores these ideas, and discusses
00:10:26.947
what is systems programming, in more detail.
00:10:29.798
I strongly encourage you to read it.
00:10:33.000
That concludes this introduction to what makes
00:10:35.948
systems programming challenging. In the next part,
00:10:38.797
we’ll briefly discuss the state of the
00:10:41.645
art in systems programming languages and operating
00:10:44.493
systems.
Part 2: The State of the Art
The 2nd part of the lecture considers the state of the art in systems
programming. It reviews the history of C and Unix, and briefly
highlights their strengths and weaknesses.
00:00:00.000
What is the state of the art
00:00:02.023
in operating systems and systems programming?
00:00:04.000
Most modern computing systems run some variant
00:00:07.059
of Unix as their operating system.
00:00:09.594
That operating system, and many of its
00:00:12.553
supporting utilities and services, are written in
00:00:15.512
the C programming language.
00:00:17.202
If we think about the computers we
00:00:20.261
carry with us every day, for example,
00:00:23.219
Android phones run a variant of Linux,
00:00:26.178
and the iPhone operating system is also
00:00:29.136
a Unix variant: it’s derived from a
00:00:32.095
combination of the Mach microkernel, from Carnegie
00:00:35.054
Mellon University, and 4.3BSD Unix, along with
00:00:38.012
a large number of user space utilities
00:00:40.971
adapted from FreeBSD. Both feature a Unix-like
00:00:43.929
kernel, runtime, and core system written in
00:00:46.888
C, augmented by user interface toolkits and
00:00:49.846
frameworks written in a higher-level language.
00:00:52.382
In the data centre, to support the
00:00:55.441
applications we use, are tens of thousands
00:00:58.399
of Linux servers.
00:00:59.667
And on the desktop, macOS is the
00:01:02.726
same core software stack as the iPhone.
00:01:05.685
About the only popular system not based
00:01:08.643
on Unix is Microsoft Windows. The Windows
00:01:11.602
kernel is, instead, heavily influenced by VMS,
00:01:14.560
another system that dates to the same
00:01:17.519
era as Unix. It’s also written in
00:01:20.477
C.
00:01:21.000
The core of Unix is not a
00:01:24.340
modern design. It was originally written in
00:01:27.579
assembly for the PDP-7 minicomputer in 1969,
00:01:30.819
and ported to the PDP-11 in the
00:01:34.058
early 1970s. The PDP-11, as shown in
00:01:37.298
the photo, was an impressive machine for
00:01:40.537
its time, capable of supporting large multi-user
00:01:43.777
and multi-tasking applications with up to 124K
00:01:47.016
of memory. This more powerful machine allowed
00:01:50.256
the operating system to be rewritten into
00:01:53.495
a new, higher-level, programming language, developed in
00:01:56.735
parallel with Unix, that eventually became C.
00:01:59.974
Unix was the first operating system to
00:02:03.314
be written in a high level language,
00:02:06.553
with very little assembly code, and this
00:02:09.793
made it highly portable. Indeed, there was
00:02:13.033
a thriving open source community developing Unix
00:02:16.272
in the 1980s and early 1990s,
00:02:19.049
centred around the University of California Berkeley
00:02:22.288
and BSD Unix.
00:02:23.677
BSD Unix eventually became a core part
00:02:27.016
of Apple’s operating systems, while Linux development
00:02:30.256
was kick-started by a lawsuit between AT&T,
00:02:33.495
the original developers of Unix, and the
00:02:36.735
University of California, over the status of
00:02:39.974
BSD Unix.
00:02:41.000
The design of Unix and C has
00:02:43.836
proven surprisingly resilient and flexible. The system
00:02:46.571
calls, APIs, and user space tools have
00:02:49.307
been extended and augmented over the years,
00:02:52.042
but the core APIs, tools, and programming
00:02:54.778
language are recognisably the same.
00:02:56.732
Unix clearly did a lot of things
00:02:59.567
right – otherwise it could never have
00:03:02.303
survived for this long, and been adapted
00:03:05.038
to so many different platforms and environments
00:03:07.774
– but is it still the right
00:03:10.509
model?
00:03:12.000
Much of the success of Unix came
00:03:15.147
because it was portable, and because the
00:03:18.193
source code was available to universities,
00:03:20.805
allowing a strong developer and user community
00:03:23.851
to form. The development of the BSD
00:03:26.898
Sockets API, and one of the earliest
00:03:29.945
TCP/IP stacks, also helped Unix grow along
00:03:32.991
with the Internet.
00:03:34.297
But I think there’s more than just
00:03:37.444
community. Unix has a small and,
00:03:40.055
for its time, a relatively clean and
00:03:43.102
consistent programming API. It’s easy to understand
00:03:46.149
and extend, robust and reasonably high-performance,
00:03:48.760
and its transparent and offers low-level control
00:03:51.807
and access when needed. The kernel and
00:03:54.853
utilities are, on the whole, well designed.
00:03:58.000
Further, the C programming language was portable,
00:04:01.008
easy to use, and offered features that
00:04:03.916
were quite high-level – for it’s time.
00:04:06.824
To programmers coming from writing systems programs
00:04:09.732
in assembly language, the abstraction of pointers
00:04:12.640
in C greatly simplified writing portable device
00:04:15.548
drivers and other low-level code.
00:04:17.625
The concept of pointers in C provides
00:04:20.633
a single mechanism to solve many problems.
00:04:23.541
The same approach lets you build complex
00:04:26.449
data structures, manipulate data in memory,
00:04:28.941
and access hardware control registers – no
00:04:31.849
other programming language offers such a simple
00:04:34.757
and successful abstraction for such uses.
00:04:37.250
Further, the C type system is strong
00:04:40.258
enough to make writing systems programs significantly
00:04:43.166
easier than writing them in assembly,
00:04:45.658
yet weak enough to allow aliasing,
00:04:48.151
casting, and sharing of data in ways
00:04:51.059
that were necessary for performance.
00:04:53.136
For example, a zero copy network protocol
00:04:56.144
stack can be cleanly implemented in C,
00:04:59.052
passing pointers to different offsets inside a
00:05:01.960
packet, cast to different types, to the
00:05:04.868
next layer in the stack as the
00:05:07.776
packet is processed. Such aliasing and flexibility
00:05:10.684
with types is natural to an assembly
00:05:13.592
language programmer, and can be cleanly expressed
00:05:16.500
in C, but is a very poor
00:05:19.407
fit for most other programming languages.
00:05:22.000
The code fragment on the slide gives
00:05:25.118
an example, to show how C can
00:05:28.135
be used to cleanly write portable device
00:05:31.153
drivers.
00:05:32.334
There are two parts to this.
00:05:35.020
The first is the definition of a
00:05:38.038
data type, struct ctrl_reg, and the second
00:05:41.055
is the definition of the enable_irq() function.
00:05:44.073
The struct definition describes the format of
00:05:47.190
a control register for some hardware device.
00:05:50.208
It uses the bitfield syntax of C
00:05:53.225
to specify the exact layout of a
00:05:56.243
16 bit control word.
00:05:57.967
The enable_irq() function reads from a control
00:06:01.084
register, r, located at a fixed memory
00:06:04.102
address, 0x80 00 00 24. It checks
00:06:07.119
if the busy bit, the 5th bit
00:06:10.137
in the control register, is clear,
00:06:12.723
and if so, enables interrupts by setting
00:06:15.741
the irq_enable bit and writing to the
00:06:18.758
control register.
00:06:19.621
It’s a simple example, showing the sort
00:06:22.738
of data structure and operation that is
00:06:25.756
common in device drivers, and showing how
00:06:28.773
cleanly, portably, and readably device drivers can
00:06:31.791
be written in C.
00:06:33.515
This sort of clean, low-level, access is
00:06:36.632
one of the key strengths of C.
00:06:40.000
That explains the benefits, but what’s wrong
00:06:43.791
with Unix and C?
00:06:46.000
The Unix networking and filesystem APIs can
00:06:48.948
be performance bottlenecks. They emphasise saving buffer
00:06:51.796
space at the expense of additional system
00:06:54.644
calls in a way that made sense
00:06:57.491
when memory capacity was limited and system
00:07:00.339
calls were not so expensive, but that
00:07:03.187
doesn’t reflect modern hardware.
00:07:04.815
The Unix security model is designed to
00:07:07.762
arbitrate access by multiple users to a
00:07:10.610
single expensive machine, not to control access
00:07:13.458
to personal data owned by a single
00:07:16.306
user. It can effectively prevent other users
00:07:19.154
on my machine from seeing my personal
00:07:22.002
data, but can’t easily control access for
00:07:24.850
applications I choose to run.
00:07:26.884
And Unix has no portable APIs for
00:07:29.832
modern concerns, such as mobility, power management,
00:07:32.679
and so on.
00:07:34.000
The C programming language offers a powerful
00:07:37.248
and flexible abstraction for memory access,
00:07:39.946
in the form of pointers, but this
00:07:43.094
is perhaps too powerful. It’s too easy
00:07:46.242
to enable buffer overflows, use-after-free bugs,
00:07:48.941
or off-by-one errors, or to invoke undefined
00:07:52.089
behaviour, and cause a security vulnerability.
00:07:54.787
The weak type system offers flexibility,
00:07:57.585
but makes it difficult to reason about
00:08:00.734
the correctness of the system. Many bugs
00:08:03.882
happen because the type system is flexible
00:08:07.030
enough to allow a particular design,
00:08:09.728
but not powerful enough to check that
00:08:12.876
design for correctness.
00:08:14.225
And C and Unix have limited support
00:08:17.473
for concurrency. Modern versions of C did,
00:08:20.621
eventually, get a well-defined model for when
00:08:23.769
concurrent memory accesses are visible across threads,
00:08:26.917
along with a portable multi-threading library,
00:08:29.616
but these are not well supported.
00:08:32.314
And, perhaps more importantly, it’s not clear
00:08:35.562
that multi-threading is the right concurrency abstraction
00:08:38.710
going forward. It’s frequently forgotten today,
00:08:41.408
but multi-threading was an extremely controversial addition
00:08:44.556
to C and Unix. The suggested benefits,
00:08:47.704
providing lower-overhead concurrency, were real. Unfortunately,
00:08:50.403
so too were the concerns around race
00:08:53.551
conditions and correctness.
00:08:56.000
Overall, Unix has proven to be a
00:08:59.280
success. It has some limitations, especially around
00:09:02.460
high-performance I/O, power management, mobility, etc.,
00:09:05.185
but these can mostly be addressed with
00:09:08.365
extensions, retaining the older APIs for backwards
00:09:11.545
compatibility – and we’ll talk more about
00:09:14.724
how this is done later in the
00:09:17.904
course.
00:09:19.109
More concerning are the limitations of the
00:09:22.388
security model and package management systems.
00:09:25.114
The increasing use of containers, sandboxing,
00:09:27.839
and virtualisation to work around limitations and
00:09:31.019
improve security is a sign that the
00:09:34.199
underlying security and shared library mechanisms are
00:09:37.379
not meeting the current needs.
00:09:39.000
The C programming language is increasingly becoming
00:09:42.027
a liability. It offers a flexible approach
00:09:44.955
to memory management, and the pointer abstraction
00:09:47.882
is powerful, but it’s proven too difficult
00:09:50.809
to use correctly.
00:09:52.064
It’s too easy to introduce security vulnerabilities,
00:09:55.091
or to trip over undefined behaviour.
00:09:57.600
In practice, no-one seems able to consistently
00:10:00.527
write correct and secure C programs.
00:10:03.036
The answer here cannot be “better programmers”
00:10:05.964
– they don’t exist. Rather, we need
00:10:08.891
less error prone languages.
00:10:10.564
The paper, “Some were meant for C:
00:10:13.591
The endurance of an unmanageable language”,
00:10:16.100
by Stephen Kell, discusses some of the
00:10:19.027
issues with the C language in more
00:10:21.955
detail – both its strengths and weaknesses.
00:10:24.882
Please read it, and think about these
00:10:27.809
issues, before the discussion session.
00:10:31.000
That concludes our brief review of the
00:10:33.542
state of the art in systems programming,
00:10:35.984
and some of its limitations.
00:10:37.729
In the next part, we’ll talk more
00:10:40.271
about some of the changes in the
00:10:42.713
environment that are pushing us to reconsider
00:10:45.156
how we write systems programs.
Part 3: Challenges and Limitations
The 3rd part of the lecture discusses changes in the environment that
are affecting systems programs. It talks about the end of Moore's law
and the breakdown of Dennard scaling, and the implications of these
hardware trends on microprocessor architecture and concurrency. It
discusses the increasing need for secure systems due to the presence
of always-on connectivity and the Internet, and outlines some features
of C and Unix that are hard to secure. And it discusses the impact of
mobile, battery-powered, devices, and climate change, on need for
energy efficiency in systems programs.
00:00:00.000
What changes in the environment are affecting
00:00:04.064
systems programs?
00:00:04.940
I see four trends.
00:00:06.791
The first is the end of Moore’s
00:00:09.955
law and Dennard Scaling. This is forcing
00:00:13.019
changes in the way processors are designed,
00:00:16.083
forcing more concurrency and more specialisation.
00:00:18.709
Second is that increase in concurrency,
00:00:21.436
and its impact on the way we
00:00:24.500
design and build software.
00:00:26.251
Third is the increasing urgency to build
00:00:29.415
more secure systems, as the ubiquity of
00:00:32.479
the Internet, and ever-connected devices, exposes more
00:00:35.543
and more vulnerabilities.
00:00:36.857
Fourth, and finally, is the impact of
00:00:40.021
connectivity, and the changes to the way
00:00:43.085
we write applications caused by pervasive connectivity,
00:00:46.149
mobility, and cloud computing.
00:00:49.000
Moore’s law is the observation, made by
00:00:52.174
Gordon Moore, the co-founder of Intel,
00:00:54.809
in 1965, that the number of transistors
00:00:57.883
that can be integrated onto a single
00:01:00.957
device doubles roughly every two years,
00:01:03.591
due to improvements in manufacturing.
00:01:05.787
When plotted with time on the x-axis,
00:01:08.961
and number of transistors on a log
00:01:12.035
scale on the y-axis, such exponential growth
00:01:15.109
should appear as a straight line.
00:01:17.743
This is true for the original prediction,
00:01:20.917
shown in the graph on the left,
00:01:23.991
but isn’t very convincing. It was based
00:01:27.065
on four data points, capturing 1962-1965.
00:01:31.000
If we add modern data, though,
00:01:33.867
the pattern still holds up.
00:01:36.173
The orange points on this plot show
00:01:39.502
the number of transistors for processors released
00:01:42.731
between 1970 and 2018. They follow the
00:01:45.959
same straight-line relation, at least approximately.
00:01:48.727
The industry has, through heroic effort,
00:01:51.594
ensured that Moore’s law continues.
00:01:54.000
But, we’re rapidly approaching physical limits.
00:01:56.978
The feature size of the transistors that
00:02:00.436
form modern microprocessors is 10nm, with some
00:02:03.795
manufacturers introducing devices with 5nm features.
00:02:06.673
A 5nm feature is roughly 20 atoms
00:02:10.131
across.
00:02:11.361
Four more generators of Moore’s law would
00:02:14.819
result in features only one or two
00:02:18.177
atoms in size. There is a physical
00:02:21.535
limit in how small things can go.
00:02:24.893
Transistors will stop shrinking soon. Moore’s law
00:02:28.251
will come to an end.
00:02:31.000
An issue that’s had greater impact than
00:02:34.033
Moore’s law, although it’s perhaps less well
00:02:36.967
known, is the breakdown of Dennard scaling.
00:02:39.900
As we see in the equation on
00:02:42.933
the right, the power consumed by a
00:02:45.867
transistor is proportional to the capacitance of
00:02:48.800
the switching gate; the frequency at which
00:02:51.733
the transistor switches, that is, the clock
00:02:54.667
rate; and the square of the voltage
00:02:57.600
applied. Plus a constant term, known as
00:03:00.533
the leakage current. The voltage and capacitance,
00:03:03.467
in turn, directly relate to the size
00:03:06.400
of the transistor.
00:03:07.657
The Dennard scaling relation is the observation
00:03:10.690
that, as transistors shrink in size,
00:03:13.205
due to Moore’s law, their power consumption
00:03:16.138
will decrease, since this reduces the voltage
00:03:19.071
and capacitance.
00:03:19.910
This lets you build more power efficient
00:03:22.943
computers.
00:03:24.112
Alternatively, as the capacitance and voltage decrease,
00:03:27.145
you can increase the clock frequency to
00:03:30.079
match. This lets you build faster computers
00:03:33.012
that use roughly the same amount of
00:03:35.945
power.
00:03:37.114
From the home computers of the mid-1980s,
00:03:40.148
through to around 2005, this was the
00:03:43.081
story of the computer industry. Moore’s law
00:03:46.014
reduced the size of transistors and,
00:03:48.529
due to Dennard scaling, computers got faster.
00:03:51.462
We started with a clock rate of
00:03:54.495
around 1MHz, in the area of the
00:03:57.429
Commodore 64 and BBC Micro, and by
00:04:00.362
the end had with PCs with Intel
00:04:03.295
Pentium IV processors running at 3.8GHz.
00:04:05.810
Unfortunately, as we see in the green
00:04:08.843
points in this figure, the clock rate
00:04:11.776
stopped increasing at that point.
00:04:13.871
The reason is the leakage current,
00:04:16.486
that started to grow exponentially when the
00:04:19.419
feature size dropped below around 65nm.
00:04:21.933
Moore’s law still gave power savings,
00:04:24.448
as the size of transistors decreased,
00:04:26.962
but those savings had to be taken,
00:04:29.895
keeping the clock rate constant, to balance
00:04:32.829
the increased power consumption due to the
00:04:35.762
greater leakage current.
00:04:37.019
It simply became difficult to make processors
00:04:40.052
faster without them melting, due to the
00:04:42.986
heat from the excessive power consumption.
00:04:45.500
For this reason, while Moore’s law has
00:04:48.533
continued to give us more transistors since
00:04:51.467
the mid-2000s, clock rates have remained stalled.
00:04:54.000
Given that Moore’s law is continuing,
00:04:56.687
how do processor designers use those additional
00:04:59.705
transistors?
00:05:00.887
Mostly, as some combination of large caches,
00:05:04.005
and to support multicore operation.
00:05:06.161
Starting shortly after the breakdown of Dennard
00:05:09.279
scaling, when it became clear that processor
00:05:12.297
clock rates couldn’t increase anymore, because we’d
00:05:15.316
reached the limit in power consumption,
00:05:17.903
we see the number of processor cores
00:05:20.921
start to increase.
00:05:22.215
This is shown on the figure as
00:05:25.333
the black data points. What’s interesting is
00:05:28.351
that the increasing number of cores plots
00:05:31.369
as a straight line in this figure.
00:05:34.388
Given the log scale on the y-axis,
00:05:37.406
this implies the number of cores is
00:05:40.424
increasing exponentially.
00:05:41.287
A consequence of this, is that concurrent
00:05:44.405
programming is becoming more important. Multiple threads
00:05:47.423
of execution are needed to extract good
00:05:50.441
performance from modern processors.
00:05:52.166
Another consequence is that concurrency bugs more
00:05:55.284
becoming more visible. Race conditions, deadlocks,
00:05:57.872
and other problems with interactions between multiple
00:06:00.890
threads tend to manifest themselves more often
00:06:03.908
as the amount of parallelism increases.
00:06:06.495
Not only are we increasingly seeing the
00:06:09.613
need for concurrency, we’re also seeing how
00:06:12.632
difficult it is to write concurrent software.
00:06:16.000
In addition to the changes in computer
00:06:19.247
hardware, leading to increased concurrency, we’re also
00:06:22.394
seeing far more devices with always-on Internet
00:06:25.541
connectivity.
00:06:26.741
Unfortunately, with this rise in connectivity has
00:06:29.988
come increasing numbers of security vulnerabilities.
00:06:32.685
The graph shows the number of security
00:06:35.832
vulnerabilities reported per year, since the year
00:06:38.979
2000, colour-coded by severity.
00:06:40.778
What is clear is that the combination
00:06:44.025
of Unix and C, and the applications
00:06:47.172
that run on such systems, has not
00:06:50.319
proven easy to secure.
00:06:52.117
As we’ll see later, most of these
00:06:55.364
vulnerabilities are due to weak type systems
00:06:58.511
and a lack of memory safety in
00:07:01.658
modern systems. That is, many of these
00:07:04.805
vulnerabilities are avoidable, in principle, by changing
00:07:07.953
the way we implement software systems.
00:07:11.000
The final trend I want to highlight
00:07:13.917
is that of increasing mobility and connectivity.
00:07:16.735
Devices are mobile, always on, and always
00:07:19.652
connected to the network, and to remote
00:07:22.469
computing resources.
00:07:23.274
But they’re also deeply constrained. They’re constrained
00:07:26.191
by the limitations of battery power,
00:07:28.606
and the vagaries of the networks to
00:07:31.423
which they connect. It’s not clear that
00:07:34.241
we have the APIs, tools, and programming
00:07:37.058
models to make effective use of such
00:07:39.875
devices, or to adapt to the variation
00:07:42.693
in the environment.
00:07:45.000
This concludes the review of the challenges
00:07:47.699
and limitations facing systems programming.
00:07:49.555
I’m sure this is not complete,
00:07:51.883
and you can think of many more
00:07:54.482
issues.
00:07:55.603
What is clear, though, is that we’ve
00:07:58.302
seen a radical shift in computing hardware,
00:08:00.901
and the way in which computing devices
00:08:03.500
are used, over the last fifteen years.
00:08:06.100
This shift has not been accompanied by
00:08:08.699
a similar shift in the way we
00:08:11.298
design and implement operating systems and systems
00:08:13.897
software.
00:08:15.018
In the final part of this lecture,
00:08:17.717
we’ll discuss some possible ways operating systems
00:08:20.316
and programming languages could change, to start
00:08:22.915
to address these challenges.
Part 4: Next Steps in Systems Programming
The final part of the lecture discusses how systems programming might
evolve. It outlines how modern type systems, memory safe languages,
and techniques from functional programming, can help improve systems
programs. In particular, it focusses on how the can improve security,
support for concurrency, and system correctness. This sets the scene
for the discussion in the remainder of the course.
00:00:00.000
In the previous parts of this lecture,
00:00:03.474
I discussed the state of the art
00:00:05.948
in operating systems and systems programming languages,
00:00:08.422
and some of the challenges and changes
00:00:10.896
that such systems face.
00:00:12.310
In the following, I’ll consider how advances
00:00:14.884
in programming languages, and the use of
00:00:17.358
strong type systems and functional programming techniques,
00:00:19.832
can help to improve the expressivity,
00:00:21.952
correctness, and security of systems programs,
00:00:24.073
to begin to address some of these
00:00:26.547
challenges.
00:00:28.000
Advances in the theory of programming language
00:00:31.092
design have long provided tools that could
00:00:34.084
improve the way we write systems programs.
00:00:37.075
More recently, we’re finally starting to see
00:00:40.067
new programming languages being developed that incorporate
00:00:43.059
these ideas into practical languages and systems.
00:00:46.051
In particular, the ideas behind functional programming,
00:00:49.142
and modern type systems, can help to
00:00:52.134
improve memory management and memory safety,
00:00:54.699
while maintaining control over allocation and data
00:00:57.690
representation.
00:00:58.868
They can help to improve security by
00:01:01.960
eliminating certain classes of vulnerability. For example,
00:01:04.951
by preventing buffer overflows, or helping to
00:01:07.943
track untrusted data.
00:01:09.225
They can help to improve support for
00:01:12.317
concurrency by eliminating data races, by encouraging
00:01:15.309
immutability or tracking data ownership.
00:01:17.446
And, in general, the use of strong
00:01:20.538
type systems and functional approaches can help
00:01:23.529
eliminate entire classes of bug, by changing
00:01:26.521
the way we design programs using an
00:01:29.513
approach known as type-driven design.
00:01:32.000
To achieve these benefits, we’ll need to
00:01:34.981
apply functional programming techniques and modern type
00:01:37.862
systems to systems programming.
00:01:39.508
What do I mean by modern type
00:01:42.488
systems?
00:01:43.000
Well, a modern type system is one
00:01:45.790
that can provide useful guarantees about program
00:01:48.481
behaviour.
00:01:49.615
We’ve long had type systems that can
00:01:52.405
describe basic properties of data. For example,
00:01:55.096
languages that let us specify that a
00:01:57.786
particular value is an array that can
00:02:00.476
store integers, rather than floating point numbers,
00:02:03.166
are unsurprising.
00:02:03.935
A more interesting type system is one
00:02:06.725
that can enforce, at compile time,
00:02:09.031
that there are no out-of-bounds accesses to
00:02:11.722
that array. Or that there are no
00:02:14.412
use-after-free bugs in a program. Or no
00:02:17.102
memory leaks. No race conditions between threads.
00:02:19.793
No iterator invalidation. And so on.
00:02:22.099
Modern type systems let us reason about,
00:02:24.889
and prove, interesting properties of real systems
00:02:27.579
programs. They can help ensure concurrent code
00:02:30.269
is correct, and they can help avoid
00:02:32.960
particular classes of bugs and security vulnerabilities.
00:02:35.000
Modern type systems also help us to
00:02:37.765
model the problem space, and allow us
00:02:40.430
to detect inconsistencies and problems in our
00:02:43.095
solutions.
00:02:44.226
They support the concept of no cost
00:02:46.991
abstraction. That is, compile-time checks of system
00:02:49.656
properties that have no run-time cost.
00:02:51.941
This lets us describe, and check,
00:02:54.225
our design, and put constraints on possible
00:02:56.890
program misbehaviours, before running the code.
00:02:59.175
Modern type systems help us to describe
00:03:01.940
the problem space, and its constraints,
00:03:04.224
in a way that allows us to
00:03:06.889
check useful properties of the system as
00:03:09.555
we elaborate on our design and implementation.
00:03:12.220
Essentially they allow us to use the
00:03:14.985
compiler as a debugger for our designs.
00:03:18.000
As an example of this, think about
00:03:20.838
how we write networking code.
00:03:22.795
In the Berkeley Sockets API, used to
00:03:25.633
write network servers in C, one calls
00:03:28.372
the accept() function on a socket to
00:03:31.110
accept an incoming TCP connection. The accept()
00:03:33.848
function takes as its parameter an existing
00:03:36.587
socket that’s listening for connections, and returns
00:03:39.325
a new socket that represents the newly
00:03:42.064
accepted connection.
00:03:42.846
A common bug in networking code is
00:03:45.685
to mix up these two sockets.
00:03:48.032
For example, to attempt to send data
00:03:50.771
using the socket that’s listening for new
00:03:53.509
connections, rather than using a socket that
00:03:56.248
represents a previously accepted connection. This compiles,
00:03:58.986
but fails at runtime.
00:04:00.551
Such code compiles because sockets are identified
00:04:03.389
by simple integers in C. The read(),
00:04:06.128
write(), and accept() calls take file descriptors,
00:04:08.866
with type int, to identify the sockets.
00:04:11.605
As a result, the compiler can’t tell
00:04:14.343
the two types of socket, connected and
00:04:17.082
listening, apart.
00:04:17.864
A better API would use the type
00:04:20.703
system to distinguish the different types of
00:04:23.441
socket. There should be separate Connected Socket
00:04:26.180
and Listening Socket types. The accept() function
00:04:28.918
should take as its first parameter a
00:04:31.657
Listening Socket, and should return a Connected
00:04:34.395
Socket. And the read() and write() functions
00:04:37.134
should accept only Connected Sockets.
00:04:39.090
In systems where the networking library works
00:04:41.928
this way, a program that tries to
00:04:44.667
write() to a listening socket will fail
00:04:47.405
to compile, since the compiler can distinguish
00:04:50.144
the two types of socket, and detect
00:04:52.882
the mismatch.
00:04:53.665
Connected sockets and listening sockets can still
00:04:56.503
represented by integers, so the generated code
00:04:59.242
doesn’t change, and can be just as
00:05:01.980
efficient. But, by making them distinct types
00:05:04.718
of integer, the compiler could help us
00:05:07.457
find bugs.
00:05:08.239
This is a trivial example, but the
00:05:11.078
principle is important. The more information that
00:05:13.816
can be represented in the types,
00:05:16.164
the more the compiler can help check
00:05:18.902
that those types are used consistently.
00:05:21.249
This means we find bugs at compile
00:05:23.988
time, rather than having to debug the
00:05:26.726
code at runtime.
00:05:29.000
The other advance that can help systems
00:05:32.137
programming is the application of some ideas
00:05:35.174
from the functional programming community.
00:05:37.343
Functional programming is a programming style that
00:05:40.480
emphasises the avoidance of side effects and
00:05:43.517
shared state, and that promotes structuring code
00:05:46.554
as referentially transparent functions.
00:05:48.289
Imperative languages, and especially object-oriented languages,
00:05:50.992
tend to actively encourage the opposite approach,
00:05:54.029
leading to programs full of shared state,
00:05:57.066
side effects, and impure functions.
00:05:59.235
The entire point of objects, for example,
00:06:02.372
is to hold shared state. And methods
00:06:05.409
in object oriented languages are generally called
00:06:08.446
in order to invoke the side effect
00:06:11.483
of modifying the object’s state.
00:06:13.652
I argue that this style of programming
00:06:16.789
is problematic for systems programs, and it’s
00:06:19.826
much better to avoid shared state and
00:06:22.863
program in a functional style where possible.
00:06:26.000
Why?
00:06:27.136
Because, side effects and shared state make
00:06:29.940
code difficult to reason about, and harder
00:06:32.644
to debug. They increase coupling between seemingly
00:06:35.348
unrelated sections of code, and make it
00:06:38.052
difficult to understand the behaviour of parts
00:06:40.755
of a program in isolation.
00:06:42.687
And, importantly, the presence of side effects
00:06:45.490
and shared state makes concurrent code much
00:06:48.194
harder to write.
00:06:49.353
Concurrent accesses to shared state have to
00:06:52.157
be protected by locks, to avoid race
00:06:54.861
conditions. Experience has shown that it’s difficult
00:06:57.565
to get the locking right, in any
00:07:00.268
non-trivial program. By systematically writing code in
00:07:02.972
a functional style, where possible, and avoiding
00:07:05.676
shared state, the number of locks that
00:07:08.380
are needed can be greatly reduced.
00:07:10.697
It’s easier to get the locking right,
00:07:13.401
and avoid race conditions, if there are
00:07:16.105
fewer things to lock.
00:07:18.000
The essential benefit of writing programs in
00:07:20.838
a pure functional style is that it
00:07:23.577
constrains what’s possible.
00:07:24.751
Code written using referentially transparent functions,
00:07:27.198
without side effects, is easier to test
00:07:29.936
and debug - because there’s no hidden
00:07:32.675
state.
00:07:33.816
Such code is also thread safe,
00:07:36.263
since there are no side effects,
00:07:38.610
and no mutable state to share between
00:07:41.349
threads.
00:07:42.490
By constraining the way programs are written,
00:07:45.329
certain classes of bugs and certain problems
00:07:48.067
can be avoided. Avoiding these problems is
00:07:50.805
desirable, if we want to write concurrent,
00:07:53.544
secure, and correct systems programs.
00:07:54.000
Accordingly, when possible, I encourage you to
00:07:56.832
adopt a functional style when writing code.
00:07:59.564
Note, though, that I’m not saying “write
00:08:02.396
your programs in Haskell”.
00:08:03.957
Haskell is a great language, if your
00:08:06.789
goal is to develop the concept of
00:08:09.521
pure functional programming. It takes this one
00:08:12.253
idea, and pushes it as far as
00:08:14.985
is possible.
00:08:15.765
That’s wonderful, if you’re a programming language
00:08:18.597
researcher. And many of the ideas that
00:08:21.329
came out of Haskell are widely applicable
00:08:24.061
to programming in general. But, it’s clear
00:08:26.793
that not everything can be naturally expressed
00:08:29.525
in the functional style. Some programs are
00:08:32.257
just easier to write in an imperative,
00:08:34.989
or object oriented, way.
00:08:36.550
That’s fine.
00:08:37.431
Use functional programming ideas where they make
00:08:40.263
sense, to prevent certain classes of bugs.
00:08:42.994
If another approach works better, do that.
00:08:45.726
But, always think about how to reduce
00:08:48.458
shared state, and reduce side effects.
00:08:52.000
We can use functional programming techniques and
00:08:54.854
modern type systems to help improve memory
00:08:57.608
management, and make it safer and less
00:09:00.361
error prone.
00:09:01.148
Systems programmers are used to manual memory
00:09:04.002
management, as implemented in C. Memory is
00:09:06.756
allocated by calling the malloc() function,
00:09:09.116
that returns a pointer to the newly
00:09:11.870
allocated memory. When that memory is no
00:09:14.624
longer used, the programmer calls the free()
00:09:17.378
function with that pointer, to release the
00:09:20.131
memory.
00:09:21.275
The system performs few checks on what
00:09:24.128
is done with the memory between those
00:09:26.882
calls.
00:09:28.026
For example, the value returned by malloc()
00:09:30.879
can be cast to a pointer to
00:09:33.633
any type of object, whether or not
00:09:36.387
the size of that object matches the
00:09:39.141
size of the allocated memory, and the
00:09:41.895
program can interpret the same region of
00:09:44.648
memory as different types of object simultaneously,
00:09:47.402
whether or not that makes sense.
00:09:49.763
Similarly, the behaviour of a program that
00:09:52.616
accesses memory outside the region returned by
00:09:55.370
malloc() is undefined, but the system make
00:09:58.124
no attempt to prevent such accesses.
00:10:00.484
Finally, arrays and strings are represented as
00:10:03.338
pointers to their first element, and don’t
00:10:06.092
store the length of the array,
00:10:08.452
and array indexing is represented by pointer
00:10:11.206
arithmetic. The code fragment on the slide
00:10:13.960
illustrates this – compile and run this
00:10:16.714
code, to see if it behaves as
00:10:19.467
you expect. An important consequence of this
00:10:22.221
definition of arrays is that the runtime
00:10:24.975
cannot check whether array accesses are within
00:10:27.729
bounds, since the bounds aren’t recorded.
00:10:30.089
There were good reasons for this behaviour
00:10:33.043
when C was designed. Machines were slow,
00:10:35.797
and memory was limited. Storing the size
00:10:38.551
of the allocation along with the allocated
00:10:41.304
object takes memory, and memory was in
00:10:44.058
short supply on the machines where C
00:10:46.812
was developed. Similarly, checking array bounds takes
00:10:49.566
time and requires additional code.
00:10:51.533
These concerns mattered at the time,
00:10:53.993
but are not necessarily valid today.
00:10:56.354
On machines with kilobytes of memory and
00:10:59.107
MHz clocks, the memory and runtime overheads
00:11:01.861
mattered. On modern machines, that have millions
00:11:04.615
of times more memory, and processors that
00:11:07.369
are tens-of-thousands of times faster, the overheads
00:11:10.122
are less significant. Compiler technology and program
00:11:12.876
optimisations have also improved, to the extent
00:11:15.630
that compilers can often now determine when
00:11:18.384
bounds checks are unnecessary, and remove them
00:11:21.138
from the generated code.
00:11:22.711
We can certainly afford to check array
00:11:25.565
bounds and other memory accesses on modern
00:11:28.319
systems – but we need to move
00:11:31.073
away from C as a systems programming
00:11:33.826
language to do so.
00:11:35.000
What is now clear is that manual
00:11:37.623
memory management is a source of bugs.
00:11:40.146
It leads to use-after-free bugs, where memory
00:11:42.768
is accessed after it has been deallocated.
00:11:45.291
It leads to memory leaks, where memory
00:11:47.914
is never deallocated, even after it has
00:11:50.437
ceased to be referenced by the program.
00:11:52.959
It leads to buffer overflows, where the
00:11:55.582
program tries to access memory outside the
00:11:58.105
region it has allocated.
00:11:59.547
And it leads to iterator invalidation,
00:12:01.809
where the contents of a list can
00:12:04.332
change while a program is accessing it,
00:12:06.854
making a previously valid pointer invalid,
00:12:09.017
and leading to out of bounds memory
00:12:11.540
access.
00:12:12.000
All of these problems can be eliminated
00:12:14.858
when using programming languages with modern type
00:12:17.715
systems.
00:12:18.873
Managed languages such as Java, running on
00:12:21.831
virtual machines, have been able to trap
00:12:24.688
such events at run time for many
00:12:27.546
years. They take a performance hit to
00:12:30.403
do so, compared to systems languages that
00:12:33.261
compile to native code, but gain safety.
00:12:36.118
Recently, though, we’ve seen new programming languages
00:12:39.076
start to emerge that can track and
00:12:41.934
detect these problems at compile time.
00:12:44.383
The Rust programming language, for example,
00:12:46.832
will detect iterator invalidation and use-after-free bugs,
00:12:49.690
and throw a compile time error if
00:12:52.547
they occur in a program. The Idris
00:12:55.405
programming language can do the same with
00:12:58.262
buffer overflows.
00:12:59.079
As programmers, we fix the same types
00:13:02.036
of problem, again and again, in heroic
00:13:04.894
debugging sessions. Alternatively, we should consider if
00:13:07.751
there’s a better solution, that can prevent
00:13:10.609
such classes of bug entirely.
00:13:13.000
The concern about memory management becomes critical
00:13:16.467
when we look at causes of security
00:13:19.833
vulnerabilities.
00:13:21.064
The figure on the slide is from
00:13:24.531
Microsoft. It tracks the root cause of
00:13:27.898
bugs found in software developed by Microsoft,
00:13:31.264
Windows, Office, Teams, etc., for the period
00:13:34.631
from 2006 to 2018.
00:13:36.555
What it shows is that around 70%
00:13:40.021
of all reported security vulnerabilities in Microsoft
00:13:43.388
software, for that time period, relate to
00:13:46.755
memory safety.
00:13:47.717
70% of reported security vulnerabilities are buffer
00:13:51.183
overflows, use-after-free bugs, memory corruption, iterator invalidation,
00:13:54.550
and so on.
00:13:55.993
By switching to a memory safe language,
00:13:59.460
Microsoft could eliminate two-thirds of the security
00:14:02.826
vulnerabilities in their systems.
00:14:04.000
Now, obviously this is difficult to do.
00:14:07.115
Rewriting Windows and Office would be an
00:14:10.131
enormous job, and would almost certainly introduce
00:14:13.146
more bugs than it would fix.
00:14:15.731
But surely there’s a lesson that can
00:14:18.846
be learnt for new code?
00:14:21.000
Once we’ve started to address the low-handing
00:14:23.630
fruit around memory safety, we should start
00:14:26.160
to think about the root causes of
00:14:28.691
other security vulnerabilities.
00:14:29.775
What is common, is that vulnerabilities are
00:14:32.405
the result of mismatched assumptions. One part
00:14:34.935
of the code assumes some property is
00:14:37.466
true of data it processes. Other code
00:14:39.996
is supposed to validate that the property
00:14:42.526
holds, but a check is missed somewhere.
00:14:45.056
Such problems are easy to miss,
00:14:47.325
because the assumptions and constraints tend to
00:14:49.855
be visible only in the comments and
00:14:52.385
documentation around the code, and so can’t
00:14:54.916
be automatically checked.
00:14:56.000
One approach to reducing security vulnerabilities is
00:14:59.068
to encode as many of these assumptions,
00:15:02.035
and as much of this knowledge as
00:15:05.003
possible, into the types. Once this knowledge
00:15:07.970
is available in a machine readable form,
00:15:10.938
a compiler can check the code for
00:15:13.905
consistency, and highlight problems before they become
00:15:16.873
security vulnerabilities.
00:15:17.721
Doing so is more up-front work.
00:15:20.364
It’s more effort to write code in
00:15:23.432
a way that records, and can automatically
00:15:26.399
check your assumptions, using the type system.
00:15:29.367
But, over time, that effort pays off
00:15:32.334
through having fewer surprises when the code
00:15:35.302
is used. You trade-off design time,
00:15:37.845
and compile time, effort, for less time
00:15:40.813
spent debugging the system, and correcting security
00:15:43.780
vulnerabilities, after it’s been deployed.
00:15:47.000
We see similar classes of behaviour around
00:15:49.744
concurrency.
00:15:50.872
The common abstraction is that of threads,
00:15:53.617
locks, and shared mutable state. This is
00:15:56.261
pthreads if you’re a C programmer,
00:15:58.528
or synchronised methods and objects if you’re
00:16:01.172
a Java programmer.
00:16:02.306
Before accessing a shared resource, you must
00:16:05.050
acquire the appropriate lock. Then, once you’ve
00:16:07.694
finished the resource, release the lock.
00:16:09.961
And, if you access multiple resources at
00:16:12.606
once, you must remember to acquire and
00:16:15.250
release those locks in the right order
00:16:17.894
to avoid deadlock.
00:16:19.028
The problem is, as anyone who’s tried
00:16:21.772
this at any scale knows, that it’s
00:16:24.417
easy to get the locking wrong.
00:16:26.683
To hold too many, or too few,
00:16:29.328
locks. Or to acquire or release the
00:16:31.972
locks at the wrong time, or in
00:16:34.617
the wrong order.
00:16:35.000
More critically, it’s clear that locks don’t
00:16:37.768
compose. If you start with two libraries
00:16:40.436
that use locking correctly, and combine them,
00:16:43.104
then the result might not have the
00:16:45.771
correct amount of locking. The locking requirements
00:16:48.439
to protect the combined data might differ
00:16:51.107
to those of the individual components.
00:16:53.394
The usual example here is a banking
00:16:56.162
program. Such a program probably has a
00:16:58.830
bank account object, recording the balance of
00:17:01.497
an account, and allowing money to be
00:17:04.165
deposited or withdrawn. Such an object needs
00:17:06.833
locking, to avoid corruption of the balance
00:17:09.501
if deposits and withdrawals are made simultaneously.
00:17:12.169
What’s less clear is that if you
00:17:14.937
transfer money from one bank account to
00:17:17.605
another, it’s not sufficient for each account
00:17:20.272
to be correctly locked. If the money
00:17:22.940
is to be transferred atomically, so it
00:17:25.608
either appears in one account or the
00:17:28.276
other, then locking the individual accounts is
00:17:30.944
not sufficient. There’s a risk the system
00:17:33.612
is observed during the transfer, and the
00:17:36.280
money either appears in neither account,
00:17:38.566
or in both, depending on how the
00:17:41.234
transfer was implemented. Additional locking is needed
00:17:43.902
to prevent this, over and above the
00:17:46.570
composition of the locking needed on the
00:17:49.238
individual accounts.
00:17:50.000
There are two approaches to avoid problems
00:17:52.739
with locking and concurrency.
00:17:54.247
One is to apply the techniques of
00:17:56.986
functional programming. Race conditions don’t occur if
00:17:59.625
only immutable data is shared, and if
00:18:02.263
the program is structured to avoid shared
00:18:04.902
state and side effects.
00:18:06.410
The other is to avoid races by
00:18:09.149
tracking ownership of data. Structure programs so
00:18:11.788
that the transfer of data is atomic,
00:18:14.427
and that every object has a single
00:18:17.066
owner at all times, and so that
00:18:19.705
data is either mutable, or visible to
00:18:22.344
others, but never both.
00:18:23.852
There are trade-offs in the two approaches,
00:18:26.590
that we’ll discuss in detail later in
00:18:29.229
the course. What’s interesting, though, is that
00:18:31.868
both eliminate entire classes of concurrency-related bugs,
00:18:34.507
and can be checked for correctness by
00:18:37.146
a compiler.
00:18:39.000
This is the key point I want
00:18:41.895
to get across. There are many types
00:18:44.690
of problem that frequently occur in systems
00:18:47.486
programs because those systems programs are written
00:18:50.281
in C.
00:18:51.079
Problems such as use-after-free bugs, memory leaks,
00:18:53.975
buffer overflows, iterator invalidation, and data races
00:18:56.770
in multi-threaded code, are common in C
00:18:59.565
programs.
00:19:00.714
Many are also common in programs written
00:19:03.610
in higher-level languages.
00:19:04.808
But, there are new and emerging languages
00:19:07.703
and tools that can not just fix
00:19:10.498
these bugs, but that can eliminate these
00:19:13.293
classes of bug, while maintaining the control
00:19:16.088
and efficiency needed for systems programming.
00:19:18.484
There exist programming languages that can flag,
00:19:21.379
at compile time, whether a given piece
00:19:24.175
of code suffers from data races,
00:19:26.571
iterator invalidation, buffer overflows, use after free
00:19:29.366
bugs, and so on.
00:19:30.963
It’s not always easy to switch to
00:19:33.858
these languages, but where possible, maybe we
00:19:36.653
should consider to do so?
00:19:39.000
In addition, modern type systems are flexible
00:19:41.890
enough to effectively model the problem space
00:19:44.680
in many case. This allows us to
00:19:47.470
use the types to check our designs
00:19:50.260
for consistency.
00:19:51.057
When writing code, define types representing the
00:19:53.947
problem domain.
00:19:54.744
For example, rather than using an int,
00:19:57.634
define a PersonID type, an Age,
00:20:00.025
or a Temperature, or similar. With modern
00:20:02.815
languages, these compile down to the exact
00:20:05.605
same code as would using the generic
00:20:08.395
primitive type, but with the advantage that
00:20:11.185
the compiler can check that you’re passing
00:20:13.975
the right type of data around.
00:20:16.367
Similarly, encode constraints on when an object
00:20:19.256
can be used as part of its
00:20:22.046
type. The example here is the ListeningSocket
00:20:24.836
vs ConnectedSocket distinction in networking code we
00:20:27.626
discussed earlier, but the technique is generally
00:20:30.416
applicable.
00:20:31.565
Modern systems programming languages let you define
00:20:34.455
new types and abstractions easily, and without
00:20:37.245
run-time cost. Use this ability: define the
00:20:40.035
types and function signatures first, and let
00:20:42.825
the compiler check your design for consistency,
00:20:45.615
and only then write the body of
00:20:48.405
the functions.
00:20:49.202
We’re used to debugging code at runtime.
00:20:52.092
What I’m suggesting is that if you
00:20:54.882
can represent the key features of the
00:20:57.672
problem domain in your program’s types,
00:21:00.063
then the compiler can also help debug
00:21:02.853
your design.
00:21:04.000
What seems clear is that systems programs,
00:21:07.250
and much software in general, have reached
00:21:10.400
the stage where people can’t manage the
00:21:13.550
complexity.
00:21:14.000
The C programming language gives precise control
00:21:17.651
over data representation, memory management, and sharing
00:21:21.202
of state. But exercising that control has
00:21:24.754
proven too difficult.
00:21:26.276
Bugs and security vulnerabilities abound in software,
00:21:29.927
and pervasive connectivity and concurrency are making
00:21:33.478
the problem worse.
00:21:35.000
Emerging, strongly-typed, languages and systems give the
00:21:37.770
same degree of control, with added safety.
00:21:40.440
They allow us to use the type
00:21:43.110
system to eliminate certain classes of common
00:21:45.780
bugs, and to model the problem space
00:21:48.450
via the types in a way that
00:21:51.120
helps us detect logic errors early.
00:21:53.409
We can improve systems programming. In the
00:21:56.179
remainder of this course, we’ll explore how,
00:21:58.849
using the Rust programming language as an
00:22:01.519
example.
00:22:03.000
So, to summarise.
00:22:04.388
Systems programming is infrastructure programming. It’s programming
00:22:07.493
where efficiency and control over data representation
00:22:10.498
matters, and where concurrency and security are
00:22:13.503
pervasive challenges.
00:22:14.362
The state of the art in deployed
00:22:17.467
systems is Unix and C. Old,
00:22:20.042
but surprisingly flexible, systems, that are reaching
00:22:23.047
the end of their life.
00:22:25.194
To address the challenge of writing secure
00:22:28.299
and highly concurrent code, we need better
00:22:31.304
tools and techniques to help tame the
00:22:34.309
complexity, and help us debug our designs.
00:22:37.314
I believe we’re now starting to see
00:22:40.319
such tools being developed. I’ll introduce these
00:22:43.324
in the remainder of this course.
Discussion
The lecture highlighted two papers: “Programming language challenges in
systems codes: why systems programmers still use C, and what to do about
it”, by Jonathan Shapiro, and “Some were meant for C: The endurance of
an unmanageable language”, by Stephen Kell.
These papers explore what is a systems programming language, and discuss
some common features of systems languages. In particular that highlight
that performance is critical, but also that systems languages tend to
offer low-level control of data representation, memory management, and
I/O, and that programmers make use of this control. They suggest that
systems languages that attempt to trade performance for safety, or that
rely on compiler optimisations to fix data layout, will not be accepted
by the broader community. Do you agree?
The lecture suggests that the combination of functional programming and
modern programming languages with improved type systems can help address
some of the challenges in systems programming. The functional style of
programming, with referentially transparent functions, no side effects,
and no shared mutable state, makes code easier to test and reduces the
risks of concurrency. And modern type systems can help to prevent some
types of unsafe behaviour, and allow us to better model the problem
space.
This suggests that we should change our approach to writing software, and
focus on writing code in a functional style, using the types to model and
debugging the design, rather than debugging the code itself. Do you
agree?