0

The problem is as described above. When I try to read values from loaded *.so file (using libdl), whih are in struct I am getting wrong values Code of application:

#include <dlfcn.h>
#include <iostream>

/* For face data type reproduction */
#define GET_FACE_XYZ_SIZE 1
/* For face_array reproduction */
#define GET_FACE_ARRAY_SIZE 2
#define GET_OBJECT_DATA 3

typedef struct face {
    float x[1000];
    float y[1000];
    float z[1000];
    int vertices;
} face;


int main()
{
    void *hook;
    int (*fn)(int request_type, void *ptr);
    hook = dlopen("/root/osms/dlopen-test/lib.so", RTLD_LAZY);
    if(!hook)
    {
        std::cout << "Couldn't find lib.so" << std::endl;
    }
    fn = dlsym(hook, "object_info");
    int face_array_size = fn(GET_FACE_ARRAY_SIZE, NULL);
    std::cout << "FACE_ARRAY_SIZE: " << face_array_size << std::endl;
    face pointer[face_array_size];
    fn(NULL, pointer);
    dlclose(hook);
    std::cout << "pointer[0].z[1]: " << pointer[0].z[1] << std::endl;
return 0;
}

and code of lib.so:

/* For face data type reproduction */
#define GET_FACE_XYZ_SIZE 1
/* For face array reproduction */
#define GET_FACE_ARRAY_SIZE 2
#define GET_OBJECT_DATA 3

typedef struct face {
    float x[1000];
    float y[1000];
    float z[1000];
    int vertices;
} face;

extern "C" int object_info(int request, void *ptr)
{
    face face_array[2];
    face_array[0].x[0] = 1.1;
    face_array[0].y[0] = 0.5;
    face_array[0].z[0] = 1.2;
    face_array[0].x[1] = 1.6;
    face_array[0].y[1] = -0.11;
    face_array[0].z[1] = -12;
    face_array[0].x[2] = -0.12;
    face_array[0].y[2] = 0.24;
    face_array[0].z[2] = -0.12;
    face_array[0].vertices = 3;

    face_array[1].x[0] = -1.1;
    face_array[1].y[0] = 0.15;
    face_array[1].z[0] = -1.2;
    face_array[1].x[1] = -1.6;
    face_array[1].y[1] = 0.11;
    face_array[1].z[1] = 1.2;
    face_array[1].x[2] = 0.12;
    face_array[1].y[2] = -0.24;
    face_array[1].z[2] = 0.12;
    face_array[1].vertices = 3;

    if(request == GET_FACE_ARRAY_SIZE)
    {
         return 2;
    }
    else
    {
         ptr = face_array;
    }
}

The expected output is pointer[0].z[1]: -12 but I am getting pointer[0].z[1]: -0.12. What's wrong in my code ?

Thanks in advance

2 Answers 2

1

Accessing

pointer[0].z[1]

Has undefined behaviour, because it has an indeterminate value.


object_info never modifies the array pointed by ptr. It simply modifies a local array, and assigns the local ptr to point to that local array.

A solution: Don't declare a local array, and instead modify the array pointed by the argument. In other words, repace face face_array[2]; with:

face* face_array = (face*)ptr;

And get rid of the ptr = face_array; that does nothing meaningful.


object_info is declared to return int, but not all code paths return a value. When the function reaches the end of object_info without a return statement, the behaviour is undefined.

A solution: Always return a value if the function is not void.


face_array_size is not a compile time constant value, so face pointer[face_array_size]; will declare a variable length array. VLA are not allowed in C++.

Either use C (VLA are supported since C99, but only optionally supported since C11) instead or use a dynamic array: std::vector<face> or make peace with the fact that your program is not standard compliant.

Sign up to request clarification or add additional context in comments.

Comments

0

The variable "face_array" in function object_info and the variable "pointer" in main are not the same variable. The statement "ptr = face_array" does not change the content of "pointer".

extern "C" int object_info(int request, face *face_array)
{
if(request == GET_FACE_ARRAY_SIZE)
    return 2;
face_array[0].x[0] = 1.1;
face_array[0].y[0] = 0.5;
face_array[0].z[0] = 1.2;
face_array[0].x[1] = 1.6;
face_array[0].y[1] = -0.11;
face_array[0].z[1] = -12;
face_array[0].x[2] = -0.12;
face_array[0].y[2] = 0.24;
face_array[0].z[2] = -0.12;
face_array[0].vertices = 3;

face_array[1].x[0] = -1.1;
face_array[1].y[0] = 0.15;
face_array[1].z[0] = -1.2;
face_array[1].x[1] = -1.6;
face_array[1].y[1] = 0.11;
face_array[1].z[1] = 1.2;
face_array[1].x[2] = 0.12;
face_array[1].y[2] = -0.24;
face_array[1].z[2] = 0.12;
face_array[1].vertices = 3;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.