LLJIT running error "Segmentation fault: 11", while linking c++ standard libraries - c++

I was using custom c++ functions, and it includes c++ standard libraries.
While i using c++ standard libraries, error occured: make: *** [start] Segmentation fault: 11
The full error message is here:
./build/main;
Stack dump:
0. Program arguments: ./build/main
0 main 0x0000000107c3ab4c llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 60
1 main 0x0000000107c3b109 PrintStackTraceSignalHandler(void*) + 25
2 main 0x0000000107c38b86 llvm::sys::RunSignalHandlers() + 118
3 main 0x0000000107c3eccc SignalHandler(int) + 252
4 libsystem_platform.dylib 0x00007fff6af3642d _sigtramp + 29
5 libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338721827824
6 libsystem_platform.dylib 0x0000000111a41213 _sigtramp + 18446603343312760323
7 libsystem_platform.dylib 0x0000000111a410d1 _sigtramp + 18446603343312760001
8 libsystem_platform.dylib 0x0000000111a4101a _sigtramp + 18446603343312759818
9 libsystem_platform.dylib 0x0000000111a40c2d _sigtramp + 18446603343312758813
10 libsystem_platform.dylib 0x0000000111a409ed _sigtramp + 18446603343312758237
11 libsystem_platform.dylib 0x0000000111a40147 _sigtramp + 18446603343312756023
12 libsystem_platform.dylib 0x0000000111a40032 _sigtramp + 18446603343312755746
13 main 0x0000000107a3b706 main + 1830
14 libdyld.dylib 0x00007fff6ad3d7fd start + 1
15 libdyld.dylib 0x0000000000000001 start + 18446603338723895301
make: *** [start] Segmentation fault: 11
I had create a demo project to show this error in minimum code: https://github.com/vifird/jit. And i had tested it in two new MacBooks(MacOSX 10.15), those both displayed this error, so i think it may not be a problem of libc++.
Here are my code files:
main.cpp
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetSelect.h"
#include <fstream>
#include <iostream>
using namespace std;
using namespace llvm;
using namespace llvm::orc;
int main(int argc, char *argv[]) {
InitLLVM X(argc, argv);
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
ThreadSafeContext context(std::make_unique<LLVMContext>());
ExitOnError ExitOnErr;
auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
JTMB.setCodeModel(CodeModel::Small);
auto jit =
ExitOnErr(LLJITBuilder()
.setJITTargetMachineBuilder(std::move(JTMB))
.create());
jit->getMainJITDylib().addGenerator(
ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
jit->getDataLayout().getGlobalPrefix())));
char ffi_file[] = "build/ffi";
llvm::Error err =
jit->addObjectFile(ExitOnErr(errorOrToExpected(MemoryBuffer::getFileAsStream(ffi_file))));
if (err) {
cout << "error addObjectFile" << endl;
return 1;
};
char func_name[] = "add";
auto func_add = ExitOnErr(jit->lookup(func_name));
int (*func)(int, int) = (int (*)(int, int))func_add.getAddress();
int result = func(111, 222);
cout << "result: " << result << endl;
return 0;
}
ffi.cpp, which will build and output build/ffifile.
#include <vector>
extern "C" int add(int a, int b) {
std::vector<int> vc;
vc.push_back(1);
return a + b;
}
When vc.push_back(1); was included in ffi.cpp, error occoured, when delete this line , it runs ok.
Here is my environment
OS: MacOSX 10.15.2
LLVM: 10
Clang: 11
Here is my cmake file
cmake_minimum_required(VERSION 3.15.0)
project(jitdemo)
SET (CMAKE_C_COMPILER /usr/bin/clang)
SET (CMAKE_CXX_COMPILER /usr/bin/clang++)
SET ( CMAKE_BUILD_TYPE Debug )
find_package(LLVM REQUIRED CONFIG)
message(STATUS ">>Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS ">>Using LLVMConfig.cmake in: ${LLVM_DIR}")
add_compile_options(-std=c++17)
add_compile_options(-stdlib=libc++)
add_definitions(${LLVM_DEFINITIONS})
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR})
add_subdirectory(link)
llvm_map_components_to_libnames(llvm_libs support core orcjit irreader nativecodegen)
add_executable(main main.cpp)
target_link_libraries(main link ${llvm_libs})

Related

SFML program crashes with non-ASCII character in window title

