DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] bpf: remove dependency on vla
@ 2025-08-03 16:16 Stephen Hemminger
  2025-08-15 14:26 ` Konstantin Ananyev
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Hemminger @ 2025-08-03 16:16 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Konstantin Ananyev

The code for ethdev callbacks was using variable length arrays
which is a feature not supported on MSVC and later C standards.
Replace with alloca().

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/bpf/bpf_pkt.c   | 16 +++++++++-------
 lib/bpf/meson.build |  2 --
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/lib/bpf/bpf_pkt.c b/lib/bpf/bpf_pkt.c
index 01f813c56b..6a425d79b6 100644
--- a/lib/bpf/bpf_pkt.c
+++ b/lib/bpf/bpf_pkt.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdint.h>
+#include <alloca.h>
 
 #include <sys/queue.h>
 
@@ -163,10 +164,11 @@ apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num,
 	uint32_t drop)
 {
 	uint32_t i, j, k;
-	struct rte_mbuf *dr[num];
+	struct rte_mbuf **dr;
 
-	for (i = 0, j = 0, k = 0; i != num; i++) {
+	dr = alloca(sizeof(struct rte_mbuf *) * num);
 
+	for (i = 0, j = 0, k = 0; i != num; i++) {
 		/* filter matches */
 		if (rc[i] != 0)
 			mb[j++] = mb[i];
@@ -193,8 +195,8 @@ pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
 	uint32_t drop)
 {
 	uint32_t i;
-	void *dp[num];
-	uint64_t rc[num];
+	void **dp = alloca(sizeof(void *) * num);
+	uint64_t *rc = alloca(sizeof(uint64_t) * num);
 
 	for (i = 0; i != num; i++)
 		dp[i] = rte_pktmbuf_mtod(mb[i], void *);
@@ -209,7 +211,7 @@ pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
 {
 	uint32_t i, n;
 	void *dp;
-	uint64_t rc[num];
+	uint64_t *rc = alloca(sizeof(uint64_t) * num);
 
 	n = 0;
 	for (i = 0; i != num; i++) {
@@ -228,7 +230,7 @@ static inline uint32_t
 pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
 	uint32_t drop)
 {
-	uint64_t rc[num];
+	uint64_t *rc = alloca(sizeof(uint64_t) * num);
 
 	rte_bpf_exec_burst(bpf, (void **)mb, rc, num);
 	return apply_filter(mb, rc, num, drop);
@@ -239,7 +241,7 @@ pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
 	uint32_t num, uint32_t drop)
 {
 	uint32_t i, n;
-	uint64_t rc[num];
+	uint64_t *rc = alloca(sizeof(uint64_t) * num);
 
 	n = 0;
 	for (i = 0; i != num; i++) {
diff --git a/lib/bpf/meson.build b/lib/bpf/meson.build
index 28df7f469a..aa258a9061 100644
--- a/lib/bpf/meson.build
+++ b/lib/bpf/meson.build
@@ -7,8 +7,6 @@ if is_windows
     subdir_done()
 endif
 
-cflags += no_wvla_cflag
-
 if arch_subdir == 'x86' and dpdk_conf.get('RTE_ARCH_32')
     build = false
     reason = 'not supported on 32-bit x86'
-- 
2.47.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [PATCH] bpf: remove dependency on vla
  2025-08-03 16:16 [PATCH] bpf: remove dependency on vla Stephen Hemminger
@ 2025-08-15 14:26 ` Konstantin Ananyev
  2025-08-15 16:03   ` Stephen Hemminger
  0 siblings, 1 reply; 3+ messages in thread
From: Konstantin Ananyev @ 2025-08-15 14:26 UTC (permalink / raw)
  To: Stephen Hemminger, dev



> The code for ethdev callbacks was using variable length arrays
> which is a feature not supported on MSVC and later C standards.
> Replace with alloca().

I am not a big fun of such mechanical replacement of vla with alloca()
Specially in that particular case, we can have internal function that uses
fixed size array and in public one just call it several times in a loop.
Again, using VLA here is probably a real security breach,
as we put some assumptions on size of input arrays provided by user,
which we probably shouldn't.    

> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/bpf/bpf_pkt.c   | 16 +++++++++-------
>  lib/bpf/meson.build |  2 --
>  2 files changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/lib/bpf/bpf_pkt.c b/lib/bpf/bpf_pkt.c
> index 01f813c56b..6a425d79b6 100644
> --- a/lib/bpf/bpf_pkt.c
> +++ b/lib/bpf/bpf_pkt.c
> @@ -6,6 +6,7 @@
>  #include <string.h>
>  #include <errno.h>
>  #include <stdint.h>
> +#include <alloca.h>
> 
>  #include <sys/queue.h>
> 
> @@ -163,10 +164,11 @@ apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num,
>  	uint32_t drop)
>  {
>  	uint32_t i, j, k;
> -	struct rte_mbuf *dr[num];
> +	struct rte_mbuf **dr;
> 
> -	for (i = 0, j = 0, k = 0; i != num; i++) {
> +	dr = alloca(sizeof(struct rte_mbuf *) * num);
> 
> +	for (i = 0, j = 0, k = 0; i != num; i++) {
>  		/* filter matches */
>  		if (rc[i] != 0)
>  			mb[j++] = mb[i];
> @@ -193,8 +195,8 @@ pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
>  	uint32_t drop)
>  {
>  	uint32_t i;
> -	void *dp[num];
> -	uint64_t rc[num];
> +	void **dp = alloca(sizeof(void *) * num);
> +	uint64_t *rc = alloca(sizeof(uint64_t) * num);
> 
>  	for (i = 0; i != num; i++)
>  		dp[i] = rte_pktmbuf_mtod(mb[i], void *);
> @@ -209,7 +211,7 @@ pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
>  {
>  	uint32_t i, n;
>  	void *dp;
> -	uint64_t rc[num];
> +	uint64_t *rc = alloca(sizeof(uint64_t) * num);
> 
>  	n = 0;
>  	for (i = 0; i != num; i++) {
> @@ -228,7 +230,7 @@ static inline uint32_t
>  pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
>  	uint32_t drop)
>  {
> -	uint64_t rc[num];
> +	uint64_t *rc = alloca(sizeof(uint64_t) * num);
> 
>  	rte_bpf_exec_burst(bpf, (void **)mb, rc, num);
>  	return apply_filter(mb, rc, num, drop);
> @@ -239,7 +241,7 @@ pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
>  	uint32_t num, uint32_t drop)
>  {
>  	uint32_t i, n;
> -	uint64_t rc[num];
> +	uint64_t *rc = alloca(sizeof(uint64_t) * num);
> 
>  	n = 0;
>  	for (i = 0; i != num; i++) {
> diff --git a/lib/bpf/meson.build b/lib/bpf/meson.build
> index 28df7f469a..aa258a9061 100644
> --- a/lib/bpf/meson.build
> +++ b/lib/bpf/meson.build
> @@ -7,8 +7,6 @@ if is_windows
>      subdir_done()
>  endif
> 
> -cflags += no_wvla_cflag
> -
>  if arch_subdir == 'x86' and dpdk_conf.get('RTE_ARCH_32')
>      build = false
>      reason = 'not supported on 32-bit x86'
> --
> 2.47.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] bpf: remove dependency on vla
  2025-08-15 14:26 ` Konstantin Ananyev
@ 2025-08-15 16:03   ` Stephen Hemminger
  0 siblings, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2025-08-15 16:03 UTC (permalink / raw)
  To: Konstantin Ananyev; +Cc: dev

On Fri, 15 Aug 2025 14:26:07 +0000
Konstantin Ananyev <konstantin.ananyev@huawei.com> wrote:

> > The code for ethdev callbacks was using variable length arrays
> > which is a feature not supported on MSVC and later C standards.
> > Replace with alloca().  
> 
> I am not a big fun of such mechanical replacement of vla with alloca()
> Specially in that particular case, we can have internal function that uses
> fixed size array and in public one just call it several times in a loop.
> Again, using VLA here is probably a real security breach,
> as we put some assumptions on size of input arrays provided by user,
> which we probably shouldn't.    
> 
> > Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

The other way to handle this is to break the loop into chunks.
But it ends up being more awkward to read, and more complex.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-15 16:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-03 16:16 [PATCH] bpf: remove dependency on vla Stephen Hemminger
2025-08-15 14:26 ` Konstantin Ananyev
2025-08-15 16:03   ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).