473,463 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Executing memory array on 64-bit architecture

I have a piece of test code that looks like this:
Expand|Select|Wrap|Line Numbers
  1. int mem_set_permissions(void* src, int flags) {
  2.     int pagesize = getpagesize();
  3.     if (mprotect(((unsigned char *)src - ((long)src % pagesize)), 100, flags) == -1) {
  4.         return 0;
  5.     }
  6.     return 1;
  7. }
  8.  
  9. void test() {
  10.     unsigned char *code = (unsigned char *)malloc(100);  
  11.     mem_set_permissions(code, PROT_READ | PROT_WRITE | PROT_EXEC); 
  12.     void(*codeptr)()=(void(*)())code;
  13.  
  14.     float f[4] __attribute__ ((aligned (16)));
  15.     f[0]=f[1]=f[2]=f[3]=0;
  16.     float **tmp = NULL;
  17.  
  18.     // set XMM[0] = 0
  19.     *code++ = 0x0F;    //Opcode for MOVAPS = OF 28 
  20.     *code++ = 0x28;    
  21.     *code++ = 0x05;    //XMM[0]
  22.     tmp = (float**)(code);
  23.     *tmp = f;
  24.     code += 4;
  25.  
  26.     *code++ = 0xc3; // OP-code for Return
  27.  
  28.     (codeptr)();
  29. }
  30.  
What it does is that it creates an array named code into which I put the opcodes for setting the SSE register XMM[0] = 0.
I then execute the array with the last call.

This works fine on 32-bit architectures (linux), I don't even have to use the mem_set_permissions instruction.
But on 64-bit it crashes on the execution of the array, (segmentation fault), even though I change
code += 4;
to
code += 8;

It works when I only run instructions like:
*code++ = 0xC3
But when I try the more complex instructions like XMM[0] = 0; it crashes.

I compile using gcc:
gcc test_perm_exec.c -o test_perm_exec

I would be very grateful for any help on this!

/Gustav
Sep 1 '08 #1
12 2031
weaknessforcats
9,208 Expert Mod 8TB
The first problem is that code is a pointer returned from malloc() and you are incrementing it therby losing the start of you allocation. Not good.

I would assign code to a temp pointer in increment it.

Secondly, the de-reference operatot an the incrment operator have the same precedence and are associated right to left. So *code++ will increment code and then de-reference it. I think you want to dereference code and then increment it so you should have ++(*code). Of course, using a temp variable instead of code.
Sep 1 '08 #2
JosAH
11,448 Expert 8TB
The first problem is that code is a pointer returned from malloc() and you are incrementing it therby losing the start of you allocation. Not good.

I would assign code to a temp pointer in increment it.

Secondly, the de-reference operatot an the incrment operator have the same precedence and are associated right to left. So *code++ will increment code and then de-reference it. I think you want to dereference code and then increment it so you should have ++(*code). Of course, using a temp variable instead of code.
Nope, the filling of the allocated memory was done alright; I don't speak 64 bits
Intel babble so I can't say anything else about it.

Expand|Select|Wrap|Line Numbers
  1. char foo[3];
  2. char* p= foo;
  3. *p++= 'h'; /* <--- this is what you were about */
  4. *p++= 'i';
  5. *p++= '\0';
  6. puts(foo);
  7.  
kind regards,

Jos
Sep 1 '08 #3
Yes, I have been using this exact method for a long time without any problems, it is just now that I have switched to 64-bit that it crashes.

I can execute the array if I only fill it with instructions that only work on the SSE-registers, for example XMM0 = XMM0 + XMM1.
But the problem seems to be when I need to load values from memory, as in the given example..
Sep 2 '08 #4
JosAH
11,448 Expert 8TB
What happens if you apply the mem_set_permissions() function on that float
array as well?

kind regards,

