Commit 3c966037 authored by Dmitry Safonov's avatar Dmitry Safonov Committed by Andrei Vagin

compel/tests: add sections table & string section tests

Now it has 4 new tests:
ok 4 - section table start oob (64-bit ELF)
ok 5 - too many sections in table (64-bit ELF)
ok 6 - strings section's header oob of section table (64-bit ELF)
ok 7 - strings section oob (64-bit ELF)

I.e, if we forget to test string section's header oob with the next diff:
>--- a/compel/handle-elf.c
>+++ b/compel/handle-elf.c
>@@ -122,7 +122,7 @@ static const char *get_strings_section(Ehdr_t *hdr, uintptr_t mem,
>                pr_err("String section @%#zx size %#lx is out of [%#zx, %#zx)\n",
>                        addr, (unsigned long)secstrings_hdr->sh_size,
>                        mem, mem + size);
>-               return NULL;
>+               return (void*)addr;
>        }
>
>        return (void*)addr;

It will yell with:
ok 1 - zero ELF header (64-bit ELF)
...
not ok 6 - strings section's header oob of section table (64-bit ELF), expected -4 but ret is -1
...
not ok 12 - strings section's header oob of section table (32-bit ELF), expected -4 but ret is -1

Should be more useful when I add relocations tests after all.
(but this seems for me useful too).

Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarDmitry Safonov <dsafonov@virtuozzo.com>
Reviewed-by: 's avatarCyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 6f58ca5a
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#define Rel_t Elf32_Rel #define Rel_t Elf32_Rel
#define Rela_t Elf32_Rela #define Rela_t Elf32_Rela
#define Off_t Elf32_Off
#define Word_t Elf32_Word
#define Half_t Elf32_Half
#define ELF_ST_TYPE ELF32_ST_TYPE #define ELF_ST_TYPE ELF32_ST_TYPE
#define ELF_ST_BIND ELF32_ST_BIND #define ELF_ST_BIND ELF32_ST_BIND
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#define Rel_t Elf64_Rel #define Rel_t Elf64_Rel
#define Rela_t Elf64_Rela #define Rela_t Elf64_Rela
#define Off_t Elf64_Off
#define Word_t Elf64_Word
#define Half_t Elf64_Half
#define ELF_ST_TYPE ELF64_ST_TYPE #define ELF_ST_TYPE ELF64_ST_TYPE
#define ELF_ST_BIND ELF64_ST_BIND #define ELF_ST_BIND ELF64_ST_BIND
......
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#include "arch_test_handle_binary.h" #include "arch_test_handle_binary.h"
extern int launch_test(void *mem, int expected_ret, const char *test_fmt, ...); extern int launch_test(void *mem, int expected_ret, const char *test_fmt, ...);
extern const size_t test_elf_buf_size;
static const unsigned int sections_nr = 1;
static void set_elf_hdr_relocatable(Ehdr_t *hdr) static void set_elf_hdr_relocatable(Ehdr_t *hdr)
{ {
...@@ -13,6 +16,53 @@ static void set_elf_hdr_relocatable(Ehdr_t *hdr) ...@@ -13,6 +16,53 @@ static void set_elf_hdr_relocatable(Ehdr_t *hdr)
hdr->e_version = EV_CURRENT; hdr->e_version = EV_CURRENT;
} }
static int test_add_strings_section(void *elf, const char *msg)
{
Ehdr_t *hdr = elf;
Shdr_t *sec_strings_hdr;
uintptr_t sections_table = (uintptr_t)elf + hdr->e_shoff;
size_t sections_table_size = sections_nr*sizeof(hdr->e_shentsize);
hdr->e_shnum = sections_nr;
hdr->e_shstrndx = sections_nr; /* off-by-one */
if (launch_test(elf, -E_NO_STR_SEC,
"strings section's header oob of section table %s", msg))
return -1;
hdr->e_shstrndx = 0;
sec_strings_hdr = (void *)sections_table;
sec_strings_hdr->sh_offset = (Off_t)-1;
if (launch_test(elf, -E_NO_STR_SEC, "strings section oob %s", msg))
return -1;
/* Put strings just right after sections table. */
sec_strings_hdr->sh_offset = sections_table - (uintptr_t)elf +
sections_table_size;
return 0;
}
static int test_prepare_section_table(void *elf, const char *msg)
{
Ehdr_t *hdr = elf;
hdr->e_shoff = (Off_t)test_elf_buf_size;
if (launch_test(elf, -E_NO_STR_SEC, "section table start oob %s", msg))
return -1;
/* Lets put sections table right after ELF header. */
hdr->e_shoff = (Off_t) sizeof(Ehdr_t);
hdr->e_shentsize = (Half_t) sizeof(Shdr_t);
hdr->e_shnum = (Half_t)-1;
if (launch_test(elf, -E_NO_STR_SEC, "too many sections in table %s", msg))
return -1;
if (test_add_strings_section(elf, msg))
return -1;
return 0;
}
static int test_prepare_elf_header(void *elf, const char *msg) static int test_prepare_elf_header(void *elf, const char *msg)
{ {
memset(elf, 0, sizeof(Ehdr_t)); memset(elf, 0, sizeof(Ehdr_t));
...@@ -29,6 +79,9 @@ static int test_prepare_elf_header(void *elf, const char *msg) ...@@ -29,6 +79,9 @@ static int test_prepare_elf_header(void *elf, const char *msg)
set_elf_hdr_relocatable(elf); set_elf_hdr_relocatable(elf);
if (test_prepare_section_table(elf, msg))
return -1;
return 0; return 0;
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "arch_test_handle_binary.h" #include "arch_test_handle_binary.h"
/* size of buffer with formed ELF file */ /* size of buffer with formed ELF file */
#define ELF_BUF_SIZE 4096 const size_t test_elf_buf_size = 4096;
extern int handle_binary(void *mem, size_t size); extern int handle_binary(void *mem, size_t size);
extern void run_tests(void *mem); extern void run_tests(void *mem);
...@@ -33,7 +33,7 @@ piegen_opt_t opts = { ...@@ -33,7 +33,7 @@ piegen_opt_t opts = {
int launch_test(void *mem, int expected_ret, const char *test_fmt, ...) int launch_test(void *mem, int expected_ret, const char *test_fmt, ...)
{ {
static unsigned test_nr = 1; static unsigned test_nr = 1;
int ret = handle_binary(mem, ELF_BUF_SIZE); int ret = handle_binary(mem, test_elf_buf_size);
va_list params; va_list params;
va_start(params, test_fmt); va_start(params, test_fmt);
...@@ -55,7 +55,7 @@ int launch_test(void *mem, int expected_ret, const char *test_fmt, ...) ...@@ -55,7 +55,7 @@ int launch_test(void *mem, int expected_ret, const char *test_fmt, ...)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
void *elf_buf = malloc(ELF_BUF_SIZE); void *elf_buf = malloc(test_elf_buf_size);
arch_run_tests(elf_buf); arch_run_tests(elf_buf);
free(elf_buf); free(elf_buf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment