@@ -1766,7 +1766,11 @@ mod test {
17661766 fn new_section ( sh_addr : u64 , sh_size : u64 ) -> Elf64Shdr {
17671767 Elf64Shdr {
17681768 sh_addr,
1769- sh_offset : sh_addr,
1769+ sh_offset : if sh_addr >= ebpf:: MM_PROGRAM_START {
1770+ sh_addr - ebpf:: MM_PROGRAM_START
1771+ } else {
1772+ sh_addr
1773+ } ,
17701774 sh_size,
17711775 sh_name : 0 ,
17721776 sh_type : 0 ,
@@ -1796,7 +1800,7 @@ mod test {
17961800 assert ! ( matches!(
17971801 ElfExecutable :: parse_ro_sections(
17981802 & config,
1799- & SBPFVersion :: V2 ,
1803+ & SBPFVersion :: V1 ,
18001804 sections,
18011805 & elf_bytes,
18021806 ) ,
@@ -1823,7 +1827,7 @@ mod test {
18231827 assert ! ( matches!(
18241828 ElfExecutable :: parse_ro_sections(
18251829 & config,
1826- & SBPFVersion :: V2 ,
1830+ & SBPFVersion :: V1 ,
18271831 sections,
18281832 & elf_bytes,
18291833 ) ,
@@ -1901,7 +1905,7 @@ mod test {
19011905 let sections: [ ( Option < & [ u8 ] > , & Elf64Shdr ) ; 2 ] =
19021906 [ ( Some ( b".text" ) , & s1) , ( Some ( b".rodata" ) , & s2) ] ;
19031907 assert_eq ! (
1904- ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes, ) ,
1908+ ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes) ,
19051909 Err ( ElfError :: ValueOutOfBounds )
19061910 ) ;
19071911 }
@@ -1923,7 +1927,7 @@ mod test {
19231927 let sections: [ ( Option < & [ u8 ] > , & Elf64Shdr ) ; 2 ] =
19241928 [ ( Some ( b".text" ) , & s1) , ( Some ( b".rodata" ) , & s2) ] ;
19251929 assert_eq ! (
1926- ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes, ) ,
1930+ ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes) ,
19271931 Ok ( Section :: Borrowed ( 10 , 100 ..120 ) )
19281932 ) ;
19291933 }
@@ -1944,7 +1948,7 @@ mod test {
19441948 ( Some ( b".rodata" ) , & s3) ,
19451949 ] ;
19461950 let ro_section =
1947- ElfExecutable :: parse_ro_sections ( & config, & SBPFVersion :: V2 , sections, & elf_bytes)
1951+ ElfExecutable :: parse_ro_sections ( & config, & SBPFVersion :: V1 , sections, & elf_bytes)
19481952 . unwrap ( ) ;
19491953 let ro_region = get_ro_region ( & ro_section, & elf_bytes) ;
19501954 let owned_section = match & ro_section {
@@ -2026,7 +2030,7 @@ mod test {
20262030 ( Some ( b".rodata" ) , & s3) ,
20272031 ] ;
20282032 let ro_section =
2029- ElfExecutable :: parse_ro_sections ( & config, & SBPFVersion :: V2 , sections, & elf_bytes)
2033+ ElfExecutable :: parse_ro_sections ( & config, & SBPFVersion :: V1 , sections, & elf_bytes)
20302034 . unwrap ( ) ;
20312035 let owned_section = match & ro_section {
20322036 Section :: Owned ( _offset, data) => data. as_slice ( ) ,
@@ -2098,10 +2102,10 @@ mod test {
20982102 let config = Config :: default ( ) ;
20992103 let elf_bytes = [ 0u8 ; 512 ] ;
21002104
2101- let s1 = new_section ( 0 , 10 ) ;
2102- let s2 = new_section ( 20 , 10 ) ;
2103- let s3 = new_section ( 40 , 10 ) ;
2104- let s4 = new_section ( 50 , 10 ) ;
2105+ let s1 = new_section ( ebpf :: MM_PROGRAM_START + 0 , 10 ) ;
2106+ let s2 = new_section ( ebpf :: MM_PROGRAM_START + 20 , 10 ) ;
2107+ let s3 = new_section ( ebpf :: MM_PROGRAM_START + 40 , 10 ) ;
2108+ let s4 = new_section ( ebpf :: MM_PROGRAM_START + 50 , 10 ) ;
21052109
21062110 let sections: [ ( Option < & [ u8 ] > , & Elf64Shdr ) ; 4 ] = [
21072111 ( Some ( b".dynsym" ) , & s1) ,
@@ -2110,7 +2114,7 @@ mod test {
21102114 ( Some ( b".dynamic" ) , & s4) ,
21112115 ] ;
21122116 assert_eq ! (
2113- ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes, ) ,
2117+ ElfExecutable :: parse_ro_sections( & config, & SBPFVersion :: V2 , sections, & elf_bytes) ,
21142118 Ok ( Section :: Borrowed ( 20 , 20 ..50 ) )
21152119 ) ;
21162120 }
@@ -2120,9 +2124,9 @@ mod test {
21202124 let config = Config :: default ( ) ;
21212125 let elf_bytes = [ 0u8 ; 512 ] ;
21222126
2123- let s1 = new_section ( 0 , 10 ) ;
2124- let s2 = new_section ( 10 , 10 ) ;
2125- let s3 = new_section ( 10 , 10 ) ;
2127+ let s1 = new_section ( ebpf :: MM_PROGRAM_START + 0 , 10 ) ;
2128+ let s2 = new_section ( ebpf :: MM_PROGRAM_START + 10 , 10 ) ;
2129+ let s3 = new_section ( ebpf :: MM_PROGRAM_START + 20 , 10 ) ;
21262130
21272131 let sections: [ ( Option < & [ u8 ] > , & Elf64Shdr ) ; 3 ] = [
21282132 ( Some ( b".text" ) , & s1) ,
@@ -2134,28 +2138,28 @@ mod test {
21342138 . unwrap ( ) ;
21352139 let ro_region = get_ro_region ( & ro_section, & elf_bytes) ;
21362140
2137- // s1 starts at sh_addr =0 so [0..s2.sh_addr + s2.sh_size] is the valid
2138- // ro memory area
2141+ // s1 starts at sh_offset =0 so [0..s2.sh_offset + s2.sh_size]
2142+ // is the valid ro memory area
21392143 assert ! ( matches!(
2140- ro_region. vm_to_host( ebpf :: MM_PROGRAM_START , s2. sh_addr + s2. sh_size) ,
2144+ ro_region. vm_to_host( s1 . sh_addr , s2. sh_offset + s2. sh_size) ,
21412145 ProgramResult :: Ok ( ptr) if ptr == elf_bytes. as_ptr( ) as u64 ,
21422146 ) ) ;
21432147
21442148 // one byte past the ro section is not mappable
21452149 assert_error ! (
2146- ro_region. vm_to_host( ebpf :: MM_PROGRAM_START + s2 . sh_addr + s2 . sh_size , 1 ) ,
2150+ ro_region. vm_to_host( s3 . sh_addr, 1 ) ,
21472151 "InvalidVirtualAddress({})" ,
2148- ebpf :: MM_PROGRAM_START + s2 . sh_addr + s2 . sh_size
2152+ s3 . sh_addr
21492153 ) ;
21502154 }
21512155
21522156 #[ test]
21532157 fn test_borrowed_ro_region_initial_gap ( ) {
21542158 let config = Config :: default ( ) ;
21552159 let elf_bytes = [ 0u8 ; 512 ] ;
2156- let s1 = new_section ( 0 , 10 ) ;
2157- let s2 = new_section ( 10 , 10 ) ;
2158- let s3 = new_section ( 20 , 10 ) ;
2160+ let s1 = new_section ( ebpf :: MM_PROGRAM_START + 0 , 10 ) ;
2161+ let s2 = new_section ( ebpf :: MM_PROGRAM_START + 10 , 10 ) ;
2162+ let s3 = new_section ( ebpf :: MM_PROGRAM_START + 20 , 10 ) ;
21592163
21602164 let sections: [ ( Option < & [ u8 ] > , & Elf64Shdr ) ; 3 ] = [
21612165 ( Some ( b".dynamic" ) , & s1) ,
@@ -2171,32 +2175,32 @@ mod test {
21712175
21722176 // the low bound of the initial gap is not mappable
21732177 assert_error ! (
2174- ro_region. vm_to_host( ebpf :: MM_PROGRAM_START , 1 ) ,
2178+ ro_region. vm_to_host( s1 . sh_addr , 1 ) ,
21752179 "InvalidVirtualAddress({})" ,
2176- ebpf :: MM_PROGRAM_START
2180+ s1 . sh_addr
21772181 ) ;
21782182
21792183 // the hi bound of the initial gap is not mappable
21802184 assert_error ! (
2181- ro_region. vm_to_host( ebpf :: MM_PROGRAM_START + s2. sh_addr - 1 , 1 ) ,
2185+ ro_region. vm_to_host( s2. sh_addr - 1 , 1 ) ,
21822186 "InvalidVirtualAddress({})" ,
2183- ebpf :: MM_PROGRAM_START + 9
2187+ s2 . sh_addr - 1
21842188 ) ;
21852189
2186- // [s2.sh_addr ..s3.sh_addr + s3.sh_size] is the valid ro memory area
2190+ // [s2.sh_offset ..s3.sh_offset + s3.sh_size] is the valid ro memory area
21872191 assert ! ( matches!(
21882192 ro_region. vm_to_host(
2189- ebpf :: MM_PROGRAM_START + s2. sh_addr,
2190- s3. sh_addr + s3. sh_size - s2. sh_addr
2193+ s2. sh_addr,
2194+ s3. sh_offset + s3. sh_size - s2. sh_offset
21912195 ) ,
2192- ProgramResult :: Ok ( ptr) if ptr == elf_bytes[ s2. sh_addr as usize ..] . as_ptr( ) as u64 ,
2196+ ProgramResult :: Ok ( ptr) if ptr == elf_bytes[ s2. sh_offset as usize ..] . as_ptr( ) as u64 ,
21932197 ) ) ;
21942198
21952199 // one byte past the ro section is not mappable
21962200 assert_error ! (
2197- ro_region. vm_to_host( ebpf :: MM_PROGRAM_START + s3. sh_addr + s3. sh_size, 1 ) ,
2201+ ro_region. vm_to_host( s3. sh_addr + s3. sh_size, 1 ) ,
21982202 "InvalidVirtualAddress({})" ,
2199- ebpf :: MM_PROGRAM_START + s3. sh_addr + s3. sh_size
2203+ s3. sh_addr + s3. sh_size
22002204 ) ;
22012205 }
22022206
0 commit comments