Programmers can't make this happen now, as there's no way to construct an Arc in constants with compile-time evaluation. We can make the Arc point to somewhere on static memories with the strong ref counter set to 2 so Arc won't accidentally release memories on static segments if dropped.
Since Arc is now available in constants, other APIs in Arc can be const fn as well.
That can't work because even ignoring the questions of how to detect if we're in consteval, when dropped (not in static but in const) it will free static memory which is UB.
That can't work because even ignoring the questions of how to detect if we're in consteval, when dropped (not in static but in const) it will free static memory which is UB.
You might be interested in the const heap effort.
I'm still confused on why it will free static memory, but I've viewed the tracking issue for const_heap and I realized that it is exactly what I'm expecting. Though const_heap is highly experimental now, It will be available in the coming future!
EDIT: const_heap is even better! Massive API that relies on Box / Arc / other heap allocation, can be const fn!
std::sync::Arc presently aborts the process if the refcount gets close to saturating, but I don't see anything in the docs that guarantees that behavior. I've seen some other Arc-like implementations which, if the refcount saturates, leaves the refcount at the maximum and leaks the allocation, and I think it would be possible for Arc to switch to this behavior (unless there's a guarantee somewhere I missed).
If we have Arcs that saturate the refcount instead of aborting, then it should be possible to make an Arc that points at static memory by constructing it with an already-saturated refcount, and then there's no risk of trying to free static memory.
A problem is that there no saturating atomic arithmetic (in Rust's atomic operations). In order to saturate the refcount, you would have to make every refcount update a compare-and-swap loop.
After reviewing the design of const_heap, I realized that what const_heap is exactly what we want. const_heap can intelligently figure out whether allocation in const evaluation is temporary or is actually used in runtime. Therefore, temporary allocation can be released and only those 'leak' to runtime is embedded into static segment.
Simpliy allowing Arc::const_new to return some memories on static segments (and set the strong ref counter to 2) is far less enough for a stable feature. Programmers can easily inject tons of unused data into static segments by calling Arc::const_new and drop the arc immediately. Implementing Arc::const_new should be scheduled after const_heap is ready,
const_heap will allow you to use Arc in const fn, but not to define const/static where there is an Arc in the final value. That will produce an error like this:
error: encountered `const_allocate` pointer in final value that was not made global
--> src/lib.rs:3:1
|
3 | const C: Vec<i32> = {
| ^^^^^^^^^^^^^^^^^
|
= note: use `const_make_global` to turn allocated pointers into immutable globals before returning
I'm not sure what a safe API for the "make refcount never reach 1" strategy would look like. (Even a refcount of 1 would be bad due to Arc::get_mut.)
You could use a scheme where the least significant bit is used for a has_saturated flag, so the stored value is 2 * count + (has_saturated as usize). Increments would look something like
let old = atomic.fetch_add(2);
if old > MAX_REFCOUNT {
atomic.fetch_or(1);
}
Once the value is odd, increments and decrements by two are free to wrap around; it can never reach zero.
A value of 1 is also problematic, however. It lets me get an &mut to the contents via get_mut. So ensuring that the value remains non-0 is not enough for soundness. You'd have to freeze the two least significant bits at 11 and use increments of 4, or something like that.
And there are deeper, more subtle problems. The const_heap allocations are always immutable at runtime (because they can be part of the value of a const and hence be shared among many instances of the value that should all behave independently). That's why Vec::const_make_global requires T: Freeze. CAS operations are UB on immutable memory. Arc needs to perform CAS in clone. So we just cannot have an Arc that internally points to immutable memory.
Arc in const is a lot harder than Vec or BTreeMap. None of the proposals we have seen so far are sufficient to make it sound.
I thought that for a moment too, but it's a count of 1 that is problematic. In this case, a count of 1 would be represented by the atomic being 2.
(So get_mut would need to check for the atomic being 2 instead of 1, and I believe it would otherwise be unchanged. strong_count and weak_count would be slightly more complicated though, they would have to branch on the has_saturated bit of the atomic.)
But there's still a window for someone to see the overflowed value before we set the last bit to 1. That's also a problem with our current "increment, check, abort" strategy. I don't think this can be solved in a strict way other than by entirely replacing the fetch_add with a CAS loop.
For those we have a much bigger problem as mentioned above. const_heap allocations that reach runtime code are read-only, and most atomic operations are not allowed on read-only memory. The fetch_add would be UB, unfortunately.