title: How do I get the return address of a function?
body: <p>I am writing a Rust library containing an implementation of the callbacks for LLVM <a href="https://clang.llvm.org/docs/SanitizerCoverage.html" rel="noreferrer">SanitizerCoverage</a>. These callbacks can be used to trace the execution of an instrumented program.</p>
  
  <p>A common way to produce a trace is to print the address of each executed basic block. However, in order to do that, it is necessary to retrieve the address of the <code>call</code> instruction that invoked the callback. The C++ examples provided by LLVM rely on the compiler intrinsic <code>__builtin_return_address(0)</code> in order to obtain this information.</p>
  
  <pre><code>extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
    if (!*guard) return;
    void *PC = __builtin_return_address(0);
    printf("guard: %p %x PC %p\n", guard, *guard, PC);
  }
  </code></pre>
  
  <p>I am trying to reproduce the same function in Rust but, apparently, there is no equivalent to <code>__builtin_return_address</code>. The only reference I found is from an <a href="https://doc.rust-lang.org/1.7.0/core/intrinsics/fn.return_address.html" rel="noreferrer">old version</a> of Rust, but the function described is not available anymore. The function is the following:</p>
  
  <p><code>pub unsafe extern "rust-intrinsic" fn return_address() -&gt; *const u8</code></p>
  
  <p>My current hacky solution involves having a C file in my crate that contains the following function:</p>
  
  <pre><code>void* get_return_address() {
    return __builtin_return_address(1);
  }
  </code></pre>
  
  <p>If I call it from a Rust function, I am able to obtain the return address of the Rust function itself. This solution, however, requires the compilation of my Rust code with <code>-C force-frame-pointers=yes</code> for it to work, since the C compiler intrinsic relies on the presence of frame pointers.</p>
  
  <p>Concluding, is there a more straightforward way of getting the return address of the current function in Rust?</p>
  
  <p><strong>edit:</strong> The removal of the <code>return_address</code> intrinsic is discussed in <a href="https://github.com/rust-lang/rust/issues/34227" rel="noreferrer">this</a> GitHub issue.</p>
  
  <p><strong>edit 2:</strong> Further testing showed that the <a href="https://crates.io/crates/backtrace" rel="noreferrer"><code>backtrace</code></a> crate is able to correctly extract the return address of the current function, thus avoiding the hack I described before. Credit goes to <a href="https://twitter.com/H2CO3_iOS/status/1102890010565595136" rel="noreferrer">this</a> tweet.</p>
  
  <p>The problem with this solution is the overhead that is generated creating a full backtrace when only the return address of the current function is needed. In addition, the crate is using C libraries to extract the backtrace; this looks like something that should be done in pure Rust.</p>
  
  <p><strong>edit 3:</strong> The compiler intrinsic <code>__builtin_return_address(0)</code> generates a call to the LLVM intrinsic <code>llvm.returnaddress</code>. The corresponding documentation can be found <a href="https://llvm.org/docs/LangRef.html#llvm-returnaddress-intrinsic" rel="noreferrer">here</a>.</p>