mm/kasan: Map the generated shadow area to the fixed segment

Since the size of the shadow area will change during the script merging of global variables, it will lead to unpredictable number of links. After fixing, only three links are required.

Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
This commit is contained in:
wangmingrong1 2024-09-19 20:09:48 +08:00 committed by Xiang Xiao
parent 071af0c993
commit 0d8b6de03a
6 changed files with 126 additions and 85 deletions

View File

@ -28,16 +28,18 @@ SECTIONS
. = 0x40101000;
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused :
{
.kasan.unused : {
*(.data..LASANLOC*)
} > ROM
.kasan.global :
{
}
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
} > ROM
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
.text : {

View File

@ -34,16 +34,18 @@ SECTIONS
. = 0x40280000; /* uboot load address */
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused :
{
.kasan.unused : {
*(.data..LASANLOC*)
}
.kasan.global :
{
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
_start = .;

View File

@ -60,16 +60,18 @@ SECTIONS
. = 0x80000000;
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused :
{
.kasan.unused : {
*(.data..LASANLOC*)
}
.kasan.global :
{
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
.text :
@ -111,17 +113,19 @@ SECTIONS
_erodata = . ;
}
.tdata : {
.tdata :
{
_stdata = ABSOLUTE(.);
*(.tdata .tdata.* .gnu.linkonce.td.*);
_etdata = ABSOLUTE(.);
}
}
.tbss : {
.tbss :
{
_stbss = ABSOLUTE(.);
*(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon);
_etbss = ABSOLUTE(.);
}
}
_eronly = ABSOLUTE(.);
@ -136,9 +140,10 @@ SECTIONS
_edata = . ;
}
/* Page tables here, align to 4K boundary */
/* Page tables here, align to 4K boundary */
.pgtables (NOLOAD) : ALIGN(0x1000) {
.pgtables (NOLOAD) : ALIGN(0x1000)
{
*(.pgtables)
. = ALIGN(4);
} > ksram
@ -155,25 +160,26 @@ SECTIONS
_ebss = . ;
} > ksram
/* Stack top */
/* Stack top */
.stack_top : {
. = ALIGN(32);
_ebss = ABSOLUTE(.);
.stack_top :
{
. = ALIGN(32);
_ebss = ABSOLUTE(.);
} > ksram
/* Stabs debugging sections. */
/* Stabs debugging sections. */
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
}

View File

@ -27,16 +27,18 @@ SECTIONS
. = TEXT_ADDR;
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused :
{
.kasan.unused : {
*(.data..LASANLOC*)
}
.kasan.global :
{
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
.text :
@ -60,7 +62,7 @@ SECTIONS
_etext = . ;
}
.init_section :
.init_section :
{
_sinit = ABSOLUTE(.);
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
@ -78,17 +80,19 @@ SECTIONS
_erodata = . ;
}
.tdata : {
.tdata :
{
_stdata = ABSOLUTE(.);
*(.tdata .tdata.* .gnu.linkonce.td.*);
_etdata = ABSOLUTE(.);
}
}
.tbss : {
.tbss :
{
_stbss = ABSOLUTE(.);
*(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon);
_etbss = ABSOLUTE(.);
}
}
_eronly = ABSOLUTE(.);
@ -116,18 +120,18 @@ SECTIONS
_ebss = . ;
}
/* Stabs debugging sections. */
/* Stabs debugging sections. */
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
}

View File

@ -51,6 +51,21 @@ SECTIONS
{
. = 0x80200000;
/* where the global variable out-of-bounds detection information located */
#ifdef CONFIG_MM_KASAN_GLOBAL
.kasan.unused : {
*(.data..LASANLOC*)
}
.kasan.global : {
KEEP (*(.data..LASAN0))
KEEP (*(.data.rel.local..LASAN0))
}
.kasan.shadows : {
*(.kasan.shadows)
}
#endif
.text :
{
_stext = . ;
@ -82,17 +97,19 @@ SECTIONS
_erodata = . ;
}
.tdata : {
.tdata :
{
_stdata = ABSOLUTE(.);
*(.tdata .tdata.* .gnu.linkonce.td.*);
_etdata = ABSOLUTE(.);
}
}
.tbss : {
.tbss :
{
_stbss = ABSOLUTE(.);
*(.tbss .tbss.* .gnu.linkonce.tb.* .tcommon);
_etbss = ABSOLUTE(.);
}
}
_eronly = ABSOLUTE(.);
@ -120,32 +137,34 @@ SECTIONS
_ebss = . ;
} > ksram
/* Page tables here, align to 4K boundary */
/* Page tables here, align to 4K boundary */
.pgtables (NOLOAD) : ALIGN(0x1000) {
*(.pgtables)
. = ALIGN(4);
.pgtables (NOLOAD) : ALIGN(0x1000)
{
*(.pgtables)
. = ALIGN(4);
} > ksram
/* Stack top */
/* Stack top */
.stack_top : {
. = ALIGN(32);
_ebss = ABSOLUTE(.);
.stack_top :
{
. = ALIGN(32);
_ebss = ABSOLUTE(.);
} > ksram
/* Stabs debugging sections. */
/* Stabs debugging sections. */
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_info 0 : { *(.debug_info) }
.debug_line 0 : { *(.debug_line) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_aranges 0 : { *(.debug_aranges) }
}

View File

@ -39,6 +39,9 @@ KASAN_MAX_DATA_GAP = 1 << 16
# generated by the compiler is located
KASAN_SECTION = ".kasan.global"
# Segments stored in the shadow area
KASAN_SHADOW_SECTION = ".kasan.shadows"
# The structure of parsing strings required for 32-bit and 64 bit
KASAN_GLOBAL_STRUCT_32 = Struct(
"beg" / Int32ul,
@ -197,9 +200,14 @@ def create_kasan_file(config: Config, region_list=[]):
region: KASanRegion = None
with open(config.outpath, "w") as file:
file.write("#include <nuttx/nuttx.h>\n")
# Write the kasan region array
for i in range(len(region_list)):
file.write("static const unsigned char\nglobals_region%d[] = {\n" % (i))
file.write(
'static const unsigned char locate_data("%s")'
"\nglobals_region%d[] = {\n" % (KASAN_SHADOW_SECTION, i)
)
region = region_list[i]
# Fill the array of regions