Keil Logo

Technical Support

On-Line Manuals

Compiler Reference Guide

Preface armclang Command-line Options Support level definitions Summary of armclang command-line options -C -c -D -E -e -fbare-metal-pie -fbracket-depth=N -fcommon, -fno-common -fdata-sections, -fno-data-sections -ffast-math, -fno-fast-math -ffixed-rN -ffp-mode -ffunction-sections, -fno-function-sections -fident, -fno-ident @file -fldm-stm, -fno-ldm-stm -fno-builtin -fno-inline-functions -flto, -fno-lto -fexceptions, -fno-exceptions -fomit-frame-pointer, -fno-omit-frame-pointer -fropi, -fno-ropi -fropi-lowering, -fno-ropi-lowering -frwpi, -fno-rwpi -frwpi-lowering, -fno-rwpi-lowering -fshort-enums, -fno-short-enums -fshort-wchar, -fno-short-wchar -fstack-protector, -fstack-protector-all, -fstack- -fstrict-aliasing, -fno-strict-aliasing -ftrapv -fvectorize, -fno-vectorize -fvisibility -fwrapv -g, -gdwarf-2, -gdwarf-3, -gdwarf-4 -I -include -L -l -M, -MM -MD, -MMD -MF -MG -MP -MT -march -marm -masm -mbig-endian -mbranch-protection -mcmse -mcpu -mexecute-only -mfloat-abi -mfpu -mimplicit-it -mlittle-endian -mmemtag-stack, -mno-memtag-stack -mno-neg-immediates -moutline, -mno-outline -mpixolib -munaligned-access, -mno-unaligned-access -mthumb -nostdlib -nostdlibinc -o -O -pedantic -pedantic-errors -Rpass -S -save-temps -std --target -U -u -v --version --version_number --vsn -W -Wl -Xlinker -x -### Compiler-specific Keywords and Operators Compiler-specific Function, Variable, and Type Att Compiler-specific Intrinsics Compiler-specific Pragmas Other Compiler-specific Features Standard C Implementation Definition Standard C++ Implementation Definition armclang Integrated Assembler armclang Inline Assembler

-mmemtag-stack, -mno-memtag-stack

1.59 -mmemtag-stack, -mno-memtag-stack

-mmemtag-stack enables the generation of stack protection code that uses the memory tagging extension. -mmemtag-stack is an [ALPHA] feature.

-mno-memtag-stack disables the generation of stack protection code that uses the memory tagging extension.

Note:

This topic describes an [ALPHA] feature. See Support level definitions.

Default

The default is -mno-memtag-stack.

Syntax

-mmemtag-stack

-mno-memtag-stack

Parameters

None.

Restrictions

Memory tagging stack protection (stack tagging) is available for the AArch64 state for architectures with the memory tagging extension. The memory tagging extension is optional in Arm®v8.5-A and later architectures. When compiling with -mmemtag-stack, the compiler uses memory tagging instructions that are not available for architectures without the memory tagging extension. The resulting code cannot execute on architectures without the memory tagging extension. For more information, see the +memtag feature in -mcpu.

Operation

Use -mmemtag-stack to enable the generation of memory tagging code for protecting the memory allocations on the stack. When you use -mmemtag-stack, the compiler generates code to ensure that pointers to objects on the stack are tagged, when the address of the object is taken, if the compiler cannot guarantee that the pointer access is always bounded. When such pointers are dereferenced, the processor checks the tag on the pointer with the tag on the memory location being accessed. If the tags mismatch, the processor causes an exception and therefore tries to prevent the pointer from accessing any object that is different from the object whose address was taken.

For example, if a pointer to a variable on the stack is passed to another function, then the compiler might be unable to guarantee that this pointer will only be used to access the same variable. In this situation, the compiler generates memory tagging code. The memory tagging instructions apply a unique tag to the pointer and to its corresponding allocation on the stack.

