Sponsored Content
Top Forums Programming Application behaving in 3 different ways on 3 different machines Post 302752799 by Don Cragun on Monday 7th of January 2013 02:06:57 PM
Old 01-07-2013
Quote:
Originally Posted by jim mcnamara
There is a set of standards for C. They dictate what will or will not happen in the language.

Doing what you did created something that has undefined behavior. I'll make one pass at this.
When you run a C program:

Code:
1 - the OS  creates a stack frame for main.
2 - the os simply overlays that stack on top of existing garbage in memory
3 - it does this for efficiency reasons and because that memory is no longer part of any process.
4 - when your program ran,  those pointers were parked on top of memory that had some existing values in it.
5 - what was in the memory depends on the program that lived in that exact memory before
6 - it could be all 00000000, it could literally be anything.
7 - since it could be anything, it is possible that the memory pointed to (0xfaaa0000) - let's pretend.
8 - 0xfaaa0000 just HAPPENED to be by random chance an OS allocated  location on your existing stack frame.
9 - Now we can use the memory for our program - no crash.
10 Why? because the memory is part of the process so you can what you want to it
11 What if 0xfaaa0000 was NOT part of allocated memory?  Boom, program crash.
12 Therefore there is no known way to predict the behavior of the code, it is undefined

I will disagree VERY slightly with Jim concerning #3 in the above list. On any UNIX system, data allocated to one process will never be given to another process for use as the stack of a new process. Doing so would create a security hole (covert channel).

Code may be shared between processes (using shared libraries); data may be shared using shared libraries, shared memory segments, mmap()ed files, etc. But address space that will be used as data (including the stack) that is not explicitly shared, will be cleared by the OS before handing it to any user-level process.

There is a lot of code run by a process when a C program starts executing before you get to the first line of code in main(). Shared libraries have to be linked in; the locale has to be initialized; the STDIO stdin, stdout, and stderr streams have to be initialized; etc. Any of these can leave random data in what will eventually become the stack frame allocated to main(), and some of them may leave different things on the stack depending on the time/date when the program was run, the version of the OS or shared libraries being used, etc.

So, the end result is the same. Uninitialized data on the stack of main() can vary from run to run. And if you have other uninitialized pointers being used by argp_parse() or s_catch_signals(), they may also be overwriting anything in your address space and may get segmentation faults on some future execution of your code.
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

ftp application behaving erratically

Hi, I am working on a custom made FTP application. The application is behaving erratically for the "ls" command. Wild card character passed to the "ls" command (like "ls *temp") is giving inconsistent results. On debuggin I have found that the "ls" command is implemented as shown below in the... (7 Replies)
Discussion started by: diganta
7 Replies

2. UNIX for Advanced & Expert Users

csplit not behaving

I have a large file with the first 2 characters of each line determining the type of record. type 03 being a subheader and then it will have multiple 04 records. eg: 03,xxx,xxxx,xxxx 04,xxxxxxxxxxxxxxxxxxxxxxxxxxxx 04,xxxxxxxxxxxxxxxxxxxxxxxxxxxx 03,xxx,xxx,xxx ... (2 Replies)
Discussion started by: badg3r
2 Replies

3. Red Hat

application to be run on machines connected in same network

I have a set up of 5 machines which are connected in same network. Now i want to run a small application so that those machines are not ideal. (0 Replies)
Discussion started by: pradeepreddy
0 Replies

4. Shell Programming and Scripting

tr command behaving unexpectedly

Im trying to execute the below command on our server to list files and replace the newline in the file list with spaces, but the character 'n' is getting replaced with a space, is there any environment variable that needs to be set in UNIX? sh -c 'ls -trx... (1 Reply)
Discussion started by: rameshrr3
1 Replies

5. UNIX for Advanced & Expert Users

FTP behaving erraneous way

Hi Gurus, I tried FTP one file to UNIX which got values like wel^come If I see the content in unix, it shows like wel^Zcome ^ coverted into ^Z (Control + Z ) Can someone please share what is happening here? Thanks, Shahnaz (5 Replies)
Discussion started by: shahnazurs
5 Replies

