473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

weird behaviour by gcc

can anyone pls tell me y is the memory allocation not alligned to 4 Bytes.
Note : compiled with gcc in linux 9
void function(int a,int b,int c)
{
char buffer1[5];
Bytes allocated(as shown by gdb)
/*buffer1[4] ---------------------> 4
buffer1[8] ---------------------> 8
buffer1[5|6|7|9...18] ----------> 18
buffer1[19...] -----------------> 28
int *ret;
ret = buffer1+28;
(*ret) += 0xa;
}
int main()
{
int x;
x=0;
function(1,2,3);
x=1;
printf("%d\n",x);
return 0;
}
I was trying the article from phrack 49 (Smashing the stack for fun n
profit) when I noticed this behaviour by gcc.
Is something wrong with gcc or am I missing on some knowledge here....

Thanks in advance.
Nov 14 '05 #1
4 1273

"gautam" <ar**********@yahoo.co.in> wrote in message
can anyone pls tell me y is the memory allocation not alligned to 4
Bytes.
Note : compiled with gcc in linux 9
The compiler can align memory as it chooses. If there is no hardware
advantage in aligning objects on 4 byte boundaries it typically won't do so.
void function(int a,int b,int c)
{
char buffer1[5];
Bytes allocated(as shown by gdb)
/*buffer1[4] ---------------------> 4
buffer1[8] ---------------------> 8
buffer1[5|6|7|9...18] ----------> 18
buffer1[19...] -----------------> 28
*/ These numbers could represent anything, such as random results from the
previous program, or they could be hardwired into the program, or they could
be stack return addresses and change on every run. int *ret;
ret = buffer1+28;
(*ret) += 0xa;
Now you are attempting to convert buffer1+28 to an integer, and add 10 to
it. If this works and ints are 4 bytes then the highest (little-endian) or
lowest (big-endian) bytes will be modified. However if the hardware doesn't
allow 32-bit writes to non-aligned addresses, and it happens that the value
doesn't fall on a boundary, anything could happen, from program termination
to memory corruption.
}
int main()
{
int x;
x=0;
function(1,2,3);
x=1;
printf("%d\n",x);
return 0;
}
function() performs an illegal operation, so anything is allowed to happen
(undefined behaviour). Since you set x to 1 after the call then, if the
program doesn't crash, it is unlikely that the prinf() call will print a
value other than 1.
I was trying the article from phrack 49 (Smashing the stack for fun > n profit) when I noticed this behaviour by gcc. Is something wrong with gcc or am I missing on some knowledge
here....

It's good to hack about with C to see how your platform implements the
language. However unless you are a hacker who likes writing security
exploits, such programs are useless for real purposes. In particular,
results don't generalise. gcc might align objects on 4 byte boundaries
whilst MSVC might not. As long as conforming C code does what the ANSI
standard says it should there is no problem here.
Nov 14 '05 #2

"gautam" <ar**********@yahoo.co.in> wrote in message news:pa****************************@yahoo.co.in...
can anyone pls tell me y is the memory allocation not
alligned to 4 Bytes.
Why do you think it should be?
Note : compiled with gcc in linux 9


This is an implementation-specific question. The C
language doesn't much care about alignment as long
as it's done such that the language works. I suggest
you ask in a newsgroup that discusses the compiler in
question.
Nov 14 '05 #3
gautam wrote:

can anyone pls tell me y is the memory allocation not alligned to 4 Bytes.
Note : compiled with gcc in linux 9
void function(int a,int b,int c)
{
char buffer1[5];
Bytes allocated(as shown by gdb)
/*buffer1[4] ---------------------> 4
buffer1[8] ---------------------> 8
buffer1[5|6|7|9...18] ----------> 18
buffer1[19...] -----------------> 28
int *ret;
ret = buffer1+28;
(*ret) += 0xa;
}
int main()
{
int x;
x=0;
function(1,2,3);
x=1;
printf("%d\n",x);
return 0;
}
I was trying the article from phrack 49 (Smashing the stack for fun n
profit) when I noticed this behaviour by gcc.
Is something wrong with gcc or am I missing on some knowledge here....


You are missing some knowledge, also the ability to spell, and the
proper attitude. I hope nobody will tell him what is going on.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4
On Sat, 06 Mar 2004 20:55:51 +0500, "gautam"
<ar**********@yahoo.co.in> wrote:
can anyone pls tell me y is the memory allocation not alligned to 4 Bytes.
Nothing in your code or post indicates how you determined memory
alignment. What memory are you talking about? On most of the popular
systems in use today, ret will be aligned on (at least) a four-byte
boundary and buffer will not have any special alignment required.
Note : compiled with gcc in linux 9
void function(int a,int b,int c)
{
char buffer1[5];
Bytes allocated(as shown by gdb)
I assume your comment really starts with this line and
/*buffer1[4] ---------------------> 4
buffer1[8] ---------------------> 8
buffer1[5|6|7|9...18] ----------> 18
buffer1[19...] -----------------> 28
ends with this line.
int *ret;
ret = buffer1+28;
I'm not sure if this is legal (assigning a pointer to a "non-existent"
address and/or one that is possibly improperly aligned) but
(*ret) += 0xa;
this definitely is not. ret points to memory you don't own. Any
attempt to dereference it invokes undefined behavior. Even if you
defined buffer to be 28+sizeof(int) bytes, the contents of buffer is
uninitialized. To attempt to evaluate any of the bytes, as += will,
would also invoke undefined behavior.
}
int main()
{
int x;
x=0;
function(1,2,3);
x=1;
printf("%d\n",x);
return 0;
}
I was trying the article from phrack 49 (Smashing the stack for fun n
profit) when I noticed this behaviour by gcc.
Is something wrong with gcc or am I missing on some knowledge here....


function() invokes undefined behavior. After that, all bets are off.
<<Remove the del for email>>
Nov 14 '05 #5

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

Similar topics

7
by: Jon Combe | last post by:
I have created the following test SQL code to illustrate a real problem I have with some SQL code. CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int...
10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
0
by: pigwin32 | last post by:
I have an asp application that uses a javascript server side include to instantiate a component for generating our menu html. The component is pooled and worked perfectly on Windows 2000. On...
3
by: Benjamin Z. Gregorian | last post by:
Hi there I have a problem with a database I wrote. On my access 2002 it is running without probs but on the comp of my collegue A2003 there is this weird behaviour. The form is a continuous...
1
by: Pankaj | last post by:
Hi All, I use a Hashtable in my program to keep unique items...at one instance I need to repopulate this hashtable through a loop. when starting repopulation hashtable.count() returns ZERO...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.