Note:

  • The ability of the compiler to determine whether a pointer access is bounded might be affected by optimizations. For example, if an optimization inlines a function, and as a result, if the compiler can guarantee that the pointer access is always safe, then the compiler might not generate memory tagging stack protection code. Therefore, the conditions for generating memory tagging stack protection code might not have a direct relationship to the source code.
  • When using -mmemtag-stack, there is a high probability that an unbounded pointer access to the stack causes a processor exception. This does not guarantee that all unbounded pointer accesses to the stack cause a processor exception.
  • The [ALPHA] implementation of stack tagging does not protect variable-length allocations on the stack.

To ensure full memory tagging stack protection, you must also link your code with the library that provides stack protection with memory tagging. For more information, see --library_security.

armlink automatically selects the library with memory tagging stack protection if at least one object file is compiled with -mmemtag-stack and at least one object file is compiled with pointer authentication, using -mbranch-protection. You can override the selected library by using the armlink --library_security option to specify the library that you want to use.

Note:

  • Use of -mmemtag-stack to protect the stack increases the amount of memory allocated on the stack. This is because, the compiler has to allocate a separate 16-byte aligned block of memory on the stack for each variable whose stack allocation is protected by memory tagging.
  • Code compiled with stack tagging can be safely linked together with code compiled without stack tagging. However, if any object file is compiled with -mmemtag-stack, and if setjmp, longjmp, or C++ exceptions are present anywhere in the image, then you must use the v8.5a library to avoid stack tagging related memory fault at runtime.
  • The -mmemtag-stack option and the -fstack-protector options are independent and provide complementary stack protection. They can be used together or in isolation.

Examples

The following example demonstrates the effect of the -mmemtag-stack option.

Source file foo.c contains the code below:

extern void func2 (int* a);

void func1(void)
{
  int x=10;
  int y=20;

  func2(&x);
  func2(&y);
}

Compile foo.c, without memory tagging stack protection, using the following command-line:

armclang --target=aarch64-arm-none-eabi -march=armv8.5-a+memtag -S -O1 foo.c -o mem_no_protect.s

The generated assembly file mem_no_protect.s contains the code below:

func1:                                  // @func1
// %bb.0:                               // %entry
        str     x30, [sp, #-16]!        // 8-byte Folded Spill
        mov     w8, #10
        mov     w9, #20
        add     x0, sp, #12             // =12
        stp     w9, w8, [sp, #8]
        bl      func2
        add     x0, sp, #8              // =8
        bl      func2
        ldr     x30, [sp], #16          // 8-byte Folded Reload
        ret

Compile foo.c, with memory tagging stack protection, using the following command-line:

armclang --target=aarch64-arm-none-eabi -march=armv8.5-a+memtag -S -O1 foo.c -mmemtag-stack -o mem_with_protect.s

The generated assembly file mem_with_protect.s contains the code below:

func1:                                  // @func1
// %bb.0:                               // %entry
        stp     x19, x30, [sp, #-16]!   // 16-byte Folded Spill
        sub     sp, sp, #32             // =32
        irg     x0, sp
        mov     w8, #10
        mov     w9, #20
        addg    x19, x0, #16, #1
        stg     x0, [x0]
        str     w8, [sp]
        stg     x19, [x19]
        str     w9, [sp, #16]
        bl      func2
        mov     x0, x19
        bl      func2
        add     x8, sp, xzr
        st2g    x8, [sp], #32
        ldp     x19, x30, [sp], #16     // 16-byte Folded Reload
        ret

When using the -mmemtag-stack option:

  • The compiler generates memory tagging instructions, for example IRG, ADDG, STG, and ST2G, to ensure that the pointers and the variables on the stack are tagged. For information on these instructions, see the A64 instruction set.
  • The compiler uses an additional 32 bytes of memory on the stack for the variables in foo.c, whose addresses are taken.

Non-ConfidentialPDF file icon PDF version100067_0612_00_en
Copyright © 2014–2019 Arm Limited or its affiliates. All rights reserved. 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.