<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>all of:</div><div>----</div><div>                                // cout << "lsr: " << lsr << "\n";</div><div>                                llvm::MemoryBuffer* mbjit =</div><div>                                        llvm::MemoryBuffer::getMemBufferCopy (sr);</div><div>------</div><div><div>string lsr = sr.str();</div><div>                                // cout << "lsr: " << lsr << "\n";</div><div>                                llvm::MemoryBuffer* mbjit =</div><div>                                        llvm::MemoryBuffer::getMemBuffer (lsr);</div></div><div>-------</div><div><div>string lsr = sr.str();</div><div>                                // cout << "lsr: " << lsr << "\n";</div><div>                                llvm::MemoryBuffer* mbjit =</div><div>                                        llvm::MemoryBuffer::getMemBufferCopy (lsr);</div></div><div><br></div><div><br></div><div>have same result as invalid bit code.</div><div>The result of valgrind, effectively, i have invalid reads in the parseBitcodeFile:</div><div><br></div><div><div>==536== Conditional jump or move depends on uninitialised value(s)</div><div>==536==    at 0x501FE3: llvm::BitstreamCursor::Read(unsigned int) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x501A19: llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50AEC8: llvm::getLazyBitcodeModule(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50B295: llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F1231: blah_runtime_hook (runtime.cpp:348)</div><div>==536==    by 0x4F46C2: ??? (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F2B60: main (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536== </div><div>==536== Invalid read of size 8</div><div>==536==    at 0x501FE8: llvm::BitstreamCursor::Read(unsigned int) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x501A19: llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50AEC8: llvm::getLazyBitcodeModule(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50B295: llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F1231: blah_runtime_hook (runtime.cpp:348)</div><div>==536==    by 0x4F46C2: ??? (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F2B60: main (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==  Address 0x0 is not stack'd, malloc'd or (recently) free'd</div><div>==536== </div><div>==536== </div><div>==536== Process terminating with default action of signal 11 (SIGSEGV)</div><div>==536==  Access not within mapped region at address 0x0</div><div>==536==    at 0x501FE8: llvm::BitstreamCursor::Read(unsigned int) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x501A19: llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50AEC8: llvm::getLazyBitcodeModule(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x50B295: llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&) (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F1231: blah_runtime_hook (runtime.cpp:348)</div><div>==536==    by 0x4F46C2: ??? (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div><div>==536==    by 0x4F2B60: main (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)</div></div><div><br></div><div><br></div><div><br></div><div apple-content-edited="true">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="font-weight: normal; "><b>--</b></div><div style="font-weight: normal; "><b>Willy WOLFF</b></div></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></div>
</div>
<br><div><div>On 19 Mar 2014, at 22:11, Vikas Bhargava wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div>Hi Willy,<br></div>If the disassembly of the module works fine, then there is nothing wrong with the module. <br><br>Stream uses the memorybuffer that you pass in parseBitcodeFile. If what Will is saying is true, there is something wrong with your code in "3:", i.e.:<br>
<br>MemoryBuffer* mbjit = MemoryBuffer::getMemBuffer (sr.str());<br>
  LLVMContext& context = getGlobalContext();<br>
  ErrorOr<Module*> ModuleOrErr = parseBitcodeFile (mbjit, context);<br>
  if (error_code EC = ModuleOrErr.getError())<br>
  {<br>
    std::cout << ModuleOrErr.getError().<div id=":14u" class="">message() << "\n";<br>
    assert(false);<br>
  }<br><br></div><div id=":14u" class="">Can you post how you modified it in your second reply? For debugging purpose, you can simply use MemoryBuffer::getMemBufferCopy() and not worry about validity of stringref or null-termination. Also, you can run your program through valgrind and check for any invalid reads.<br>
