UBUNTU on VMWARE Segmentation fault (core dumped) - c

sorry if my english is horrible: I'm realy stresed :(
I'm using Ubuntu 11.04 that work as virtual machine. Using VMWARE as virtual machine operating system is windows 7.
I'm trying to write a program on linux with C language: that using shared memory. when I try to compile there are no errors but when I execute it I see an error like this:
Segmentation fault (core dumped)
I'm not sure but as far as I learned VMWARE is causing this :S
here are the codes:
#include<stdio.h>
#include<sys/wait.h> //Process wait
#include <fcntl.h> //File
//#include <cstdlib>
//#include<fstream.h>
int main(){
printf("\n Here we go...!");
int *Numbers;
Numbers=(int*)getmem(327); // shared memory
int i,ProcID;
ProcID=fork(); //depart processor
if(ProcID==0){ // child processor
for(i=0;i<50;i++){
Numbers[i]=random()%50;
}
}else if(ProcID<0){
printf("\n Hmm... There is an error!");
}
int Waiting;
wait(&Waiting);
if(ProcID>0){ // parent processor
int fileeven,fileodd;
fileeven=open("EK_even.txt",O_RDWR|O_CREAT,0600);
fileodd=open("EK_odd.txt",O_RDWR|O_CREAT,0600);
for(i=0;i<50;i++){
if(Numbers[i]%2==0){
write(fileeven,&Numbers[i],sizeof(Numbers[i]));
}else{
write(fileodd,&Numbers[i],sizeof(Numbers[i]));
}
}
close(fileeven);
close(fileodd);
}else if(ProcID<0){
printf("\n Hmm... There is an error!");
}
return 1;
}
I'm using this to compile on terminal:gcc -o ./RUN ./EK.c -shared
to Run :./RUN
as result :Segmentation fault (core dumped)
Thanks for your time and reponds I'm realy in need...

Assuming getmem takes a number of bytes as a parameter, you allocate 327 bytes for your array of numbers:
Numbers=(int*)getmem(327);
If you are on a 64 bit system with 8-byte int, this is enough space for 40 integers.
You then proceed to put 50 numbers into that array, more than you allocated space for. This might very well cause a segmentation fault.
Generally, start your program in a debugger to see where exactly the segmentation fault occurs. This way you can more easily locate the error in your program.

The problem is VMWare as Marc B said. I tryed it on a real operating system and it worked. getmem() function is not my own function. To use it you have to add "-shared" at the end of compile line. Thanks for replies...

Related

Got segfault with a program to test shellcode

I found this C code example to run shellcode, but it cases segmentation fault for me. It doesn't cause segmentation fault on my friend's machine though, so I am wondering if it is a version issue. Is char causing the segmentation fault?
#include<stdio.h>
#include<string.h>
unsigned char code[] = \
"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
main()
{
printf("Shellcode Length: %d\n", strlen(code));
int (*ret)() = (int(*)())code;
ret();
}
In order for your shellcode to properly work, you'll have to compile your program with the -z execstack flag. This disables the NX protection which is enabled by default and prevents pages holding data from being marked as executable. If you don't disable NX the program will segfault because the memory page where your shellcode string is going to be put is not going to be executable, and the process will get killed when trying to execute its content.
I also see an int 0x80 in your shellcode so I assume this is supposed to be an x86 32bit shellcode. Other than that you're missing an int in front of main(), but I guess that's a copy-paste error.
Compile your program with:
gcc -m32 -z execstack prog.c

Segmentation fault with GDB debugger - C

