Commit e274da1a authored by David Daney's avatar David Daney Committed by David S. Miller

tools: bpf_jit_disasm: Handle large images.

Dynamically allocate memory so that JIT images larger than the size of
the statically allocated array can be handled.
Signed-off-by: default avatarDavid Daney <david.daney@cavium.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 2fae5d0e
...@@ -159,8 +159,8 @@ static void put_log_buff(char *buff) ...@@ -159,8 +159,8 @@ static void put_log_buff(char *buff)
free(buff); free(buff);
} }
static unsigned int get_last_jit_image(char *haystack, size_t hlen, static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
uint8_t *image, size_t ilen) unsigned int *ilen)
{ {
char *ptr, *pptr, *tmp; char *ptr, *pptr, *tmp;
off_t off = 0; off_t off = 0;
...@@ -168,9 +168,10 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, ...@@ -168,9 +168,10 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
regmatch_t pmatch[1]; regmatch_t pmatch[1];
unsigned long base; unsigned long base;
regex_t regex; regex_t regex;
uint8_t *image;
if (hlen == 0) if (hlen == 0)
return 0; return NULL;
ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ " ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
"pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED); "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
...@@ -194,11 +195,22 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, ...@@ -194,11 +195,22 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
&flen, &proglen, &pass, &base); &flen, &proglen, &pass, &base);
if (ret != 4) { if (ret != 4) {
regfree(&regex); regfree(&regex);
return 0; return NULL;
}
if (proglen > 1000000) {
printf("proglen of %d too big, stopping\n", proglen);
return NULL;
} }
image = malloc(proglen);
if (!image) {
printf("Out of memory\n");
return NULL;
}
memset(image, 0, proglen);
tmp = ptr = haystack + off; tmp = ptr = haystack + off;
while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) { while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
tmp = NULL; tmp = NULL;
if (!strstr(ptr, "JIT code")) if (!strstr(ptr, "JIT code"))
continue; continue;
...@@ -208,10 +220,12 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, ...@@ -208,10 +220,12 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
ptr = pptr; ptr = pptr;
do { do {
image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16); image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
if (ptr == pptr || ulen >= ilen) { if (ptr == pptr) {
ulen--; ulen--;
break; break;
} }
if (ulen >= proglen)
break;
ptr = pptr; ptr = pptr;
} while (1); } while (1);
} }
...@@ -222,7 +236,8 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen, ...@@ -222,7 +236,8 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
printf("%lx + <x>:\n", base); printf("%lx + <x>:\n", base);
regfree(&regex); regfree(&regex);
return ulen; *ilen = ulen;
return image;
} }
static void usage(void) static void usage(void)
...@@ -237,12 +252,12 @@ static void usage(void) ...@@ -237,12 +252,12 @@ static void usage(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
unsigned int len, klen, opt, opcodes = 0; unsigned int len, klen, opt, opcodes = 0;
static uint8_t image[32768];
char *kbuff, *file = NULL; char *kbuff, *file = NULL;
char *ofile = NULL; char *ofile = NULL;
int ofd; int ofd;
ssize_t nr; ssize_t nr;
uint8_t *pos; uint8_t *pos;
uint8_t *image = NULL;
while ((opt = getopt(argc, argv, "of:O:")) != -1) { while ((opt = getopt(argc, argv, "of:O:")) != -1) {
switch (opt) { switch (opt) {
...@@ -262,7 +277,6 @@ int main(int argc, char **argv) ...@@ -262,7 +277,6 @@ int main(int argc, char **argv)
} }
bfd_init(); bfd_init();
memset(image, 0, sizeof(image));
kbuff = get_log_buff(file, &klen); kbuff = get_log_buff(file, &klen);
if (!kbuff) { if (!kbuff) {
...@@ -270,8 +284,8 @@ int main(int argc, char **argv) ...@@ -270,8 +284,8 @@ int main(int argc, char **argv)
return -1; return -1;
} }
len = get_last_jit_image(kbuff, klen, image, sizeof(image)); image = get_last_jit_image(kbuff, klen, &len);
if (len <= 0) { if (!image) {
fprintf(stderr, "No JIT image found!\n"); fprintf(stderr, "No JIT image found!\n");
goto done; goto done;
} }
...@@ -301,5 +315,6 @@ int main(int argc, char **argv) ...@@ -301,5 +315,6 @@ int main(int argc, char **argv)
done: done:
put_log_buff(kbuff); put_log_buff(kbuff);
free(image);
return 0; return 0;
} }
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