4 ms·
What's the advantage of IFUNCS, over putting a function in the library that selects the implementation, either via function pointers or a switch/if? In particul
by jcarrano 3y ago
What's the advantage of IFUNCS, over putting a function in the library that selects the implementation, either via function pointers or a switch/if? In particular given that they seem to be quite fragile and exploitable too.
I don't have much experience in low-level optimization, but would a modern CPU not be able to predict the path taken by a brach that tests the CPU features.
- jcalvinowens 3y ago> either via function pointers or a switch/if? > but would a modern CPU not be able to predict the path taken by a brach that tests the CPU features. That's true, but the CPU has finite branch predictor state, and now you've wasted some of it. Indirect calls hurt too, especially in you need retpolines. This is a great read: https://www.agner.org/optimize/microarchitecture.pdf https://www.agner.org/optimize/microarchitecture.pdf The Linux kernel has interfaces for doing the same thing, more explicitly than ifunc: https://docs.kernel.org/next/staging/static-keys.html https://docs.kernel.org/next/staging/static-keys.html https://lwn.net/Articles/815908/ https://lwn.net/Articles/815908/
- account42 3y agoThe advantage is that it avoids an extra function pointer indirection when the function you are trying to swap out is already an exported library function. For purely internal functions like in this case the only advantage is that it allows this exploit.