3 ms·
> Any instance of undefined behavior should result in a Warning, if not an Error How would you implement this? For example, use-after-free is undefined, and di
by millstone 7y ago
> Any instance of undefined behavior should result in a Warning, if not an Error
How would you implement this? For example, use-after-free is undefined, and difficult to detect.
- vnorilo 7y agoWarning when optimization passes take advantage of UB is a reasonable desire. The compilers are getting increasingly good at this, but I believe it would be far too noisy to blanket enable all of that.
- mjevans 7y agoThe point is to push the info to the programmer so that the code can be re-written as only defined behavior. Yes this would eliminate the 'gains' of abusing UB, but it would actually fix the errors of abusing UB.
- vnorilo 7y agoI guess signed indvars would be a prime example. Compilers famously ignore overflow of signed integers when computing loop trip counts. Would it be better to only ever use unsigned integers for indvars and loop limits? Maybe. But that would reach deep into the types in stdlib for example. And then there's https://software.intel.com/en-us/forums/intel-c-compiler/topic/698664 https://software.intel.com/en-us/forums/intel-c-compiler/top... So, all in all, not simple. Not that that invalidates your point: I just wanted to add some nuance.
- marcan_42 7y agoThe problem is the compiler cannot prove UB actually happens. The point of these "use UB to optimize" approaches isn't to screw over simple programs with bugs. It's to better optimize larger, correct programs where higher levels of logic that the compiler is unaware of ensure UB never happens.
- jcelerier 7y agoC++ is able to do it in constexpr contexts - a constexpr function cannot exhibit UB (else it's a compile error)
- saagarjha 7y agoThis is technically correct, but I believe this is also a very buggy area of the compiler and there are cases where it will accept undefined behavior in a constexpr context: https://shafik.github.io/c++/undefined%20behavior/2019/05/11/explporing_undefined_behavior_using_constexpr.html https://shafik.github.io/c++/undefined%20behavior/2019/05/11...
- jcelerier 7y agoright, but all compilers / interpreters for all languages have bugs.
- saagarjha 7y agoThey do, but I'm just pointing out that trying to use constexpr as the judge of what is defined is generally not a great idea at the moment unless what you're actually trying to do is find some language-lawyer compiler bugs to file.
- pjmlp 7y agoMost static analyzers are already able to spot a big chunck of such cases, even if not everything, e.g. clang tidy and VC++ 2019 lifetime profile. Additionally, SPARC ADI, ARM MTE and CHERRY can be used to invalidate memory regions after free and trap on further accesses. While not perfect, better than nothing.