<div dir="ltr">Dear Dr. Artem,<div><br></div><div>I have the following queries.</div><div><ul><li>Will I ever require SchrodingerLocked? (As you mentioned, a mutex becomes a Schrodinger only when pthread_mutex_destroy() is called. Now, calling pthread_mutex_destroy() after pthread_mutex_lock() is anyway wrong. So, a warning should be raised in this case.)</li><li>SVal corresponding to lck_mtx_destroy() is Unknown or Undefined. Hence, I am not able to obtain a symbol for the return value of lck_mtx_destroy() which is leading to incorrect output on the testcase pthreadlock.c.</li></ul><div>I'm also attaching the modified code.</div></div><div>I want you to check certain parts of the code. (I have added capitalized comments just before the parts which I want you to check.)</div><div><br></div><div>Thank you.</div><div><br></div><div><br></div><div>Regards,</div><div>Malhar</div></div><div hspace="streak-pt-mark" style="max-height:1px"><img alt="" style="width:0px;max-height:0px;overflow:hidden" src="https://mailfoogae.appspot.com/t?sender=aY3MxM2IxMDMxQGlpdGguYWMuaW4%3D&type=zerocontent&guid=4cba4501-03e2-461b-9219-db4196c855d0"><font color="#ffffff" size="1">ᐧ</font></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 19, 2017 at 12:11 PM, Artem Dergachev <span dir="ltr"><<a href="mailto:noqnoqneo@gmail.com" target="_blank">noqnoqneo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">For completeness, i'd point out that there's a simpler alternative approach to this: force the analyzer to split the state immediately on pthread_mutex_destroy(): construct a state where the mutex is destroyed and symbol is known-to-be-zero, construct a state where the mutex remains the same and symbol is known-to-be-non-zero, and addTransition to both of them. This forces the analyzer to investigate two concrete states in parallel, rather than one "Schrödinger" state that would still be split when the return value is checked.<br>
<br>
However, that approach pays a huge price of doubling analysis time - the time we would spend in the "Schrödinger" state in the original approach would be doubled here (and if the return value is truly unchecked, that may go on until the end of the analysis, because even after the symbol dies the mutex state is still different, so the paths wouldn't be merged). This is why we try to avoid state splits unless it is definitely necessary.<div class="HOEnZb"><div class="h5"><br>
<br>
On 4/19/17 9:22 AM, Artem Dergachev wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Yep!<br>
<br>
You are right that simply unlocking the failed-to-be-destroyed mutex in checkDeadSymbols is not enough. The reason for that is, the moment when the symbol dies is quite unpredictable, and we don't know how many things have happened to the mutex between the failed destroy and the symbol death - it might have been locked and unlocked many times in between.<br>
<br>
I'd propose that in order to maintain the correct mutex state you'd need to check the destruction-return-value symbol in every operation as well. For instance, when somebody tries to lock a mutex, see if it was destroyed previously. If it was destroyed, look at the symbol:<br>
* If the destruction was checked and failed on that execution path (as you can find out by looking at the symbol's constraints in the program state), then it's ok, remove that symbol from the program state and mark the mutex as locked.<br>
* If the destruction cannot be proven to have failed on this path, then either it is known to have succeeded, or destruction wasn't checked for failure properly on this path. It means we have a use-after-destroy, and we report the warning accordingly.<br>
<br>
At the same time, checkDeadSymbols is still necessary: if no mutex state changes happen after destroy before symbol death, we need to collect the information from the dying symbol. Because the symbol is dying, this information will no longer ever be made more accurate (that is, constraints wouldn't ever become more strict anymore), so it's as accurate as possible. So we can be sure if the symbol should be truly set to destroyed or reverted to the previous state.<br>
<br>
Most path-sensitive checkers are kind of state machines (see also "typestate"). They assign a state to an object, and then various events (coming from checker callbacks) affect this state in one way or another. You can draw the state diagram.<br>
<br>
So my proposal is to have the following states: Initialized, Locked, Unlocked, SchrödingerInitialized, SchrödingerLocked, SchrödingerUnlocked, Destroyed. Schrödinger states indicate that we're having a Schrödinger mutex that is alive and destroyed at the same time on different execution paths. The mutex becomes a Schrödinger mutex after pthread_mutex_destroy() and remains there until the box is opened^W^W^W^W either we try to conduct more operations on the mutex, or the return-symbol of pthread_mutex_destroy() is dead. At such moments we collapse the mutex state to either the previous state (eg. SchrödingerLocked -> Locked)  or to Destroyed. The easiest way to represent Schrödinger states would be something like "still Locked, but with destroy-return-symbol set present for this mutex"; upon collapse we remove the return symbol.<br>
<br>
On 4/19/17 8:54 AM, Malhar Thakkar wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dear Dr. Artem,<br>
<br>
Thank you for your response. After incorporating 'addTransition', I was able to suppress the double-destroy. But, when 'pthread_mutex_destroy' was called for the second time (i.e., inside the if branch), LockState was found to be NULL (which it should be according to my code) but ideally it should be in the Unlocked state. For, it to be in Unlocked state, I think state->set<LockMap>() should be executed inside checkDeadSymbols() instead of executing it inside AcquireLock(), ReleaseLock(), DestroyLock() and InitLock() as I am unable to find a way to do it otherwise.<br>
<br>
Regards,<br>
Malhar<br>
ᐧ<br>
<br>
On Tue, Apr 18, 2017 at 8:30 PM, Artem Dergachev <<a href="mailto:noqnoqneo@gmail.com" target="_blank">noqnoqneo@gmail.com</a> <mailto:<a href="mailto:noqnoqneo@gmail.com" target="_blank">noqnoqneo@gmail.com</a>>> wrote:<br>
<br>
    Before anything, please note that `State' is your local variable;<br>
    assigning to `State' doesn't immediately affect analysis in any<br>
    way. In order to make your changes to the program state take<br>
    effect, you'd need to put the changed state back into the<br>
    `CheckerContext' by calling its `addTransition' method, like other<br>
    checker callbacks do.<br>
<br>
    Otherwise, your code looks reasonable to me. If everything is<br>
    implemented correctly, i believe that your code would already<br>
    suppress the double-destroy warning on this branch.<br>
<br>
<br>
    On 4/18/17 5:53 PM, Malhar Thakkar wrote:<br>
<br>
        Test-case:<br>
<br>
        pthread_mutex_lock(&mtx1);<br>
        pthread_mutex_unlock(&mtx1);<br>
        int retval = pthread_mutex_destroy(&mtx1);<br>
        if(retval != 0){<br>
        pthread_mutex_destroy(&mtx1); (1)<br>
        }<br>
        else<br>
        printf("Already destroyed\n");<br>
<br>
<br>
        Regarding my previous query about reverting the LockState, how<br>
        would I ensure that the LockState at (1) is Unlocked and not<br>
        Destroyed?<br>
<br>
        Following is the code for checkDeadSymbols()<br>
        void PthreadLockChecker::checkDeadS<wbr>ymbols(SymbolReaper &SymReaper,<br>
                           CheckerContext &C) const {<br>
          ProgramStateRef State = C.getState();<br>
          ConstraintManager &CMgr = State->getConstraintManager();<br>
<br>
          RetValConstraintTy TrackedSymbols =<br>
        State->get<RetValConstraint>()<wbr>;<br>
          for (RetValConstraintTy::iterator I = TrackedSymbols.begin(),<br>
             E = TrackedSymbols.end(); I != E; ++I) {<br>
            SymbolRef Sym = I->second;<br>
            const MemRegion* lockR = I->first;<br>
            bool IsSymDead = SymReaper.isDead(Sym);<br>
            const LockState* LState = State->get<LockMap>(lockR);<br>
            // Remove the dead symbol from the return value symbols map.<br>
            if (IsSymDead){<br>
              ConditionTruthVal retZero = CMgr.isNull(State, Sym);<br>
        if(retZero.isConstrainedFalse(<wbr>)){<br>
                // Update LockMap<br>
                State = State->remove<LockMap>(lockR); // I know this<br>
        is incorrect but even after removing this entry                      from the map, (1) raises a warning saying, "This lock has<br>
        already been destroyed".<br>
              }<br>
              State = State->remove<RetValConstraint<wbr>>(lockR);<br>
            }<br>
          }<br>
        }<br>
<br>
        Thank you.<br>
<br>
<br>
        Regards,<br>
        Malhar<br>
<br>
<br>
        ᐧ<br>
<br>
        On Mon, Apr 17, 2017 at 8:34 PM, Devin Coughlin<br>
        <<a href="mailto:dcoughlin@apple.com" target="_blank">dcoughlin@apple.com</a> <mailto:<a href="mailto:dcoughlin@apple.com" target="_blank">dcoughlin@apple.com</a>><br>
        <mailto:<a href="mailto:dcoughlin@apple.com" target="_blank">dcoughlin@apple.com</a> <mailto:<a href="mailto:dcoughlin@apple.com" target="_blank">dcoughlin@apple.com</a>>>> wrote:<br>
<br>
<br>
                On Apr 17, 2017, at 4:55 AM, Malhar Thakkar<br>
            <<a href="mailto:cs13b1031@iith.ac.in" target="_blank">cs13b1031@iith.ac.in</a> <mailto:<a href="mailto:cs13b1031@iith.ac.in" target="_blank">cs13b1031@iith.ac.in</a>><br>
                <mailto:<a href="mailto:cs13b1031@iith.ac.in" target="_blank">cs13b1031@iith.ac.in</a><br>
            <mailto:<a href="mailto:cs13b1031@iith.ac.in" target="_blank">cs13b1031@iith.ac.in</a>>><wbr>> wrote:<br>
<br>
                Consider the following test-case:<br>
<br>
                int retval = pthread_mutex_destroy(&mtx1);<br>
                if(retval == 0){<br>
                pthread_mutex_lock(&mtx1);<br>
                }<br>
<br>
<br>
                REGISTER_MAP_WITH_PROGRAMSTATE<wbr>(RetValConstraint, const<br>
            MemRegion<br>
                *, SymbolRef)<br>
<br>
                I have added the following snippet for checkDeadSymbols:<br>
                void PthreadLockChecker::checkDeadS<wbr>ymbols(SymbolReaper<br>
            &SymReaper,<br>
                 CheckerContext &C) const {<br>
                  // std::cout<<"Checking dead symbols\n";<br>
                ProgramStateRef State = C.getState();<br>
                ConstraintManager &CMgr = State->getConstraintManager();<br>
<br>
                RetValConstraintTy TrackedSymbols =<br>
            State->get<RetValConstraint>()<wbr>;<br>
                  // int counter = 0;<br>
                  for (RetValConstraintTy::iterator I =<br>
            TrackedSymbols.begin(),<br>
                                       E = TrackedSymbols.end(); I !=<br>
            E; ++I) {<br>
                // counter++;<br>
                // std::cout << "Counter: "<<counter<<std::endl;<br>
                SymbolRef Sym = I->second;<br>
                const MemRegion* lockR = I->first;<br>
                bool IsSymDead = SymReaper.isDead(Sym);<br>
<br>
                // Remove the dead symbol from the return value<br>
            symbols map.<br>
                if (IsSymDead){<br>
                ConditionTruthVal retZero = CMgr.isNull(State, Sym);<br>
                if(retZero.isConstrainedFalse(<wbr>)){<br>
                  std::cout<<"False\n";<br>
                  // Update LockMap<br>
                }<br>
                else if(retZero.isConstrainedTrue()<wbr>){<br>
                  std::cout<<"True\n";<br>
                }<br>
                State = State->remove<RetValConstraint<wbr>>(lockR);<br>
                std::cout<<"Removed\n";<br>
                    }<br>
                  }<br>
                }<br>
<br>
                Now, after the execution of<br>
            PthreadLockChecker::DestroyLoc<wbr>k(),<br>
                checkDeadSymbols() is executed twice before<br>
                PthreadLockChecker::AcquireLoc<wbr>k() is executed.<br>
<br>
                When checkDeadSymbols is first executed, there is just<br>
            one symbol<br>
                in RetValConstraint(trivial). But, this symbol is<br>
            constrained<br>
                /not /to be NULL. Then, the symbol is removed. During the<br>
<br>
                execution of checkDeadSymbols for the second time,<br>
            there is again<br>
                one symbol in RetValConstraint(I don't understand<br>
            this. Shouldn't<br>
                this symbol have been destroyed during the previous<br>
            execution?).<br>
                Nevertheless, this time, this symbol is constrained to<br>
            be NULL.<br>
<br>
<br>
            The static analyzer performs a path-sensitive exploration<br>
        of the<br>
            program. This means that when it sees a branch it will explore<br>
            both both sides of the branch independently (unless it can<br>
        tell<br>
            that one side is infeasible). In this case it means the<br>
        analyzer<br>
            will explore the path when retval is 0 and when it is not.<br>
        If you<br>
            haven’t read it yet, the “Debugging” section the the checker<br>
            development manual describes some tricks for how to<br>
        visualize the<br>
            analyzer’s exploration.<br>
<<a href="https://clang-analyzer.llvm.org/checker_dev_manual.html#commands" rel="noreferrer" target="_blank">https://clang-analyzer.llvm.o<wbr>rg/checker_dev_manual.html#com<wbr>mands</a><br>
<<a href="https://clang-analyzer.llvm.org/checker_dev_manual.html#commands" rel="noreferrer" target="_blank">https://clang-analyzer.llvm.o<wbr>rg/checker_dev_manual.html#com<wbr>mands</a>><br>
<<a href="https://clang-analyzer.llvm.org/checker_dev_manual.html#commands" rel="noreferrer" target="_blank">https://clang-analyzer.llvm.o<wbr>rg/checker_dev_manual.html#com<wbr>mands</a><br>
<<a href="https://clang-analyzer.llvm.org/checker_dev_manual.html#commands" rel="noreferrer" target="_blank">https://clang-analyzer.llvm.o<wbr>rg/checker_dev_manual.html#com<wbr>mands</a>>>>.<br>
            I find the debug.ViewExplodedGraph checker particularly<br>
        helpful.<br>
<br>
                Also, I have one more query. After fixing the above<br>
            mentioned<br>
                issue, how do I update LockMap in the sense that I'll<br>
            need to<br>
                revert the change made to it if the symbol is<br>
            constrained not to<br>
                be NULL. Apart from keeping track of the previous<br>
            LockState, is<br>
                there any other way to do it?<br>
<br>
<br>
            Since the analyzer keeps its representation of the program<br>
        state<br>
            per-path and explores paths independently, you shouldn’t<br>
        need to<br>
            do any reverting to undo the effects of simulating other<br>
        paths to<br>
            reaching a particular program point.<br>
<br>
            Devin<br>
<br>
<br>
<br>
<br>
</blockquote>
<br>
</blockquote>
<br>
</div></div></blockquote></div><br></div>