How to create SIGSEGV at particular memory?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to create SIGSEGV at particular memory?
# 1  
Old 03-24-2011
Question How to create SIGSEGV at particular memory?

Hi
I want to create a SIGSEGV when program tries to access one particular area in memory. Is this possible. If so whats the command.
# 2  
Old 03-24-2011
You can't protect an individual address. You can only protect entire pages -- 4K blocks on PC systems, 8K blocks on some others -- with mprotect. You can call mprotect on any valid memory in your process but it must be on page boundaries.

Code:
char *mem;
// Allocate a page of memory that's guaranteed to be aligned
posix_memalign(&mem, getpagesize(), getpagesize());
// Write A into it
memset(mem, 'A', getpagesize());
// Prevent any access to this memory
mprotect(mem, getpagesize(), PROT_NONE);
// Either a segfault or bus error
printf("mem[0]=%d\n", mem[0]);

# 3  
Old 03-24-2011
Thanks

This is fine. But I want to protect only some byte of memory.
Else is there any way to trigger an function (like HANDLER) when some data been written over that memory?
# 4  
Old 03-24-2011
Quote:
Originally Posted by jionnet
This is fine. But I want to protect only some byte of memory.
Doesn't work that way.

Did you see my suggestion in the other thread of using pointers to pages?
Quote:
Else is there any way to trigger an function (like HANDLER) when some data been written over that memory?
You could use a hardware memory watch in a debugger. These are very limited in capability and system+architecture dependent -- an AMD64 processor running in 64-bit mode can only watch four different 32-bit spots of memory in hardware AFAIK.
# 5  
Old 03-24-2011
Thanks

I'd seen the reply...
I guess that should solve my problem...
I'll come back if some problem hits me.
Thanks again....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

SIGSEGV when allocate a certain size

The problem: I need to work with large arrays and after one of my structures grew in size my program started getting segmentation fault. My code where I allocate the arrays: static R1 *tarr; static R2 *rarr; proc_init_mem() { const int t_sz = sizeof(R1) *... (9 Replies)
Discussion started by: migurus
9 Replies

2. Solaris

sigsegv Fault

I receive a sigsegv failure. I was under the impression that a core file is created everytime a sigsegv occurrs and the process is terminated. I have had two recent occurrances of a core file not being created. Does anyone know why a core file would not be created. (2 Replies)
Discussion started by: amp4cats
2 Replies

3. UNIX and Linux Applications

SIGSEGV Signal handling

Hello, Can anybody tell me how can i handle segmentation fault signal, in C code? (2 Replies)
Discussion started by: mustus
2 Replies

4. Programming

problem in SIGSEGV signal handling

i wrote handler for sigsegv such that i can allocate memory for a variable to which sigsegv generated for illlegal acces of memory. my code is #include <signal.h> #include<stdio.h> #include<stdlib.h> #include<string.h> char *j; void segv_handler(int dummy) { j=(char *)malloc(10); ... (4 Replies)
Discussion started by: pavan6754
4 Replies

5. UNIX for Advanced & Expert Users

Need to create a memory leak

Hi. This might be a strange request, but does anyone have any idea on a simple shell script that would use more and more memory as it ran? Like a purposeful leak. I want to test the behaviour of an already running program when the machine runs out of memory. Thanks! (4 Replies)
Discussion started by: rebelbuttmunch
4 Replies

6. Programming

Gdb classifies a Bus error as SIGSEGV

Consider the following program. int main(){ struct a_t { int *a; unsigned char b; unsigned char c; unsigned char d; } A; unsigned char e; unsigned char f; unsigned char g; g = 1; A.a = (int *)&g; -> Memory misalignment... (1 Reply)
Discussion started by: balaji280283
1 Replies

7. Programming

SigSegV during stack unwind (AIX)

Hi I am getting a strange segmentation fault during the unwind process. I am trying to throw an object of an Exception class. During the DoThrow (in libC.a) the stack starts growing until a Signal is received. The object thrown is of a class that inherits from a common class, and the signal... (0 Replies)
Discussion started by: barak
0 Replies

8. Programming

SIGSEGV, Segmentation fault

Here is my initiating code: #define NUM 20 static struct tab { int count; int use; } tab; int curtab = 0; int tab_create(int tab_count) { curtab++; tab.use = 1; tab.count = tab_count; kprintf("here!"); return curtab; } (2 Replies)
Discussion started by: micmac700
2 Replies

9. SCO

Clear / Remove / Create Shared Memory...

Hi all... am not so much brilliant in this area... here i think many good hearted peoples around... so if anybody have intention to reply for my problem please give me a cut and clear picture of how... Thanks. ok comming to my problem... In our MHC server (Sco Unix 4.*) running 19 C process... (1 Reply)
Discussion started by: yocks
1 Replies

10. Programming

SIGSEGV problem

Hi. Can someone to help me in a segfault problem? I have a big C++ program that crash from time to time because it receive the SIGSEGV signal. So my question is: Can I find, without using gdb or other debugging tools, which line from source code cause that problem? Or if exist some gdb API... (1 Reply)
Discussion started by: vaidac_coder
1 Replies
Login or Register to Ask a Question