Passing struct to main via char* pointer

1.1k views Asked by At

What I'm about to ask is a bit hacky (in that it's a very poor method of attacking the problem) - I understand that and do not plan on using this as a long-term solution, only for a proof-of-concept.

That said, I'm working on a project that uses QEMU as a foundation to migrate processes (eventually threads) from one machine to another. I'm starting the process on the native machine, pausing it using ptrace, and then copying the CPU's registers and stack and pushing those values into a newly-created instance of QEMU (with the same underlying architecture, i.e. x86-64 --> x86_64, ARM64 --> ARM64). I then (eventually) resume execution in QEMU.

I've reached the stage where I need to pass the registers + stack into QEMU, but I've hit a bit of a wall; ideally, I would break apart QEMU and compile the entire program as part of my own program, but this is where the proof-of-concept comes into play. QEMU is a massive program, and breaking apart/reconstructing the Makefiles is something I'm not terribly keen on approaching right now. So...

Is it possible for me to fill a struct with the registers + stack in my program, create a pointer to that struct, cast that pointer as a char*, and then pass that char* pointer into execlp, to be recast in (a modified version of) QEMU? The goal is to access those values from within QEMU. For example:

struct regs_and_stack my_struct = {...};
struct regs_and_stack *my_struct_ptr = &my_struct;
execlp("qemu", "qemu", "test", "100000", (char*)my_struct_ptr);

I can post the rest of my code to get a better sense of the big picture, if requested. As always, thank you for the help!!!

EDIT

I've identified the point in the qemu main function (linux-users/main.c) where I can pop the final pointer from argv before it reaches the point where it parses through the options; I'll use this information later in the program's execution. The question is just how to get this struct into the main function in the first place.

1

There are 1 answers

0
FazeL On

here if I understood well you want to serialize your structs and then deserialize them on the other end. the following code fragment form answers of this question Serialization of struct is gonna solve problem , you have to edit your own serialize and deserialize functions because they depend to your strut's structuer. just skip the q variable enough to open a place for your structs variable to fit in the text sequence.

#include <iostream>
#include <cstring>

#define BUFSIZE 512
#define PACKETSIZE sizeof(MSG)

using namespace std;

typedef struct MSG
{
    int type;
    int priority;
    int sender;
    char message[BUFSIZE];
}MSG;

void serialize(MSG* msgPacket, char *data);
void deserialize(char *data, MSG* msgPacket);
void printMsg(MSG* msgPacket);

int main()
{
    MSG* newMsg = new MSG;
    newMsg->type = 1;
    newMsg->priority = 9;
    newMsg->sender = 2;
    strcpy(newMsg->message, "hello from server\0");
    printMsg(newMsg);

    char data[PACKETSIZE];

    serialize(newMsg, data);

    MSG* temp = new MSG;
    deserialize(data, temp);
    printMsg(temp);

    return 0;
}    

void     serialize(MSG* msgPacket, char *data)
{
    int *q = (int*)data;    
    *q = msgPacket->type;       q++;    
    *q = msgPacket->priority;   q++;    
    *q = msgPacket->sender;     q++;

    char *p = (char*)q;
    int     i = 0;
    while (i < BUFSIZE)
    {
        *p = msgPacket->message[i];
        p++;
        i++;
    }
}

void deserialize(char *data, MSG* msgPacket)
{
    int *q = (int*)data;    
    msgPacket->type = *q;       q++;    
    msgPacket->priority = *q;   q++;    
    msgPacket->sender = *q;     q++;

    char *p = (char*)q;
    int i = 0;
    while (i < BUFSIZE)
    {
        msgPacket->message[i] = *p;
        p++;
        i++;
    }
}

void printMsg(MSG* msgPacket)
{
    cout << msgPacket->type << endl;
    cout << msgPacket->priority << endl;
    cout << msgPacket->sender << endl;
    cout << msgPacket->message << endl;
}

this link would be useful too: stackoverflow.com/questions/1653681/serialization-deserialization-of-a-struct-to-a-char-in-c