Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2011-10

Segmentation fault for Firebird exception inside thread

17 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Stefan

Hello, this is my first post and I hope this is the correct list for my question.

I'm getting a segmentation fault ('violación de segmento' in spanish) when running the code from below on a Linux 64-bit machine (OpenSuse 10.2 64-bits).

I'm using FPC 2.2.5, so perhaps the error is already known, but I did not find any solution in the web.

I tested the code on Windows 32 bits and Linux 32 bits too, but there everything seems ok.

The example is about opening a Firebird connection in a thread. When there is a Firebird exception inside this connection, the application terminates with the segmentation fault.

Does anyone know about this error, or can anyone give me a hint, where to look for a solution?

This is the example code:

<code>
program fbthreadtest;
{$IFDEF FPC}
   {$MODE DELPHI}{$H+}
{$ENDIF}
{$APPTYPE CONSOLE}

uses

   {$IFDEF UNIX}
   cthreads,
   cwstring,
   {$ENDIF}
   Classes,
   IBConnection, sqldb;
type
   TFBThread = class(TThread)
   private
   protected
     procedure Execute; override;
   public
     constructor Create;
   end;
constructor TFBThread.Create;
begin
   inherited Create(True);
   FreeOnTerminate := False;
   Resume;
end;
procedure TFBThread.Execute;
var
   db: TIBConnection;
   tr: TSQLTransaction;
   q: TSQLQuery;
begin
   db := TIBConnection.Create(nil);
   try
     db.DatabaseName := 'localhost:/home/data/Database/ssstst.gdb';
     db.LoginPrompt := False;
     db.HostName := '';
     db.UserName := 'SYSDBA';
     db.Password := 'masterkey';
     db.Connected := TRUE;
     tr := TSQLTransaction.Create(nil);
     try
       tr.DataBase := db;
       tr.Action   := caCommit;
       tr.Params.Clear;
       tr.Params.Add('isc_tpb_read_committed');
       tr.Params.Add('isc_tpb_rec_version');
       tr.Params.Add('isc_tpb_nowait');
       db.Transaction := tr;
       tr.Active := True;
       q := TSQLQuery.Create(nil);
       try
         q.Database    := db;
         q.Transaction := tr;
         try
           Writeln('select...');
           // the following line raises an exception because rdb$databases doesn't exist.
           q.SQL.Text := 'select count(*) from rdb$databases';
           q.Prepare;
         except
           Writeln('...exception');
         end;
       finally
         q.Free;
       end;
     finally
       tr.Commit;
       tr.Free;
     end;
   finally
     db.Connected := False;
     db.Free;
   end;
end;
begin
   with TFBThread.Create do begin
     WaitFor;
     Free;
   end;
end.
</code>

Thanks,

Stefan

Reply by email

Unknown author
to Stefan
Stefan wrote 16 lines
Hello, this is my first post and I hope this is the correct list for my question. I'm getting a segmentation fault ('violación de segmento' in spanish) when running the code from below on a Linux 64-bit machine (OpenSuse 10.2 64-bits). I'm using FPC 2.2.5, so perhaps the error is already known, but I did not find any solution in the web. I tested the code on Windows 32 bits and Linux 32 bits too, but there everything seems ok. The example is about opening a Firebird connection in a thread. When there is a Firebird exception inside this connection, the application terminates with the segmentation fault.

What do you mean by 'a firebird exception' ?

An exception generated by the firebird engine itself (i.e. fbclient.so) ?

Michael.

Reply by email

LacaK
to Stefan: Hello, this is my first post and I hope this is the correct list for my question. I'm…

May be that is is (or not ;-)) related to
http://bugs.freepascal.org/view.php?id=17360
-Laco.

