<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>