The case for lack of recursive functions in kernel

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: The case for lack of recursive functions in kernel

Post by iansjack »

catnikita255 wrote:Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.
I wouldn't be that happy. It's a gross oversimplification to suppose that a circular reference can only happen directly.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: The case for lack of recursive functions in kernel

Post by Roman »

Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.
What if symbolic link #1 points to symbolic link #2, which then points to symbolic link #3, which returns to the first symbolic link?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: The case for lack of recursive functions in kernel

Post by Combuster »

To guarantee stack safety you'd want to write out the tail recursion optimisation by hand and don't rely on the compiler to fix the recursive function call for you:

Code: Select all

while (file->is_symlink()) file = resolve_symlink(file);
Note that while() loops, as opposed to your common for(i=...;i<...;i++), loops provokes pretty much the same demand for code scrutiny as recursive calls do.


The second part of the problem is less trivial. To detect circular dependencies exactly you'd need to cache all resolutions thus far:

Code: Select all

hash_set->add(file);
file = resolve_symlink(file);
if (hash_set->contains(file)) generate_error();
Of course caching all symlinks might be a optimisation issue for the common 1-2 symlink case and a potentional memory issue for (an attacker's) really long symlink chains, so taking a heuristic approach to the issue to prevent abuse might be an overall better idea.

Code: Select all

iterations++;
if (iterations > 8) generate_error();
file = resolve_symlink(file);
$.02
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: The case for lack of recursive functions in kernel

Post by alexfru »

Combuster wrote: The second part of the problem is less trivial. To detect circular dependencies exactly you'd need to cache all resolutions thus far:
Loop detection in a linked list a classical problem, which is often asked on interviews.
See Floyd's "tortoise and hare" cycle detection algorithm.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: The case for lack of recursive functions in kernel

Post by Combuster »

Mind that symlink resolution means (slow) disk I/O, and tortoise-hare solutions make up to thrice as many accesses to disk as strictly needed.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply