From c12b965f99771654387146264aec0befe0f73b46 Mon Sep 17 00:00:00 2001 From: Jung-uk Kim Date: Fri, 20 Nov 2009 21:12:40 +0000 Subject: [PATCH] General style cleanup, no functional change. --- sys/amd64/amd64/bpf_jit_machdep.c | 61 +++++++++++++++---------------- sys/i386/i386/bpf_jit_machdep.c | 61 +++++++++++++++---------------- sys/net/bpf_jitter.c | 49 +++++++------------------ 3 files changed, 72 insertions(+), 99 deletions(-) diff --git a/sys/amd64/amd64/bpf_jit_machdep.c b/sys/amd64/amd64/bpf_jit_machdep.c index bbaebed29fe7..ff5500fa630f 100644 --- a/sys/amd64/amd64/bpf_jit_machdep.c +++ b/sys/amd64/amd64/bpf_jit_machdep.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include #else #include +#include #include #include #endif @@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) u_int i, pass; /* - * 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 * the macros to emit code. */ emit_func emitm; - /* Allocate the reference table for the jumps */ + /* Allocate the reference table for the jumps. */ #ifdef _KERNEL - stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT); + stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, + M_NOWAIT | M_ZERO); #else stream.refs = malloc((nins + 1) * sizeof(u_int)); #endif if (stream.refs == NULL) return (NULL); - - /* Reset the reference table */ - for (i = 0; i < nins + 1; i++) - stream.refs[i] = 0; +#ifndef _KERNEL + memset(stream.refs, 0, (nins + 1) * sizeof(u_int)); +#endif stream.cur_ip = 0; stream.bpf_pc = 0; + stream.ibuf = NULL; /* - * the first pass will emit the lengths of the instructions - * to create the reference table + * The first pass will emit the lengths of the instructions + * to create the reference table. */ emitm = emit_length; - pass = 0; - for (;;) { + for (pass = 0; pass < 2; pass++) { ins = prog; - /* create the procedure header */ + /* Create the procedure header. */ PUSH(RBP); MOVrq(RSP, RBP); SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP); @@ -470,25 +471,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) ins++; } - pass++; - if (pass >= 2) { -#ifndef _KERNEL - if (mprotect(stream.ibuf, stream.cur_ip, - PROT_READ | PROT_EXEC) != 0) { - munmap(stream.ibuf, stream.cur_ip); - stream.ibuf = NULL; - } -#endif - *size = stream.cur_ip; - break; - } + if (pass > 0) + continue; + *size = stream.cur_ip; #ifdef _KERNEL - stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); + stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT); if (stream.ibuf == NULL) break; #else - stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE, + stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); if (stream.ibuf == MAP_FAILED) { stream.ibuf = NULL; @@ -497,28 +489,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) #endif /* - * modify the reference table to contain the offsets and - * not the lengths of the instructions + * Modify the reference table to contain the offsets and + * not the lengths of the instructions. */ for (i = 1; i < nins + 1; i++) stream.refs[i] += stream.refs[i - 1]; - /* Reset the counters */ + /* Reset the counters. */ stream.cur_ip = 0; stream.bpf_pc = 0; - /* the second pass creates the actual code */ + /* The second pass creates the actual code. */ emitm = emit_code; } /* - * the reference table is needed only during compilation, - * now we can free it + * The reference table is needed only during compilation, + * now we can free it. */ #ifdef _KERNEL free(stream.refs, M_BPFJIT); #else free(stream.refs); + if (stream.ibuf != NULL && + mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) { + munmap(stream.ibuf, *size); + stream.ibuf = NULL; + } #endif return ((bpf_filter_func)stream.ibuf); diff --git a/sys/i386/i386/bpf_jit_machdep.c b/sys/i386/i386/bpf_jit_machdep.c index 8e51f7bd0cbe..2a4ed468b782 100644 --- a/sys/i386/i386/bpf_jit_machdep.c +++ b/sys/i386/i386/bpf_jit_machdep.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include #else #include +#include #include #include #endif @@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) u_int i, pass; /* - * 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 * the macros to emit code. */ emit_func emitm; - /* Allocate the reference table for the jumps */ + /* Allocate the reference table for the jumps. */ #ifdef _KERNEL - stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT); + stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, + M_NOWAIT | M_ZERO); #else stream.refs = malloc((nins + 1) * sizeof(u_int)); #endif if (stream.refs == NULL) return (NULL); - - /* Reset the reference table */ - for (i = 0; i < nins + 1; i++) - stream.refs[i] = 0; +#ifndef _KERNEL + memset(stream.refs, 0, (nins + 1) * sizeof(u_int)); +#endif stream.cur_ip = 0; stream.bpf_pc = 0; + stream.ibuf = NULL; /* - * the first pass will emit the lengths of the instructions - * to create the reference table + * The first pass will emit the lengths of the instructions + * to create the reference table. */ emitm = emit_length; - pass = 0; - for (;;) { + for (pass = 0; pass < 2; pass++) { ins = prog; - /* create the procedure header */ + /* Create the procedure header. */ PUSH(EBP); MOVrd(ESP, EBP); SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP); @@ -503,25 +504,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) ins++; } - pass++; - if (pass >= 2) { -#ifndef _KERNEL - if (mprotect(stream.ibuf, stream.cur_ip, - PROT_READ | PROT_EXEC) != 0) { - munmap(stream.ibuf, stream.cur_ip); - stream.ibuf = NULL; - } -#endif - *size = stream.cur_ip; - break; - } + if (pass > 0) + continue; + *size = stream.cur_ip; #ifdef _KERNEL - stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); + stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT); if (stream.ibuf == NULL) break; #else - stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE, + stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); if (stream.ibuf == MAP_FAILED) { stream.ibuf = NULL; @@ -530,28 +522,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) #endif /* - * modify the reference table to contain the offsets and - * not the lengths of the instructions + * Modify the reference table to contain the offsets and + * not the lengths of the instructions. */ for (i = 1; i < nins + 1; i++) stream.refs[i] += stream.refs[i - 1]; - /* Reset the counters */ + /* Reset the counters. */ stream.cur_ip = 0; stream.bpf_pc = 0; - /* the second pass creates the actual code */ + /* The second pass creates the actual code. */ emitm = emit_code; } /* - * the reference table is needed only during compilation, - * now we can free it + * The reference table is needed only during compilation, + * now we can free it. */ #ifdef _KERNEL free(stream.refs, M_BPFJIT); #else free(stream.refs); + if (stream.ibuf != NULL && + mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) { + munmap(stream.ibuf, *size); + stream.ibuf = NULL; + } #endif return ((bpf_filter_func)stream.ibuf); diff --git a/sys/net/bpf_jitter.c b/sys/net/bpf_jitter.c index a72d2d4c6b12..d024047582bd 100644 --- a/sys/net/bpf_jitter.c +++ b/sys/net/bpf_jitter.c @@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$"); #include #else #include -#include #include #include #include @@ -62,27 +61,36 @@ SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW, 0, "BPF JIT compiler"); int bpf_jitter_enable = 1; SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW, &bpf_jitter_enable, 0, "enable BPF JIT compiler"); +#endif bpf_jit_filter * bpf_jitter(struct bpf_insn *fp, int nins) { bpf_jit_filter *filter; - /* Allocate the filter structure */ + /* Allocate the filter structure. */ +#ifdef _KERNEL filter = (struct bpf_jit_filter *)malloc(sizeof(*filter), M_BPFJIT, M_NOWAIT); +#else + filter = (struct bpf_jit_filter *)malloc(sizeof(*filter)); +#endif if (filter == NULL) return (NULL); - /* No filter means accept all */ + /* No filter means accept all. */ if (fp == NULL || nins == 0) { filter->func = bpf_jit_accept_all; return (filter); } - /* Create the binary */ + /* Create the binary. */ if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) { +#ifdef _KERNEL free(filter, M_BPFJIT); +#else + free(filter); +#endif return (NULL); } @@ -93,45 +101,16 @@ void bpf_destroy_jit_filter(bpf_jit_filter *filter) { +#ifdef _KERNEL if (filter->func != bpf_jit_accept_all) free(filter->func, M_BPFJIT); free(filter, M_BPFJIT); -} #else -bpf_jit_filter * -bpf_jitter(struct bpf_insn *fp, int nins) -{ - bpf_jit_filter *filter; - - /* Allocate the filter structure */ - filter = (struct bpf_jit_filter *)malloc(sizeof(*filter)); - if (filter == NULL) - return (NULL); - - /* No filter means accept all */ - if (fp == NULL || nins == 0) { - filter->func = bpf_jit_accept_all; - return (filter); - } - - /* Create the binary */ - if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) { - free(filter); - return (NULL); - } - - return (filter); -} - -void -bpf_destroy_jit_filter(bpf_jit_filter *filter) -{ - if (filter->func != bpf_jit_accept_all) munmap(filter->func, filter->size); free(filter); -} #endif +} static u_int bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,