473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hi , the following program segfault with g++3.3 , works with g++2.95 why ????

Hi , this program does a segfault in g++3.2 onwards , works with
g++2.95 ,
Any ideas are welcome , basically there is this instruction when
compiled with -S option , call *%eax , where the one with 2.95 goes on
to execude the given machine code , whereas the one compiled in g++3.2
segfaults.
Also if you feel , this is not an appropriate group to post this , I
would be obliged if you could pardon me for the mistake and guide me to
a better group.

#include <iostream>
#include <string>
using namespace std;
typedef long (*fptr) (long, long);
void
write (string & str)
{
str += (char) 0x55;//push %ebp
str += (char) 0x8B;//mov %esp,%ebp
str += (char) 0xEC;
str += (char) 0x8B;//mov 8(%ebp),%eax
str += (char) 0x45;
str += (char) 0x08;
str += (char) 0x03;//add 12(%ebp),%eax
str += (char) 0x45;
str += (char) 0x0C;
str += (char) 0x5D;//pop %ebp
str += (char) 0xC3;//ret
}

main ()
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();
val1 = 123456;
val2 = 654321;
retVal = Func (val1, val2);
cout << "Ans: " << retVal << endl;
return 0;
}

PS : CC the replies to me would be appreciated.

Jul 22 '05 #1
7 1371

<de************ @gmail.com> wrote in message
main ()
The "implicit int" rule of C is no longer supported in C++.
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();
What kind of conversion is this ? You are trying to convert a const char *
to fptr!
val1 = 123456;
val2 = 654321;
retVal = Func (val1, val2);
cout << "Ans: " << retVal << endl;
return 0;
}

PS : CC the replies to me would be appreciated.


Post here, read here.

Sharad
Jul 22 '05 #2
de************@ gmail.com wrote:
Hi , this program does a segfault in g++3.2 onwards , works with
g++2.95 ,
Any ideas are welcome , basically there is this instruction when
compiled with -S option , call *%eax , where the one with 2.95 goes on
to execude the given machine code , whereas the one compiled in g++3.2
segfaults.
Also if you feel , this is not an appropriate group to post this , I
would be obliged if you could pardon me for the mistake and guide me to
a better group.

#include <iostream>
#include <string>
using namespace std;
typedef long (*fptr) (long, long);
I wrote a simple sum function:
long sum(long a, long b)
{
return a + b;
}

The disassembly of this produces the exact same code for
g++2.95. But for 3.3, there are slight differences.
void
write (string & str)
{
str += (char) 0x55;//push %ebp
str += (char) 0x8B;//mov %esp,%ebp
str += (char) 0xEC;
str += (char) 0x8B;//mov 8(%ebp),%eax
str += (char) 0x45;
str += (char) 0x08;
str += (char) 0x03;//add 12(%ebp),%eax
str += (char) 0x45;
str += (char) 0x0C;
str += (char) 0x5D;//pop %ebp
g++ 3.3 puts a 'leave' instruction instead of 'pop ebp'. But
leave does pretty much the same thing, I guess. But modifying
the above to put a leave doesn't seem to work either.
str += (char) 0xC3;//ret
There's also a 'nop' added to the function assembly code
after the return (in 3.3). Could the segfault be because of
alignment problems?
}

main ()
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();
I'm relatively a newbie, but I read somewhere that assigning a data item
to a function pointer like the above invokes undefined behaviour. Could
that be the reason that compilers behave differently?
val1 = 123456;
val2 = 654321;
retVal = Func (val1, val2);
cout << "Ans: " << retVal << endl;
return 0;
}

PS : CC the replies to me would be appreciated.


Basically, I'm stumped too. Maybe someone else can find out where I
didn't think hard enough.
-- Anirudh