<br></div><div id=":14u" class="">HTH<br>Vikas.<br>=======<br></div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Mar 19, 2014 at 10:32 AM, Willy WOLFF <span dir="ltr"><<a href="mailto:willy.wolff@etu.unistra.fr" target="_blank">willy.wolff@etu.unistra.fr</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I mad the change, and still have the problem.<br>
<br>
I investigate more the source code of llvm.<br>
<br>
First, I change isRawBitcode function to print the content of the parameter like this:<br>
original: <a href="http://llvm.org/docs/doxygen/html/ReaderWriter_8h_source.html#l00081" target="_blank">http://llvm.org/docs/doxygen/<u></u>html/ReaderWriter_8h_source.<u></u>html#l00081</a><div class=""><br>
  inline bool isRawBitcode(const unsigned char *BufPtr,<br></div>
                           const unsigned char *BufEnd) {<br>
    // These bytes sort of have a hidden message, but it's not in<br>
    // little-endian this time, and it's a little redundant.<br>
          errs()<< "isRawBitcode output:\n";<br>
          for (int i = 0; i < 4; i++)<br>
                  errs() << BufPtr[i] << "\n";<br>
          if (BufPtr != BufEnd )<br>
                errs() << "BP != BE ok\n";<br>
          if (BufPtr[0] == 'B')<br>
                errs() << "B ok\n";<br>
          if (BufPtr[1] == 'C')<br>
                errs() << "C ok\n";<br>
          if (BufPtr[2] ==  0xc0)<br>
                errs() << "0xc0 ok\n";<br>
          if (BufPtr[3] ==  0xde)<br>
                errs() << "0xde ok\n";<br>
<br>
    return BufPtr != BufEnd &&<br>
           BufPtr[0] == 'B' &&<br>
           BufPtr[1] == 'C' &&<br>
           BufPtr[2] == 0xc0 &&<br>
           BufPtr[3] == 0xde;<br>
  }<br>
<br>
<br>
Second, I change ParseBitcodeInto as this:<br>
original: <a href="http://llvm.org/docs/doxygen/html/BitcodeReader_8cpp_source.html#l01971" target="_blank">http://llvm.org/docs/doxygen/<u></u>html/BitcodeReader_8cpp_<u></u>source.html#l01971</a><br>
...<br>
        errs() << "parsebitcodeinto sniff the signature\n";<br>
        uint32_t bvar = Stream.Read(8);<br>
                        errs() << "B :" << bvar << "\n";<br>
        if (bvar != 'B') {<br>
                errs() << "B :" << bvar << "\n";<br>
                return Error(InvalidBitcodeSignature)<u></u>;<br>
        }<br>
<br>
        if (Stream.Read(8) != 'C') {<br>
                errs() << "C\n";<br>
                return Error(InvalidBitcodeSignature)<u></u>;<br>
        }<br>
        if (  Stream.Read(8) != 0xc0 ) {<br>
                errs() << "0xc0\n";<br>
                return Error(InvalidBitcodeSignature)<u></u>;<br>
        }<br>
        if (  Stream.Read(8) != 0xde ) {<br>
                errs() << "0xde\n";<br>
                return Error(InvalidBitcodeSignature)<u></u>;<br>
        }<br>
        // if (Stream.Read(8) != 'B' ||<br>
        //     Stream.Read(8) != 'C' ||<br>
        //     Stream.Read(4) != 0x0 ||<br>
        //     Stream.Read(4) != 0xC ||<br>
        //     Stream.Read(4) != 0xE ||<br>
        //     Stream.Read(4) != 0xD<br>
        //      ) {<br>
