unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Matt Wette <matt.wette@gmail.com>, guile-devel@gnu.org
Subject: Re: patch for mmap and friends
Date: Sat, 14 Jan 2023 23:42:30 +0100	[thread overview]
Message-ID: <23890f8a-8891-d888-b289-c0d06304fff1@telenet.be> (raw)
In-Reply-To: <c7e20254-6387-00c4-ea15-f3ed64668923@gmail.com>


[-- Attachment #1.1.1: Type: text/plain, Size: 3540 bytes --]



On 14-01-2023 23:08, Matt Wette wrote:
> 2) had to copy/modify dynwind_acquire_port and release_port from ports.c

Is it because of the 'static' qualifier?  If so, you could use the 
'SCM_INTERNAL [...] scm_i_[...]' pattern, e.g. see 
'scm_i_is_mutable_bitvector' in libguile/bitvectors.h and 
libguile/bitvectors.c.


>    if (SCM_UNBNDP (file))
>     c_fd = -1;
>   else {
>     /* Use the fd under clobber protection from GC or another thread. */
>     if (SCM_PORTP (file))
>       c_fd = scm_to_int (scm_fileno (file));
>     else {
>       c_fd = scm_to_int (file);
>       file = SCM_CAR (scm_fdes_to_ports (file));
>     }
>     scm_dynwind_acquire_port (file);
>   }

You need to acquire the port _before_ taking its file descriptor! 
Otherwise, it is protected too late.  I.e., scm_dynwind_acquire_port 
needs to be moved before the 'scm_fileno'.

On the second branch, in particular 'c_fd = scm_to_int (file);':
IIUC, the idea is to, when a raw fd is passed, look up the corresponding 
port to lock it, right?

If so, I think it's too late for that -- another thread might change 
things between 'c_fd = ...' and 'file = SCM_CAR (scm_fdes_to_ports (file))'.

More generally, when a raw fd is passed, I think it's impossible to 
solve the 'other thread/GC interfering' problem.

As such, to simplify things, I propose to only do the 
'scm_dynwind_acquire_port' when a port is passed, instead of failing to 
solve the interference problems (if the user passed a raw fd, then only 
they can make sure there are no problems, e.g. by changing their code to 
use ports or by not using move->fdes stuff).:

Proposed code (untested):

{
   void *c_mem, *c_addr;
   size_t c_len;
   int c_prot, c_flags, c_fd;
   scm_t_off c_offset;
   SCM pointer, bvec;

   if (SCM_POINTER_P (addr))
     c_addr = SCM_POINTER_VALUE (addr);
   else if (scm_is_integer (addr))
     c_addr = (void*) scm_to_uintptr_t (addr);
   else
     scm_misc_error ("mmap", "bad addr", addr);

   c_len = scm_to_size_t (len);

   if (SCM_UNBNDP (prot))
     c_prot = PROT_READ | PROT_WRITE;
   else
     c_prot = scm_to_int (prot);

   if (SCM_UNBNDP (flags))
     c_flags = MAP_ANONYMOUS | MAP_PRIVATE;
   else
     c_flags = scm_to_int (flags);

   scm_dynwind_begin (0);
   if (SCM_UNBNDP (file))
     c_fd = -1;
   else if (scm_is_integer (file))
     c_fd = scm_to_int (file);
   else
     {
       /* Use the fd of the port under clobber protection from
          concurrency. As scm_dynwind_acquire_port assumes that
          FILE is a port, check that first. */
       SCM_VALIDATE_PORT (SCM_ARG5, file);
       scm_dynwind_acquire_port (file);
       c_fd = scm_fileno (file);
     }

   if (SCM_UNBNDP (offset))
     c_offset = 0;
   else
     c_offset = scm_to_off_t (offset);

   if ((c_addr == NULL) && (c_flags & MAP_FIXED))
     scm_misc_error ("mmap", "cannot have NULL addr w/ MAP_FIXED", SCM_EOL);

   SCM_SYSCALL (c_mem = mmap(c_addr, c_len, c_prot, c_flags, c_fd, 
c_offset));
   if (c_mem == MAP_FAILED)
     scm_syserror ("mmap");              /* errno set */

   /* The fd is free to go now. */
   scm_dynwind_end ();

   pointer = scm_cell (scm_tc7_pointer, (scm_t_bits) c_mem);
   bvec = scm_c_take_typed_bytevector((signed char *) c_mem + c_offset, 
c_len,
                      SCM_ARRAY_ELEMENT_TYPE_VU8, pointer);
   scm_i_set_finalizer (SCM2PTR (bvec), mmap_finalizer, (void*) c_len);
   return bvec;
}
#undef FUNC_NAME



[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

  reply	other threads:[~2023-01-14 22:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14  0:49 patch for mmap and friends Matt Wette
2023-01-14  1:00 ` Matt Wette
2023-01-14 15:18 ` Maxime Devos
2023-01-14 16:31   ` Matt Wette
2023-01-14 22:08     ` Matt Wette
2023-01-14 22:42       ` Maxime Devos [this message]
2023-01-14 23:46         ` Matt Wette
2023-01-14 16:41   ` tomas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=23890f8a-8891-d888-b289-c0d06304fff1@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=guile-devel@gnu.org \
    --cc=matt.wette@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).