Re: Memory management for io_read(), etc.

2021-04-24 Thread Samuel Thibault
Andrew Eggenberger, le sam. 24 avril 2021 17:29:23 -0500, a ecrit:
> Thanks! But now I'm getting a segfault when I try to vm_deallocate. It seems 
> to
> be the result of calling memchr on the vm_allocated buffer. Here is the
> function that calls memchr:

Where is the segfault exactly? Are you sure that the file contains a
\0 ?

> memchr returns a pointer to the first instance of the char argument, but does
> it also change where its first argument points?  

? functions cannot change their parameters. More precisely they can
change their parameter, but cannot magically change whatever the caller
happened to pass to the call.

Samuel



Re: Memory management for io_read(), etc.

2021-04-24 Thread Andrew Eggenberger
Thanks! But now I'm getting a segfault when I try to vm_deallocate. It
seems to be the result of calling memchr on the vm_allocated buffer. Here
is the function that calls memchr:

int
binaryfilep(char* buf)
{
  return memchr(buf, '\0', 1024) != NULL;
}

if I change it to

int
binaryfilep(char* buf)
{
  return 1;
}

I'm able to use vm_deallocate.

The whole sequence looks like this:

err = io_read(innerdir, (char**)&filebuf, &amt_written, 0, s.st_size + 1);

isbinfile = binaryfilep((char*)filebuf);

vm_deallocate(mach_task_self(), (vm_offset_t)filebuf, filebufsize);

memchr returns a pointer to the first instance of the char argument, but
does it also change where its first argument points?


*Andrew Eggenberger*


Re: Memory management for io_read(), etc.

2021-04-24 Thread Samuel Thibault
Andrew Eggenberger, le sam. 24 avril 2021 16:06:25 -0500, a ecrit:
> Thanks Samuel. So if I'm understanding correctly, if I let the RPC vm_allocate
> () filebuf I don't need to worry about vm_deallocating it?

You have to vm_deallocate() it, otherwise it'll leak.

> And if I wanted to pass a buffer in and reuse it, I should use
> vm_allocate() instead of malloc()? 

You can use malloc, you'll then have to use free on it *and*
vm_deallocate the value set by the RPC if it did change the pointer.
That's clearer to just use vm_deallocate in both case. See how it's done
in various codeplaces, you'll find tests such as if (buf != alloc_buf)
vm_deallocate(alloc_buf);

Samuel



Re: Memory management for io_read(), etc.

2021-04-24 Thread Andrew Eggenberger
Thanks Samuel. So if I'm understanding correctly, if I let the RPC
vm_allocate() filebuf I don't need to worry about vm_deallocating it? And
if I wanted to pass a buffer in and reuse it, I should use vm_allocate()
instead of malloc()?

*Andrew Eggenberger*


Re: Memory management for io_read(), etc.

2021-04-24 Thread Samuel Thibault
Andrew Eggenberger, le jeu. 22 avril 2021 20:37:02 -0500, a ecrit:
> But when I try to do the same in my translator, I get a "free(): invalid
> pointer"
> error. Here is the relevant portion of my code.
> 
>       filebuf = malloc(s.st_size + 1);
>       err = io_read(innerdir, &filebuf, &amt_written, 0, s.st_size);
>       free(filebuf);

Note that the RPC may replace filebuf with its own vm_allocate()-ed
buffer. The guide should not be documenting the use of malloc(), that's
confusing. Either the program has an already-allocated buffer to put the
data directly in, that it won't actually want to free. Or it wants to
allocate dynamically, and it's simpler to just make it NULL so the RPC
does the allocation. Possibly on successive RPC calls it's useful to
reuse an allocation, and eventually use vm_deallocate(), but surely not
free().

Samuel



Memory management for io_read(), etc.

2021-04-22 Thread Andrew Eggenberger
In its example of reading from a file the kurdish way, the hurd hacking
guide mallocs a buffer an passes it to io_read. The program doesn't
call free() on the malloced buffer but when I add it it doesn't complain.

  /* Read */
  err = io_read (f, &buf, &amount, -1, amount);
  if (err)
error (1, errno, "Could not read from file %s", argv[1]);
  buf[amount] = '\0';
  mach_port_deallocate (mach_task_self (), f);

  /* Output */
  printf ("%s", buf);
  free(buf); /* added by me */

But when I try to do the same in my translator, I get a "free(): invalid
pointer"
error. Here is the relevant portion of my code.

  filebuf = malloc(s.st_size + 1);
  err = io_read(innerdir, &filebuf, &amt_written, 0, s.st_size);
  free(filebuf);

I put the free right after the io_read to test whether something I was
doing after
reading it into the buffer might be causing the error, but it still
occurred.

The program works fine without mallocing the buffer at all, and it works
fine when
I malloc but don't free. But both of those would seem to create memory
leaks. Can
anyone help me understand what's going on here?

Thanks

*Andrew Eggenberger*