Zig 2024 roadmap
Zig 2024 roadmap
Posted Feb 4, 2024 16:39 UTC (Sun) by matthias (subscriber, #94967)In reply to: Zig 2024 roadmap by ballombe
Parent article: Zig 2024 roadmap
Rust is quite precise in what memory safety means. First and foremost it means that it is impossible to invoke undefined behavior, e.g. data races. It certainly does not mean that the owner of a portion of memory cannot write data to the memory.
> I work on a C software that use a custom memory manager. It works this way:
> Use mmap to allocate a large ( 1GB to 1TB) array of virtual memory.
> The custom memory allocator will give you a range of indices that you can use in this array.
The question is: Who owns the array? If it is owned by the allocator, then all reads and writes must go through the allocator. Only the function/struct that owns this array is allowed to access it. Of course you can give away mutable access to the array, but again, there can be only a single owner of the mutable reference.
> From the point of view of rust (and valgrind!), all the memory is owned by main(), there is no pointers, all accesses are checked to be within the bound of the allocation, all memory is initialized etc.
Why should the memory be owned by main()? It should be owned by your custom allocator (which is transitively owned by main()).
> But really, there is nothing that prevent the code to write outside the allocated range of indices as long as it is inside the array. So buffer overflow are still possible, even though the memory manager is safer than malloc.
It depends how you give access to the memory. If you really only give indices and have a functions in the allocator for reads and writes, then yes, you have a function that can write to arbitrary locations in the array. It is perfectly safe to do so and this will not invoke undefined behavior. The program might not do what you want, but this is not what memory safety means.
If you you give slices of the array, then each receiver of a slice can only write to that slice and you have bounds checking etc. This is the only way different parts of the program can modify parts of the array independent of each other. However, rust will make sure that they only write to the parts of the array, the custom allocator has reserved for them. Writing such an allocator certainly requires some unsafe code to split up the one array into several smaller pieces that have independent lifetimes. There is at least split_at_mut() as a safe abstraction, so you could build this type of allocator using only safe code, but you certainly will need a bit more fine grained control if you really write an allocator yourself.
Valgrind will only see individual allocations, but rusts ownership tracking can be much more fine grained than a single allocation from the system allocator. And clearly it will ensure that you cannot overwrite the stack and that there are no data races and much more.
Posted Feb 4, 2024 17:18 UTC (Sun)
by mb (subscriber, #50428)
[Link]
Certainly.
But note that the allocator traits are unsafe. That means if an allocator implements these traits, it promises to not violate memory safety rules. Therefore, an allocator can't reduce Rust's 100%-safe guarantee, unless it is unsound. An unsound allocator is a bug.
And if you don't register the allocator to Rust by implementing the unsafe trait and your allocator is 100% safe Rust, then it's just a normal piece of code that is also 100% memory safe. Doesn't reduce Rust's guarantees either.
Rust's safety guarantees assume and depend on all unsafe code and the operating environment (all linked libraries, the operating system and the hardware) to be sound and not violate Rust's memory model.
Of course that means in practice one will probably find bugs in unsafe or foreign language code that breaks Rust's safety guarantees. A CVE in libc, for example (https://lwn.net/Articles/960289/). But that is an argument for pushing Rust code even further down the chain of dependencies, into the operating system.
Posted Feb 5, 2024 10:13 UTC (Mon)
by ballombe (subscriber, #9523)
[Link]
This is my point. rust defines memory safety, not the other way round.
Zig 2024 roadmap
Zig 2024 roadmap