2016-05-11 15:49:00 +03:00
|
|
|
/*
|
|
|
|
* Test for handle_binary().
|
|
|
|
* In this test ELF binary file is constructed from
|
|
|
|
* header up to sections and relocations.
|
|
|
|
* On each stage it tests non-valid ELF binaries to be parsed.
|
|
|
|
* For passing test, handle_binary should return errors for all
|
|
|
|
* non-valid binaries and handle all relocations.
|
|
|
|
*
|
|
|
|
* Test author: Dmitry Safonov <dsafonov@virtuozzo.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2016-05-25 15:52:59 +03:00
|
|
|
#include <stdarg.h>
|
2016-05-11 15:49:00 +03:00
|
|
|
|
|
|
|
#include "piegen.h"
|
|
|
|
#include "arch_test_handle_binary.h"
|
|
|
|
|
|
|
|
/* size of buffer with formed ELF file */
|
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: Dmitry Safonov <dsafonov@virtuozzo.com>
Reviewed-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
2016-05-30 16:14:00 +03:00
|
|
|
const size_t test_elf_buf_size = 4096;
|
2016-05-11 15:49:00 +03:00
|
|
|
|
|
|
|
extern int handle_binary(void *mem, size_t size);
|
|
|
|
extern void run_tests(void *mem);
|
|
|
|
|
2016-05-25 15:52:59 +03:00
|
|
|
int launch_test(void *mem, int expected_ret, const char *test_fmt, ...)
|
2016-05-11 15:49:00 +03:00
|
|
|
{
|
|
|
|
static unsigned test_nr = 1;
|
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: Dmitry Safonov <dsafonov@virtuozzo.com>
Reviewed-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
2016-05-30 16:14:00 +03:00
|
|
|
int ret = handle_binary(mem, test_elf_buf_size);
|
2016-05-25 15:52:59 +03:00
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, test_fmt);
|
|
|
|
if (ret != expected_ret) {
|
|
|
|
printf("not ok %u - ", test_nr);
|
|
|
|
vprintf(test_fmt, params);
|
|
|
|
printf(", expected %d but ret is %d\n", expected_ret, ret);
|
|
|
|
} else {
|
|
|
|
printf("ok %u - ", test_nr);
|
|
|
|
vprintf(test_fmt, params);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
va_end(params);
|
2016-05-11 15:49:00 +03:00
|
|
|
test_nr++;
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
return ret != expected_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
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: Dmitry Safonov <dsafonov@virtuozzo.com>
Reviewed-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
2016-05-30 16:14:00 +03:00
|
|
|
void *elf_buf = malloc(test_elf_buf_size);
|
2016-05-30 15:48:48 +03:00
|
|
|
int ret;
|
2016-05-11 15:49:00 +03:00
|
|
|
|
2016-05-30 15:48:48 +03:00
|
|
|
ret = arch_run_tests(elf_buf);
|
2016-05-11 15:49:00 +03:00
|
|
|
free(elf_buf);
|
2016-05-30 15:48:48 +03:00
|
|
|
return ret;
|
2016-05-11 15:49:00 +03:00
|
|
|
}
|