So i just install SFML and was trying to do a simple programme, and the compilation works fine, but it's when I try to run the executable that it fails.
First I needed to do a trick to authorize the application to launch because it wasn't recognize by apple I follow those instruction to install SFML https://www.sfml-dev.org/tutorials/2.5/start-osx.php
After I managed to authorize the application when I run it I got this error message:
2020-11-24 10:20:55.638 exemple-graphisme1[2768:29657] *** Assertion failure in -[SFWindow setTitle:], NSWindow.m:2490
2020-11-24 10:20:55.640 exemple-graphisme1[2768:29657] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aString != nil'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff204956af __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff201cd3c9 objc_exception_throw + 48
2 CoreFoundation 0x00007fff204be512 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff212776c9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff22c7da75 -[NSWindow setTitle:] + 142
5 libsfml-window.2.5.dylib 0x000000010e5bf4fd _ZN2sf4priv15WindowImplCocoaC2ENS_9VideoModeERKNS_6StringEmRKNS_15ContextSettingsE + 269
6 libsfml-window.2.5.dylib 0x000000010e5b0e03 _ZN2sf4priv10WindowImpl6createENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 67
7 libsfml-window.2.5.dylib 0x000000010e5b02e5 _ZN2sf6Window6createENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 469
8 libsfml-graphics.2.5.dylib 0x000000010e61e823 _ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE + 99
9 exemple-graphisme1 0x000000010e582849 main + 233
10 libdyld.dylib 0x00007fff2033e631 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
[1] 2768 abort ./exemple-graphisme1
My code is very simple and only draw a point on the window
#include <SFML/Graphics.hpp>
using namespace sf;
using Point = Vector2f;
void draw_point(RenderWindow& w, Point pos, Color color) {
Vertex p[] = { Vertex(pos, color) };
w.draw(p, 1, sf::Points);
}
int main()
{
// ed: The line below originally contained "Hello World"
RenderWindow window(VideoMode(640, 480), "Ma super fenêtre");
window.clear(Color::White);
draw_point(window, {120, 5}, Color::Red);
window.display();
sleep(seconds(10));
return 0;
}
And I compiled my programme like this:
g++ -std=c++11 exemple-graphisme1.cpp -o exemple-graphisme1 -lsfml-system -lsfml-window -lsfml-graphics
Thanks for reading !
So the problem was very simple, I edited my code to translate it in english but in the beginning the first line was
RenderWindow window(VideoMode(640, 480), "Ma super fenêtre");
And the error occured because of the character 'ê'.
Thanks for your answer and sorry for this disappointing answer !

CGAL Segmentation Fault using CGAL::create_offset_polygons_2

I'm trying the CGAL library. Specifically the straight skeleton package.
I'm getting a segmentation fault when executing one of the simple examples in the documentation and I don't know if it is a possible bug or I am doing something wrong. I appreciate any help. Thanks in advance.
This is the example:
#include <vector>
#include <boost/shared_ptr.hpp>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/create_offset_polygons_2.h>
//#include "print.h"
typedef CGAL::Exact_predicates_inexact_constructions_kernel K ;
typedef K::Point_2 Point ;
typedef CGAL::Polygon_2<K> Polygon_2 ;
typedef CGAL::Straight_skeleton_2<K> Ss ;
typedef boost::shared_ptr<Polygon_2> PolygonPtr ;
typedef boost::shared_ptr<Ss> SsPtr ;
typedef std::vector<PolygonPtr> PolygonPtrVector ;
int main()
{
Polygon_2 poly ;
poly.push_back( Point(-1,-1) ) ;
poly.push_back( Point(0,-12) ) ;
poly.push_back( Point(1,-1) ) ;
poly.push_back( Point(12,0) ) ;
poly.push_back( Point(1,1) ) ;
poly.push_back( Point(0,12) ) ;
poly.push_back( Point(-1,1) ) ;
poly.push_back( Point(-12,0) ) ;
SsPtr ss = CGAL::create_interior_straight_skeleton_2(poly);
double lOffset = 1 ;
PolygonPtrVector offset_polygons = CGAL::create_offset_polygons_2<Polygon_2>(lOffset,*ss);
//print_polygons(offset_polygons);
return 0;
}
The segmentation fault happens when the program ends, in one of the destructors, according to the debugger.
If I comment the line with create_offset_polygons_2 the program exits normally.
The debugger says that the error happens in the file Handle.hin line 62:
60 ~Handle()
61 {
62 if ( PTR && (--PTR->count == 0))
63 delete PTR;
64 }
This is the stack trace:
1 CGAL::Handle::~Handle Handle.h 62 0x41d3c4
2 CGAL::Lazy<CGAL::Line_2<CGAL::Simple_cartesian<CGAL::Interval_nt<false>>>, CGAL::Line_2<CGAL::Simple_cartesian<CGAL::Gmpq>>, CGAL::Gmpq, CGAL::Cartesian_converter<CGAL::Simple_cartesian<CGAL::Gmpq>, CGAL::Simple_cartesian<CGAL::Interval_nt<false>>, CGAL::NT_converter<CGAL::Gmpq, CGAL::Interval_nt<false>>>>::~Lazy Lazy.h 712 0x41ccbf
3 libstdc++-6!_ZN11__gnu_debug30_Safe_unordered_container_base7_M_swapERS0_ 0x6fe5a72a
4 libstdc++-6!_ZN11__gnu_debug30_Safe_unordered_container_base7_M_swapERS0_ 0x6fe5a780
5 ?? 0x6fe411b4
6 ?? 0x6fe41325
7 ?? 0x6fe41433
8 ntdll!RtlDecompressBuffer 0x77eae58e
9 ntdll!LdrShutdownThread 0x77e80e46
10 ?? 0x6fe41410
11 ntdll!LdrShutdownProcess 0x77e6a862
12 ??
I'm using:
MinGW 32 with gcc 5.3.0 (the one that comes with Qt 5.8)
CGAL version 4.9.1 with CGAL_HEADER_ONLY option
Boost version 1.64.0
libgmp version 6.1.2
libmpfr version 3.1.5
The third party libraries are compiled with the same MinGW