...<br>
<br>
<br>
<br>
The output of the code is :<br>
<br>
<br>
isRawBitcode output:<br>
B<br>
C<br>
<br>
<br>
BP != BE ok<div class=""><br>
B ok<br>
C ok<br>
0xc0 ok<br>
0xde ok<br>
<br></div>
parsebitcodeinto sniff the signature<br>
B :37<br>
B :37<br>
<br>
<br>
<br>
<br>
It's possible that Stream object is not correctly initialized?<div class="HOEnZb"><div class="h5"><br>
<br>
On 03/13/2014 06:37 PM, Will Dietz wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Mar 13, 2014 at 9:02 AM, Willy WOLFF <<a href="mailto:willy.wolff@etu.unistra.fr" target="_blank">willy.wolff@etu.unistra.fr</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
I having a weird problem while writing a bytecode module to a string,<br>
and after read/parse it for unsing on a jit.<br>
<br>
I write a pass to export function to module, and put this module inside<br>
a global variable.<br>
I use WriteBitcodeToFile for this.<br>
For debuging, after this write, I try to load the exported module with<br>
parseBitcodeFile.<br>
This two step works.<br>
<br>
<br>
<br>
After, while the compiled program is running, I try to read and parse<br>
this global variable for jiting the function.<br>
<br>
1) I read the global variable with<br>
   StringRef sr (gv, gv_length);<br>
<br>
2) I manually test this bytecode by<br>
(inspired by  inline bool isRawBitcode(const unsigned char *BufPtr,<br>
const unsigned char *BufEnd) at<br>
<a href="http://llvm.org/docs/doxygen/html/ReaderWriter_8h_source.html#l00067" target="_blank">http://llvm.org/docs/doxygen/<u></u>html/ReaderWriter_8h_source.<u></u>html#l00067</a>)<br>
   if (sr.str()[0] == 'B')<br>
     std::cout << "B ok\n";<br>
   if (sr.str()[1] == 'C')<br>
     std::cout << "C ok\n";<br>
   if (sr.str()[2] == (char) 0xc0)<br>
     std::cout << "0xc0 ok\n";<br>
   if (sr.str()[3] == (char) 0xde)<br>
     std::cout << "0xde ok\n";<br>
<br>
3) I try to parse the gv by<br>
   MemoryBuffer* mbjit = MemoryBuffer::getMemBuffer (sr.str());<br>
</blockquote>
<br>
Not sure if this is your issue, but should be fixed anyway:<br>
<br>
The std::string created by "sr.str()" ends its lifetime in this<br>
statement, and MemoryBuffer for efficiency reasons<br>
avoids copying data it doesn't have to (like StringRef) so will be<br>
referencing the freed memory.<br>
<br>
To resolve this:<br>
* Pass MemoryBuffer your StringRef directly<br>
* Use getMemBufferCopy()<br>
* Preserve the result of sr.str() into a stack variable and pass that<br>
to getMemoryBuffer() instead.<br>
<br>
As a final note, check if your bitcode buffer "string" is<br>
null-terminated or not.  If not, be sure to be careful and<br>
do things like informing MemoryBuffer that this is the case.<br>
<br>
Hope this helps,<br>
~Will<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
   LLVMContext& context = getGlobalContext();<br>
   ErrorOr<Module*> ModuleOrErr = parseBitcodeFile (mbjit, context);<br>
   if (error_code EC = ModuleOrErr.getError())<br>
   {<br>
     std::cout << ModuleOrErr.getError().<u></u>message() << "\n";<br>
     assert(false);<br>
   }<br>
<br>
<br>
<br>
<br>
This is the execution result:<br>
B ok<br>
C ok<br>
0xc0 ok<br>
0xde ok<br>
Invalid bitcode signature<br>
<br>
<br>
<br>
Ok is not working :/<br>
But why ???<br>
<br>
<br>
<br>
For debuging, between 2) and 3), I export the readed module and write to<br>
a file on my hard drive,<br>
and try llvm-dis, and the dissasembly of the module works.<br>
<br>
Wath's wrong? Any idea for solve this problem?<br>
<br>
Thanks you very much.<br>
<br>
Regards,<br>
Willy<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu/" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</blockquote></blockquote>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu/" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</div></div></blockquote></div><br></div>
</blockquote></div><br></body></html>