- Make BPF JIT compiler working again in userland. We are limiting size of

generated native binary to page size for now.
- Update copyright date and fix some style nits.
This commit is contained in:
jkim 2009-11-18 19:26:17 +00:00
parent f62107b530
commit efc247aeb3
6 changed files with 57 additions and 27 deletions

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$");
#include <net/if.h> #include <net/if.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <sys/mman.h>
#include <sys/param.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -97,9 +99,9 @@ emit_code(bpf_bin_stream *stream, u_int value, u_int len)
bpf_filter_func bpf_filter_func
bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem) bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
{ {
bpf_bin_stream stream;
struct bpf_insn *ins; struct bpf_insn *ins;
u_int i, pass; u_int i, pass;
bpf_bin_stream stream;
/* /*
* NOTE: do not modify the name of this variable, as it's used by * NOTE: do not modify the name of this variable, as it's used by
@ -475,20 +477,31 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
} }
pass++; pass++;
if (pass == 2) if (pass >= 2) {
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, BPF_JIT_MAXSIZE);
stream.ibuf = NULL;
}
#endif
break; break;
}
#ifdef _KERNEL #ifdef _KERNEL
stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) { if (stream.ibuf == NULL)
free(stream.refs, M_BPFJIT); break;
return (NULL);
}
#else #else
stream.ibuf = (char *)malloc(stream.cur_ip); if (stream.cur_ip > BPF_JIT_MAXSIZE) {
if (stream.ibuf == NULL) { stream.ibuf = NULL;
free(stream.refs); break;
return (NULL); }
stream.ibuf = (char *)mmap(NULL, BPF_JIT_MAXSIZE,
PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL;
break;
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$");
#include <net/if.h> #include <net/if.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <sys/mman.h>
#include <sys/param.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -97,9 +99,9 @@ emit_code(bpf_bin_stream *stream, u_int value, u_int len)
bpf_filter_func bpf_filter_func
bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem) bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
{ {
bpf_bin_stream stream;
struct bpf_insn *ins; struct bpf_insn *ins;
u_int i, pass; u_int i, pass;
bpf_bin_stream stream;
/* /*
* NOTE: do not modify the name of this variable, as it's used by * NOTE: do not modify the name of this variable, as it's used by
@ -498,20 +500,31 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
} }
pass++; pass++;
if (pass == 2) if (pass >= 2) {
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, BPF_JIT_MAXSIZE);
stream.ibuf = NULL;
}
#endif
break; break;
}
#ifdef _KERNEL #ifdef _KERNEL
stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL) { if (stream.ibuf == NULL)
free(stream.refs, M_BPFJIT); break;
return (NULL);
}
#else #else
stream.ibuf = (char *)malloc(stream.cur_ip); if (stream.cur_ip > BPF_JIT_MAXSIZE) {
if (stream.ibuf == NULL) { stream.ibuf = NULL;
free(stream.refs); break;
return (NULL); }
stream.ibuf = (char *)mmap(NULL, BPF_JIT_MAXSIZE,
PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL;
break;
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -43,6 +43,8 @@ __FBSDID("$FreeBSD$");
#else #else
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/types.h> #include <sys/types.h>
#endif #endif
@ -127,7 +129,7 @@ bpf_destroy_jit_filter(bpf_jit_filter *filter)
{ {
if (filter->func != bpf_jit_accept_all) if (filter->func != bpf_jit_accept_all)
free(filter->func); munmap(filter->func, BPF_JIT_MAXSIZE);
free(filter); free(filter);
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/*- /*-
* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
* Copyright (C) 2005-2008 Jung-uk Kim <jkim@FreeBSD.org> * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -36,6 +36,8 @@
#ifdef _KERNEL #ifdef _KERNEL
MALLOC_DECLARE(M_BPFJIT); MALLOC_DECLARE(M_BPFJIT);
#else
#define BPF_JIT_MAXSIZE PAGE_SIZE
#endif #endif
extern int bpf_jitter_enable; extern int bpf_jitter_enable;