Show quoted text 120 lines
Hello, this is my first post and I hope this is the correct list for my question. I'm getting a segmentation fault ('violación de segmento' in spanish) when running the code from below on a Linux 64-bit machine (OpenSuse 10.2 64-bits). I'm using FPC 2.2.5, so perhaps the error is already known, but I did not find any solution in the web. I tested the code on Windows 32 bits and Linux 32 bits too, but there everything seems ok. The example is about opening a Firebird connection in a thread. When there is a Firebird exception inside this connection, the application terminates with the segmentation fault. Does anyone know about this error, or can anyone give me a hint, where to look for a solution? This is the example code: <code> program fbthreadtest; {$IFDEF FPC} {$MODE DELPHI}{$H+} {$ENDIF} {$APPTYPE CONSOLE} uses {$IFDEF UNIX} cthreads, cwstring, {$ENDIF} Classes, IBConnection, sqldb; type TFBThread = class(TThread) private protected procedure Execute; override; public constructor Create; end; constructor TFBThread.Create; begin inherited Create(True); FreeOnTerminate := False; Resume; end; procedure TFBThread.Execute; var db: TIBConnection; tr: TSQLTransaction; q: TSQLQuery; begin db := TIBConnection.Create(nil); try db.DatabaseName := 'localhost:/home/data/Database/ssstst.gdb'; db.LoginPrompt := False; db.HostName := ''; db.UserName := 'SYSDBA'; db.Password := 'masterkey'; db.Connected := TRUE; tr := TSQLTransaction.Create(nil); try tr.DataBase := db; tr.Action := caCommit; tr.Params.Clear; tr.Params.Add('isc_tpb_read_committed'); tr.Params.Add('isc_tpb_rec_version'); tr.Params.Add('isc_tpb_nowait'); db.Transaction := tr; tr.Active := True; q := TSQLQuery.Create(nil); try q.Database := db; q.Transaction := tr; try Writeln('select...'); // the following line raises an exception because rdb$databases doesn't exist. q.SQL.Text := 'select count(*) from rdb$databases'; q.Prepare; except Writeln('...exception'); end; finally q.Free; end; finally tr.Commit; tr.Free; end; finally db.Connected := False; db.Free; end; end; begin with TFBThread.Create do begin WaitFor; Free; end; end. </code> Thanks, Stefan _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Unknown author
to LacaK
LacaK wrote
May be that is is (or not ;-)) related to http://bugs.freepascal.org/view.php?id=17360

I don't think linux has vectored exception handling ?

Michael.

-Laco. > Hello, this is my first post and I hope this is the correct list for my > question.
Show quoted text 123 lines
> > I'm getting a segmentation fault ('violación de segmento' in spanish) > when running the code from below on a Linux 64-bit machine (OpenSuse 10.2 > 64-bits). > > I'm using FPC 2.2.5, so perhaps the error is already known, but I did not > find any solution in the web. > > I tested the code on Windows 32 bits and Linux 32 bits too, but there > everything seems ok. > > The example is about opening a Firebird connection in a thread. > When there is a Firebird exception inside this connection, > the application terminates with the segmentation fault. > > Does anyone know about this error, or can anyone give me a hint, where to > look for a solution? > > This is the example code: > > <code> > program fbthreadtest; > {$IFDEF FPC} > {$MODE DELPHI}{$H+} > {$ENDIF} > > {$APPTYPE CONSOLE} > > uses > {$IFDEF UNIX} > cthreads, > cwstring, > {$ENDIF} > Classes, > IBConnection, sqldb; > > type > TFBThread = class(TThread) > private > protected > procedure Execute; override; > public > constructor Create; > end; > > constructor TFBThread.Create; > begin > inherited Create(True); > FreeOnTerminate := False; > Resume; > end; > > procedure TFBThread.Execute; > var > db: TIBConnection; > tr: TSQLTransaction; > q: TSQLQuery; > begin > db := TIBConnection.Create(nil); > try > db.DatabaseName := 'localhost:/home/data/Database/ssstst.gdb'; > db.LoginPrompt := False; > db.HostName := ''; > db.UserName := 'SYSDBA'; > db.Password := 'masterkey'; > db.Connected := TRUE; > tr := TSQLTransaction.Create(nil); > try > tr.DataBase := db; > tr.Action := caCommit; > tr.Params.Clear; > tr.Params.Add('isc_tpb_read_committed'); > tr.Params.Add('isc_tpb_rec_version'); > tr.Params.Add('isc_tpb_nowait'); > db.Transaction := tr; > tr.Active := True; > q := TSQLQuery.Create(nil); > try > q.Database := db; > q.Transaction := tr; > > try > Writeln('select...'); > // the following line raises an exception because rdb$databases > doesn't exist. > q.SQL.Text := 'select count(*) from rdb$databases'; > q.Prepare; > except > Writeln('...exception'); > end; > finally > q.Free; > end; > finally > tr.Commit; > tr.Free; > end; > finally > db.Connected := False; > db.Free; > end; > end; > > begin > with TFBThread.Create do begin > WaitFor; > Free; > end; > end. > </code> > > Thanks, > > Stefan > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel > _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Ludo Brands
to Stefan: Hello, this is my first post and I hope this is the correct list for my question. I'm…
Show quoted text 6 lines
I'm using FPC 2.2.5, so perhaps the error is already known, but I did not find any solution in the web. I tested the code on Windows 32 bits and Linux 32 bits too, but there everything seems ok.

