Enabling ARM64 Memory Tagging Extension to Catch Memory Safety Bugs in C/C++
Step‑by‑step guide to enable ARM64 Memory Tagging Extension, compile a test program with -fsanitize=memtag, and verify that memory safety faults are reported via synchronous tag‑check exceptions.
25 Aug 2026, 23:20 UTC

Desired Outcome
Run a C/C++ program under ARM64 Memory Tagging Extension (MTE) so that spatial or temporal memory errors trigger a synchronous tag‑check fault, allowing you to detect bugs with low runtime overhead.
Prerequisites
- Hardware: an ARMv8.5‑A or later core that implements MTE (check for the
MTEfeature flag). - Kernel: a build with
CONFIG_ARM64_MTE=y(or a compatible distribution kernel that enables it). - Toolchain: LLVM/Clang version that supports
-fsanitize=memtag(e.g., clang 15+). - Root or sudo access to load kernel parameters and view kernel logs.
Focused Procedure
- Verify CPU support
# Run as any user lscpu | grep -i mte # Expected output includes a line like: Flags: ... mte ...If no
mteflag appears, the processor cannot use MTE. - Ensure the kernel has MTE enabled
Check the running kernel configuration:
zgrep CONFIG_ARM64_MTE /proc/config.gz # or, if config is not available: cat /boot/config-$(uname -r) | grep CONFIG_ARM64_MTEThe value should be
y. If it isnor missing, you need a kernel built with MTE support or a boot parameter that activates it. - Activate MTE at boot (if not already active)
Add the kernel command‑line option
memtag=on(e.g., via GRUB) and reboot. After reboot, confirm:cat /sys/kernel/debug/arm64/mte/enable # Should print 1 - Create a test program with a known bug
# test.c #include #include int main(void) { char buf[8]; strcpy(buf, "this is too long"); // intentional overflow printf("%s\n", buf); return 0; } - Compile with MTE sanitizer
# Run in a normal user directory clang -fsanitize=memtag -g -O1 test.c -o test-mteThe
-fsanitize=memtagflag instructs the compiler to emitIRG(Insert Random Tag) instructions for allocations and to preserve the tag in the top byte of pointers. - Run the binary and observe the fault
./test-mte # The program should abort with a SIGSEGV (or SIGILL) and print a message like: # tag check failureIf the program terminates without a tag‑check message, MTE may not be active for the process.
- Check kernel logs for the tag‑check fault
dmesg | grep -i tag # or journalctl -k | grep -i tagLook for lines containing "MTE tag check fault" or similar, which include the faulting address and the process name.
Expected Checks
- CPU reports the
mtefeature. - Kernel shows
CONFIG_ARM64_MTE=y(ormemtag=onactive). - The compiled binary runs and aborts with a tag‑check fault when the overflow occurs.
- Kernel log contains a tag‑check fault entry matching the faulting instruction.
Recovery Options
- If MTE causes unwanted crashes due to legitimate pointer arithmetic that modifies the top byte, rebuild the application without
-fsanitize=memtag. - To temporarily disable MTE for all processes, reboot with the kernel parameter
memtag=offor set/sys/kernel/debug/arm64/mte/enableto0(requires root). - For production use, consider enabling MTE only in testing or staging environments; the performance impact is low (~2‑5 %) compared with AddressSanitizer.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.