Skip to main content

How does Scudo heap tagging with MTE compare to LLVM compiler-generated stack variable tagging?

Android: Scudo

While both Scudo heap tagging and LLVM stack variable tagging rely on ARMv9 Memory Tagging Extension (MTE) hardware to enforce memory safety, they operate at different layers of the software stack and target distinct memory domains.

Scudo handles dynamic memory at runtime via the C library allocator, whereas LLVM stack tagging is generated at compile time by the compiler backend (-fsanitize=memtag-stack).

1. Architectural Comparison Matrix

Architectural Feature
Scudo Heap Tagging (-fsanitize=memtag-heap)
LLVM Stack Tagging (-fsanitize=memtag-stack)

Execution Layer
Runtime Allocator (libc / Bionic)
Compiler Code Generation (AArch64StackTagging LLVM Pass)

Target Memory Domain
Heap (malloc, calloc, free, new, delete)
Stack (Local variables, function frames, stack buffers)

Tag Generation Method
IRG instruction generates a new random tag per malloc() call.
One base IRG tag for the stack frame; individual local variables derive offset tags via ADDG.

Lifecycle & Scope
Bound to explicit allocation lifetime (malloc $\rightarrow$ free).
Bound to function scope / lexical scope (Prologue $\rightarrow$ Epilogue).

Target Vulnerabilities
Heap Buffer Overflows, Use-After-Free (UAF), Double Free.
Stack Buffer Overflows, Use-After-Return, Use-After-Scope.

Typical CPU Overhead
Low: $\sim 1\% - 6\%$ (Only triggers during heap operations).
Moderate to High: $\sim 15\%$ average (up to $>100\%$ on function-heavy code).

Stack / Heap Size Impact
$16$-byte minimum allocation alignment + Scudo chunk header ($8\text{--}16$ bytes).
Forces $16$-byte alignment and padding between every tagged local variable.

2. Tag Generation & Instruction Mechanics

A. Scudo Heap Tagging (Dynamic Runtime)

When malloc(32) is called, Scudo executes a runtime routine:

  1. Random Tagging: Generates a random $4$-bit tag for the requested buffer using IRG.

  2. Memory Tagging: Uses ST2G or STGP (Set Tag and Pair) to write the tag across two $16$-byte granules ($32$ bytes) of memory.

  3. Deallocation Invalidation: Upon free(), Scudo uses IRG to generate a different tag and overwrites physical memory tags via STG. Any lingering pointer still holding the old tag immediately fails on subsequent access.

ARM assembler

// Scudo Malloc Generation (Conceptual ARM Assembly)
IRG   X0, SP              // Generate random 4-bit tag in top byte of X0
ST2G  X0, [X0]            // Tag 32 bytes of heap payload in physical RAM
// Returns tagged pointer X0 to application
B. LLVM Stack Tagging (Static Compiler Instrumentation)

For local stack variables, generating an IRG random tag for every local variable would create unacceptable register pressure and instruction bloat. Instead, the LLVM compiler uses an optimized base-tag derivation strategy:

  1. Base Frame Tag: On function entry (prologue), IRG generates a single base tag for the stack pointer SP.

  2. Tag Derivation via ADDG: The compiler assigns unique tags to individual local variables using ADDG (Add Tagged Immediate). ADDG increments both the memory offset and the $4$-bit tag value (modulo $16$) in a single instruction.

  3. Prologue Tagging: Writes variable tags to stack memory using STG, ST2G, or STGP.

  4. Epilogue Untagging: Upon function return, the epilogue uses STG to restore stack memory tags back to the default SP tag, preventing Use-After-Return attacks.

ARM assembler

// LLVM Stack Instrumentation for two local variables (int a, int b)
IRG   X19, SP             // 1. Generate single base tag for the function frame
ADDG  X0, X19, #0, #1     // Variable 'a': Offset +0 bytes, Tag +1
ADDG  X1, X19, #16, #2    // Variable 'b': Offset +16 bytes, Tag +2
STG   X0, [X0]            // Tag memory for 'a'
STG   X1, [X1]            // Tag memory for 'b'

BL    use_vars            // Call function body

ST2G  SP, [SP]            // Epilogue: Revert stack memory tags back to SP tag before return

