Segmentation fault while using AAResultsWrapperPass in LLVM 5.0.1 - llvm

I am trying to use AliasAnalysis (LLVM 5.01) and my code is as following:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<AAResultsWrapperPass>();
}
And I am get the AliasAnalysis as follow:
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
This code is getting compiled properly. But when I run this, I am getting following errors:
clang -Xclang -load -Xclang /home/zhangjun/tools/llvm/llvm-build/lib/LLVMCPI.so -O0 -c test.c
Pass 'Unnamed pass: implement Pass::getPassName()' is not initialized.
Verify if there is a pass dependency cycle.
Required Passes:clang-5.0: /home/zhangjun/tools/llvm/llvm/lib/IR/LegacyPassManager.cpp:653: void llvm::PMTopLevelManager::schedulePass(llvm::Pass*): Assertion `PI && "Expected required passes to be initialized"' failed.
#0 0x0000000002dae43f llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/zhangjun/tools/llvm/llvm/lib/Support/Unix/Signals.inc:398:0
#1 0x0000000002dae4d0 PrintStackTraceSignalHandler(void*) /home/zhangjun/tools/llvm/llvm/lib/Support/Unix/Signals.inc:462:0
#2 0x0000000002dac916 llvm::sys::RunSignalHandlers() /home/zhangjun/tools/llvm/llvm/lib/Support/Signals.cpp:49:0
#3 0x0000000002daddd7 SignalHandler(int) /home/zhangjun/tools/llvm/llvm/lib/Support/Unix/Signals.inc:252:0
Some one propose passing current function as argument to this function to solve this issue (Segmentation fault while using AAResultsWrapperPass in llvm3.8.1)
getAnalysis<AAResultsWrapperPass>().getAAResults() - NOT working
getAnalysis<AAResultsWrapperPass>().getAAResults(F)- working
However, his method does not work for me. How to deal with this problems?

Try calling getAnalysis with the function as the parameter, i.e.
getAnalysis<AAResultsWrapperPass>(F).getAAResults();

Related

LLVM 6.0.0 HowToUseJit example program seg faults

I compiled LLVM 6.0.0 on Windows for x64 as a DLL, and tried running the HowToUseJit example program, and it seg faulted. How do you fix it?
Source code for the example:
https://github.com/llvm-mirror/llvm/blob/release_60/examples/HowToUseJIT/HowToUseJIT.cpp
This line was causing the seg fault in the example:
GenericValue gv = EE->runFunction(FooF, noargs);
I think the HowToUseJit example is incomplete. You need to make the following changes in order to make it work:
Add the following include:
#include "llvm/ExecutionEngine/MCJIT.h"
This will call some static initializer function which is required for JITting to work in LLVM.
You also need to add a call to this function inside main before running the JITted function:
LLVMInitializeNativeAsmPrinter();
This function is required to produce assembly for the target machine.

clang++ will not build first example in boost signals2

Hey everyguys I've been taking a look at boost signals recently because I'd like to switch over to it from my own custom code for handling signal notification. I ran into a problem compiling the first example from here: http://www.boost.org/doc/libs/1_53_0/doc/html/signals2/tutorial.html, here is the example source code:
struct HelloWorld
{
void operator()() const
{
std::cout << "Hello, World!" << std::endl;
}
};
// Signal with no arguments and a void return value
boost::signals2::signal<void ()> sig;
// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);
// Call all of the slots
sig();
The problem that arose from attempting to compile this with: clang++ -std=c++11 signals2example.cpp is this error here:
error: no matching function for call to 'get'
func(std::get<indices>(args)...);
To narrow down the problem I commented out all the lines until I figured out which one caused it, it was the line that simply says "sig();" and the problem seems to be related to the std::get function which is for tuples or something. There are not many helpful posts online with regards to boost::signal2 and clang++ clashing. I should also note that g++ compiles this document with no complaints at all.
When you are compiling with Clang and you use the STL, the system STL (usually libstdc++) is used. It can be an old version (Do you use OSX?).
Clang has perfect support for C++11 with libc++, try adding -stdlib=libc++ to the command line.
You may also try to run both gcc and clang with -v and check the include paths to see which stdlib is used in each case.

Pure virtual method called - cross compiling

I'm writing an event-based programming library for use on the BeagleBone Black and have encountered a strange error.
When I compile the exact same code with the exact same flags I receive the following errors on the ARM-based processor, but not when I run the code compiled for my x86 computer.
$ ./missionControl
pure virtual method called
pure virtual method called
pure virtual method called
terminate called recursively
terminate called recursively
Aborted
When I compile and run on my laptop, the program runs correctly.
This is the command I'm using to compile (ish, I'm using a Makefile, but both methods of compilation exhibit precisely the same behavior):
g++ -std=gnu++11 -pthread -O3 -D_GLIBCXX_USE_NANOSLEEP -o missionControl `find . -name *.cpp`
It doesn't matter whether I cross-compile with Ubuntu's arm-linux-gnueabi-g++ or the ARM-compatible g++ on the actual BeagleBone, I still get errors on ARM.
My question is this: What could be causing this error, and what can I do to try to find the source? Why would this happen on one processor architecture, but not another, for the same version of G++?
Thanks!
Here's a backtrace from the ARM processor's GDB:
#0 0xb6d4adf8 in raise () from /lib/libc.so.6
#1 0xb6d4e870 in abort () from /lib/libc.so.6
#2 0xb6f50ab4 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
#3 0xb6f4ea4c in ?? () from /usr/lib/libstdc++.so.6
#4 0xb6f4ea4c in ?? () from /usr/lib/libstdc++.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
The problem turned out to be due to a bug in the ARM version of libstdc++ that runs on the BeagleBone. A small toy program that has no virtual functions at all causes the same error ("pure virtual function called") when std::thread is created.
I'm going to try to compile a custom version of gcc/libstdc++ 4.8 on the BeagleBone itself -- even if it takes a long time.
The pure virtual method called error occurs when you attempt to use dynamic dispatch to call a function that is pure virtual in a base before the derived type that implements it has been constructed or after it has already destructed.
The most common cause for this is if the base class attempts to call a virtual function that is pure at this level through the constructor or destructor. Other than that, as it has been pointed out in some comments, if you attempt to access a dead object, you might also run into this same issue.
Just attach a debugger to the program and see what virtual function is called and from where.
See: https://groups.google.com/forum/#!topic/automatak-dnp3/Jisp_zGhd5I
And: Why does this simple c++11 threading-example fail, when compiled with clang 3.2?
Now, I have no idea why this works, but it does for me at least. Add the following four preprocessor definitions to the compiler command line:
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
I haven't experimented to see if they are all required, or whether or not you can get away with only some. But this solved the problem for me. Thanks to those who wrote the above answers, and thanks to my colleague for out-googling me :)

Jsoncpp - very simple test crashes when Json::reader goes ot of scope

I have downloaded and installed the jsoncpp library. I then try to use the library in my own application:
#include <json/json.h>
void parseJson() {
Json::Reader reader;
}
int main(int argc, char ** argv) {
parseJson();
exit(0);
}
The program compiles and links fine, but it crashes with SIGSEGV when running. The gdb backtrace looks like this:
(gdb) bt
#0 0x0000003a560b7672 in __gnu_cxx::__exchange_and_add () from /usr/lib64/libstdc++.so.6
#1 0x00000000004031e9 in std::string::_Rep::_M_dispose (this=0xffffffffffffffe9, __a=#0x7fffbfe60e57)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:232
#2 0x0000000000403236 in ~basic_string (this=0x7fffbfe60fb0)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:478
#3 0x00000000004038d4 in ~Reader (this=0x7fffbfe60eb0) at /private/joaho/Parser/opm-parser/external/json/json-cpp/include/json/reader.h:23
#4 0x0000000000402990 in parseJson () at /private/joaho/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:51
#5 0x00000000004029ab in main (argc=1, argv=0x7fffbfe610c8)
at /home/user/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:56
I.e. to me it seems to crash in the destructor. As far as I can tell the Json::Reader does not have it's own dstructor, so this must be a default destructor. As you can see I am running a quite old version of g++ - could that be the problem?
As I commented:
When compiled with GCC version 4.8.1 on Debian/Sid (so libjsoncpp-dev 0.6.0~rc2-3) as g++-4.8 -g -Wall -I/usr/include/jsoncpp/ esjson.cc -ljsoncpp -o esjson your program is compiled without warnings, and does not crash when running.
And GCC 4.1.2 is really old (febr. 2007 !) and is not supported anymore, and not very well C++ standard conforming (GCC, now at version 4.8.1, has made huge progress on C++ standard conformance since 4.1).
So I am not sure GCC 4.1. is faulty, but I won't be surprised it is: it had bad C++ reputation, and both the C++ standard and the GCC compiler have been improved a lot since that. Upgrading your GCC is worth the effort, both for better support of C++ and for improved diagnostics and optimizations.
So I suggest you to use a newer GCC; if you don't have root access, consider compiling its from its source tarball; build it outside of the source tree with e.g. ../gcc-4.8.1/configure --program-suffix=-4.8 --prefix=$HOME/pub then make then make install - after having installed its dependencies

Calling LLVM bitcode function in LLVM 2.8

I'm trying to read and call a function parsed from LLVM bitcode in LLVM 2.8. I have everything working apart from the actual call, which crashes the program.
First I have this C code:
void hello() {}
I've compiled this with:
llvm-gcc -c -emit-llvm hello.c -o hello.bc
Here's a trimmed down version of the code that's supposed to read it:
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
Module* m = getLazyBitcodeModule(buff, context, &error);
// Check the module parsed here.
// ...
ExecutionEngine* engine = ExecutionEngine::create(m);
// Check the engine started up correctly here.
// ...
Function* func = m->getFunction(function);
// Check the function was found here.
// ..
vector<GenericValue> args(0);
// This is what crashes.
engine->runFunction(func, args);
}
I've included plenty of LLVM headers, including ExecutionEngine/JIT.h, and the code checks at each step to make sure values aren't NULL. It parses the bitcode, and I have examined the function it finds to confirm it was as expected.
I've also tried building a module and function myself, which works as expected, so the problem definitely arises from the fact that the function is produced by the bitcode.
I've managed to get this running as expected. I was curious if the problem lay in the above process, but this is obviously not the case. The system I was running this as a part of was causing the crash, and the code above does work on its own.