<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - Clang does not implement the noclone attribute"
   href="http://llvm.org/bugs/show_bug.cgi?id=22311">22311</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang does not implement the noclone attribute
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.5
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Driver
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sstewartgallus00@mylangara.bc.ca
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>noclone is a companion attribute to the noinline attribute that GCC implements.
It prevents constant propagation from happening.  If one is doing magic low
level hackery then it is a necessity.  A specific use case I have for this is
creating a safe wrapper for vfork (which most compilers understandably mess up
on).

The wrapper:

__attribute__((noinline))
__attribute__((noclone))
__attribute__((no_sanitize_address))
static pid_t safe_vfork(int (*f)(void *), void * arg)
{
        __atomic_signal_fence(__ATOMIC_SEQ_CST);

        pid_t child = vfork();
        if (0 == child)
                _Exit(f(arg));

        return child;
}

Some workarounds:

Use assembly: This is annoying but would work.
Put safe_vfork in a separate module: This doesn't work with link-time
optimization but is the typical workaround.
Use volatile on the function arguments as follows: Strangely, this doesn't
actually work.  I can further work around this with more indirection but I am
hesitant to do so because the semantics of volatile are very unclear and
compiler specific.

__attribute__((noinline))
__attribute__((noclone))
__attribute__((no_sanitize_address))
static pid_t safe_vfork(int (*volatile f)(void *), void * volatile arg)
{
        __atomic_signal_fence(__ATOMIC_SEQ_CST);

        pid_t child = vfork();
        if (0 == child)
                _Exit(f(arg));

        return child;
}</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>