^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifdef HAVE_JITDUMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <libelf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "../util/genelf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define TEMPL "/tmp/perf-test-XXXXXX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int test__jit_write_elf(struct test *test __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef HAVE_JITDUMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static unsigned char x86_code[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 0xCD, 0x80 /* int $0x80 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int fd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) strcpy(path, TEMPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) fd = mkstemp(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) perror("mkstemp failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return TEST_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) pr_info("Writing jit code to: %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ret = jit_write_elf(fd, 0, "main", x86_code, sizeof(x86_code),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) NULL, 0, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unlink(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return ret ? TEST_FAIL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return TEST_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }