mprotect fails with ENOMEM in text segment


 
Thread Tools Search this Thread
Operating Systems AIX mprotect fails with ENOMEM in text segment
# 1  
Old 04-13-2011
mprotect fails with ENOMEM in text segment

Hi guys,

I use AIX version 5 on IBM Power 5+ machine. I am currently trying to experiment with sort of self-modifying code, like this:

Code:
ucontext_t ut;
getcontext(&ut);
int iar = ut.uc_mcontext.jmp_context.iar;
int pageSize = getpagesize();
int rest = iar % pageSize;
void *ptr = iar - rest;
mprotect(ptr,pageSize,PROT_READ | PROT_WRITE | PROT_EXEC);

However, mprotect fails with ENOMEM ("Not enough space"). The above code works perfectly on Linux machine.

I also set environment variables MPROTECT_TXT and XPG_SUS_ENV to ON, with no effect.

Is there a way to make it work, or is there any other way to make changes to program's own text segment?

Regards
# 2  
Old 04-15-2011
Registers are os architecture specific?
# 3  
Old 04-15-2011
Pointers are not integers. On some platforms integers can hold pointers safely but that's just coincidence; here, you're cramming 64-bit values into 32-bit variables and hence truncating them quite a lot. If you include stdint.h I think you can use the pointer-safe ptrdiff_t type instead of int
# 4  
Old 05-05-2011
I've been trying to get over this problem for some time now, without success. I use type intptr_t as you advised:

Code:
uintptr_t ptr2 = iar;

Then I have a loop with mprotect:
Code:
mprotect(ptr2+i,pageSize,PROT_READ | PROT_WRITE | PROT_EXEC);

It failes with "Invalid argument" every time the address is not aligned to page size (as expected) but also fails with "Not enough memory" when the (ptr2-i) is properly aligned. Any more ideas?
# 5  
Old 05-05-2011
That is still an unsigned int pointer type, implying you are assigning only 32 bits. IBM says some pages are not mprotect friendly this way: pSeries and AIX Information Center

You might need that env variable. I am familiar with self-modifying code from machine language, what do you want it to do?
# 6  
Old 05-05-2011
Thank you for a quick reply.

Quote:
That is still an unsigned int pointer type, implying you are assigning only 32 bits.
I don't think that's the problem since the routine works well when performed on non-text segment (for example on page which contains area acquired via malloc).

Quote:
IBM says some pages are not mprotect friendly this way: pSeries and AIX Information Center You might need that env variable.
I have these in my ~/.profile:
Code:
MPROTECT_TXT=ON
XPG_SUS_ENV=ON

Is this enough for these env vars to be 'seen'? I tried different values of those, no effect.

Quote:
I am familiar with self-modifying code from machine language, what do you want it to do?
I am trying to write code to reload the program text from the original executable file in case an error occurs (e.g. a hardware error which scrambled some memory). Is there another way of doing it, instead of mprotect/memset ?
# 7  
Old 06-22-2011
If anyone was wandering, switching to AIX 6.1 did the trick. Documentation for 6.1 states that mprotect for text segment will work with MPROTECT_TXT env variable set to ON and it does. Documentation for 5.3 does not say such thing apparently, I primarily thought that the call in both versions will have the same effect, but apparently it doesn't.

Anyway, thanks for help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

2. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

3. Programming

Segment Violation

Hi to all. I'm reciving a "Segment violation" error from this code and I don't know why. void insertAtEnd(NodeType *pList) { char element; printf("Introduce a element: \n"); setbuf(stdin, NULL); scanf("%c", &element); //Find the end of the list; while... (4 Replies)
Discussion started by: daniel.gbaena
4 Replies

4. Programming

How can I know where the segment of memory is all Zero?

I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx (4 Replies)
Discussion started by: macroideal
4 Replies

5. Shell Programming and Scripting

How to change a segment in a particular position

I need help in removing a leading zero in a particular position. For eg.: XYZ*04567472*0099*020091231*0123*0.12 In the above line, I want to replace "*0123" with "123" and "0.12" with ".12". I want to remove the leading zero only in position number 4 and 5 (the bolded segments) I was able... (10 Replies)
Discussion started by: ananthmm
10 Replies

6. UNIX for Dummies Questions & Answers

code segment

how do i close a do code segment? od? (1 Reply)
Discussion started by: trob
1 Replies

7. Shell Programming and Scripting

extract segment

Hey all, could someone please direct me on how to extract a segment from a file between two tags? Thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

8. IP Networking

Network Access on Different Segment

I do have 2 different segment network which different platform on each segment. E.g. 20 segment, Windows OS can talk to Unix OS and 21 segment practice same rule. Further more, Windows OS from 20 segment can talk to 21 segment other OS but only the UNIX OS in 20 segment fail to talk to 21 segment.... (3 Replies)
Discussion started by: HASM
3 Replies

9. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies

10. UNIX for Advanced & Expert Users

Enomem in Journal Retry Error

Hi, Does anyone seen this error before.. kernel: ENOMEM in journal_alloc_journal_head, retrying. I encounter this problem on IBM eServers where when the above error appears usually the machine is dead or hanged. Unless a hard reboot is been done. Is this something have to do with the memory... (1 Reply)
Discussion started by: killerserv
1 Replies
Login or Register to Ask a Question