git: c89a246a7dfa - 2023Q3 - comms/gnuradio: Fix crash with GNU Radio buffers

From: Ganael LAPLANCHE <martymac_at_FreeBSD.org>
Date: Wed, 20 Sep 2023 10:45:38 UTC
The branch 2023Q3 has been updated by martymac:

URL: https://cgit.FreeBSD.org/ports/commit/?id=c89a246a7dfa8d60803076dfef09deab3c7a9ae7

commit c89a246a7dfa8d60803076dfef09deab3c7a9ae7
Author:     Ganael LAPLANCHE <martymac@FreeBSD.org>
AuthorDate: 2023-09-20 10:28:48 +0000
Commit:     Ganael LAPLANCHE <martymac@FreeBSD.org>
CommitDate: 2023-09-20 10:45:07 +0000

    comms/gnuradio: Fix crash with GNU Radio buffers
    
    This patch is a backport of ca44241 from upstream:
    runtime: Use MAP_FIXED flag to ensure buffer halves are contiguous
    
    It fixes SIGSEGV observed with GNU Radio buffers consumers such as
    comms/gqrx.
    
    Discussed here:
      https://github.com/gqrx-sdr/gqrx/issues/1275
      https://github.com/gnuradio/gnuradio/pull/6854
    
    PR:     272543
    Reported by:    trasz
    Obtained from:  GNU Radio team (GH pull request: 6854)
    MFH:    2023Q3
    
    (cherry picked from commit 35f73836d156fc5bcec6eb9d8d3cd60c81c453af)
---
 comms/gnuradio/Makefile                            |  2 +-
 ...gnuradio-runtime-lib-vmcircbuf_mmap_shm_open.cc | 28 ++++++++++++++
 ...-gnuradio-runtime-lib-vmcircbuf_mmap_tmpfile.cc | 44 ++++++++++++++++++++++
 3 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/comms/gnuradio/Makefile b/comms/gnuradio/Makefile
index 95c946d062b5..314148e6f64a 100644
--- a/comms/gnuradio/Makefile
+++ b/comms/gnuradio/Makefile
@@ -1,7 +1,7 @@
 PORTNAME=	gnuradio
 DISTVERSIONPREFIX=	v
 DISTVERSION=	3.8.4.0
-PORTREVISION=	11
+PORTREVISION=	12
 CATEGORIES=	comms astro hamradio
 
 MAINTAINER=	hamradio@FreeBSD.org
diff --git a/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_shm_open.cc b/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_shm_open.cc
new file mode 100644
index 000000000000..efec2bb34ad1
--- /dev/null
+++ b/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_shm_open.cc
@@ -0,0 +1,28 @@
+Use MAP_FIXED flag to ensure buffer halves are contiguous
+
+(backport of ca44241)
+
+--- gnuradio-runtime/lib/vmcircbuf_mmap_shm_open.cc.orig	2021-09-30 14:10:55 UTC
++++ gnuradio-runtime/lib/vmcircbuf_mmap_shm_open.cc
+@@ -122,19 +122,11 @@ vmcircbuf_mmap_shm_open::vmcircbuf_mmap_shm_open(int s
+         throw std::runtime_error("gr::vmcircbuf_mmap_shm_open");
+     }
+ 
+-    // unmap the 2nd half
+-    if (munmap((char*)first_copy + size, size) == -1) {
+-        close(shm_fd); // cleanup
+-        perror("gr::vmcircbuf_mmap_shm_open: munmap (1)");
+-        throw std::runtime_error("gr::vmcircbuf_mmap_shm_open");
+-    }
+-
+-    // map the first half into the now available hole where the
+-    // second half used to be.
++    // map the first half into the second half of the address space.
+     void* second_copy = mmap((char*)first_copy + size,
+                              size,
+                              PROT_READ | PROT_WRITE,
+-                             MAP_SHARED,
++                             MAP_SHARED | MAP_FIXED,
+                              shm_fd,
+                              (off_t)0);
+ 
diff --git a/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_tmpfile.cc b/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_tmpfile.cc
new file mode 100644
index 000000000000..a3bb6811815c
--- /dev/null
+++ b/comms/gnuradio/files/patch-gnuradio-runtime-lib-vmcircbuf_mmap_tmpfile.cc
@@ -0,0 +1,44 @@
+Use MAP_FIXED flag to ensure buffer halves are contiguous
+
+(backport of ca44241)
+
+--- gnuradio-runtime/lib/vmcircbuf_mmap_tmpfile.cc.orig	2021-09-30 14:10:55 UTC
++++ gnuradio-runtime/lib/vmcircbuf_mmap_tmpfile.cc
+@@ -107,19 +107,11 @@ vmcircbuf_mmap_tmpfile::vmcircbuf_mmap_tmpfile(int siz
+         throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
+     }
+ 
+-    // unmap the 2nd half
+-    if (munmap((char*)first_copy + size, size) == -1) {
+-        close(seg_fd); // cleanup
+-        perror("gr::vmcircbuf_mmap_tmpfile: munmap (1)");
+-        throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
+-    }
+-
+-    // map the first half into the now available hole where the
+-    // second half used to be.
++    // map the first half into the second half of the address space.
+     void* second_copy = mmap((char*)first_copy + size,
+                              size,
+                              PROT_READ | PROT_WRITE,
+-                             MAP_SHARED,
++                             MAP_SHARED | MAP_FIXED,
+                              seg_fd,
+                              (off_t)0);
+ 
+@@ -127,15 +119,6 @@ vmcircbuf_mmap_tmpfile::vmcircbuf_mmap_tmpfile(int siz
+         munmap(first_copy, size); // cleanup
+         close(seg_fd);
+         perror("gr::vmcircbuf_mmap_tmpfile: mmap(2)");
+-        throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
+-    }
+-
+-    // check for contiguity
+-    if ((char*)second_copy != (char*)first_copy + size) {
+-        munmap(first_copy, size); // cleanup
+-        munmap(second_copy, size);
+-        close(seg_fd);
+-        perror("gr::vmcircbuf_mmap_tmpfile: non-contiguous second copy");
+         throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
+     }
+