SDL_CreateWindowFrom() with Qt widget under Mac OS

I am using libSDL2 in a Qt app. The code is working well under Linux and Windows but is failing under Mac OS. Here is the crash output:
2015-05-12 10:24:35.598 testapp[4621:105425] -[QNSView title]: unrecognized selector sent to instance 0x7fdfbac7b8e0
2015-05-12 10:24:35.643 testapp[4621:105425] An uncaught exception was raised
2015-05-12 10:24:35.643 testapp[4621:105425] -[QNSView title]: unrecognized selector sent to instance 0x7fdfbac7b8e0
2015-05-12 10:24:35.643 testapp[4621:105425] (
0 CoreFoundation 0x00007fff9332764c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff967686de objc_exception_throw + 43
2 CoreFoundation 0x00007fff9332a6bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff93271a84 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff932715f8 _CF_forwarding_prep_0 + 120
5 testapp 0x000000010446af59 Cocoa_CreateWindowFrom + 73
6 testapp 0x000000010445fe1d SDL_CreateWindowFrom_REAL + 205
7 testapp 0x000000010417c83c main + 380
8 testapp 0x000000010417c6b4 start + 52
9 ??? 0x0000000000000001 0x0 + 1
)
That sample code reproduce the crash. Again it is working under Linux and Windows but not under Mac OS.
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
if (SDL_VideoInit(NULL) != 0) {
qCritical("SDL_VideoInit() error: %s", SDL_GetError());
return EXIT_FAILURE;
}
QWidget* qwidget = new QWidget();
SDL_Window* sdlWindow = SDL_CreateWindowFrom((void*) qwidget->winId());
if (sdlWindow == NULL) {
qCritical("SDL_CreateWindowFrom error: %s", SDL_GetError());
return EXIT_FAILURE;
}
return app.exec();
}

trying to connect to postgresql via c++ program

