ENGINEERING STORY 008

The Safest Language
Might Be Two Languages

Architecture first. SPARK where proof matters. Rust where memory-safe systems engineering matters.

SPARK safety core within a Rust systems layer, separated by an engineered assurance boundary.

Software engineering has a habit of turning programming languages into religions.

C versus C++. Ada versus C. Rust versus almost everything.

For safety-critical engineering, I think that is the wrong argument.

If I were starting the design of a new safety-critical system today, two technologies would be very high on my list: Ada/SPARK and Rust.

Not because one should replace the other.

Because I would seriously consider using both.

But there is an important condition.

The architecture must come first.

Otherwise, we haven't created a safer system. We may simply have created multilingual spaghetti.

Don't choose the language first.

The first decision should never be:

What language are we going to use?

It should be:

What can this system do that could cause harm?

From there come the hazards, safety requirements, architecture, integrity levels and, ultimately, the evidence required to demonstrate that the system is acceptably safe.

Only then should we choose the technologies used to implement it.

Different parts of the system may require very different kinds of evidence.

That is where the combination of SPARK and Rust becomes interesting.

Decide where the safety decisions live.

Before anybody writes code, I want to know something much more fundamental.

Which parts of this system are actually responsible for keeping it safe?

Those responsibilities should be identified through hazard analysis and deliberately allocated within the architecture.

Imagine a relatively small safety core.

It receives defined information about the outside world. It evaluates that information against defined rules. It permits, inhibits or commands particular actions.

Its interfaces are small. Its behaviour is tightly specified. Its assumptions are explicit.

And, critically, other parts of the system cannot quietly bypass it.

That is where SPARK starts to become extremely attractive.

Where SPARK earns its place.

Ada already has a long history in high-integrity systems. SPARK takes that further by allowing important properties of software to be expressed as contracts and subjected to formal verification.

Testing tells us what happened for the cases we tested.

Formal verification can provide evidence that specified properties hold across all cases covered by the proof.

For sufficiently critical logic, that distinction matters enormously.

If a piece of software decides whether an actuator may move, whether a train may proceed, whether aircraft separation conditions are satisfied, or whether some other potentially hazardous action is permitted, I may want considerably more evidence than a large collection of successful tests.

I may want mathematical evidence about its behaviour.

But that does not necessarily mean the entire surrounding system should be engineered in the same way.

Then there is Rust.

A substantial amount of software has to exist around that safety core.

Something has to communicate with equipment. Something has to handle data. Something has to manage concurrent processes, interfaces, communications, logging and infrastructure.

Historically, systems software has been particularly vulnerable to memory corruption, invalid pointers, use-after-free errors, data races and similar failures.

Rust attacks many of those problems through the language and compiler.

That makes it extremely attractive for the surrounding systems environment.

Safety-qualified Rust toolchains now exist for particular standards, targets and development environments, making Rust a credible candidate for safety-related software where the applicable assurance regime permits it.

This does not mean Rust provides something SPARK fundamentally cannot. SPARK itself provides powerful protections against many classes of programming error.

The question is architectural and economic as well as technical.

Where does formal proof provide sufficient additional assurance to justify its cost, and where does a modern memory-safe systems language provide the appropriate engineering solution?

Perhaps Rust should not replace SPARK.

Perhaps they are solving different parts of the problem.

Two languages can make things worse.

There is an obvious objection.

If one language creates complexity, surely two languages can create even more.

Absolutely.

Allow Rust code to call SPARK functions throughout the system, allow SPARK to call back into arbitrary Rust components, add shared structures and undocumented dependencies, and we haven't improved anything.

We've built spaghetti in two languages.

So the boundary matters enormously.

I would want it to look less like two software libraries calling each other and more like an engineered interface between two pieces of equipment.

Inputs → Safety Core → Outputs

Information crossing that boundary should have defined meaning, units, ranges, timing requirements, assumptions and failure behaviour.

The interface should be deliberately small.

And crossing it should never be casual.

The boundary is part of the safety case.

It is not enough to put the most obviously critical algorithms into SPARK and declare the remainder to be supporting software.

Hazard analysis must determine where each safety responsibility belongs.

Every assumption made by the safety core about information supplied by the Rust environment must therefore be explicit, justified and, where necessary, independently checked.

