From 968f523d9ce251a0355e4bf7ff558f399024d744 Mon Sep 17 00:00:00 2001 From: Antoine Busque Date: Fri, 15 Apr 2016 13:18:04 -0400 Subject: [PATCH] Tests: add tests for lttng_ust_elf_is_pic This adds tests for the lttng_ust_elf_is_pic function used to tell whether an executable is position independent code or not. Three sample executables are used, one non-PIC executable, one PIE, and a PIC shared object. Signed-off-by: Antoine Busque Signed-off-by: Mathieu Desnoyers --- tests/ust-elf/README.md | 10 +++++++++ tests/ust-elf/data/pic/hello.c | 8 ++++++++ tests/ust-elf/data/pic/hello.exec | Bin 0 -> 6728 bytes tests/ust-elf/data/pic/hello.pic | Bin 0 -> 6312 bytes tests/ust-elf/data/pic/hello.pie | Bin 0 -> 7408 bytes tests/ust-elf/data/pic/libhello.c | 6 ++++++ tests/ust-elf/prog.c | 33 +++++++++++++++++++++++++++++- 7 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/ust-elf/data/pic/hello.c create mode 100644 tests/ust-elf/data/pic/hello.exec create mode 100644 tests/ust-elf/data/pic/hello.pic create mode 100644 tests/ust-elf/data/pic/hello.pie create mode 100644 tests/ust-elf/data/pic/libhello.c diff --git a/tests/ust-elf/README.md b/tests/ust-elf/README.md index 787af8c7..0091876f 100644 --- a/tests/ust-elf/README.md +++ b/tests/ust-elf/README.md @@ -40,3 +40,13 @@ is added to the executable. The commands used are as follow: $ objcopy --only-keep-debug main.elf main.elf.debug $ strip -g main.elf $ objcopy --add-gnu-debuglink=main.elf.debug main.elf + +There is also a series of tests used to check detection of +position-independent code (PIC). These tests use three pre-compiled +ELF files found under `data/pic/`, namely `hello.exec`, `hello.pie`, +and `hello.pic`. These can be re-generated using the files `hello.c` +and `libhello.c`, with the following commands: + + $ gcc hello.c -o hello.exec + $ gcc hello.c -fPIC -pie -o hello.pie + $ gcc -shared -o hello.pic -fPIC libhello.c diff --git a/tests/ust-elf/data/pic/hello.c b/tests/ust-elf/data/pic/hello.c new file mode 100644 index 00000000..e89f5fb2 --- /dev/null +++ b/tests/ust-elf/data/pic/hello.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + puts("hello, world"); + + return 0; +} diff --git a/tests/ust-elf/data/pic/hello.exec b/tests/ust-elf/data/pic/hello.exec new file mode 100644 index 0000000000000000000000000000000000000000..9af50ea5d6314378147ed7534ffb06630189cfde GIT binary patch literal 6728 zcmcIoeQZJBc^PX7)1$ zHfdqds_6!cG$!?*N=U0FCQVZ+O&WhpBa{lVX`0eMn9zo_bQ)D?$C|1wLfeY>JNMn= z_`R47P1=$C&ON_-e&?S1_1?YyPFK%PBM=}FgT6>?&6}2j+$Yh?%ay6IZB$1Vt)phD z0aA^hrD|BaCDBtlwCFnRXF^wT+0cMtxdpA8T?;L^A{nyg_7=p_MOXid6M~2)3Mrd; zTs5WZr)pvf7u}-s*eLmE9-BCik1eee1PM)o{l&9!6Wf6%M-j~7CDQb`ZtQvLo6Y12z*0v|oiPm%~Q#jgsblcX} zt!?3aHoRHo&wkv+U3(8IhKxrD7PtKfems|4pQCS1-n~86^xAXRPMqnTSUJ1fu?oX;9NxlF481@D=mqyf#;vd>^$^!?(oLPOi>VUN0gs2sw8m=zFkfV*4A)CU zRYgz!tuFe#>ZR2oioQ7IRL^Y(96k9~-DQ_q++v`rxS#l|t=S@E!#}EE zb5plwl>H{`p$V5wwCQ&D_}hn~<9~^s{OkPwzV542hq1z%USR1{C;6 zwRCZ&h7;RCh3fZz2wo}JhfFBW^xrxT4wo;ULJ#MDcduAfKAb4}7x_x$*~oY?axl`@ zb9%$i10f`Px|Q{T?(sX_K}4RZ9w+fHcHpQL|1bx@{>Y(-eR*PGJU+ras(5-z16V^N$#gpV`Opj5Tspyn7-%xL zZ-lPKuNh5#8~Sa^jpw3pqF5_c^|R_j`vdFXk(yhNp0Okptg~5euvh9 z|2R8nsq9a*9`)@1>Zipb=Rm>6oxxSR8|q)E8>1a-wm<&ldfsZvrxW@oh`W-3NU-rG zqho2!el!e|>k{~#LwypO+JlWhFxrEw&X`@n(CMo7V9WQ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ePdSg;7^OQk60Dyo6zaq^%4p9RKu4B^ij7A(R!RH@QE z#kCfX-yu}+9YOeVPF<@nF4CUlFMguG0Y2Y3#Fq1Hi{^)P!~Xm=Ovh2+F%`a?d!w2! z_Zn$m@)7)$=C|vBG1F3|nC8ocf%(kw^Una|apnA2CNzIS^X2~N*1EY$ne{k+<@`y_ zpY*jaoJL&`eb&dH)%;n_mwbiqw_o$|<-0ns`QPQ_%0$`~=*K9O=P&1cM-RB%FU8-# z{(q|ZGJj+G95AN&%Y>-4pZ|uBFZTxdAGPhj@GtuKa=)0??SJg!`^WD$sK@C=?ICuR zbK#U;5PpBZeGQz3GQQkbrXP_14!Au2db&#YnwbaWzY8wA3SaI&^1nBqt5|fn()yQm z@G9!c+dr?*Yv*-G{rweN^ann^+@~hBLkl-vCc+ba7j@wNfX-B`m9uA?C34pPUCB!WV-@kr^FPiJ6?eY-c(nS7`uUvelYFqYBxgY)lP!iJ< aw-y%XDE*Q5AG=j*KgNp^(&P*7Z~xy}wIF@~ literal 0 HcmV?d00001 diff --git a/tests/ust-elf/data/pic/hello.pic b/tests/ust-elf/data/pic/hello.pic new file mode 100644 index 0000000000000000000000000000000000000000..a9e3f474b63a702c02e8564d6b759257bf1ab96c GIT binary patch literal 6312 zcmcgwU2Ggz6+XM`pLOHd+a_t2q$r~(5iXFC`{T;cgsguviJa6p4k{96I`*!;TlOdH z&Zc(wi)tup+DK`6f(WUqiq!Iekaz(hfvccAz)LEqC~r}ql3f*%sv zII4KyTH|xicg}b2o%=WE?)Szf#^bRVp(N-_B-%6W0g};%{QbJZB141J!T&!^`;@PC z-Es)kx;p~}lE_kkw9&y&O50PN0S?(0S8mj=C3vC=uTS&(G!Jq`zY1hB2t#cw<+-Zu zraYwmAjMb8Mu*95%Qq`CP?tU%sfre9`_b0Z5IA+eiHngICG9ZcyHzQwcSUL z-dZ?2(S7Y3&&Th(dqiBtF7JkMe0jx6P+O);u>1Jw=4W{9-4B2J_gBCD-Z!6q=r>nB z>iOW$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=I?=6f8d+#zh&@|pF(Cq)Y2TkUoqap57rm*Sp0r!`kVUi znJYj1fscGQt@9<*-_-xaTp1Jakr!K^kbjf8vUb5o{*2#oSM-2m+|WMO!ShD@$g@|q zV;|eY1$@ZAu`%3#)=7&Hu%Wf`!yea2xIE2Iwsy9`P1Mwd~^N%Q@7ub z2DAh}^z*#3fDb8lv>gsn)&qPu400Vjz@lUC024W>`~S2)4`d%4;Ro-Jb4}c$+J93P MHJ2MkgN91~1wPq_YybcN literal 0 HcmV?d00001 diff --git a/tests/ust-elf/data/pic/hello.pie b/tests/ust-elf/data/pic/hello.pie new file mode 100644 index 0000000000000000000000000000000000000000..384795f470463f26d74be80a41492e5e7a4ebaaa GIT binary patch literal 7408 zcmcgxeQZd174WuW+0Qhf zYHOReBBfzy+N5sMq;;z%ZK^6NRokTfu}~1|CT*fpnY0aQbX}*wS`E||1$B*g&VA>X zJl{BD(xhD}KKK02$36Gn^X|Ln<(uvC-Bp2rLJO+TE9P$2cu2Ddwk($pk!IDTLiD>v ztyc4hXVqr<o3>I4RIdeL2j6j%+<$Yv zr(n`Xu>*zO7s*b~Pvmza5mC4=lkVNwcwZ*DIg`#7hc^#5?cBVxFtPYcV=tpW-bN?VymVOqQ_U84LyRq= zKW;7USKj^A+uwfdk6-V`c6$QjH%JzzjLq*L9m~*TmVGa+fUlVYPZ4fa(0Av+PtAez zdabSCXDeY5wNiC2vdW}w8gEcX4ZKNkQ(w`3_3`%XRL6>LK{-y}aKha+?bXGa3WWr65ptn%)aDuW)<#crIbCRiiY9L*3Q~9oat(jal)s^VYu$O_M zTvog|%GrC|=@h-zOrlUo(YS10(Rfe1qovi^7Tp%zAv3M=J>&t(1Wo`@fPwg6K2uPpOa(7uQ-p;=|?9)OfuQ z$Ji`r5}bAU89;0H;dpjgW{e%<~dozpVKN;``5*)^E~u>w6ueZyd2l|7wr??dHL*_*l))NY@@) zyj_!BwL~UGxdV zsQROIYo5{pll?7L8ryM$2-#1u{rRb&J$lnVKk=X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I zr>Mgi=8yYgx7eHdvK%m1K>`@tGL3j0*qiwadr+2RY`=>&%RG+1CHkAe>ap?c`1OuS e{KK+wpClp2i230B<20!>{)7bn8N<0b{=Wghmq?=k literal 0 HcmV?d00001 diff --git a/tests/ust-elf/data/pic/libhello.c b/tests/ust-elf/data/pic/libhello.c new file mode 100644 index 00000000..d3f02e88 --- /dev/null +++ b/tests/ust-elf/data/pic/libhello.c @@ -0,0 +1,6 @@ +#include + +void hello() +{ + puts("hello, world"); +} diff --git a/tests/ust-elf/prog.c b/tests/ust-elf/prog.c index 85b9c347..305f2349 100644 --- a/tests/ust-elf/prog.c +++ b/tests/ust-elf/prog.c @@ -26,7 +26,8 @@ #define NUM_ARCH 4 #define NUM_TESTS_PER_ARCH 11 -#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + 1 +#define NUM_TESTS_PIC 3 +#define NUM_TESTS (NUM_ARCH * NUM_TESTS_PER_ARCH) + NUM_TESTS_PIC + 1 /* * Expected memsz were computed using libelf, build ID and debug link @@ -115,6 +116,35 @@ void test_elf(const char *test_dir, const char *arch, uint64_t exp_memsz, lttng_ust_elf_destroy(elf); } +static +void test_pic(const char *test_dir) +{ + char exec_path[PATH_MAX]; + char pie_path[PATH_MAX]; + char pic_path[PATH_MAX]; + struct lttng_ust_elf *elf = NULL; + uint8_t is_pic; + + snprintf(exec_path, PATH_MAX, "%s/data/pic/hello.exec", test_dir); + snprintf(pie_path, PATH_MAX, "%s/data/pic/hello.pie", test_dir); + snprintf(pic_path, PATH_MAX, "%s/data/pic/hello.pic", test_dir); + + elf = lttng_ust_elf_create(exec_path); + is_pic = lttng_ust_elf_is_pic(elf); + ok(is_pic == 0, "hello.exec is not PIC"); + lttng_ust_elf_destroy(elf); + + elf = lttng_ust_elf_create(pie_path); + is_pic = lttng_ust_elf_is_pic(elf); + ok(is_pic == 1, "hello.pie is PIC"); + lttng_ust_elf_destroy(elf); + + elf = lttng_ust_elf_create(pic_path); + is_pic = lttng_ust_elf_is_pic(elf); + ok(is_pic == 1, "hello.pic is PIC"); + lttng_ust_elf_destroy(elf); +} + int main(int argc, char **argv) { const char *test_dir; @@ -133,6 +163,7 @@ int main(int argc, char **argv) test_elf(test_dir, "armeb", ARMEB_MEMSZ, armeb_build_id, ARMEB_CRC); test_elf(test_dir, "aarch64_be", AARCH64_BE_MEMSZ, aarch64_be_build_id, AARCH64_BE_CRC); + test_pic(test_dir); return EXIT_SUCCESS; } -- 2.34.1