Jul 22 '05 #3
Anirudh wrote:
de************@ gmail.com wrote:
> Hi , this program does a segfault in g++3.2 onwards , works with
> g++2.95 ,
> Any ideas are welcome , basically there is this instruction when
> compiled with -S option , call *%eax , where the one with 2.95 goes on
> to execude the given machine code , whereas the one compiled in g++3.2
> segfaults.
> Also if you feel , this is not an appropriate group to post this , I
> would be obliged if you could pardon me for the mistake and guide me to
> a better group.
>
> #include <iostream>
> #include <string>
> using namespace std;
> typedef long (*fptr) (long, long);


I wrote a simple sum function:
long sum(long a, long b)
{
return a + b;
}

The disassembly of this produces the exact same code for
g++2.95. But for 3.3, there are slight differences.


Sorry, the 'long' should be replaced with 'int' to produce
the code below. Otherwise, everything turns out to be pushl,
popl etc...
> void
> write (string & str)
> {
> str += (char) 0x55;//push %ebp
> str += (char) 0x8B;//mov %esp,%ebp
> str += (char) 0xEC;
> str += (char) 0x8B;//mov 8(%ebp),%eax
> str += (char) 0x45;
> str += (char) 0x08;
> str += (char) 0x03;//add 12(%ebp),%eax
> str += (char) 0x45;
> str += (char) 0x0C;
> str += (char) 0x5D;//pop %ebp


g++ 3.3 puts a 'leave' instruction instead of 'pop ebp'. But
leave does pretty much the same thing, I guess. But modifying
the above to put a leave doesn't seem to work either.
> str += (char) 0xC3;//ret


There's also a 'nop' added to the function assembly code
after the return (in 3.3). Could the segfault be because of
alignment problems?
> }
>
> main ()
> {
> fptr Func;
> unsigned int val1, val2, retVal;
> string str;
> write (str);
> Func = (fptr) str.c_str ();


I'm relatively a newbie, but I read somewhere that assigning a data item
to a function pointer like the above invokes undefined behaviour. Could
that be the reason that compilers behave differently?
> val1 = 123456;
> val2 = 654321;
> retVal = Func (val1, val2);
> cout << "Ans: " << retVal << endl;
> return 0;
> }
>
> PS : CC the replies to me would be appreciated.
>


Basically, I'm stumped too. Maybe someone else can find out where I
didn't think hard enough.
-- Anirudh


-- Anirudh

Jul 22 '05 #4

<de************ @gmail.com> schrieb im Newsbeitrag
news:10******** **************@ f14g2000cwb.goo glegroups.com.. .

main ()
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();
val1 = 123456;
val2 = 654321;
retVal = Func (val1, val2);
cout << "Ans: " << retVal << endl;
return 0;
}

PS : CC the replies to me would be appreciated.


Which OS are you working on?
=> are you allowd to execut code within the address space your code is in?
(In Windows: VirtualProtect( ..) )
perhapes try a simple ret. I tried on my debian linux so I guess its a
problem with the memory permissions, or the segment descriptors. Im not so
familiy with this stuff on linux (I only used to work with it on windows).

But when I placed the code on the stack it worked. Your sample places the
code on the heap (new) as this is the function std::string::c_ str() is
probably using. If you would use data on the stack or even global it should
work.
Regards
Michael

<snip>
char gByte = 0xC3;

int main(){
// Failed as I guess the no execution on the heaps address space. (even
the segment descriptors could be different)
std::string fct_code = "0xC3";
LPFN_Func Func = (LPFN_Func)fct_ code.c_str();
Func();

// Worked on my machine P4, gcc 3.3.4 debian
char byte = 0xC3;
LPFN_Func Func2 = (LPFN_Func)&byt e;
Func2();

// Worked on my machine P4, gcc 3.3.4 debian
LPFN_Func Func3 = (LPFN_Func)&gBy te;
Func3();

}


</snip>

Jul 22 '05 #5
Sharad Kala wrote:
<de************ @gmail.com> wrote in message

main ()

The "implicit int" rule of C is no longer supported in C++.


nor in C.