Jos (just guessing)
Sep 2 '08 #5
I have tried using it on the f-array, but that did not help unfortunately..
Sep 2 '08 #6
JosAH
11,448 Expert 8TB
I have tried using it on the f-array, but that did not help unfortunately..
... and what happens if the memory locations are part of that code array itself?

kind regards,

Jos
Sep 2 '08 #7
I have checked that and the f-array are at a completely different location than the code-array, so they should not mix up.
Sep 2 '08 #8
JosAH
11,448 Expert 8TB
I have checked that and the f-array are at a completely different location than the code-array, so they should not mix up.
Yes I know but my question was what would happen if you'd put these four
floating point values in that code array as well? (at non-overlapping locations)

kind regards,

Jos
Sep 2 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
Nope, the filling of the allocated memory was done alright; I don't speak 64 bits
Intel babble so I can't say anything else about it.
Apparently, I don't speak 64-bits either. That code in 32-bit puts the value in the wrong array element.
Sep 2 '08 #10
The exact instruction that is creating problem is
(gdb) stepi

Program received signal SIGSEGV, Segmentation fault.
0x0000000000501010 in ?? ()
2: x/i $pc 0x501010: movaps 1741983040(%rip),%xmm0 # 0x68249d57
-----------------------------------------------------------
Now, the address content and address of variable f is as below
(gdb) x/12xb 0x501010
0x501010: 0x0f 0x28 0x05 0x40 0x8d 0xd4 0x67 0xff
0x501018: 0x7f 0x00 0x00 0xc3
(gdb) p &f
$4 = (float (*)[4]) 0x7fff67d48d40

Now if you convert (1741983040) into Hex, it is coming out (67d48d40) which is only 4bytes and it should have read 8 bytes.....

So seg fault is very much understandable.... But, how to fix this ?
Sep 3 '08 #11
It seems like we have managed to solve the problem.
It turns out that on 64-bit system you cannot give a direct memory address in this way, you need use relative addressing and declare the f-vector globally.
It might also work if you change the last byte of the opcode to another value but that has not been tested properly yet...
Anyway, thanks for all the help!
Sep 4 '08 #12
It seems like...
I will really appreciate if u can post your modified code for 64bit...
Or the changes tht u made...
Sep 8 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: elziko | last post by:
I have an .NET application that calls a DLL compiled with a fortran compiler. Users are having a problem that the fortran DLL complains that is unable to allocate memory when the arrays it is using...
10
by: Craig Keightley | last post by:
I have the following array: function showCPUs(){ //name brandID dualCore dualProcessor var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=;
9
by: Evangelista Sami | last post by:
hello everybody what is the size of the memory allocated for this declaration int array; it it sizeof(int) * 11 ? (ten for the elements of the array and one for the pointer) or sizeof(int)...
5
by: Crimzon | last post by:
I am using MSVC++ 6.0 compiler. I am declaring an array char ch. Program works fine for these arbitrary sizes. But if I make the size of the array bigger like ch, the program gives me an error...
2
by: Frank Pool | last post by:
Hi, I have on large threedimensional array int largeArray; In a particular function I only need a part of this array So I'm using a new variable and assign it the follwoing way: int...
29
by: Tuvas | last post by:
I have a function in a program that works something like this. def load_pic_data(width,heigth,inpdat, filt=TRUE): data='' total=0 tnum=0 size=100 for y in range(0,heigth): row='' for x in...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
7
by: brett.estabrook | last post by:
I have written a multi-threaded c# windows service in .net 1.1 (Visual Studio .net 2003). The service has several threads that poll a Sql 6.5 database on another machine. Each thread will execute a...
11
by: harsh123 | last post by:
I wish to add a new datatype to help me in doing mathametical computations.We all know that the system has got limited amount of memory ie we cannot create an array of very big size. for example a....
6
by: CANCER.0707 | last post by:
The problem statement is as follows Create a library that creates pools of memory of different sizes.For e.g. one pool of 32 byte size, another pool of 64 byte size and so on.Create an array of...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.