Tried your code on ubuntu 10.04 x86_64 using both libfbclient and libfbembed 2.1, fpc 2.7.1. It doesn't create a SIGSEGV. Just a EIBDataBaseError. Problem seems to have been solved in the mean time.

Ludo

Reply by email

Joost van der Sluis
to Ludo Brands
Ludo Brands wrote 10 lines
> I'm using FPC 2.2.5, so perhaps the error is already known, > but I did not find any solution in the web. > > I tested the code on Windows 32 bits and Linux 32 bits too, > but there everything seems ok. > Tried your code on ubuntu 10.04 x86_64 using both libfbclient and libfbembed 2.1, fpc 2.7.1. It doesn't create a SIGSEGV. Just a EIBDataBaseError. Problem seems to have been solved in the mean time.

Stefan, maybe that the debugger had problems showing the error. Please try to run your application outside the debugger. Maybe that in that case you'll see an EIBDatabaseError.

Joost.

Reply by email

Stefan
to Joost van der Sluis

Ok, today I installed the fixes_2_4 branch of fpc subversion repository on Ubuntu 11.10 64-bits.

Then I compiled the code from my initial post like this:

<code>
fpc -dUseCThreads -ofbthreadtest fbthreadtest.lpr
Free Pascal Compiler version 2.4.5 [2011/10/26] for x86_64 Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling fbthreadtest.lpr
fbthreadtest.lpr(6,2) Note: APPTYPE is not supported by the target OS fbthreadtest.lpr(33,9) Warning: Symbol "Resume" is deprecated Linking fbthreadtest
/usr/bin/ld: warning: link.res contains output sections; did you forget -T? 90 lines compiled, 0.5 sec
1 warning(s) issued
1 note(s) issued
</code>

Then I called the program:

<code>

select...
...exception
Violación de segmento
</code>

This is the output when in fact a connection to the Firebird database has been established.

When the connection to the database could not be established, for example because the database does not exist, the program returns nothing.

I've installed Firebird 2.5.1 with the corresponding client library, but I think that the poblem is on the FreePascal side.

Thanks for all the suggestions so far,

Stefan

Joost van der Sluis wrote 15 lines
On Tue, 2011-10-25 at 14:10 +0200, Ludo Brands wrote: >> I'm using FPC 2.2.5, so perhaps the error is already known, >> but I did not find any solution in the web. >> >> I tested the code on Windows 32 bits and Linux 32 bits too, >> but there everything seems ok. >> > Tried your code on ubuntu 10.04 x86_64 using both libfbclient and libfbembed > 2.1, fpc 2.7.1. It doesn't create a SIGSEGV. Just a EIBDataBaseError. > Problem seems to have been solved in the mean time. Stefan, maybe that the debugger had problems showing the error. Please try to run your application outside the debugger. Maybe that in that case you'll see an EIBDatabaseError. Joost.

Reply by email

Michael Van Canneyt
to Stefan

What happens if you comipile with -gl and run the program under GDB ? can you post a backtrace ?
Note that you may need to add a {$linklib pthread} to the program source, otherwise gdb may complain about missing threads support.

The backtrace will show us exactly where the error occurs (or it should).

Michael.

