DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD Packages and Ports

OpenBSD Packages and Ports Installation and upgrading of packages and ports on OpenBSD.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 9th June 2008
gor gor is offline
New User
 
Join Date: Jun 2008
Posts: 3
Thanked 0 Times in 0 Posts
Default MySQL C library - segmentation fault on mysql_select_db() when using CC optimisation

Hello,

I have segmentation fault while trying to execute a binary that uses MySQL C API. An error occurs every time mysql_select_db() is called. The output says:
Code:
in free(): error: free_pages: pointer to wrong page
Abort trap (core dumped)
After hours of debugging, i've discovered that -O compiler flag causes that error to come. If compiled without -O flag - everything works like a charm. e.g. (compiler options from Makefile):
Code:
COPT=-I /usr/local/include/mysql -L /usr/local/lib/mysql -O
Binary crashes with segfault

Code:
COPT=-I /usr/local/include/mysql -L /usr/local/lib/mysql
Binary works fine

I am not sure whether the problem is caused by MySQL C API bug or error in cc (or maybe error in programmer )

The OS is OpenBSD 4.2 GENERIC.MP amd64

Thank you for suggestions.
Regards.
Reply With Quote
  #2   (View Single Post)  
Old 9th June 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,935
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by gor View Post
I have segmentation fault while trying to execute a binary that uses MySQL C API.
Are you compiling MySQL from ports?

Last edited by ocicat; 9th June 2008 at 01:47 AM.
Reply With Quote
  #3   (View Single Post)  
Old 9th June 2008
gor gor is offline
New User
 
Join Date: Jun 2008
Posts: 3
Thanked 0 Times in 0 Posts
Default

Thank you for reply.

No, i trying to compile my own C application which should use MySQL C API. MySQL was installed from official package.
Reply With Quote
  #4   (View Single Post)  
Old 9th June 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,935
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by gor View Post
No, i trying to compile my own C application which should use MySQL C API. MySQL was installed from official package.
Implementing compiler optimizations is a black art, & frequently exhibits brittle behaviour irregardless of the platform or compiler used. Without seeing the code and makefile used, conjecture is about all anyone can provide.

My advice is to avoid optimizations altogether. There is nothing preventing compiler vendors from changing their internals in subsequent releases which will cause optimizations to break later even if you resolve this particular situation.
Reply With Quote
  #5   (View Single Post)  
Old 9th June 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Well the error message suggests that a pointer is being free()'d that points to a wrong page, my guess is either you or the MySQL-Heads have screwed up a pointer.


When a pointer goes through free() it probably will still point to whatever it pointed to before and the page gets marked so it can be tucked away for re-allocation, or whatever the memory manager decides to do.


allocated memory is never 'just' the size you asked for because the memory manager needs to know how to work with it.


My guess is the memory manager is being told to free() memory that has been free()'d, and already merged with another segment of memory which you no longer have direct (safe) access to. Which may have been re-allocated in which case, one could be trying to free the wrong memory, which would probably be bad.



general disclaimer:

for as much as I love C, I'm probably not a good C programmer, so take my words with a grain of salt unless a good C programmer speakers up to related effect.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #6   (View Single Post)  
Old 9th June 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,935
Thanked 190 Times in 160 Posts
Default

TerryP, the OP indicates that the code compiles & executes as expected without specifying -O, but when compiler optimizations are used, execution is going into the weeds as described.

Compiler optimizations incorporate very aggressive algorithms which attempt to reduce code size & execution time of the resulting binary. Techniques such as peephole optimization attempts to substitute more efficient instructions for patterns detected in the machine code generated. Unfortunately, such algorithms can lose context which may result in errant substitutions being made. The segmentation fault mentioned by the OP can be due to an incorrect table entry being referenced, be it a one-off addressing issue or the fact that a table is now being flushed earlier than what was used without optimizations being turned on. Without code, all we can do is guess.

It should be mentioned that some less aggressive optimization techniques are already incorporated into most compilers even when optimizations are not specified. These more conservative methods have been vetted for a long time & are deemed "safe". Harsher algorithms can be specified, but with them comes greater assumptions & more instability. Compiler vendors make them available as knobs to adjust, but they are available as additional switches for a reason.
Reply With Quote
  #7   (View Single Post)  
Old 9th June 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

I should also mention there is no guarantee that my brain cells are actually useful ;-)

thanks.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #8   (View Single Post)  
Old 10th June 2008
gor gor is offline
New User
 
Join Date: Jun 2008
Posts: 3
Thanked 0 Times in 0 Posts
Default

Hello,

I am really shamed to say now that the problem was caused by "error in programmer" i've made... That happens so often, especially after 20 hours of continuous coding. Due to stupid typo in mysql structure declaration.
I used
Code:
MYSQL *db_conn; /* WRONG */
instead of
Code:
MYSQL db_conn; /* CORRECT */
Shure, i should read compiler warnings more carefully.

Thats all. Problem solved.

Thanks to everyone for replies and suggestions!

Regards.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault error139 delboy FreeBSD Ports and Packages 8 9th July 2009 06:32 PM
MYSQL 's fault? disappearedng FreeBSD General 3 14th October 2008 11:53 PM
What include and shared library directories are searched by gcc kasse FreeBSD General 3 16th July 2008 08:44 PM
Segmentation fault (11) - Apache ijk FreeBSD Ports and Packages 16 15th July 2008 11:04 AM
Segmentation fault ccc FreeBSD General 8 28th June 2008 02:15 PM


All times are GMT. The time now is 03:34 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick