forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zdtm: check offset restoration on ELF files
Signed-off-by: Michal Clapinski <[email protected]>
- Loading branch information
1 parent
ab73a84
commit d27e376
Showing
4 changed files
with
59 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1650,39 +1650,36 @@ static int get_build_id_64(Elf64_Ehdr *file_header, unsigned char **build_id, co | |
*/ | ||
static int get_build_id(const int fd, const struct stat *fd_status, unsigned char **build_id) | ||
{ | ||
char buf[SELFMAG + 1]; | ||
void *start_addr; | ||
char *start_addr; | ||
size_t mapped_size; | ||
int ret = -1; | ||
|
||
if (read(fd, buf, SELFMAG + 1) != SELFMAG + 1) | ||
return -1; | ||
|
||
/* | ||
* The first 4 bytes contain a magic number identifying the file as an | ||
* ELF file. They should contain the characters ‘\x7f’, ‘E’, ‘L’, and | ||
* ‘F’, respectively. These characters are together defined as ELFMAG. | ||
*/ | ||
if (strncmp(buf, ELFMAG, SELFMAG)) | ||
return -1; | ||
|
||
/* | ||
* If the build-id exists, then it will most likely be present in the | ||
* beginning of the file. Therefore at most only the first 1 MB of the | ||
* file is mapped. | ||
*/ | ||
mapped_size = min_t(size_t, fd_status->st_size, BUILD_ID_MAP_SIZE); | ||
start_addr = mmap(0, mapped_size, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0); | ||
if (start_addr == MAP_FAILED) { | ||
if ((void*)start_addr == MAP_FAILED) { | ||
Check warning on line 1664 in criu/files-reg.c GitHub Actions / build
|
||
pr_warn("Couldn't mmap file with fd %d\n", fd); | ||
return -1; | ||
} | ||
|
||
if (buf[EI_CLASS] == ELFCLASS32) | ||
ret = get_build_id_32(start_addr, build_id, fd, mapped_size); | ||
if (buf[EI_CLASS] == ELFCLASS64) | ||
ret = get_build_id_64(start_addr, build_id, fd, mapped_size); | ||
/* | ||
* The first 4 bytes contain a magic number identifying the file as an | ||
* ELF file. They should contain the characters ‘\x7f’, ‘E’, ‘L’, and | ||
* ‘F’, respectively. These characters are together defined as ELFMAG. | ||
*/ | ||
if (strncmp(start_addr, ELFMAG, SELFMAG)) | ||
goto out; | ||
|
||
if (start_addr[EI_CLASS] == ELFCLASS32) | ||
ret = get_build_id_32((Elf32_Ehdr *)start_addr, build_id, fd, mapped_size); | ||
if (start_addr[EI_CLASS] == ELFCLASS64) | ||
ret = get_build_id_64((Elf64_Ehdr *)start_addr, build_id, fd, mapped_size); | ||
|
||
out: | ||
munmap(start_addr, mapped_size); | ||
return ret; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <fcntl.h> | ||
|
||
#include "zdtmtst.h" | ||
#include "lock.h" | ||
|
||
const char *test_doc = "Check that criu properly restores offsets on ELF files"; | ||
const char *test_author = "Michal Clapinski <[email protected]>"; | ||
|
||
void check_offset(int fd) | ||
{ | ||
int offset = lseek(fd, 0, SEEK_CUR); | ||
if (offset < 0) { | ||
fail("lseek"); | ||
exit(1); | ||
} | ||
if (offset != 0) { | ||
fail("wrong offset; expected: 0, got: %d", offset); | ||
exit(1); | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int fd; | ||
|
||
test_init(argc, argv); | ||
|
||
fd = open("/proc/self/exe", O_RDONLY); | ||
if (fd < 0) { | ||
fail("open"); | ||
exit(1); | ||
} | ||
check_offset(fd); | ||
|
||
test_daemon(); | ||
test_waitsig(); | ||
|
||
check_offset(fd); | ||
|
||
pass(); | ||
return 0; | ||
} |