Stefan wrote 68 lines
Ok, today I installed the fixes_2_4 branch of fpc subversion repository on Ubuntu 11.10 64-bits. Then I compiled the code from my initial post like this: <code> fpc -dUseCThreads -ofbthreadtest fbthreadtest.lpr Free Pascal Compiler version 2.4.5 [2011/10/26] for x86_64 Copyright (c) 1993-2010 by Florian Klaempfl Target OS: Linux for x86-64 Compiling fbthreadtest.lpr fbthreadtest.lpr(6,2) Note: APPTYPE is not supported by the target OS fbthreadtest.lpr(33,9) Warning: Symbol "Resume" is deprecated Linking fbthreadtest /usr/bin/ld: warning: link.res contains output sections; did you forget -T? 90 lines compiled, 0.5 sec 1 warning(s) issued 1 note(s) issued </code> Then I called the program: <code> select... ...exception Violación de segmento </code> This is the output when in fact a connection to the Firebird database has been established. When the connection to the database could not be established, for example because the database does not exist, the program returns nothing. I've installed Firebird 2.5.1 with the corresponding client library, but I think that the poblem is on the FreePascal side. Thanks for all the suggestions so far, Stefan El 25/10/2011 14:23, Joost van der Sluis escribió: > On Tue, 2011-10-25 at 14:10 +0200, Ludo Brands wrote: >>> I'm using FPC 2.2.5, so perhaps the error is already known, >>> but I did not find any solution in the web. >>> >>> I tested the code on Windows 32 bits and Linux 32 bits too, >>> but there everything seems ok. >>> >> Tried your code on ubuntu 10.04 x86_64 using both libfbclient and >> libfbembed >> 2.1, fpc 2.7.1. It doesn't create a SIGSEGV. Just a EIBDataBaseError. >> Problem seems to have been solved in the mean time. > Stefan, maybe that the debugger had problems showing the error. Please > try to run your application outside the debugger. Maybe that in that > case you'll see an EIBDatabaseError. > > Joost. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Ludo Brands
to Michael Van Canneyt: What happens if you comipile with -gl and run the program under GDB ? can you post a…
Show quoted text 13 lines
What happens if you comipile with -gl and run the program under GDB ? can you post a backtrace ? Note that you may need to add a {$linklib pthread} to the program source, otherwise gdb may complain about missing threads support. The backtrace will show us exactly where the error occurs (or it should). Michael.

Was able to reproduce with fb 2.5 on ubuntu 11.10 64 bit. The crash happens in the thread clean up code. Backtrace shows:
#0 0x00007ffff66b5b00 in ?? ()
#1 0x00007ffff7bc6ce3 in __nptl_deallocate_tsd () from /lib/x86_64-linux-gnu/libpthread.so.0
#2 0x00007ffff7bc6f0a in start_thread ()
from /lib/x86_64-linux-gnu/libpthread.so.0
#3 0x00007ffff76fd89d in clone () from /lib/x86_64-linux-gnu/libc.so.6 #4 0x0000000000000000 in ?? ()

Traced into __pthread_exit and the sigsegv happens in __pthread_unwind calling _Unwind_ForcedUnwind. This is an indication of c++ exception being not dealt with correctly and the exception (re?)raised by _Unwind_ForcedUnwind isn't caught.

Ludo

Reply by email

Unknown author
to Ludo Brands
Ludo Brands wrote 28 lines
> > What happens if you comipile with -gl and run the program > under GDB ? > can you post a backtrace ? > Note that you may need to add a {$linklib pthread} to the > program source, > otherwise gdb may complain about missing threads support. > > The backtrace will show us exactly where the error occurs (or > it should). > > Michael. > Was able to reproduce with fb 2.5 on ubuntu 11.10 64 bit. The crash happens in the thread clean up code. Backtrace shows: #0 0x00007ffff66b5b00 in ?? () #1 0x00007ffff7bc6ce3 in __nptl_deallocate_tsd () from /lib/x86_64-linux-gnu/libpthread.so.0 #2 0x00007ffff7bc6f0a in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0 #3 0x00007ffff76fd89d in clone () from /lib/x86_64-linux-gnu/libc.so.6 #4 0x0000000000000000 in ?? () Traced into __pthread_exit and the sigsegv happens in __pthread_unwind calling _Unwind_ForcedUnwind. This is an indication of c++ exception being not dealt with correctly and the exception (re?)raised by _Unwind_ForcedUnwind isn't caught.

If it is a C++ exception, then it is a firebird client bug. The exceptions are not supposed to leave the fbclient library...

Michael.

Reply by email

Jonas Maebe
to Ludo Brands
Ludo Brands wrote 5 lines
> If it is a C++ exception, then it is a firebird client bug. > The exceptions are not supposed to leave the fbclient library... Or fpc is catching the c++ exception and not chaining them. Similar to win64.