I am trying to "debug" this program using GDB debugger. I get the Segmentation fault (core dumped) when I execute the program.
This is my first time using GDB, so I do not really know what command to use or what to expect.
EDIT: I know what the error is. I need to find it using the GDB Debugger
This is the code:
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Introduzca un entero: ");
scanf("%d",n);
if (n < 0)
printf("Error! Factorial de un numero negativo no existe.");
else
{
for(i=0; i<=n; ++i)
{
factorial *= i;
}
printf("Factorial de %d = %llu", n, factorial);
}
return 0;
}
Here is the problem:
scanf("%d",n);
As you wrote, n is declared as a variable of type int. What you want to do is to pass the address of n instead of n itself into the function.
scanf("%d", &n);
To better understand the implementation of scanf(), check out stdio.h.
Also, set n = 1. Or otherwise the variable factorial will remain 0 regardless how many loops you've gone through.
EDIT: what you are trying to do to is to access a memory location passed in by the user, which is highly likely to map to a memory location that belongs to a completely different process or even OS. The segmentation fault is generated simply because the location is not accessible. What you can do in gdb is using bt in the gdb to a stack trace of segmentation fault.
I know what the error is. I need to find it using the GDB Debugger
You need to read the documentation of gdb (and you should compile your source code with all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC; this puts DWARF debug information inside your executable).
The GDB user manual contains a Sample GDB session section. You should read it carefully, and experiment gdb in your terminal. The debugger will help you to run your program step by step, and to query its state (and also to analyze core dumps post-mortem). Thus, you will understand what is happening.
Don't expect us to repeat what is in that tutorial section.
Try also the gdb -tui option.
PS. Don't expect StackOverflow to tell you what is easily and well documented. You are expected to find and read documentation before asking on SO.

Detecting a segmention fault in C

I have Win7 Pro (32 bit) and CodeBlocks IDE.
I would like to know is there any way to detect line with a segmention fault in C. My code is PRIME1.c
I find somewhere on Stack Overflow that this is possible on linux in terminal, but I would like to do that in Windows.
Could anyone tell me how to do that?
Many thanks!
In other words, I would like to know how to use debugger from cmd in windows 7 and how it can tell me which line is problematic.
I just found this link
Determine the line of C code that causes a segmentation fault?
But, as you can see, this is for Linux.
I would like to know how can I do that in Windows cmd?
You can catch seg fault. But, unfortunately, can not handle this event anyhow or get any info about that fault (in standard way, there are workarounds specific for compilers). So, maybe put printf in every line with __LINE__ macro and just wait until it fails.
#include <signal.h>
#include <conio.h>
#include <stdio.h>
void listener(int sig) {
printf("listener: access violation");
_getch();
}
void main() {
char a = 10;
char *p = &a;
signal(SIGSEGV, listener);
do {
printf("%d", *p++);
} while (1);
_getch();
}

Shared memory: segmentation fault core dumped

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/shm.h>
int main()
{
int shmid;
char *viraddr;
char buffer[BUFSIZ];
shmid=shmget(1234,BUFSIZ,0666|IPC_CREAT);
viraddr=(char*)shmat(shmid, 0,0);
while(1)
{
puts("Enter some text:");
fgets(buffer,BUFSIZ,stdin);
strcat(viraddr,buffer);
if(strncmp(buffer,"end",3)==0)
break;
}
shmdt(viraddr);
exit(0);
}
I am learning shared memory.I want to realize the communication between two processes. When I run this program, it always indicates this error -- segmentation fault core dumped. Any help on this? Thanks.
Here both viraddr and buffer have the same size. For strcat() it is recommended dst size to be atleast a byte larger than the src size. viraddr should have enough space to hold buffer data just to avoid buffer overrun.
Perhaps, sometimes the memory allocation returns FFFFF and then throws Segmentation Fault Core Dumped. Reason being you don't have enough rights to access the Memory for your processes.
Simply use sudo to compile and run the code. For instance say you have these files
shm1.c
shm2.c
shm.h
Then your commands need to be fired as following
sudo gcc shm1.c -o s1
sudo gcc shm2.c -o s2
sudo ./s1 &
sudo ./s2
If you are willing to do it on 2 different terminals, just fire sudo ./s1 in terminal1 & sudo ./s2 in different terminal 2

ASM code in C program causes segmentation fault

I included some sample ASM code in a small program to do a test.
My program is:
#include <stdio.h>
static inline
unsigned char inb (int port) {
unsigned char data;
asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
return data;
}
int main()
{
printf("hello world %d\n", inb(22));
return 0;
}
When I run the program, it crashes with a segmentation fault when executing the ASM code.
Could someone tell me what's wrong with this small program? Thanks a lot.
You need to use ioperm before you're allowed to use port I/O. Also, note the kernel already provides inb and outb functions.
Use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the
user space application to access the I/O ports in question. Failure
to do this will cause the application to receive a segmentation fault.
If your OS is Windows or Linux, most likely your program is terminated because the OS doesn't allow regular applications access I/O ports.
You syntax is absolutely correct. Just find and use the valid or unused port on your system.

Resources