I tried to call C function in so file inside Python3 script.
The C function prototype is:
const char* _decrypt(const char* str); // always return NULL or malloc'ed buffer
I loaded the so in Python3, and call the function as below:
slib = ctypes.cdll.LoadLibrary('path_to_crypto.so')
ret = slib._decrypt(ctypes.c_char_p(ciphertext)) # ciphertext is valid
ctypes.c_char_p(ret).value.decode('utf-8') # this may cause SEGV fault
Occationally, ret is minus, such as -1744677792. By occationally, I mean sometimes I got minus value, sometime I got valid positive return value..
Since C function always return either NULL or valid malloc'ed buffer.
How is it possible to get minus value inside Python?
--
The issue is caused by address incorrectly returned to Python3. By applying
ret = ret & 0xffffffff
I can get the right address.
But the so is based on 64 bits address, and I tried to use 0xffffffffffffffff to mask the returned value, it does not work this way, that's it will also cause SEGV fault.
Is this because Python3 use 32bits address by default?