Picture of Yee Wei Law

Safe programming languages

by Yee Wei Law - Saturday, 24 May 2025, 2:46 PM
 

A safe programming language is one that is memory-safe (see Definition 1), type-safe (see Definition 2) and thread-safe (see Definition 3).

Definition 1: Memory safety [WWK+21]

Assurance that adversaries cannot read or write to memory locations other than those intended by the programmer.

A significant percentage of software vulnerabilities have been attributed to memory safety issues [NSA22], hence memory safety is of critical importance.

Examples of violations of memory safety can be found in the discussion of common weakness CWE-787 and CWE-125.

Definition 2: Type safety [Fru07, Sec. 1.1]

Type safety is a formal guarantee that the execution of any program is free of type errors, which are undesirable program behaviours resulting from attempts to perform on some value an operation that is inappropriate to the type of the value.

For example, applying a factorial function to any value that is not an integer should result in a type error.

Type safety ⇒ memory safety, but the converse is not true [SM07, Sec. 6.5.2], hence type safety is commonly considered to be a central theme in language-based security [Fru07].

Type-safe programming languages, e.g., Java , Ruby, C#, Go, Kotlin, Swift and Rust, have been around for a while. However, memory-unsafe languages are still being used because:

  • Type-safety features come at the expense of performance. There is for example overhead associated with checking the bounds on every array access [NSA22].

    Even the current speed champion, Rust, among the type-safe languages, and the only type-safe language that has made it to the Linux kernel [VN22] and Windows kernel [Thu23], is not efficient enough for all use cases [Iva22]. This is one of the reasons why Google is still trying to create a successor to C++ called Carbon.

  • Type-safety features also come at the expense of resource requirements. Most memory-safe languages use garbage collection for memory management [NSA22], and this translates to higher memory usage.
  • Although most type-safe languages are supported on the mainstream computing platforms (e.g., Wintel), the same cannot be said of embedded platforms.

    It can be challenging to program a resource-constrained platform using a type-safe language.

  • There is already a vast amount of legacy code in C/C++ and other memory-unsafe languages.

    The cost to port legacy code, including the cost of training programmers, is often prohibitive.

    Depending on the language, interfacing memory-safe code with unsafe legacy code can be cumbersome.

  • Besides invoking unsafe code, it is all too easy to do unsafe things with a type-safe language, e.g., not checking user input, not implementing access control.

    Programmers often use this as an excuse to not use a different language than what they are familiar with.

Nevertheless, adoption of type-safe languages, especially Rust, has been on the rise [Cla23].

Thread safety rounds up the desirable properties of type-safe languages.

Definition 3: Thread safety [Ora19, Ch. 7]

The avoidance of data races, which occur when data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data.

Watch the following LinkedIn Learning video about thread safety:

Thread safety from IoT Foundations: Operating Systems Fundamentals by Ryan Hu

Rust is an example of a type-safe language that is also thread-safe.

References

[Cla23] T. Claburn, Memory safety is the new black, fashionable and fit for any occasion: Calls to avoid C/C++ and embrace Rust grow louder, The Register, January 2023. Available at https://www.theregister.com/2023/01/26/memory_safety_mainstream/.
[Fru07] N. G. Fruja, Type safety of C# and .Net CLR, Ph.D. thesis, ETH Zürich, 2007. https://doi.org/10.3929/ethz-a-005357653.
[Iva22] N. Ivanov, Is Rust C++-fast? Benchmarking System Languages on Everyday Routines, arXiv preprint arXiv:2209.09127, 2022. https://doi.org/10.48550/ARXIV.2209.09127.
[NSA22] NSA, Software memory safety, Cybersecurity Information Sheet, November 2022. Available at https://media.defense.gov/2022/Nov/10/2003112742/-1/-1/0/CSI_SOFTWARE_MEMORY_SAFETY.PDF.
[Ora19] Oracle, Multithreaded programming guide, Part No: E54803, March 2019. Available at https://docs.oracle.com/cd/E53394_01/pdf/E54803.pdf.
[SM07] S. Smith and J. Marchesini, The Craft of System Security, Addison-Wesley Professional, 2007. Available at https://learning.oreilly.com/library/view/the-craft-of/9780321434838/.
[Thu23] P. Thurrott, First Rust Code Shows Up in the Windows 11 Kernel, blog post, May 2023. Available at https://www.thurrott.com/windows/windows-11/282995/first-rust-code-shows-up-in-the-windows-11-kernel.
[VN22] S. Vaughan-Nichols, Linus Torvalds: Rust will go into Linux 6.1, ZDNET, September 2022. Available at https://www.zdnet.com/article/linus-torvalds-rust-will-go-into-linux-6-1/.
[WWK+21] D. Wagner, N. Weaver, P. Kao, F. Shakir, A. Law, and N. Ngai, Computer security, online textbook for CS 161 Computer Security at UC Berkeley, 2021. Available at https://textbook.cs161.org/.

» Networking and cybersecurity (including cryptology)