4 ms·
I'm pretty sure catching this in all cases would involve solving the Halting Problem.
by 1f60c 5y ago
I'm pretty sure catching this in all cases would involve solving the Halting Problem.
- Waterluvian 5y agoI think if we wanted a concrete answer you’d be right. But I feel like a static analyzer could clearly identify “this line can allocate” just by knowing what language features can allocate. And for third party library methods “does this function have anything inside of it that could allocate?”
- oconnor663 5y agoIf you're allowed to have false positives, it's not so hard. Like "this function allocates if A or B is true" just becomes "this function can allocate". It's not so different from finding functions that can panic.
- Xylakant 5y agoI’m fairly certain that this does not require solving the halting problem. Fundamentally, every line of code can be transformed to the underlying code constructs, and those could be checked for allocations. Doing this in an efficient manner is probably hard, but certainly not impossible
- umanwizard 5y agofn maybe_alloc() -> Vec<usize> { if collatz_conjecture_is_true() { vec![42] } else { vec![] } } fn main() { let v = maybe_alloc(); // Does this allocate? }
- wtetzner 5y agoYou would just assume it does. Really you want to detect all places that might allocate.
- umanwizard 5y agoYou are right. It’s definitely possible if you accept false positives.
- wtetzner 5y agoYou can absolutely statically determine where allocations happen. You can't know for sure if all of those places are reachable in a running program, but that's not necessary to solve this problem.