It is impossible for FPC to catch a C++ exception on non-Windows platforms with FPC containing special code tying into the C++ run time (which it doesn't). What is possible is that if you have C++ code calling FPC code calling other C++ code, and the inner C++ code throws an exception, that it doesn't manage to crawl its way back to the outer C++ code because missing/incomplete/incorrect FPC dwarf-eh frame information.

Jonas

Reply by email

Sven Barth
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 27 Oct 2011, at 16:17, Ludo Brands wrote: >> If it is a C++ exception, then it is a firebird client bug. >> The exceptions are not supposed to leave the fbclient library... > > Or fpc is catching the c++ exception and not chaining them. Similar to > win64. It is impossible for FPC to catch a C++ exception on non-Windows platforms with FPC containing special code tying into the C++ run time (which it doesn't).

And on Windows it is likely that FPC will pass-through C++ exceptions (AFAIK they have a special error code) to the operating system. As a sidenote: FPC will also pass-through Delphi exceptions which use a special error code as well.

Regards,
Sven

Reply by email

Stefan
to Ludo Brands: Was able to reproduce with fb 2.5 on ubuntu 11.10 64 bit. The crash happens in the thread…

Thanks a lot. I've made some more tests as well,
and it turns out that the error occurs on Linux 32 and 64 bits, when using the Firebird 2.5.x client.
No problems on Windows (tested on Windows 7 64 bits and XP), nor on Linux when connecting with a 2.1 Firebird client.

Sorry for bothering, seems that I better post this problem to the Firebird development group.

Meanwhile I just use the 2.1 client library to connect to the database.

Stefan

Ludo Brands wrote 23 lines
Was able to reproduce with fb 2.5 on ubuntu 11.10 64 bit. The crash happens in the thread clean up code. Backtrace shows: #0 0x00007ffff66b5b00 in ?? () #1 0x00007ffff7bc6ce3 in __nptl_deallocate_tsd () from /lib/x86_64-linux-gnu/libpthread.so.0 #2 0x00007ffff7bc6f0a in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0 #3 0x00007ffff76fd89d in clone () from /lib/x86_64-linux-gnu/libc.so.6 #4 0x0000000000000000 in ?? () Traced into __pthread_exit and the sigsegv happens in __pthread_unwind calling _Unwind_ForcedUnwind. This is an indication of c++ exception being not dealt with correctly and the exception (re?)raised by _Unwind_ForcedUnwind isn't caught. Ludo _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Unknown author
to Sven Barth: And on Windows it is likely that FPC will pass-through C++ exceptions (AFAIK they have a…
Sven Barth wrote 16 lines
Am 27.10.2011 16:38, schrieb Jonas Maebe: > > On 27 Oct 2011, at 16:17, Ludo Brands wrote: > >>> If it is a C++ exception, then it is a firebird client bug. >>> The exceptions are not supposed to leave the fbclient library... >> >> Or fpc is catching the c++ exception and not chaining them. Similar to >> win64. > > It is impossible for FPC to catch a C++ exception on non-Windows > platforms with FPC containing special code tying into the C++ run time > (which it doesn't). And on Windows it is likely that FPC will pass-through C++ exceptions (AFAIK they have a special error code) to the operating system.

No, it does not (at least not correctly) , there is a bug for this.

Michael.

Reply by email

Sven Barth
to Unknown author

Am 27.10.2011 17:17, schrieb

Show quoted text 22 lines
On Thu, 27 Oct 2011, Sven Barth wrote: > Am 27.10.2011 16:38, schrieb Jonas Maebe: >> >> On 27 Oct 2011, at 16:17, Ludo Brands wrote: >> >>>> If it is a C++ exception, then it is a firebird client bug. >>>> The exceptions are not supposed to leave the fbclient library... >>> >>> Or fpc is catching the c++ exception and not chaining them. Similar to >>> win64. >> >> It is impossible for FPC to catch a C++ exception on non-Windows >> platforms with FPC containing special code tying into the C++ run time >> (which it doesn't). > > And on Windows it is likely that FPC will pass-through C++ exceptions > (AFAIK they have a special error code) to the operating system. No, it does not (at least not correctly) , there is a bug for this.

You are indeed right. The exception code for C++ is "$e06d7363" (according to an article on CodeProject) and FPC's handler checks in its "else"-part whether the "SEVERITY_ERROR" flag ($c0000000) is set (which is the case here...).

Regards,
Sven

Reply by email