Background
I'm running linux... and I'm trying to write a basic little c++ program that connects to a postgresql database.
I'm trying to follow this article
http://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
Problem
I've been able to compile the library... and I can see now that I have the following folder on my computer
/usr/local/include/pqxx
But when i try to write some basic code and compile it, I get the following error:
devbox2:/var/abus# g++ testdb.cpp -lpqxx -lpq
testdb.cpp:2:22: fatal error: pqxx/pqxx: No such file or directory
#include <pqxx/pqxx>
^
compilation terminated.
Source Code
Here's what the code looks like:
1 #include <iostream>
2 #include <pqxx/pqxx>
3
4 using namespace std;
5 using namespace pqxx;
6
7 int main(int argc, char* argv[])
8 {
9 try{
10 connection C("dbname=testdestination user=testuser password=testpassword \
11 hostaddr=127.0.0.1 port=5432");
12 if (C.is_open()) {
13 cout << "Opened database successfully: " << C.dbname() << endl;
14 } else {
15 cout << "Can't open database" << endl;
16 return 1;
17 }
18 C.disconnect ();
19 }catch (const std::exception &e){
20 cerr << e.what() << std::endl;
21 return 1;
22 }
23 }
What I've tried so far:
I've been poking around the /usr/local/include/pqxx folder and I can see that there is a file called pqxx... but it doesn't have any extension on it.
Here's a snippet from the ls -lah command for that folder:
-rw-r--r-- 1 root root 637 Dec 8 21:42 pipeline
-rw-r--r-- 1 root root 7.5K Dec 8 21:42 pipeline.hxx
-rw-r--r-- 1 root root 1.1K Dec 8 21:42 pqxx
-rw-r--r-- 1 root root 728 Dec 8 21:42 prepared_statement
-rw-r--r-- 1 root root 8.2K Dec 8 21:42 prepared_statement.hxx
I've also made sure that my PATH includes the /usr/local/include/pqxx folder. This is what my PATH looks like:
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/gcc:/usr/local/include/pqxx:/usr/local/include'
I'm not sure what else I should check. Any suggestions would be appreciated.
Thanks.
To find the include files, you must add an -I option, e.g.
g++ -I/usr/local/include testdb.cpp -lpqxx -lpq
Adding directories to PATH doesn't help here, PATH is for locating executables from the shell.

lldb assertion failure when attempting to print vector

I get the error
lldb: /home/hannes/.llvm/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2271: uint64_t ::RecordLayoutBuilder::updateExternalFieldOffset(const clang::FieldDecl *, uint64_t): Assertion `ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && "Field does not have an external offset"' failed.
Aborted (core dumped)
when I try to print a vector<string>. Does anyone know why this happens, and how to fix it? The equivalent works just fine in gdb (there are a number of reason why I'd rather use / have to use lldb over gdb).
I'm running Ubuntu 12.10 with llvm, clang and lldb trunk.
The program, build instructions and lldb command sequence:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using std::for_each;
using std::begin;
using std::end;
int main() {
std::vector<std::string> vec{"Hello","World","!"};
for_each(begin(vec),end(vec),[](const std::string& s) {
std::cout << s << " ";
});
std::cout << std::endl;
return 0;
}
clang++ -g -c -std=c++11 main.cpp
clang++ -std=c++11 main.o -o test
lldb test
Current executable set to 'test' (x86_64).
(lldb) break -n main
invalid command 'breakpoint -n'
(lldb) breat set -n main
error: 'breat' is not a valid command.
(lldb) break set -n main
Breakpoint 1: where = test`main + 26 at main.cpp:5, address = 0x0000000000400aea
(lldb) run
Process 24489 launched: '/home/hannes/Documents/Programming/CXX/test/test' (x86_64)
Process 24489 stopped
* thread #1: tid = 0x5fa9, 0x0000000000400aea test`main + 26 at main.cpp:5, stop reason = breakpoint 1.1
frame #0: 0x0000000000400aea test`main + 26 at main.cpp:5
2 #include <string>
3
4 int main() {
-> 5 std::vector<std::string> vec{"Hello","World","!"};
6 return 0;
7 }
n
Process 24489 stopped
* thread #1: tid = 0x5fa9, 0x0000000000400c72 test`main + 418 at main.cpp:6, stop reason = step over
frame #0: 0x0000000000400c72 test`main + 418 at main.cpp:6
3
4 int main() {
5 std::vector<std::string> vec{"Hello","World","!"};
-> 6 return 0;
7 }
frame variable
lldb: /home/hannes/.llvm/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:2271: uint64_t <anonymous namespace>::RecordLayoutBuilder::updateExternalFieldOffset(const clang::FieldDecl *, uint64_t): Assertion `ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() && "Field does not have an external offset"' failed.
Aborted (core dumped)
Log output with debug level 10:
Logging from function (<frame object at 0x3172f20>, '/usr/lib/python2.7/dist-packages/lldb/formatters/cpp/gnu_libstdcpp.py', 141, '__init__', ['\t\tlogger = lldb.formatters.Logger.Logger()\n'], 0)
Providing synthetic children for a map named vec
Logging from function (<frame object at 0x3170d10>, '/usr/lib/python2.7/dist-packages/lldb/formatters/cpp/gnu_libstdcpp.py', 214, 'update', ['\t\tlogger = lldb.formatters.Logger.Logger()\n'], 0)
Does the same thing happen if you say frame variable --raw?
This command is meant to dump your vector disabling data formatters. In the specific case you will get (assuming no crashes) the in-memory layout of the vector instead of the printout of the strings you stored there.
I am mostly trying to figure out if the data formatters are or not a part in this issue.