6. Red Hat

nslookup behaving strangely

I have two servers on same domain. one can nslookup other cannot Psu100 can lookup to psu000, psu010 & psu011 Psu110 can NOT lookup to psu000, psu010 & psu011 I verified resolv.conf entries on both psu000 and psu010 and it contains both name servers (10.200.10.21 & 10.200.11.22).I am... (1 Reply)
Discussion started by: scorohan
1 Replies

7. UNIX and Linux Applications

Linux application upgrade ways

Hello. I need upgrade memcached. This software is installed throuth yum. In official repositories isn`t newest version of memcached, but this one is vulnerable. So looks like I need built it from source, but I dont really want to install c libraries un compilers on system. 1.) So can I compile... (0 Replies)
Discussion started by: jabalv
0 Replies

8. Shell Programming and Scripting

awk not behaving as expected

Hi, Immediate help on below will be appreciated. I have to read a file (max of 10MB) which will have no new line characters, i.e. data in single line. and have to inster '\n' at every 100 characters. and if record starts with 'BUCA' then need to pick value of length 10 at position 71 and... (7 Replies)
Discussion started by: maks475
7 Replies

9. UNIX for Advanced & Expert Users

[Solved] wc behaving weirdly

Can anyone explain why wc is behaving weirdly? Their are only 2 occurrences but wc thinks their are 7 occurrences. I have even manually checked this. $ grep -i base * lit: base xx lit.lst:003- 00103 BASE XX $ grep -i base * | wc -w ... (2 Replies)
Discussion started by: cokedude
2 Replies
execstack(8)						      System Manager's Manual						      execstack(8)

NAME
execstack - tool to set, clear, or query executable stack flag of ELF binaries and shared libraries SYNOPSIS
execstack [OPTION...] [FILES] DESCRIPTION
execstack is a program which sets, clears, or queries executable stack flag of ELF binaries and shared libraries. Linux has in the past allowed execution of instructions on the stack and there are lots of binaries and shared libraries assuming this behaviour. Furthermore, GCC trampoline code for e.g. nested functions requires executable stack on many architectures. To avoid breaking binaries and shared libraries which need executable stack, ELF binaries and shared libraries now can be marked as requiring executable stack or not requiring it. This marking is done through the p_flags field in the PT_GNU_STACK program header entry. If the marking is missing, kernel or dynamic linker need to assume it might need executable stack. The marking is done automatically by recent GCC versions (objects using trampolines on the stack are marked as requiring executable stack, all other newly built objects are marked as not requiring it) and linker collects these markings into marking of the whole binary or shared library. The user can override this at assembly time (through --execstack or --noexecstack assembler options), at link time (through -z execstack or -z noexecstack linker options) and using the execstack tool also on an already linker binary or shared library. This tool is especially useful for third party shared libraries where it is known that they don't need executable stack or testing proves it. OPTIONS
-s --set-execstack Mark binary or shared library as requiring executable stack. -c --clear-execstack Mark binary or shared library as not requiring executable stack. -q --query Query executable stack marking of binaries and shared libraries. For each file it prints either - when executable stack is not required, X when executable stack is required or ? when it is unknown whether the object requires or doesn't require executable stack (the marking is missing). -V Print execstack version and exit. -? --help Print help message. --usage Print a short usage message. ARGUMENTS
Command line arguments should be names of ELF binaries and shared libraries which should be modified or queried. EXAMPLES
# execstack -s ~/lib/libfoo.so.1 will mark ~/lib/libfoo.so.1 as requiring executable stack. # execstack -c ~/bin/bar will mark ~/bin/bar as not requiring executable stack. # execstack -q ~/lib/libfoo.so.1 ~/bin/bar will query executable stack marking of the given files. SEE ALSO
ld.so(8). BUGS
execstack doesn't support yet marking of executables if they do not have PT_GNU_STACK program header entry nor they have room for program segment header table growth. AUTHORS
Jakub Jelinek <jakub@redhat.com>. 28 October 2003 execstack(8)
All times are GMT -4. The time now is 05:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy