<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - GetArgsAndEnv() can not get correct 'envp' pointer on linux"
   href="https://bugs.llvm.org/show_bug.cgi?id=46598">46598</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>GetArgsAndEnv() can not get correct 'envp' pointer on linux
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>compiler-rt
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>zhaomaosu@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>GetArgsAndEnv() in sanitizer_linux.cpp:589 will count argc by detecting '\0':

============================================================================
static void GetArgsAndEnv(char ***argv, char ***envp) {
..........................................                                      
  if (&__libc_stack_end) {                                                      
    int argc = 0;                                                               
    while (stack_end[argc + 1]) argc++;  // <------------
..........................................
============================================================================
However googletest will shift the remainder of the argv list left by one
(llvm/utils/unittest/googletest/src/gtest.cc:ParseGoogleTestFlagsOnlyImpl())
and fork a new child process to run tests. 

============================================================================
template <typename CharType>
void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
..................................................
  if (remove_flag) {
    for (int j = i; j != *argc; j++) {
      argv[j] = argv[j + 1];
    }
    (*argc)--;
    i--;
  }
.................................................
}
============================================================================

If we exec unittest with one arg, then the stack end layout of new process will
like:

"argv[0]\0\0envp[0]....."

This will cause GetArgsAndEnv() get wrong argc and envp.

I think the second pass to read '/proc/self/environ' for GetArgsAndEnv() may be
a best choice.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>