The interface between Rust and SPARK is consequently not just an API.

It is an assurance boundary.

Changes to it may change requirements, proofs, tests and, ultimately, the safety argument itself.

That includes the less comfortable parts of systems programming.

Hardware interfaces, foreign-function interfaces, external libraries and any necessary use of unsafe Rust cannot simply inherit the guarantees normally associated with safe Rust.

Their assumptions and behaviour have to be included explicitly within the assurance argument.

The architecture creates the validation structure.

Something rather useful happens when we do this properly.

By defining the architecture before implementation, we have already started defining how the system will be verified and validated.

For every important interface, we can ask:

What is the input?
What assumptions are being made about it?
What behaviour is required?
What output should result?
What happens if the input is missing, late, corrupt or contradictory?

Those questions create requirements.

The requirements create contracts.

The contracts support formal proof.

The interfaces create integration tests.

The failure behaviour creates fault-injection tests.

And the architecture gives us somewhere to attach the evidence.

A good safety architecture does much more than organise the software.

It creates the structure by which the software can be proved, tested and ultimately trusted.

That may be more valuable than the choice of programming language itself.

Find the errors before they become software errors.

Suppose one component believes a value is expressed in metres while another assumes feet.

Or one component considers data valid for five seconds while another assumes fifty milliseconds.

Or two components each believe the other is responsible for detecting an invalid state.

Those aren't really programming errors. They are architectural errors.

And no memory-safe language will necessarily save us from them.

By forcing assumptions, ownership and interfaces to be explicit before implementation begins, we have an opportunity to discover an entire class of problems before anybody writes the software that would eventually expose them.

Finding an error on an architecture diagram is rather cheaper than finding it during system integration.

Finding it there is considerably cheaper than finding it operationally.

What happens when the surrounding system disappears?

There is another question I would ask of this architecture.

What happens if the Rust environment disappears completely?

That doesn't necessarily mean the system must continue operating normally.

Availability and safety are not the same thing.

The system may degrade. It may lose capability. It may stop altogether.

But loss of supporting software should not silently remove the protection provided by the safety architecture.

Where practicable, the safety core should retain sufficient authority to prevent an unsafe action even when the environment around it has failed.

That is a much stronger architectural property than simply saying that both languages are considered suitable for safety-related development.

Memory safety is still not system safety.

Rust can prevent whole classes of memory errors.

That doesn't make a system written in Rust safe.

A perfectly memory-safe program can implement the wrong requirement. It can misunderstand a sensor. It can use an incorrect algorithm.

It can make precisely the wrong decision without corrupting a single byte of memory.

Memory safety is enormously valuable. But memory safety is not system safety.

The same warning applies to formal methods.

SPARK can help demonstrate that software satisfies specified properties.

It cannot magically prove that we specified the right properties.

A perfectly implemented bad requirement remains a bad requirement.

That problem belongs further up the engineering lifecycle.

Which brings us back to architecture.

Perhaps the question:

Which is the safest programming language?

was wrong from the beginning.

The better question is:

What evidence do we need to demonstrate that this system is acceptably safe, and which engineering technology gives us the strongest evidence for each part of it?

Ask the question that way, and Ada/SPARK and Rust stop being competitors.

SPARK becomes a compelling candidate for deliberately constrained parts of the system where formal evidence has particular value.

Rust becomes a compelling candidate for the memory-safe systems environment surrounding them.

And the architecture defines the boundary between the two.

More importantly, that boundary creates requirements, contracts, tests and evidence before implementation has even begun.

Architect the safety first.

Use SPARK where you need proof.
Use Rust where you need memory-safe systems software.
Make the boundaries explicit, small and testable.

Then make every technology justify its place against the safety argument.

Because ultimately, the safest programming language isn't SPARK.

It isn't Rust.

It isn't even both.

The safest system is the one whose architecture allows us to understand what must be true, demonstrate that it is true, and know what happens when it isn't.

What do you think?

Is combining formal verification with memory-safe systems programming a useful direction for new safety-critical architectures? Or would you draw the boundary somewhere else?

Loading comments...

Join the discussion

Comments are moderated before publication.

Only used if we need to reply. It will never be published.

Share your thoughts about this story.