4 ms·
Double-free's can be tracked by doing data flow analysis on the function. This is how D does it in its nascent implementation of an Ownership/Borrowing system.
by WalterBright 7y ago
Double-free's can be tracked by doing data flow analysis on the function. This is how D does it in its nascent implementation of an Ownership/Borrowing system. It can be done without DFA if getting it only 90% right and having numerous false positives is acceptable.
I've used many static checkers in the past, and the rate of false positives was high enough to dissuade me from using them. This is why D uses DFA to catch 100% of the positives with 0% negatives. I knew this could be done because the compilers were using DFA in the optimization pass.
In order to get the tracking to work, one cannot just track things for a function named "free". After all, a common thing to do is write one's own custom storage allocators, and the compiler won't know what they are. Hence, there has to be some mechanism to tell the compiler when a pointer parameter to a function is getting "consumed" by the caller, and when it is just "loaned" to the caller (hence the nomenclature of an Ownership/Borrowing system).
One of the difficulties to overcome with D in doing this is there are several complex semantic constructs that needed to be deconstructed into their component pointer operations. I noticed that Rust simplified this problem by simplifying the language :-)
But once done, it works, and works satisfyingly well.
Note that none of this is a criticism of what GCC 10 does, because the article gives insufficient detail to draw any informed conclusions. But I do see this as part of a general trend that people are sick and tired of memory safety bugs in programming languages, and it's good to see progress on all fronts here.
- anchpop 7y ago> This is why, for D, I was determined to use DFA to catch 100% of the positives with 0% negatives. I knew this could be done because my compilers were using DFA in the optimization pass. Is this really true? I thought this was impossible due to Rice's theorem
- WalterBright 7y agoFortunately, I am unaware that it was impossible and did it anyway :-) But it is possible I made a mistake. It is also true that for it to work, one has to change the way one writes code, like Rust does. This is why D requires and @live attribute be added to functions to enable the checking for just those functions, so it doesn't break every program out there. It will enable incremental use of the checking at the user's option.
- nullc 7y agoYou're probably using a different definition of 100% than any impossibility proof would use. Consider some code: --- a=malloc(1); needfree=true; if (hashfn(first_factor(huge_static_rsanum1))&1){needfree=false;free(a);} if (hashfn(first_factor(huge_static_rsanum2))&1){needfree=false;free(a);} if(needfree)free(a); --- The decision if this has a double free or not depends on the factorizations of two huge difficult to factor constants. It either double-frees or not depending on those constants. Surely your software cannot decide that... What you probably mean is something like "100% on real programs rather than contrived cases". Of course, in that case, your definition of 'real programs' is the catch. :P Sometimes things that seem like they should always work except on contrived junk like the above example actually run into limitations in practice because macros and machine code generation produce ... well ... contrived junk from time to time.
- jjnoakes 7y agoI personally would want code like this flagged by static analysis. I would fix the code by not calling free in the second 'if' body if needfree was already false (just like the call at the end of the function is guarded).
- nullc 7y agoYou might want it flagged-- I would--, but it would still be a false positive if it did so (assuming the constants didn't result in a double-free). :)
- kccqzy 7y agoI think the argument is that static analyzers don't have to work on completely unchanged code, but rather rely on humans to structure the code in a way to avoid issues. Many years ago people regularly wrote things like "if (retval = f())" but after compilers started to complain about it people changed to write "if ((retval = f()))" instead, if they want an assignment and truthfulness testing at the same time.
- deleted 7y ago
- chrisseaton 7y ago> This is why D uses DFA to catch 100% of the positives with 0% negatives. For example if we have a graph data structure and we are visiting it and freeing nodes, how can your static analysis know that nodes will not be visited twice by my logic and freed twice?
- WalterBright 7y agoBecause the DFA follows edges, not logic. If free appears twice along any sequence of edges, the compiler rejects it.
- chrisseaton 7y agoI think that's why you're getting the response you are in this thread - if it really did '100% of the positives with 0% negatives', it'd have to consider the logic. There's an impossibility proof for that which has already been cited in the thread. It's usually illustrated using a function that solves the halting problem rather than the Goldbach conjecture. If the compiler rejects things that have free appear twice on branching control flow paths but aren't actually broken due to dynamic control flow (like your looping example given elsewhere) then that's a false positive, isn't it? And people thought '0% negatives' was meaning 'no false positives'.
- WalterBright 7y agoI see your point, and it's a valid criticism of my statement. I meant it more in the sense that the static checkers I have used would use hackish ad-hoc heuristics which did not work very well, and gets constantly tweaked by the implementor. The D implementation, in contrast, uses a well-defined rule outlined above, and it works predictably and reliably (absent implementation bugs, rather than ad hoc rule shortcomings). Another way of putting it is that the rules of the language are changed to make 0%/100% results workable. The compiler never says "there may be a problem here". It passes or is rejected. If it is passes, it is memory safe.
- 7y ago
- vanderZwan 7y ago> I noticed that Rust simplified this problem by simplifying the language :-) That's a bit of an open-ended qualifier. Would you say that is a positive simplification (an increase in elegance) or one that has a trade-off in reducing its expressive power?
- WalterBright 7y agoI mentioned it as another way of solving the problem. Which way you prefer is a matter of professional taste, something I'd really prefer not diverging into in this thread.