Skip to content

Commit

Permalink
tools: bpf_jit_disasm: Handle large images.
Browse files Browse the repository at this point in the history
Dynamically allocate memory so that JIT images larger than the size of
the statically allocated array can be handled.

Signed-off-by: David Daney <david.daney@cavium.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David Daney authored and David S. Miller committed Jun 14, 2017
1 parent 2fae5d0 commit e274da1
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions tools/net/bpf_jit_disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,19 @@ static void put_log_buff(char *buff)
free(buff);
}

static unsigned int get_last_jit_image(char *haystack, size_t hlen,
uint8_t *image, size_t ilen)
static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
unsigned int *ilen)
{
char *ptr, *pptr, *tmp;
off_t off = 0;
int ret, flen, proglen, pass, ulen = 0;
regmatch_t pmatch[1];
unsigned long base;
regex_t regex;
uint8_t *image;

if (hlen == 0)
return 0;
return NULL;

ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
"pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
Expand All @@ -194,11 +195,22 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
&flen, &proglen, &pass, &base);
if (ret != 4) {
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;
while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) {
while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
tmp = NULL;
if (!strstr(ptr, "JIT code"))
continue;
Expand All @@ -208,10 +220,12 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
ptr = pptr;
do {
image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
if (ptr == pptr || ulen >= ilen) {
if (ptr == pptr) {
ulen--;
break;
}
if (ulen >= proglen)
break;
ptr = pptr;
} while (1);
}
Expand All @@ -222,7 +236,8 @@ static unsigned int get_last_jit_image(char *haystack, size_t hlen,
printf("%lx + <x>:\n", base);

regfree(&regex);
return ulen;
*ilen = ulen;
return image;
}

static void usage(void)
Expand All @@ -237,12 +252,12 @@ static void usage(void)
int main(int argc, char **argv)
{
unsigned int len, klen, opt, opcodes = 0;
static uint8_t image[32768];
char *kbuff, *file = NULL;
char *ofile = NULL;
int ofd;
ssize_t nr;
uint8_t *pos;
uint8_t *image = NULL;

while ((opt = getopt(argc, argv, "of:O:")) != -1) {
switch (opt) {
Expand All @@ -262,16 +277,15 @@ int main(int argc, char **argv)
}

bfd_init();
memset(image, 0, sizeof(image));

kbuff = get_log_buff(file, &klen);
if (!kbuff) {
fprintf(stderr, "Could not retrieve log buffer!\n");
return -1;
}

len = get_last_jit_image(kbuff, klen, image, sizeof(image));
if (len <= 0) {
image = get_last_jit_image(kbuff, klen, &len);
if (!image) {
fprintf(stderr, "No JIT image found!\n");
goto done;
}
Expand Down Expand Up @@ -301,5 +315,6 @@ int main(int argc, char **argv)

done:
put_log_buff(kbuff);
free(image);
return 0;
}

0 comments on commit e274da1

Please sign in to comment.