<div dir="ltr">On Fri, Sep 13, 2013 at 11:38 AM, Charles Davis <span dir="ltr"><<a href="mailto:cdavis5x@gmail.com" target="_blank">cdavis5x@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im"><br>
<br>
================<br>
Comment at: lib/Transforms/Instrumentation/MemorySanitizer.cpp:1941<br>
@@ +1940,3 @@<br>
+      if (Cast->getSrcTy() ==<br>
+            Type::getInt8PtrTy(Cast->getContext())->getPointerTo())<br>
+        return;<br>
----------------<br>
Evgeniy Stepanov wrote:<br>
> Evgeniy Stepanov wrote:<br>
> > This looks very fragile.<br>
> > Why can't you decide this based on the calling convention of the instrumented function?<br>
> OK, va_list may be from a function with a different calling convention.<br>
> I don't see a better way to detect this, but... how does va_copy work in this case? It gets plain i8* as an argument.<br>
</div>Remember how LLVM IR works--it's single-static assignment. All instructions are values. The IR for a Win64 ABI `va_copy` typically looks something like this:<br>
<br>
  lang=llvm<br>
  %ap = alloca i8*, align 8<br>
  %cp = alloca i8*, align 8<br>
  %ap1 = bitcast i8** %ap to i8*<br>
  %cp1 = bitcast i8** %cp to i8*<br>
  call void @llvm.va_copy(i8* %cp1, i8* %ap1)<br>
<br>
So, I `dyn_cast` the argument to an `llvm::BitCastInst` (and hope that the optimizer didn't make the bitcast go away--yes, this is very fragile, but I can't think of any better way to do this), and check if the source type is a `i8**` (the argument to the `bitcast` is a pointer to the `va_list` itself). If it is, we assume it's a Win64 ABI `va_copy`, else, a System V ABI `va_copy`.<br>

<br>
Now that I think about it, it might be better if I just add a new intrinsic (`llvm.ms_va_copy`, maybe?) to handle Win64 ABI `va_copy` calls, but when I tried to do it this way, I couldn't work out how to use the generic lowering code (that I pulled out of `LegalizeDAG`) to lower it. (In retrospect, I should probably have asked about it earlier on the list.)<br>

<div class=""><div class="h5"></div></div></blockquote></div><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">If you can't reuse the code, it's fine... it's only 10 lines.</div><div class="gmail_extra">
<br></div><div class="gmail_extra">Alternatively, you might want to consider modifying the existing va_copy variadic intrinsic to use llvm_anyptr_ty.</div><div class="gmail_extra"><br></div><div class="gmail_extra">-Eli</div>
</div>