3. Performance and Overhead Trade-offs

Why Stack Tagging Is More Expensive Than Heap Tagging
  • Execution Frequency: Programs call malloc/free periodically, but functions enter and exit continuously. Adding prologue and epilogue tagging instructions to every function with local stack arrays creates significant code size and execution overhead.

  • Cache & Memory Bus Contention: Frequently modifying stack tags in function prologues forces frequent writes to $L1$ data cache lines and tag memory arrays. On benchmarks like SPEC CPU, stack tagging alone (-fsanitize=memtag-stack) averages a $\sim 15\%$ CPU overhead, whereas Scudo heap tagging averages $\sim 3\text{--}6\%$.

Stack Alignment & Bloat

LLVM stack tagging forces all tagged local variables to be aligned to $16$-byte boundaries. If a function declares three char variables, a standard compiler packs them into 3 bytes on the stack. With MTE stack tagging enabled, each variable grows to a full 16-byte granule (48 bytes total), increasing stack frame size and cache footprint.

4. Production Strategy

Because stack tagging carries higher CPU overhead, production platforms like Android 14+ enforce a split deployment model:

  • Production Default: Scudo Heap Tagging is enabled globally for system daemons and apps due to its low CPU overhead ($\sim 1\text{--}3\%$ in ASYNC mode) and high effectiveness against heap-based exploits.

  • Testing / Hardened Deployments: LLVM Stack Tagging (-fsanitize=memtag-stack) is enabled alongside heap tagging in security-critical components, continuous integration testing, or high-assurance environments to achieve complete end-to-end memory safety.

Comments

Popular posts from this blog

Windows Media Player 12 Themes for Windows 7

Bored of your default Windows Media Player Skins?. Grab some fresh and new Windows Media Player 12 themes for Windows 7 ! Windows Media Player 12 Codecs Windows Media Player 12 comes with support for DivX and MP4, but it still lacks supports for many other video formats. (amr | mpc | ofr | divx | mka | ape | flac | evo | flv | m4b | mkv | ogg | ogv | ogm | rmvb | xvid)ù A popular codec pack can be downloaded here and here . Windows Media Player 12 Skins The following skin packages includes some really awesome themes for your Windows Media Player: Alienware Theme Batman Theme Catwoman Theme Darkstar Theme Half-Life Theme Halo 2 Theme The Last Samurai Theme Stalker Theme XBOX Theme XSN Sports Them Download Windows Media Player Themes Pack 1 (70 Skins) Download Windows Media Player Themes Pack 2 (12 Skins) New Year Theme 2010 for Windows 7 This theme can be downloaded for free from uploaded.to Custom Search If you liked this article, subscribe t...

Windows 7 Keyboard Shortcuts.

Keyboard shortcuts are combinations of two or more keys that, when pressed, can be used to perform a task that would typically require a mouse or other pointing device. Keyboard shortcuts can make it easier to interact with your computer, saving you time and effort as you work with Windows and other programs.  Most programs also provide accelerator keys that can make it easier to work with menus and other commands. Check the menus of programs for accelerator keys.  If a letter is underlined in a menu, that usually means that pressing the Alt key in combination with the underlined key will have the same effect as clicking that menu item. Pressing the Alt key in some programs, such as Paint and WordPad, shows commands that are labeled with additional keys that you can press to use them. Dialog box keyboard shortcuts. The following table contains keyboard shortcuts for use in dialog boxes. Press this key To do this: Ctrl+Tab Move forward through tabs...

Windows 7 consumer security software providers.

We recommend that you install security software to help protect your computer from viruses and other security threats, and that you keep your security software up to date. Some companies use products that appear to be antivirus programs to install viruses or malware on your computer. When you install the program, you might also be installing the virus or other malware, without knowing it. Many companies, including those listed on this page, distribute antivirus programs. You should carefully investigate the source of antivirus and other products before downloading and installing them. The companies listed below provide consumer security software that is compatible with Windows 7. Just click the company name to see the Windows 7-compatible product they offer. For business security software that is compatible with Windows 7, please visit the Windows 7 Compatibility Center or contact your security vendor of choice. Important: Before you install antivirus software, check to make s...