From b95cf271975c171cde1ae5e5a7992138612028f8 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Fri, 31 Jan 2025 11:40:42 -0300 Subject: [PATCH] Add SbpfV3 elf tests --- tests/elfs/relative_call.so | Bin 0 -> 2064 bytes tests/elfs/reloc_64_64.so | Bin 0 -> 1616 bytes tests/elfs/reloc_64_relative.so | Bin 0 -> 1696 bytes tests/elfs/reloc_64_relative_data.so | Bin 0 -> 1712 bytes tests/elfs/rodata_section.so | Bin 0 -> 1704 bytes tests/elfs/struct_func_pointer.so | Bin 0 -> 1624 bytes tests/elfs/syscall_static.so | Bin 0 -> 1648 bytes tests/execution.rs | 121 ++++++++++++++++++++++++--- 8 files changed, 111 insertions(+), 10 deletions(-) create mode 100755 tests/elfs/relative_call.so create mode 100755 tests/elfs/reloc_64_64.so create mode 100755 tests/elfs/reloc_64_relative.so create mode 100755 tests/elfs/reloc_64_relative_data.so create mode 100755 tests/elfs/rodata_section.so create mode 100755 tests/elfs/struct_func_pointer.so create mode 100755 tests/elfs/syscall_static.so diff --git a/tests/elfs/relative_call.so b/tests/elfs/relative_call.so new file mode 100755 index 0000000000000000000000000000000000000000..f3bfa907ef8c182713dae1102e41216a0770256c GIT binary patch literal 2064 zcmd5-zi$&U6n4|nlrl78K!_;=LJZ|}m$XeHF=R<6REZ6-M84dGq$HPeaZn>6F)^@3 z2r+?5R|a;bWMO9IA3$PdVTJeX_Y$AEN=Pg`$^QJl_sc%NIL}-64hw5*f@NJ4wN@sg zNjSS?7V7{_Q5TyqZ;LH~kvL|n6vmO}nS)!Tqx%Moa)Af{PJzgNF5ds9qD`>hw&Hln zs56%IoXP3N;aN}^wL)=RS937g^?rdMkGQ}E$U@JneUmC)Ws)m0CwvB2qMNYq@g?)LZ*mu}HIE&Xy}$RMm$0+I zKlAC^508Rs8V{uEKaJZRIT!@>?j-4`{wQgWmFzrjr*Su^^{U;j41-G0k%4T;J=qdD zp_NtWWC%g~epss=gpDW+f~es~EfFUwot}*Pi4v3^ilj>=P9o9n#qtD=Nb5DSBC|

Z3^f5I{3H++P*cLPLg6Dskp^fG5~4=Y?q>N2lAz5X5gIr`Ef>TgY zasduNMG1bhc9;wiLSSXao@c-H*fW#c?Y*7C&=9E%Q^{EoRmAZ;oEuOT+MpuO<1|WO zVsySfkq62=>*-NhBYcEM*WsxNt#Q6Scr~>m`yVuz?E(*oYP*=Wv)vmkg!?=xiBwyL z@HDT*cyjZ7umFedNPudechk2i33k+zd`HW96yLk(dnfiM6a1N8wM5$YNyew8LU${3 zigUd#zF={j4E6OTRGj1c`js|J*1l|>H4If-CQ8mRXT3$*s8RJajV=|oHc-mai)KyQ2(|R_xiVM z@$daRRNU`hVA5+tLDsBACZZs2wF2A5+(fr1sI?odcAeOfnJ8x6uIKtx5k#1bbNpA( z9}K(|SM+`By;7fl-%IQO-@Q{<>pS=3;d5n*7l?8BuzT;1`{#x7MS5S!As5eefEh{{u()vN!wtFSEzZt${BBb_Kxs0P^$WUJlBC aC*Z#DRr&dMgs*!*y{Nw;z@`#K`}|L04S;I^ literal 0 HcmV?d00001 diff --git a/tests/elfs/reloc_64_relative.so b/tests/elfs/reloc_64_relative.so new file mode 100755 index 0000000000000000000000000000000000000000..41d93a38c6f83b4db5dec90eb203592e02018c11 GIT binary patch literal 1696 zcmbtUJ#Q2-5cMTd5&{w@DxjjELSSW&yUQJ-NJB&sAtBMPcGnJxNcN&V2e>LysFCgu z(DMT*D5tsHUQ4`PigA6$QoC)Gn-NnM9m>jv!JdUjM1LwYz2lkF*Tf1g34pq+i~lx`-baD~$4bgF$hLKmm5w3*e@*@>b=?HU@LBMbg z&GEm6Gr@$Ks5b{6>f}P!QdG8=W^Io{Oh_6bU(mz1^ zlotL0cK#c$5o;?;{l4899M=m8WMFq@zWrurc5ZJ^cK7=8b8}*{Aj{4Z+0ljGVloz> zcI1gHVZJUa!bpw}f44|Por_Gbs82e#{Osb)r0yxtw8g#p9)?R;n;9AQymC!PcP)WN zqt4@a7K-WUXq@GX7$%?ZH-tF#nG#Irao_$aO36A%?9m1+{|!5hwPFsB&}r8;QpV}qkj^!&bDabJ|% zDeTRn{EYX&6Yjw2>g#>rU47jI-qqJ_!?V6VkNE$6eQ9{!*D^HkgOu%<_e9G5$)t>( zO(q*C<#0NhOpgQ$ZK(I5r$jeuzH#th^kVNv;X&AT{|sJ+!T5&;sBiB` z&*3!l_?wFPyT^Mw!N3 literal 0 HcmV?d00001 diff --git a/tests/elfs/rodata_section.so b/tests/elfs/rodata_section.so new file mode 100755 index 0000000000000000000000000000000000000000..bd4e3bb045b701b2c2c3c6e0898c0384c4175c27 GIT binary patch literal 1704 zcmbtUy>1gh5FRH{5`H8iszBu;5(vR5#&)9c4y3bu@F*5+MU^Nzi(%E=WgHcA08~VTBNp2Yu<=xPbO}! zxL}#x9>uiE{bgDwDA6W=mT*Mr+ksz~G3Z$GtEbZtenWWbRrlb-RLNwS|0@{f3;_-`WkuIiHVjS=<%hzo%`*neXFNR;C`GjE*ZqNn_IdE&6&k zD64{!L7AQ?Y-6BQkBZc!NmJ(LcvM9@@ot`G_v237&3do%?!ElYyJ(w|mq(r6JhlJg z@nQ0$^I%Y9{a&2)ce;6}pLcij{R`}d&$q~MuPgM+*59AM#ew^P&}+dWXe-Rk-I61vZje_Ib4War>GcL&3H&WQJXYlKOQD|?gS10pl|waw0)3cy+4K5IDp>&HZj)sgO5LFq5c}r0`v8Vr&t6QETFKPB)Z8# zsKcai^c*4)EyiHkj qDgf#OD9ryzaG}lI578gZ5Eq2`4;Thj_ki=_{7(gV=w!1#)Sm;D&XWWH literal 0 HcmV?d00001 diff --git a/tests/elfs/struct_func_pointer.so b/tests/elfs/struct_func_pointer.so new file mode 100755 index 0000000000000000000000000000000000000000..0415993e0d5848846fd07af115034f6024e6f191 GIT binary patch literal 1624 zcmbtUJ5B>Z40S#v;Uj^lfSM9khy=(YiWESFK-3h`vROVtEYa>nA~dv=xd44i3QoZZ zC}}tV6(#u1j>BY#5CTgxwx9jtv3Dl7JA1qNT#iHvRJJ^#h6y|uG6SkXbt>^bK@|cM ztM~O2S)$qJ99=O!(u=(M7EerQ$#eDLt7?@v{vgS0=XpUyYiCC9`fRX}?($4aBw8~J zPwHxnH@n^k3vlS3Nf7PwZiWtJk}dUQen;AQWWQ1_jlHuj7V^1!EB7Z8{MkXbL^Ag& z#%D~2_EF{q=YDN`QRDbBG&GkL!8w0uUQw))Zu{yyR@6*Y5ME$U22K>WsCgCzZGp87 zlv0&+Le)I!#9>qaK$Li)@B6EDzgAset41gO>T-D6@mA>XMRX%0Hq5I?Z~8mh;U#uP z&2fYAQFGiD+?nHn#=p<;NN{(Ko{Fv&dC65PPyu8=7WVdV~f0VxnB}@~ literal 0 HcmV?d00001 diff --git a/tests/elfs/syscall_static.so b/tests/elfs/syscall_static.so new file mode 100755 index 0000000000000000000000000000000000000000..fc4de61b41fc9e7f4a15fb3eafe02b05574c160a GIT binary patch literal 1648 zcmbtU&r4fD5T0m=R)q#nwr8OieWXnST7>3e4pIa!#hZP}dp04z` z1<;@^D)GETi-bsw@%~LN24WCX9D(EopgbyB3>(uWjVQ9;uGb$4=%rMc~nAE{lb1_RLK5QeT;`D z$xcSyp`oMHgzX;YWU`2d5gm2n@I+~=gQYY}+hM;CgXy+|?P{x5snw&}@$>3dJ&HHm z@7@O$diehK41b%@uOeOB@9X+JYNSgfA;Z>@_ru!6MrxiB%>%a zAqA~84QxAe6Y%yko96QRu!z7&Je+B))z*}-f-#_kyEB<>}U}JtK z@4c|T#KhwJ3vJ&3?2pCAy_?V{@00uIipDGVlojM~<8g2QHGZ9am%VuW2Q+@nHpn9$ z$gSsD1Ie<%W9pq)mPr2-Z)L6E*I#3UTf6%G67pOKd>>GL{vGCK<^L??zQil|`I{22 RHQ>BB|Dg~sHBiux{|%{giq`-D literal 0 HcmV?d00001 diff --git a/tests/execution.rs b/tests/execution.rs index 0a0c622c..7b69c1ed 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -2124,7 +2124,7 @@ fn test_err_mem_access_out_of_bound() { // CALL_IMM & CALL_REG : Procedure Calls #[test] -fn test_relative_call() { +fn test_relative_call_sbpfv0() { let config = Config { enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0, ..Config::default() @@ -2139,6 +2139,22 @@ fn test_relative_call() { ); } +#[test] +fn test_relative_call_sbpfv3() { + let config = Config { + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, + ..Config::default() + }; + test_interpreter_and_jit_elf!( + "tests/elfs/relative_call.so", + config, + [1], + (), + TestContextObject::new(18), + ProgramResult::Ok(3), + ); +} + #[test] fn test_bpf_to_bpf_scratch_registers() { test_interpreter_and_jit_asm!( @@ -2828,6 +2844,24 @@ fn test_err_call_unresolved() { ); } +#[test] +fn test_syscall_static() { + let config = Config { + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, + ..Config::default() + }; + test_interpreter_and_jit_elf!( + "tests/elfs/syscall_static.so", + config, + [], + ( + "log" => syscalls::SyscallString::vm, + ), + TestContextObject::new(6), + ProgramResult::Ok(0), + ); +} + #[test] fn test_syscall_reloc_64_32() { let config = Config { @@ -2860,6 +2894,19 @@ fn test_reloc_64_64_sbpfv0() { ); } +#[test] +fn test_reloc_64_64() { + // Tests the correctness of link-time R_BPF_64_64 relocations. The program returns the + // address of the entrypoint. + test_interpreter_and_jit_elf!( + "tests/elfs/reloc_64_64.so", + [], + (), + TestContextObject::new(3), + ProgramResult::Ok(ebpf::MM_BYTECODE_START), + ); +} + #[test] fn test_reloc_64_relative_sbpfv0() { // Tests the correctness of R_BPF_64_RELATIVE relocations. The program @@ -2881,23 +2928,41 @@ fn test_reloc_64_relative_sbpfv0() { } #[test] -fn test_reloc_64_relative_data_sbfv1() { - // Tests the correctness of R_BPF_64_RELATIVE relocations in sections other +fn test_reloc_64_relative() { + // Tests the correctness of link-time R_BPF_64_RELATIVE relocations. The program + // returns the address of the first .rodata byte. + let config = Config { + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, + ..Config::default() + }; + test_interpreter_and_jit_elf!( + "tests/elfs/reloc_64_relative.so", + config, + [], + (), + TestContextObject::new(3), + ProgramResult::Ok(ebpf::MM_RODATA_START), + ); +} + +#[test] +fn test_reloc_64_relative_data() { + // Tests the correctness of link-time R_BPF_64_RELATIVE relocations in sections other // than .text. The program returns the address of the first .rodata byte. - // [ 1] .text PROGBITS 0000000000000120 000120 000020 00 AX 0 0 8 - // [ 2] .rodata PROGBITS 0000000000000140 000140 000019 01 AMS 0 0 1 + // [ 1] .text PROGBITS 0000000000000000 000190 000020 00 AX 0 0 8 + // [ 2] .rodata PROGBITS 0000000100000000 0001b0 000030 00 WAMS 0 0 8 // let config = Config { - enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0, + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, ..Config::default() }; test_interpreter_and_jit_elf!( - "tests/elfs/reloc_64_relative_data_sbpfv0.so", + "tests/elfs/reloc_64_relative_data.so", config, [], (), - TestContextObject::new(3), - ProgramResult::Ok(ebpf::MM_RODATA_START + 0x140), + TestContextObject::new(4), + ProgramResult::Ok(ebpf::MM_RODATA_START), ); } @@ -2946,7 +3011,24 @@ fn test_load_elf_rodata_sbpfv0() { } #[test] -fn test_struct_func_pointer() { +fn test_load_elf_rodata() { + let config = Config { + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, + optimize_rodata: false, + ..Config::default() + }; + test_interpreter_and_jit_elf!( + "tests/elfs/rodata_section.so", + config, + [], + (), + TestContextObject::new(4), + ProgramResult::Ok(42), + ); +} + +#[test] +fn test_struct_func_pointer_sbpfv0() { // This tests checks that a struct field adjacent to another field // which is a relocatable function pointer is not overwritten when // the function pointer is relocated at load time. @@ -2975,6 +3057,25 @@ fn test_strict_header() { ); } +#[test] +fn test_struct_func_pointer() { + // This tests checks that a struct field adjacent to another field + // which is a relocatable function pointer is not overwritten when + // the function pointer is relocated at load time. + let config = Config { + enabled_sbpf_versions: SBPFVersion::V3..=SBPFVersion::V3, + ..Config::default() + }; + test_interpreter_and_jit_elf!( + "tests/elfs/struct_func_pointer.so", + config, + [], + (), + TestContextObject::new(3), + ProgramResult::Ok(0x102030405060708), + ); +} + // Programs #[test]