Jul 22 '05 #6
On 8 Oct 2004 01:08:36 -0700, de************@ gmail.com wrote in
comp.lang.c++:
Hi , this program does a segfault in g++3.2 onwards , works with
g++2.95 ,
Any ideas are welcome , basically there is this instruction when
compiled with -S option , call *%eax , where the one with 2.95 goes on
to execude the given machine code , whereas the one compiled in g++3.2
segfaults.
Also if you feel , this is not an appropriate group to post this , I
would be obliged if you could pardon me for the mistake and guide me to
a better group.

#include <iostream>
#include <string>
using namespace std;
typedef long (*fptr) (long, long);
void
write (string & str)
{
str += (char) 0x55;//push %ebp
str += (char) 0x8B;//mov %esp,%ebp
str += (char) 0xEC;
str += (char) 0x8B;//mov 8(%ebp),%eax
str += (char) 0x45;
str += (char) 0x08;
str += (char) 0x03;//add 12(%ebp),%eax
str += (char) 0x45;
str += (char) 0x0C;
str += (char) 0x5D;//pop %ebp
str += (char) 0xC3;//ret
}

main ()
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();


[snip]

This line is completely illegal in either C or C++. There is
absolutely no conversion defined at all between pointers to functions
and pointers to objects.

The behavior of this code is completely undefined. Whatever happens
once you execute the last line above is not a matter for the C++
language at all. It does not specify, nor does it care.

Either stop writing such nonsense, or take it to a compiler specific
support group. It is not a language issue because you have violated
the rules of the language.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7

"Jack Klein" <ja*******@spam cop.net> schrieb im Newsbeitrag
news:33******** *************** *********@4ax.c om...
On 8 Oct 2004 01:08:36 -0700, de************@ gmail.com wrote in
comp.lang.c++:

main ()
{
fptr Func;
unsigned int val1, val2, retVal;
string str;
write (str);
Func = (fptr) str.c_str ();
[snip]
Either stop writing such nonsense, or take it to a compiler specific
support group. It is not a language issue because you have violated
the rules of the language.


I would say its definitely not nonsense, and even if so, telling someone
with these words sounds not very polite to me.
IMHO it's quite similar to a downcast or all other constructs where the
compiler does not have the type information anymore and its completely in
the hands
of the programmer to know what he is doing. I can agree that such situations
should be avoided if possible but if not, I'd say it always was a strength
of C to allow
such things and so it is in C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1566
by: Phil Powell | last post by:
Consider these two classes. Class Accepter in placement_classes.inc.php works as a form validation object, and it works like a charm: PHP: // placement_classes.inc.php - THIS ONE WORKS! class Accepter { function Accepter() {
1
1694
by: bertoulin | last post by:
When I run my script via the shell (e.g. "php test.php") the script generates the correct output everytime. If I attempt to access the script via the web (e.g. http://localhost/test.php), Apache reports a segfault: - child pid 11005 exit signal Segmentation fault (11) Occasionally, the script will create vaild output to the browser (but...
6
3004
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find on www.python.org, compiled with default options (./configure && make). I'm using the pyPgSQL plugin to connect to a PostGreSQL database, and have...
3
3361
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I call the callback from the parent process, it works fine. The PyObject is static, and holds the same value in both Parent and thread, so I'm at a...
0
1812
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run without issue? If not can you point me to a description of why. While updating objects on the screen I get a segfault after an indeterminate number...
10
1933
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go beyond the file. At some point, I tried to mend that behavior using feof() but without success. The functionality is not harmed, but this has started...
165
6758
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But when the struct is declared in main (block scope) it will segfault when passing namelist to freeFileNames().
7
10972
by: Avaenuha | last post by:
Hi, It appears my program can't get past a particular printf() statement. Code excerpt: printf("Sales Report\n--------------"); printf("Testing code - pre loop entry"); while(category != null)
10
1858
by: somebody | last post by:
There are two files below named search.c and search.h. In the for loop in search.c, the for loop never exits, even if mystruct.field1 has no match. Instead of exiting the for loop it keeps going until it segfaults. This seems to be related to the strcmp with the NULL value. There are 2 comments below that indicate the segfaults. I guess...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.