ArticleslgStudy

computer science

Memory safety

Memory safety is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Memory safety rather than just read about it. In short: Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers. For example, Java is memory-safe because its runtime error detection checks array bounds and pointer dereferences.

Key takeaways

  • Memory safety belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Memory safety to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Memory safety from memory before moving on to harder problems.

Reference excerpt

Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers. For example, Java is memory-safe because its runtime error detection checks array bounds and pointer dereferences. By contrast, programming languages like C, C++, and Fortran allow arbitrary pointer arithmetic with pointers implemented as direct memory addresses with no provision for bounds checking, making them memory-unsafe. Memory-unsafe code is typically found in low level programming, with higher level programming languages generally incorporating either garbage collection or static analysis (as in Rust) to prevent such errors.

History Memory errors were first considered in the context of resource management and time-sharing systems, in an effort to avoid problems such as fork bombs. Developments were mostly theoretical until the Morris worm, which exploited a buffer overflow in fingerd. The field of computer security developed quickly thereafter, escalating with multitudes of new attacks such as the return-to-libc attack and defense techniques such as the non-executable stack and address space layout randomization. Randomization prevents most buffer overflow attacks and requires the attacker to use heap spraying or other application-dependent methods to obtain addresses, although its adoption has been slow. However, deployments of the technology are typically limited to randomizing libraries and the location of the stack.

Impact In 2019, a Microsoft security engineer reported that 70% of all security vulnerabilities were caused by memory safety issues. In 2020, a team at Google similarly reported that 70% of all "severe security bugs" in Chromium were caused by memory safety problems. Many other high-profile vulnerabilities and exploits in critical software have ultimately stemmed from a lack of memory safety, including Heartbleed and a long-standing privilege escalation bug in sudo. The pervasiveness and severity of vulnerabilities and exploits arising from memory safety issues have led several security researchers to describe identifying memory safety issues as "shooting fish in a barrel".

Classification of memory safety errors Many different types of memory errors can occur:

Spatial Buffer overflow – out-of-bound writes can corrupt the content of adjacent objects, or internal data (like bookkeeping information for the heap) or return addresses. Buffer over-read – out-of-bound reads can reveal sensitive data or help attackers bypass address space layout randomization. Temporal Use after free – dereferencing a dangling pointer storing the address of an object that has been deleted. Double free – repeated calls to free may prematurely free a new object at the same address. If the exact address has not been reused, other corruption may occur, especially in allocators that use free lists. Uninitialized variables – a variable that has not been assigned a value is used. It may contain sensitive information or bits that are not valid for the type. Wild pointers arise when a pointer is used prior to initialization to some known state. They show the same erratic behaviour as dangling pointers, though they are less likely to stay undetected. Invalid free – passing an invalid address to free can corrupt the heap. Mismatched free – when multiple allocators are in use, attempting to free memory with a deallocation function of a different allocator. Spatiotemporal Struct tearing or non-atomic reads may be observed when data larger than the cpu word size is read from memory.

Contributing bugs Depending on the language and environment, other types of bugs can contribute to memory unsafety:

Stack exhaustion – occurs when a program runs out of stack space, typically because of too deep recursion. A guard page typically halts the program, preventing memory corruption, but functions with large stack frames may bypass the page, and kernel code may not have the benefit of guard pages. Heap exhaustion – the program tries to allocate more memory than the amount available. In some languages, this condition must be checked for manually after each allocation. Memory leak – Failing to return memory to the allocator may set the stage for heap exhaustion (above). Failing to run the destructor of an RAII object may lead to unexpected results. Null pointer dereference – A null pointer dereference will often cause an exception or program termination in most environments, but can cause corruption in operating system kernels or systems without memory protection or when use of the null pointer involves a large or negative offset. In C++, because dereferencing a null pointer is undefined behavior, compiler optimizations may cause other checks to be removed, leading to vulnerabilities elsewhere in the code. Some lists may also include race conditions (concurrent reads/writes to shared memory) as being part of memory safety (e.g., for access control). The Rust programming language prevents many kinds of memory-based race conditions by default, because it ensures there is at most one writer or one or more readers. Many other programming languages, such as Java, do not automatically prevent memory-based race conditions, yet are still generally considered "memory safe" languages. Therefore, countering race conditions is generally not considered necessary for a language to be considered memory safe.

Approaches

Automatic memory management Some modern high-level programming languages are memory-safe by default, though not completely since they only check their own code and not the system they interact with. Automatic memory management in the form of garbage collection is the most common technique for preventing some of the memory safety problems, since it prevents common memory safety errors like use-after-free for all data allocated within the language runtime. When combined with automatic bounds checking on all array accesses and no support for raw pointer arithmetic, garbage collected languages provide strong memory safety guarantees (though the guarantees may be weaker for low-level operations explicitly marked unsafe, such as use of a foreign function interface). However, the performance overhead of garbage collection makes these languages unsuitable for certain performance-critical applications.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Memory safety

Start with the simplest possible case. Write down what Memory safety claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Memory safety before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Memory safety ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Memory safety

In research
Memory safety appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Memory safety in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Memory safety is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer security exploits, Programming language implementation, Software bugs, so understanding it makes those chapters shorter.
In everyday life
Look for Memory safety outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Memory safety” →

How to study Memory safety in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Memory safety means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Memory safety out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Memory safety in simple terms?

Memory safety is the state of being protected from various software bugs and security vulnerabilities when dealing with memory access, such as buffer overflows and dangling pointers. For example, Java is memory-safe because its runtime error detection checks array bounds and pointer dereferences.

Why does Memory safety matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Memory safety?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Memory safety.

Tags

  • Computer security exploits
  • Programming language implementation
  • Software bugs

Keep exploring