diff options
author | Domen Kožar <domen@dev.si> | 2021-08-05 17:59:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-05 17:59:01 +0200 |
commit | eed0a903c52aa75c19b6e74fe7d8badccb530b5b (patch) | |
tree | 996a6d893c1815387ee3c5b574be6a3e1ea0d2fd /tests | |
parent | a89d508130a7c34539e8754b729f68c7658c9a19 (diff) | |
parent | a949ff23315bbb5863627c4655fe216ecbf341a2 (diff) | |
download | patchelf-eed0a903c52aa75c19b6e74fe7d8badccb530b5b.tar.gz |
Merge branch 'master' into multiple
Diffstat (limited to 'tests')
22 files changed, 247 insertions, 5 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 47947a1..eb77313 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ LIBS = -check_PROGRAMS = simple main too-many-strtab main-scoped big-dynstr no-rpath +check_PROGRAMS = simple main too-many-strtab main-scoped big-dynstr no-rpath contiguous_note_sections no_rpath_arch_TESTS = \ no-rpath-amd64.sh \ @@ -20,20 +20,24 @@ no_rpath_arch_TESTS = \ src_TESTS = \ plain-fail.sh plain-run.sh shrink-rpath.sh set-interpreter-short.sh \ - set-interpreter-long.sh set-rpath.sh no-rpath.sh big-dynstr.sh \ + set-interpreter-long.sh set-rpath.sh add-rpath.sh no-rpath.sh big-dynstr.sh \ set-rpath-library.sh soname.sh shrink-rpath-with-allowed-prefixes.sh \ force-rpath.sh \ plain-needed.sh \ output-flag.sh \ + too-many-strtab.sh \ + no-rpath-pie-powerpc.sh \ build-id.sh \ - too-many-strtab.sh + invalid-elf.sh \ + endianness.sh \ + contiguous_note_sections.sh build_TESTS = \ $(no_rpath_arch_TESTS) TESTS = $(src_TESTS) $(build_TESTS) -EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh +EXTRA_DIST = no-rpath-prebuild $(src_TESTS) no-rpath-prebuild.sh invalid-elf endianness TESTS_ENVIRONMENT = PATCHELF_DEBUG=1 @@ -108,3 +112,7 @@ libtoomanystrtab_so_LDFLAGS = $(LDFLAGS_sharedlib) no_rpath_SOURCES = no-rpath.c # no -fpic for no-rpath.o no_rpath_CFLAGS = + +contiguous_note_sections_SOURCES = contiguous_note_sections.s contiguous_note_sections.ld +contiguous_note_sections_LDFLAGS = -nostdlib -T contiguous_note_sections.ld +contiguous_note_sections_CFLAGS = -pie diff --git a/tests/PR243-reproducer.sh b/tests/PR243-reproducer.sh new file mode 100755 index 0000000..5fa4d55 --- /dev/null +++ b/tests/PR243-reproducer.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -ex +# PR243-reproducer.sh +curl -OLf https://github.com/NixOS/patchelf/files/6501509/ld-linux-x86-64.so.2.tar.gz +curl -OLf https://github.com/NixOS/patchelf/files/6501457/repro.tar.gz +tar fx repro.tar.gz +tar fx ld-linux-x86-64.so.2.tar.gz +chmod +x repro +cp repro repro.orig +../src/patchelf --set-interpreter ./ld-linux-x86-64.so.2 ./repro +patchelf --print-interpreter repro.orig +readelf -a repro > /dev/null +./repro diff --git a/tests/add-rpath.sh b/tests/add-rpath.sh new file mode 100755 index 0000000..3368708 --- /dev/null +++ b/tests/add-rpath.sh @@ -0,0 +1,26 @@ +#! /bin/sh -e +SCRATCH=scratch/$(basename $0 .sh) + +rm -rf ${SCRATCH} +mkdir -p ${SCRATCH} +mkdir -p ${SCRATCH}/libsA +mkdir -p ${SCRATCH}/libsB + +cp main ${SCRATCH}/ +cp libfoo.so ${SCRATCH}/libsA/ +cp libbar.so ${SCRATCH}/libsB/ + +../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsA ${SCRATCH}/main +../src/patchelf --force-rpath --add-rpath $(pwd)/${SCRATCH}/libsB ${SCRATCH}/main + +if test "$(uname)" = FreeBSD; then + export LD_LIBRARY_PATH=$(pwd)/${SCRATCH}/libsB +fi + +exitCode=0 +(cd ${SCRATCH} && ./main) || exitCode=$? + +if test "$exitCode" != 46; then + echo "bad exit code!" + exit 1 +fi diff --git a/tests/contiguous_note_sections.ld b/tests/contiguous_note_sections.ld new file mode 100644 index 0000000..230b8dc --- /dev/null +++ b/tests/contiguous_note_sections.ld @@ -0,0 +1,24 @@ +PHDRS +{ + headers PT_PHDR PHDRS ; + notes PT_NOTE; + text PT_LOAD FILEHDR PHDRS ; + data PT_LOAD ; + interp PT_INTERP ; + dynamic PT_DYNAMIC ; +} + +SECTIONS +{ + . = SIZEOF_HEADERS; + . = ALIGN(4); + + .note.my-section0 : { *(.note.my-section0) } :notes :text + .note.my-section1 : { *(.note.my-section1) } :notes :text + + .interp : { *(.interp) } :text :interp + .text : { *(.text) } :text + .rodata : { *(.rodata) } /* defaults to :text */ + + .data : { *(.data) } :data +} diff --git a/tests/contiguous_note_sections.s b/tests/contiguous_note_sections.s new file mode 100644 index 0000000..87f6044 --- /dev/null +++ b/tests/contiguous_note_sections.s @@ -0,0 +1,23 @@ +/* + * Testcase for error: + * patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections + */ +.section ".note.my-section0", "a", @note + .align 4 + .long 1f - 0f /* name length (not including padding) */ + .long 3f - 2f /* desc length (not including padding) */ + .long 1 /* type = NT_VERSION */ +0: .asciz "my-version-12345" /* name */ +1: .align 4 +2: .long 1 /* desc - toolchain version number, 32-bit LE */ +3: .align 4 + +.section ".note.my-section1", "a", @note + .align 8 + .long 1f - 0f /* name length (not including padding) */ + .long 3f - 2f /* desc length (not including padding) */ + .long 1 /* type = NT_VERSION */ +0: .asciz "my-version-1" /* name */ +1: .align 4 +2: .long 1 /* desc - toolchain version number, 32-bit LE */ +3: .align 4 diff --git a/tests/contiguous_note_sections.sh b/tests/contiguous_note_sections.sh new file mode 100755 index 0000000..8bc8f3c --- /dev/null +++ b/tests/contiguous_note_sections.sh @@ -0,0 +1,6 @@ +#! /bin/sh -e + +# Running --set-interpreter on this binary should not produce the following +# error: +# patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections +../src/patchelf --set-interpreter ld-linux-x86-64.so.2 contiguous_note_sections diff --git a/tests/endianness.sh b/tests/endianness.sh new file mode 100755 index 0000000..23b4aea --- /dev/null +++ b/tests/endianness.sh @@ -0,0 +1,24 @@ +#! /bin/sh -e +SCRATCH=scratch/$(basename $0 .sh) +PATCHELF="../src/patchelf" + +for arch in ppc64 ppc64le; do + rm -rf ${SCRATCH} + mkdir -p ${SCRATCH} + + cp endianness/${arch}/main endianness/${arch}/libtest.so ${SCRATCH}/ + + rpath="${PWD}/${SCRATCH}" + + # set rpath to scratch dir + ${PATCHELF} --output ${SCRATCH}/main-rpath --set-rpath $rpath ${SCRATCH}/main + + # attempt to shrink rpath, should not result in empty rpath + ${PATCHELF} --output ${SCRATCH}/main-shrunk --shrink-rpath --debug ${SCRATCH}/main-rpath + + # check whether rpath is still present + if ! ${PATCHELF} --print-rpath ${SCRATCH}/main-shrunk | grep -q "$rpath"; then + echo "rpath was removed for ${arch}" + exit 1 + fi +done diff --git a/tests/endianness/Makefile b/tests/endianness/Makefile new file mode 100644 index 0000000..914e363 --- /dev/null +++ b/tests/endianness/Makefile @@ -0,0 +1,3 @@ +all: + $(CC) -xc -nostdlib -shared -o libtest.so - <<< 'void test() {}' + $(CC) -xc -nostdlib -L. -ltest -o main - <<< 'void _start() {}' diff --git a/tests/endianness/ppc64/libtest.so b/tests/endianness/ppc64/libtest.so Binary files differnew file mode 100755 index 0000000..3134078 --- /dev/null +++ b/tests/endianness/ppc64/libtest.so diff --git a/tests/endianness/ppc64/main b/tests/endianness/ppc64/main Binary files differnew file mode 100755 index 0000000..c7c8869 --- /dev/null +++ b/tests/endianness/ppc64/main diff --git a/tests/endianness/ppc64le/libtest.so b/tests/endianness/ppc64le/libtest.so Binary files differnew file mode 100755 index 0000000..07d7611 --- /dev/null +++ b/tests/endianness/ppc64le/libtest.so diff --git a/tests/endianness/ppc64le/main b/tests/endianness/ppc64le/main Binary files differnew file mode 100755 index 0000000..386c3be --- /dev/null +++ b/tests/endianness/ppc64le/main diff --git a/tests/invalid-elf.sh b/tests/invalid-elf.sh new file mode 100755 index 0000000..0f020e8 --- /dev/null +++ b/tests/invalid-elf.sh @@ -0,0 +1,40 @@ +#! /bin/sh -u + +# Usage: killed_by_signal $? +# +# Returns true if the exit code indicates that the program was killed +# by a signal. This works because the exit code of processes that were +# killed by a signal is 128 plus the signal number. +killed_by_signal() { + [ $1 -ge 128 ] +} + + +# The directory containing all our input files. +TEST_DIR=$(dirname $(readlink -f $0))/invalid-elf + +# Each test case is listed here. The names should roughly indicate +# what makes the given ELF file invalid. +TEST_CASES="invalid-shrstrtab-idx invalid-shrstrtab-size invalid-shrstrtab-zero + invalid-shrstrtab-nonterm invalid-shdr-name invalid-phdr-offset" + +FAILED_TESTS="" + +for tcase in $TEST_CASES; do + if [ ! -r "$TEST_DIR/$tcase" ]; then + echo "Cannot read test case: $tcase" + exit 1 + fi + + ../src/patchelf --output /dev/null "$TEST_DIR/$tcase" + if killed_by_signal $?; then + FAILED_TESTS="$FAILED_TESTS $tcase" + fi +done + +if [ -z "$FAILED_TESTS" ]; then + exit 0 +else + echo "Failed tests: $FAILED_TESTS" + exit 1 +fi diff --git a/tests/invalid-elf/invalid-phdr-offset b/tests/invalid-elf/invalid-phdr-offset Binary files differnew file mode 100644 index 0000000..b6ffdb9 --- /dev/null +++ b/tests/invalid-elf/invalid-phdr-offset diff --git a/tests/invalid-elf/invalid-shdr-name b/tests/invalid-elf/invalid-shdr-name Binary files differnew file mode 100644 index 0000000..394f13b --- /dev/null +++ b/tests/invalid-elf/invalid-shdr-name diff --git a/tests/invalid-elf/invalid-shrstrtab-idx b/tests/invalid-elf/invalid-shrstrtab-idx Binary files differnew file mode 100644 index 0000000..0face93 --- /dev/null +++ b/tests/invalid-elf/invalid-shrstrtab-idx diff --git a/tests/invalid-elf/invalid-shrstrtab-nonterm b/tests/invalid-elf/invalid-shrstrtab-nonterm Binary files differnew file mode 100644 index 0000000..3243acd --- /dev/null +++ b/tests/invalid-elf/invalid-shrstrtab-nonterm diff --git a/tests/invalid-elf/invalid-shrstrtab-size b/tests/invalid-elf/invalid-shrstrtab-size Binary files differnew file mode 100644 index 0000000..6b777ab --- /dev/null +++ b/tests/invalid-elf/invalid-shrstrtab-size diff --git a/tests/invalid-elf/invalid-shrstrtab-zero b/tests/invalid-elf/invalid-shrstrtab-zero Binary files differnew file mode 100644 index 0000000..0b73ae3 --- /dev/null +++ b/tests/invalid-elf/invalid-shrstrtab-zero diff --git a/tests/no-rpath-pie-powerpc.sh b/tests/no-rpath-pie-powerpc.sh new file mode 100755 index 0000000..5bdb785 --- /dev/null +++ b/tests/no-rpath-pie-powerpc.sh @@ -0,0 +1,54 @@ +#! /bin/sh -e +set -x +SCRATCH=scratch/no-rpath-pie-powerpc + +no_rpath_bin="${srcdir}/no-rpath-prebuild/no-rpath-pie-powerpc" + +if [ ! -f $no_rpath_bin ]; then + echo "no 'no-rpath' binary for '$ARCH' in '${srcdir}/no-rpath-prebuild'" + exit 1 +fi + +rm -rf ${SCRATCH} +mkdir -p ${SCRATCH} + +cp $no_rpath_bin ${SCRATCH}/no-rpath + +oldRPath=$(../src/patchelf --print-rpath ${SCRATCH}/no-rpath) +if test -n "$oldRPath"; then exit 1; fi +../src/patchelf \ + --set-interpreter "$(../src/patchelf --print-interpreter ../src/patchelf)" \ + --set-rpath /foo:/bar:/xxxxxxxxxxxxxxx ${SCRATCH}/no-rpath + +newRPath=$(../src/patchelf --print-rpath ${SCRATCH}/no-rpath) +if ! echo "$newRPath" | grep -q '/foo:/bar'; then + echo "incomplete RPATH" + exit 1 +fi + +# Tests for powerpc PIE endianness regressions +readelfData=$(readelf -l ${SCRATCH}/no-rpath 2>&1) + +if [ $(echo "$readelfData" | grep --count "PHDR") != 1 ]; then + # Triggered if PHDR errors appear on stderr + echo "Unexpected number of occurences of PHDR in readelf results" + exit 1 +fi + +virtAddr=$(echo "$readelfData" | grep "PHDR" | awk '{print $3}') +if [ "$virtAddr" != "0x00000034" ]; then + # Triggered if the virtual address is the incorrect endianness + echo "Unexpected virt addr, expected [0x00000034] got [$virtAddr]" + exit 1 +fi + +echo "$readelfData" | grep "LOAD" | while read -r line ; do + align=$(echo "$line" | awk '{print $NF}') + if [ "$align" != "0x10000" ]; then + # Triggered if the target arch was not detected properly + echo "Unexpected Align for LOAD segment, expected [0x10000] got [$align]" + echo "Load segment: [$line]" + exit 1 + fi +done + diff --git a/tests/no-rpath-prebuild/no-rpath-pie-powerpc b/tests/no-rpath-prebuild/no-rpath-pie-powerpc Binary files differnew file mode 100755 index 0000000..09462b8 --- /dev/null +++ b/tests/no-rpath-prebuild/no-rpath-pie-powerpc diff --git a/tests/plain-needed.sh b/tests/plain-needed.sh index 36267fb..8967303 100755 --- a/tests/plain-needed.sh +++ b/tests/plain-needed.sh @@ -1,4 +1,25 @@ #! /bin/sh set -e + +SCRATCH=scratch/$(basename $0 .sh) +MAIN_ELF="${SCRATCH}/main" + +PATCHELF="../src/patchelf" + +rm -rf ${SCRATCH} +mkdir -p ${SCRATCH} +cp main ${SCRATCH}/ + echo "Confirming main requires libfoo" -../src/patchelf --print-needed main | grep -q libfoo.so +${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q libfoo.so + +echo "Testing --add-needed functionality" +${PATCHELF} --add-needed bar.so "${MAIN_ELF}" +${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q bar.so + +echo "Testing --remove-needed functionality" +${PATCHELF} --remove-needed bar.so "${MAIN_ELF}" +if ${PATCHELF} --print-needed "${MAIN_ELF}" | grep -q bar.so; then + echo "ERROR: --remove-needed did not eliminate bar.so!" + exit 1 +fi |