ArticleslgStudy

computer science

Code sanitizer

Code sanitizer 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 Code sanitizer rather than just read about it. In short: A code sanitizer is a programming tool that detects bugs in the form of undefined or suspicious behavior by a compiler inserting instrumentation code at runtime. Sanitizers have evolved over several decades to detect memory errors in unsafe programming languages, beginning with early pointer and array access checks in 1994 and type-safe retrofitting in 2005, followed by a significant expansion into sophisticated spa…

Key takeaways

  • Code sanitizer 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 Code sanitizer to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code sanitizer from memory before moving on to harder problems.

Reference excerpt

A code sanitizer is a programming tool that detects bugs in the form of undefined or suspicious behavior by a compiler inserting instrumentation code at runtime. Sanitizers have evolved over several decades to detect memory errors in unsafe programming languages, beginning with early pointer and array access checks in 1994 and type-safe retrofitting in 2005, followed by a significant expansion into sophisticated spatial and temporal memory safety tools between 2009 and 2019. As of 2024, the most popular sanitizer is ASan, originally designed by Google. It is integrated into most mainstream compilers due to its relatively small memory and performance overhead.

AddressSanitizer Google's ASan, introduced in 2012, uses a shadow memory scheme to detect memory bugs. It is available in:

Clang (starting from version 3.1) GCC (starting from version 4.8) Xcode (starting from version 7.0) MSVC (widely available starting from version 16.9). The original design of the sanitizer increases processing time by about 73% and memory usage by 240%. There is a hardware-accelerated ASan called HWAsan available for AArch64 and (in a limited fashion) x86_64. AddressSanitizer does not detect any uninitialized memory reads (but this is detected by MemorySanitizer), and detects only some use-after-return bugs. It is also not capable of detecting all arbitrary memory corruption bugs, nor all arbitrary write bugs due to integer underflow/overflows (when the integer with undefined behavior is used to calculate memory address offsets). Adjacent buffers in structs and classes are not protected from overflow, in part to prevent breaking backwards compatibility.

KernelAddressSanitizer The KernelAddressSanitizer (KASan) detects dynamic memory errors in the Linux kernel. Kernel instrumentation requires a special feature in the compiler supplying the -fsanitize=kernel-address command line option, since kernels do not use the same address space as normal programs. KASan is also available for use with Windows kernel drivers beginning in Windows 11 22H2 and above. Similarly to Linux, compiling a Windows driver with KASAN requires passing the /fsanitize=kernel-address command line option to the MSVC compiler.

Other sanitizers Google also produced LeakSanitizer (LSan, memory leaks), ThreadSanitizer (TSan, data races and deadlocks), MemorySanitizer (MSan, uninitialized memory), and UndefinedBehaviorSanitizer (UBSan, undefined behaviors, with fine-grained control). These tools are generally available in Clang/LLVM and GCC. Similar to KASan, there are kernel-specific versions of LSan, MSan, TSan, as well as completely original kernel sanitizers such as KFENCE and KCSan. Additional sanitizer tools (grouped by compilers under -fsanitize or a similar flag) include:

LLVM control-flow integrity and its kernel counterpart, which checks virtual tables and type casts for forward-edge CFI MemTagSanitizer, an ASan-like tool that uses Armv8.5-A features for very low overhead ShadowCallStack, an AArch64 tool that provides a shadow stack protection Scudo Hardened Allocator, an alternative memory allocator that includes GWP-ASan, a probabilistic ASan analogue with low overhead libFuzzer, an LLVM tool that adds code coverage to fuzzing

Usage A code sanitizer detects suspicious behavior as the program runs. One common way to use a sanitizer is to combine it with fuzzing, which generates inputs likely to trigger bugs.

Users

Chromium and Firefox developers are active users of AddressSanitizer; the tool has found hundreds of bugs in these web browsers. A number of bugs were found in FFmpeg and FreeType. The Linux kernel has enabled the AddressSanitizer for the x86-64 architecture as of Linux version 4.0.

Examples

ASan: Heap-use-after-free

$ ./a.out ==5587==ERROR: AddressSanitizer: heap-use-after-free on address 0x61400000fe44 at pc 0x47b55f bp 0x7ffc36b28200 sp 0x7ffc36b281f8 READ of size 4 at 0x61400000fe44 thread T0 #0 0x47b55e in main /home/test/example_UseAfterFree.cc:5 #1 0x7f15cfe71b14 in __libc_start_main (/lib64/libc.so.6+0x21b14) #2 0x47b44c in _start (/root/a.out+0x47b44c)

0x61400000fe44 is located 4 bytes inside of 400-byte region [0x61400000fe40,0x61400000ffd0) freed by thread T0 here: #0 0x465da9 in operator delete[](void*) (/root/a.out+0x465da9) #1 0x47b529 in main /home/test/example_UseAfterFree.cc:4

previously allocated by thread T0 here: #0 0x465aa9 in operator new[](unsigned long) (/root/a.out+0x465aa9) #1 0x47b51e in main /home/test/example_UseAfterFree.cc:3

SUMMARY: AddressSanitizer: heap-use-after-free /home/test/example_UseAfterFree.cc:5 main Shadow bytes around the buggy address: [...] 0x0c287fff9fb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =>0x0c287fff9fc0: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd 0x0c287fff9fd0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd [...] Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Heap right redzone: fb Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 ASan internal: fe ==5587==ABORTING

ASan: Heap-buffer-overflow

25372==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000ffd4 at pc 0x0000004ddb59 bp 0x7fffea6005a0 sp 0x7fffea600598 READ of size 4 at 0x61400000ffd4 thread T0 #0 0x46bfee in main /tmp/main.cpp:4:13

0x61400000ffd4 is located 4 bytes to the right of 400-byte region [0x61400000fe40,0x61400000ffd0) allocated by thread T0 here: #0 0x4536e1 in operator delete[](void*) #1 0x46bfb9 in main /tmp/main.cpp:2:16

ASan: Stack-buffer-overflow

7405==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff64740634 at pc 0x46c103 bp 0x7fff64740470 sp 0x7fff64740468 READ of size 4 at 0x7fff64740634 thread T0 #0 0x46c102 in main /tmp/example_StackOutOfBounds.cc:5

Address 0x7fff64740634 is located in stack of thread T0 at offset 436 in frame #0 0x46bfaf in main /tmp/example_StackOutOfBounds.cc:2

This frame has 1 object(s): [32, 432) 'stack_array' <== Memory access at offset 436 overflows this variable

ASan: Global-buffer-overflow

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Code sanitizer

Start with the simplest possible case. Write down what Code sanitizer 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 Code sanitizer 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 Code sanitizer 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 Code sanitizer

In research
Code sanitizer 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 Code sanitizer 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
Code sanitizer is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer security procedures, Free memory debuggers, Free memory management software, so understanding it makes those chapters shorter.
In everyday life
Look for Code sanitizer 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 “Code sanitizer” →

How to study Code sanitizer in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code sanitizer 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 Code sanitizer out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code sanitizer in simple terms?

A code sanitizer is a programming tool that detects bugs in the form of undefined or suspicious behavior by a compiler inserting instrumentation code at runtime. Sanitizers have evolved over several decades to detect memory errors in unsafe programming languages, beginning with early pointer and ar…

Why does Code sanitizer 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 Code sanitizer?

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 Code sanitizer.

Tags

  • Computer security procedures
  • Free memory debuggers
  • Free memory management software
  • Security testing tools

Keep exploring