flat assembler
Message board for the users of flat assembler.

Index > Linux > How to shm_open everything?

Author
Thread Post new topic Reply to topic
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 04 Oct 2017, 13:13
Hi, I dont need code, just wondered if this was possible and if so how I might go about it.
Basically I want process B to make its entire address space (particularly the stack and the heap) available to process A. The problem I forsee is that the beginning of the stack could change dynamically, and shm_open treats the memory as a file whose beginning is constant. And I don't even know how to find out where the heap begins. I can't use ptrace BTW, since the code must work on both Linux and MacOS.
If shm_open isn't a viable option, any other ideas?
Thanks.
Post 04 Oct 2017, 13:13
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 04 Oct 2017, 16:52
Note: when I said "entire address space" I really meant all the pages that are in use.
Post 04 Oct 2017, 16:52
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 04 Oct 2017, 23:56
It just occurred to me that I could just set rbp and rsp to point to some already existing block of shared memory, then carry on and hope that the OS/kernel doesn't notice and get upset. The drawbacks of this I see are 1) would the OS let me place the new stack in a relatively high region of memory? and 2) would my new "stack" automatically grow downwards or would I have to allocate crazy amounts of memory "just in case"? and 3) I'm no further forward with the heap - and I'm not thrilled at the idea of writing my own malloc/free.
Post 04 Oct 2017, 23:56
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 05 Oct 2017, 01:00
Post 05 Oct 2017, 01:00
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 05 Oct 2017, 02:39
Hi fasmnewbie,
Thanks for your reply. I read the entire page with great interest but unfortunately I don't think it's what I'm looking for. I want to share already existing memory (heap and stack) with another process. mmap creates new memory. I'm familiar with mmap... In fact I'm using it in my debugger/debuggee for sharing source code (probably frowned upon I know but I didn't want to get bogged down in all that ELF/DWARF nonsense). I got excited when I read about the amalloc/acreate functions on that page, because they seemed really useful when combined with my "new stack" idea, but it seems like those functions are not available on Linux or MacOS. Sad

Here's where I am at the moment: I've got a simple debugger working that forks a process, notices a division by zero or a breakpoint and prints a simple backtrace. Now I want to examine local variables and follow pointers around in the traced process. I can send the local variables to the debugger using a pipe, but when you consider that these locals might be pointing to heap objects, which in turn might point to other heap objects (to arbitrary depths) the whole operation becomes ridiculously inefficient. If it was just Linux I could probably examine the memory whilst the process was stopped, but MacOS lacks both a working ptrace and a /proc filesystem. Sad

Any more thoughts?
Post 05 Oct 2017, 02:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 05 Oct 2017, 03:46
ProphetOfDoom wrote:
It just occurred to me that I could just set rbp and rsp to point to some already existing block of shared memory, then carry on and hope that the OS/kernel doesn't notice and get upset. The drawbacks of this I see are 1) would the OS let me place the new stack in a relatively high region of memory? and 2) would my new "stack" automatically grow downwards or would I have to allocate crazy amounts of memory "just in case"? and 3) I'm no further forward with the heap - and I'm not thrilled at the idea of writing my own malloc/free.
Stack memory is created differently, it uses different settings for the memory region. You won't be able to create a new stack in a new region of memory by simply altering RSP.

Most OSes only allow one stack region per thread, I expect Linux is the same in this regard also.
Post 05 Oct 2017, 03:46
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 05 Oct 2017, 03:58
Thanks revolution. Interesting to know. If annoying.
Post 05 Oct 2017, 03:58
View user's profile Send private message Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 05 Oct 2017, 05:36
Okay so it might be the end for my crazy workarounds to please MacOS and make it accept code written for Linux.
After Googling and stressing all night, I finally managed to get the mach kernel system call task_for_pid() working, which suggests that I may be able to read and write memory with some other mach functions. So I'll just do an #if #elif #endif and insert OS-specific code.
No more mad memory gymnastics.
The solution in the end was quite simple: I needed to create a self-signed certificate for development, then sign my debugger with the certificate, then run it using sudo. I am very relieved. I'll post here if I run into any more issues.Now I'm going to bed.
Post 05 Oct 2017, 05:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.