<div dir="ltr">the segfault is not happening because BitStream is NULL, it is happening because BitcodeBytes member variable in BitstreamReader class is NULL. As you can see, getBitcodeBytes de-references BitcodeBytes:<br>
<pre class=""> <a class="" href="http://llvm.org/docs/doxygen/html/classllvm_1_1StreamableMemoryObject.html">StreamableMemoryObject</a> &<a class="" href="http://llvm.org/docs/doxygen/html/classllvm_1_1BitstreamReader.html#a55ac0f8d247d2404c59c5e04d2d4e25f">getBitcodeBytes</a>() { <span class="">return</span> *BitcodeBytes; }</pre>
<div class="gmail_extra"><br></div><div class="gmail_extra">I would still suggest that you put a watchpoint on the underlying memory as it is freed somewhere.<br><br></div><div class="gmail_extra">thx<br>Vikas.<br>=======<br>
</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 20, 2014 at 3:39 PM, WOLFF Willy (ETU MAI) <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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Maybe I found the problem.<br>
For debuging, I modify isEndPos of BitstreamReader as follows:<br>
original: <a href="http://llvm.org/docs/doxygen/html/BitstreamReader_8h_source.html#l00234" target="_blank">http://llvm.org/docs/doxygen/html/BitstreamReader_8h_source.html#l00234</a><br>
<br>
bool isEndPos(size_t pos) {<br>
if (BitStream == NULL) errs() << "BitStream is null\n";<br>
else errs() << "BitStream n'est pas null\n";<br>
errs() << "isEndPos prob " << pos << " & " << BitStream <<"\n";<br>
StreamableMemoryObject& smo = BitStream->getBitcodeBytes();<br>
errs() << "isEndPos prob smo\n";<br>
bool m = smo.isObjectEnd(static_cast<uint64_t>(pos));<br>
errs() << "isEndPos prob " << m << "\n";<br>
return m;<br>
}<br>
<br>
<br>
and on my output i never see "isEndPos prob smo\n";<br>
The segfault occuring when i call getBitcodeBytes().<br>
Also, i don't see the message of "BitStream is null" or "BitStream n'est pas null" ...<br>
Is possible that this variable is not correctly initialized?<br>
<br>
Thanks<br>
<span class=""><font color="#888888">--<br>
Willy WOLFF<br>
</font></span><div class=""><div class="h5"><br>
<br>
On Thursday, March 20, 2014 22:32 CET, Vikas Bhargava <<a href="mailto:vikasbhargava@gmail.com">vikasbhargava@gmail.com</a>> wrote:<br>
<br>
> Hello Willy,<br>
> Here is the dump from one of my bitcode files:<br>
><br>
> 0000000 42 43 c0 de 21 0c 00 00 25 05 00 00 0b 82 20 00<br>
><br>
> As expected, 0x42 (= B), 0x43 (= C), xc0 and 0xde are in correct order. In<br>
> your case, the first byte is read as 37 (= 0x25). I wonder why? When you<br>
> check the bytes yourself, you get expected results. When the same bytes are<br>
> read from Stream object, you get a different result (maybe garbage). I<br>
> would suggest that you put a watchpoint on mbjit->getBufferStart() and<br>
> single step your program to make sure it is not freed, over written<br>
> somewhere.<br>
><br>
> thx<br>
> Vikas.<br>
> =======<br>
><br>
><br>
><br>
> On Thu, Mar 20, 2014 at 7:50 AM, Willy WOLFF <<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a>>wrote:<br>
><br>
> > The stack trace is:<br>
> > (gdb) bt<br>
> > #0 0x00000000004fa8c8 in llvm::BitstreamCursor::Read(unsigned int) ()<br>
> > #1 0x00000000004fa1d2 in llvm::BitcodeReader::<br>
> > ParseBitcodeInto(llvm::Module*) ()<br>
> > #2 0x0000000000503ae9 in llvm::getLazyBitcodeModule(llvm::MemoryBuffer*,<br>
> > llvm::LLVMContext&) ()<br>
> > #3 0x0000000000503eb6 in llvm::parseBitcodeFile(llvm::MemoryBuffer*,<br>
> > llvm::LLVMContext&) ()<br>
> > #4 0x00000000004ec195 in jitter (skeletons=<optimized out>,<br>
> > params=0x7fffffffdf40, phi_state=0x11adbc0, lower=0, upper=250,<br>
> > inst_outer=8, inst_inner=<optimized out>)<br>
> > at /home/willy/hello_stuff/with_apollo/simple_loop/runtime.cpp:263<br>
> > #5 0x00000000004ec8fa in apollo_runtime_hook (info=<optimized out>,<br>
> > skeletons=0xc8b1f0, skeleton_size=<optimized out>, params=0x7fffffffdf40,<br>
> > phi_state_size=<optimized out>)<br>
> > at /home/willy/hello_stuff/with_apollo/simple_loop/runtime.cpp:438<br>
> > #6 0x00000000004ee753 in ?? ()<br>
> > #7 0x00000000004ecbf1 in main (argc=<optimized out>, argv=<optimized<br>
> > out>) at /home/willy/hello_stuff/with_apollo/simple_loop/simple_<br>
> > scev_dynamic_array.c:84<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > On 03/20/2014 10:10 AM, Willy WOLFF wrote:<br>
> ><br>
> >> This segfault occuring only under valgrind,<br>
> >> in shell way, and in gdb way i have<br>
> >><br>
> >> Invalid bitcode signature<br>
> >> simple_scev_dynamic_array:<br>
> >> /home/willy/apollo/llvm/include/llvm/Support/ErrorOr.h:258: storage_type<br>
> >> *llvm::ErrorOr<llvm::Module *>::getStorage() [T = llvm::Module *]:<br>
> >> Assertion `!HasError && "Cannot get value when an error exists!"' failed.<br>
> >> Command terminated by signal 6<br>
> >><br>
> >><br>
> >> this is the code I use:<br>
> >><br>
> >><br>
> >> long jitter(void* info, skeleton_pair *skeletons, long skeleton_size,<br>
> >> param_t params, long phi_state_size) {<br>
> >> InitializeNativeTarget();<br>
> >> InitializeNativeTargetAsmPrinter();<br>
> >><br>
> >> llvm::StringRef sr (skeletons[idx].jit_bytecode,<br>
> >> skeletons[idx].jit_bytecode_length);<br>
> >><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>
> >> llvm::MemoryBuffer* mbjit = llvm::MemoryBuffer::getMemBufferCopy (sr);<br>
> >><br>
> >> llvm::ErrorOr<llvm::Module*> ModuleOrErr = llvm::parseBitcodeFile<br>
> >> (mbjit, context);<br>
> >> if (llvm::error_code EC = ModuleOrErr.getError()) {<br>
> >> std::cout << ModuleOrErr.getError().message() << "\n";<br>
> >> }<br>
> >><br>
> >> Module* Mjit = ModuleOrErr.get();<br>
> >><br>
> >> std::string eeError;<br>
> >> ExecutionEngine* nee =<br>
> >> EngineBuilder(Mjit).setEngineKind(EngineKind::JIT).setUseMCJIT(true).<br>
> >> setErrorStr(&eeError).create();<br>
> >> if (!nee) {<br>
> >> fprintf(stderr, "Could not create ExecutionEngine: %s\n",<br>
> >> eeError.c_str());<br>
> >> assert(false);<br>
> >> }<br>
> >><br>
> >> Function f = ret_fct(Mjit); // Function* ret_fct (Module*); return<br>
> >> the function we want to jit.<br>
> >> uint64_t f_ptr = nee->getFunctionAddress(f->getName());<br>
> >><br>
> >> long (*fjited)(param_t, phi_state_t, long, long, long, long)<br>
> >> = (long (*)(param_t, phi_state_t, long, long, long, long))<br>
> >> (intptr_t)f_ptr;<br>
> >><br>
> >> returnfjited (params, phi_state, lower, upper, inst_outer, inst_inner);<br>
> >> }<br>
> >><br>
> >><br>
> >> Thanks,<br>
> >> *--*<br>
> >> *Willy WOLFF*<br>
> >><br>
> >><br>
> >> On 20 Mar 2014, at 00:37, Vikas Bhargava wrote:<br>
> >><br>
> >> segmentation fault indicates memory corruption and it's hard to tell<br>
> >>> without seeing the exact use of the APIs. If possible, please post a<br>
> >>> complete program and gdb stack trace from the core file. If there are<br>
> >>> multiple threads using the global variables, please let us know.<br>
> >>><br>
> >>> FWIW, I have some tests to write llvm::Module to bitcode files and<br>
> >>> read them back into llvm::Module and they work just fine with 3.4<br>
> >>> (never tried with tip).<br>
> >>><br>
> >>> thx<br>
> >>> vikas.<br>
> >>> ========<br>
> >>><br>
> >>><br>
> >>> On Wed, Mar 19, 2014 at 2:58 PM, Willy WOLFF<br>
> >>> <<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a> <mailto:<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a>>> wrote:<br>
> >>><br>
> >>> all of:<br>
> >>> ----<br>
> >>> // cout << "lsr: " << lsr << "\n";<br>
> >>> llvm::MemoryBuffer* mbjit =<br>
> >>><br>
> >>> llvm::MemoryBuffer::getMemBufferCopy (sr);<br>
> >>> ------<br>
> >>> string lsr = sr.str();<br>
> >>> // cout << "lsr: " << lsr << "\n";<br>
> >>> llvm::MemoryBuffer* mbjit =<br>
> >>><br>
> >>> llvm::MemoryBuffer::getMemBuffer (lsr);<br>
> >>> -------<br>
> >>> string lsr = sr.str();<br>
> >>> // cout << "lsr: " << lsr << "\n";<br>
> >>> llvm::MemoryBuffer* mbjit =<br>
> >>><br>
> >>> llvm::MemoryBuffer::getMemBufferCopy (lsr);<br>
> >>><br>
> >>><br>
> >>> have same result as invalid bit code.<br>
> >>> The result of valgrind, effectively, i have invalid reads in the<br>
> >>> parseBitcodeFile:<br>
> >>><br>
> >>> ==536== Conditional jump or move depends on uninitialised value(s)<br>
> >>> ==536== at 0x501FE3: llvm::BitstreamCursor::Read(unsigned int)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x501A19:<br>
> >>> llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50AEC8:<br>
> >>> llvm::getLazyBitcodeModule(llvm::MemoryBuffer*,<br>
> >>> llvm::LLVMContext&) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50B295:<br>
> >>> llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F1231: blah_runtime_hook (runtime.cpp:348)<br>
> >>> ==536== by 0x4F46C2: ??? (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F2B60: main (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536==<br>
> >>> ==536== Invalid read of size 8<br>
> >>> ==536== at 0x501FE8: llvm::BitstreamCursor::Read(unsigned int)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x501A19:<br>
> >>> llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50AEC8:<br>
> >>> llvm::getLazyBitcodeModule(llvm::MemoryBuffer*,<br>
> >>> llvm::LLVMContext&) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50B295:<br>
> >>> llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F1231: blah_runtime_hook (runtime.cpp:348)<br>
> >>> ==536== by 0x4F46C2: ??? (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F2B60: main (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== Address 0x0 is not stack'd, malloc'd or (recently) free'd<br>
> >>> ==536==<br>
> >>> ==536==<br>
> >>> ==536== Process terminating with default action of signal 11<br>
> >>> (SIGSEGV)<br>
> >>> ==536== Access not within mapped region at address 0x0<br>
> >>> ==536== at 0x501FE8: llvm::BitstreamCursor::Read(unsigned int)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x501A19:<br>
> >>> llvm::BitcodeReader::ParseBitcodeInto(llvm::Module*) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50AEC8:<br>
> >>> llvm::getLazyBitcodeModule(llvm::MemoryBuffer*,<br>
> >>> llvm::LLVMContext&) (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x50B295:<br>
> >>> llvm::parseBitcodeFile(llvm::MemoryBuffer*, llvm::LLVMContext&)<br>
> >>> (in /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F1231: blah_runtime_hook (runtime.cpp:348)<br>
> >>> ==536== by 0x4F46C2: ??? (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>> ==536== by 0x4F2B60: main (in<br>
> >>> /home/willy/blah_test_script/new_blah/simple_scev_dynamic_array)<br>
> >>><br>
> >>><br>
> >>><br>
> >>> *--*<br>
> >>> *Willy WOLFF*<br>
> >>><br>
> >>><br>
> >>> On 19 Mar 2014, at 22:11, Vikas Bhargava wrote:<br>
> >>><br>
> >>> Hi Willy,<br>
> >>>> If the disassembly of the module works fine, then there is<br>
> >>>> nothing wrong with the module.<br>
> >>>><br>
> >>>> Stream uses the memorybuffer that you pass in parseBitcodeFile.<br>
> >>>> If what Will is saying is true, there is something wrong with<br>
> >>>> 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().<br>
> >>>> message() << "\n";<br>
> >>>> assert(false);<br>
> >>>> }<br>
> >>>><br>
> >>>> Can you post how you modified it in your second reply? For<br>
> >>>> debugging purpose, you can simply use<br>
> >>>> MemoryBuffer::getMemBufferCopy() and not worry about validity of<br>
> >>>> stringref or null-termination. Also, you can run your program<br>
> >>>> through valgrind and check for any invalid reads.<br>
> >>>><br>
> >>>> HTH<br>
> >>>> Vikas.<br>
> >>>> =======<br>
> >>>><br>
> >>>><br>
> >>>><br>
> >>>> On Wed, Mar 19, 2014 at 10:32 AM, Willy WOLFF<br>
> >>>> <<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a> <mailto:<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a>>><br>
> >>>><br>
> >>>> wrote:<br>
> >>>><br>
> >>>> 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<br>
> >>>> the parameter like this:<br>
> >>>> original:<br>
> >>>> <a href="http://llvm.org/docs/doxygen/__html/ReaderWriter_8h_source._" target="_blank">http://llvm.org/docs/doxygen/__html/ReaderWriter_8h_source._</a><br>
> >>>> _html#l00081<br>
> >>>><br>
> >>>> <<a href="http://llvm.org/docs/doxygen/html/ReaderWriter_8h_source" target="_blank">http://llvm.org/docs/doxygen/html/ReaderWriter_8h_source</a>.<br>
> >>>> html#l00081><br>
> >>>><br>
> >>>> inline bool isRawBitcode(const unsigned char *BufPtr,<br>
> >>>> const unsigned char *BufEnd) {<br>
> >>>> // These bytes sort of have a hidden message, but it's not<br>
> >>>> 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:<br>
> >>>> <a href="http://llvm.org/docs/doxygen/__html/BitcodeReader_8cpp___" target="_blank">http://llvm.org/docs/doxygen/__html/BitcodeReader_8cpp___</a><br>
> >>>> source.html#l01971<br>
> >>>><br>
> >>>> <<a href="http://llvm.org/docs/doxygen/html/BitcodeReader_8cpp_" target="_blank">http://llvm.org/docs/doxygen/html/BitcodeReader_8cpp_</a><br>
> >>>> source.html#l01971><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)__;<br>
> >>>><br>
> >>>> }<br>
> >>>><br>
> >>>> if (Stream.Read(8) != 'C') {<br>
> >>>> errs() << "C\n";<br>
> >>>> return Error(InvalidBitcodeSignature)__;<br>
> >>>><br>
> >>>> }<br>
> >>>> if ( Stream.Read(8) != 0xc0 ) {<br>
> >>>> errs() << "0xc0\n";<br>
> >>>> return Error(InvalidBitcodeSignature)__;<br>
> >>>><br>
> >>>> }<br>
> >>>> if ( Stream.Read(8) != 0xde ) {<br>
> >>>> errs() << "0xde\n";<br>
> >>>> return Error(InvalidBitcodeSignature)__;<br>
> >>>><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<br>
> >>>><br>
> >>>> B ok<br>
> >>>> C ok<br>
> >>>> 0xc0 ok<br>
> >>>> 0xde ok<br>
> >>>><br>
> >>>> 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?<br>
> >>>><br>
> >>>><br>
> >>>> On 03/13/2014 06:37 PM, Will Dietz wrote:<br>
> >>>><br>
> >>>> On Thu, Mar 13, 2014 at 9:02 AM, Willy WOLFF<br>
> >>>> <<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a><br>
> >>>> <mailto:<a href="mailto:willy.wolff@etu.unistra.fr">willy.wolff@etu.unistra.fr</a>>> wrote:<br>
> >>>><br>
> >>>> Hello,<br>
> >>>><br>
> >>>> I having a weird problem while writing a bytecode<br>
> >>>> 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<br>
> >>>> this module inside<br>
> >>>> a global variable.<br>
> >>>> I use WriteBitcodeToFile for this.<br>
> >>>> For debuging, after this write, I try to load the<br>
> >>>> exported module with<br>
> >>>> parseBitcodeFile.<br>
> >>>> This two step works.<br>
> >>>><br>
> >>>><br>
> >>>><br>
> >>>> After, while the compiled program is running, I try<br>
> >>>> 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<br>
> >>>> char *BufPtr,<br>
> >>>> const unsigned char *BufEnd) at<br>
> >>>> <a href="http://llvm.org/docs/doxygen/_" target="_blank">http://llvm.org/docs/doxygen/_</a><br>
> >>>> _html/ReaderWriter_8h_source.__html#l00067<br>
> >>>> <<a href="http://llvm.org/docs/doxygen/" target="_blank">http://llvm.org/docs/doxygen/</a><br>
> >>>> html/ReaderWriter_8h_source.html#l00067>)<br>
> >>>><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<br>
> >>>> (sr.str());<br>
> >>>><br>
> >>>><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<br>
> >>>> in this<br>
> >>>> statement, and MemoryBuffer for efficiency reasons<br>
> >>>> avoids copying data it doesn't have to (like StringRef)<br>
> >>>> 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<br>
> >>>> 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>
> >>>> LLVMContext& context = getGlobalContext();<br>
> >>>> ErrorOr<Module*> ModuleOrErr = parseBitcodeFile<br>
> >>>> (mbjit, context);<br>
> >>>> if (error_code EC = ModuleOrErr.getError())<br>
> >>>> {<br>
> >>>> std::cout << ModuleOrErr.getError().__message()<br>
> >>>><br>
> >>>> << "\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<br>
> >>>> module and write to<br>
> >>>> a file on my hard drive,<br>
> >>>> and try llvm-dis, and the dissasembly of the module<br>
> >>>> works.<br>
> >>>><br>
> >>>> Wath's wrong? Any idea for solve this problem?<br>
> >>>><br>
> >>>> Thanks you very much.<br>
> >>>><br>
> >>>> Regards,<br>
> >>>> Willy<br>
> >>>> _________________________________________________<br>
> >>>> LLVM Developers mailing list<br>
> >>>> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>><br>
> >>>> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.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/__mailman/listinfo/llvmdev</a><br>
> >>>> <<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>><br>
> >>>><br>
> >>>> _________________________________________________<br>
> >>>> LLVM Developers mailing list<br>
> >>>> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>><br>
> >>>> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.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/__mailman/listinfo/llvmdev</a><br>
> >>>> <<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>><br>
> >>>><br>
> >>>><br>
> >>>><br>
> >>><br>
> >>><br>
> >><br>
> >><br>
> >> _______________________________________________<br>
> >> LLVM Developers mailing list<br>
> >> <a href="mailto:LLVMdev@cs.uiuc.edu">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/mailman/listinfo/llvmdev</a><br>
> >><br>
> >><br>
<br>
<br>
</div></div></blockquote></div><br></div></div>