I know that if you have a region or buffer of uninitialized memory, say, from malloc(), it's UB to read from that memory - you have to initialize it first by writing to it.
However, you can pass uninitialized memory to the read
method of the Read
trait, as long as the implementation guarantees it ever only writes to the memory and returns the correct amount of bytes written. That memory is then considered initialized.
My question is, how is it that it is then initialized? You pass a pointer and a length to libc::read()
, how does the compiler "know" the kernel has written data into it?
Same thing for libc::mmap()
, you call a libc function, get back a pointer and a length, you call slice::from_raw_parts
and then that memory is considered initialized? How?
If it is because the pointer is passed to or gotten from an FFI function, how does the compiler know the length of the memory region?
And it is is FFI, how come memory acquired from libc::malloc()
is considered uninitialized?
I hope someone can explain