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,13 +113,15 @@ 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(.);
@ -138,7 +142,8 @@ SECTIONS
/* Page tables here, align to 4K boundary */
.pgtables (NOLOAD) : ALIGN(0x1000) {
.pgtables (NOLOAD) : ALIGN(0x1000)
{
*(.pgtables)
. = ALIGN(4);
} > ksram
@ -157,7 +162,8 @@ SECTIONS
/* Stack top */
.stack_top : {
.stack_top :
{
. = ALIGN(32);
_ebss = ABSOLUTE(.);
} > ksram

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 :
@ -78,13 +80,15 @@ 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(.);

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,13 +97,15 @@ 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(.);
@ -122,14 +139,16 @@ SECTIONS
/* Page tables here, align to 4K boundary */
.pgtables (NOLOAD) : ALIGN(0x1000) {
.pgtables (NOLOAD) : ALIGN(0x1000)
{
*(.pgtables)
. = ALIGN(4);
} > ksram
/* Stack top */
.stack_top : {
.stack_top :
{
. = ALIGN(32);
_ebss = ABSOLUTE(.);
} > ksram

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