struct pointer returned by function seems partially messed up in the returned cdata

83 views
Skip to first unread message

Sarvi Shanmugham

unread,
Dec 13, 2013, 7:46:15 PM12/13/13
to pytho...@googlegroups.com

The following is contained in the header file as well as what is passed to CFFI in the py file.

struct token* add_number_expr_after(struct token* where, int num, struct expression** ret);
struct token* add_addr_space_attr_begin(struct token* tok);        //problem function
struct token* add_addr_space_attr_end(struct token* tok);

I have a C function below that I am wrapping. I added some printfs to make sure i see what porinters are being passed/returned by the C function

struct token* add_addr_space_attr_begin(struct token* tok)
{
        struct token* ret = create_built_in_token_before(tok, "__attribute__");
    printf("\nadd_addr_space_attr_begin: %p",ret);
        ret = create_char_token_after(ret, '(');
    printf("\nadd_addr_space_attr_begin: %p",ret);
        return ret;
}


I then stepped into the python debugger to step the piece of code that invokes it
> /nobackup/sarvi/spark1/rules.py(29)fixit()
-> tok = warn_info.symbol1.start_tok
(Pdb) n
> /nobackup/sarvi/spark1/rules.py(30)fixit()
-> if api_mod.check_addr_space(warn_info.symbol1, 1):
(Pdb) n
> /nobackup/sarvi/spark1/rules.py(32)fixit()
-> tok = api_mod.add_addr_space_attr_begin(tok)
(Pdb) n

add_addr_space_attr_begin: 0x2a9a81bfd0
add_addr_space_attr_begin: 0x2a9a81c010> /nobackup/sarvi/spark1/rules.py(33)fixit()
-> tok = api_mod.add_number_expr_after(tok, 1, exprptr)
(Pdb) print tok
<cdata 'struct token *' 0xffffffff9a81c010>


The value I see being returned seems like a valid pointer right to the end where it does 
return ret

But the tok that I print from the python debugger seems off on the top.
<cdata 'struct token *' 0xffffffff9a81c010>

CFFI version
>>> import cffi
>>> cffi.__version__
'0.7.2'
>>>

I can't seem to figure what is going wrong. From the looks of it looks like the function is returning a valid pointer
but the cdata object returned by CFFI has the pointer address wrong. 

Has anyone seen this before? How did you resolve it?

Sarvi 

Sarvi Shanmugham

unread,
Dec 13, 2013, 8:02:52 PM12/13/13
to pytho...@googlegroups.com
I upgarded to 0.8.1. and now my build hangs at the following
LD_LIBRARY_PATH=/ws/sarvi-sjc/mytools/lib:./DebugLNX: PYTHONPATH=./DebugLNX/py_install/lib/python2.7/site-packages: /ws/sarvi-sjc/mytools/bin/python setup.py install --prefix=./DebugLNX/py_install
In file included from ./expression.h:14,
                 from ./parse.h:13,
                 from __pycache__/_cffi_lib_x9cd4e6d7x4f3c2bc3.c:159:
./allocate.h:31:1: warning: multi-line comment
./allocate.h:40:1: warning: multi-line comment
In file included from ./expression.h:14,
                 from ./parse.h:13,
                 from __pycache__/_cffi_token_x415b8b8dx3952ec87.c:159:
./allocate.h:31:1: warning: multi-line comment
./allocate.h:40:1: warning: multi-line comment
__pycache__/_cffi_token_x415b8b8dx3952ec87.c: In function `_cffi_var_includepath':
__pycache__/_cffi_token_x415b8b8dx3952ec87.c:2280: warning: assignment from incompatible pointer type
In file included from ./expression.h:14,
                 from ./parse.h:13,
                 from __pycache__/_cffi_symbol_x3642b1a5xb28472c0.c:157:
./allocate.h:31:1: warning: multi-line comment
./allocate.h:40:1: warning: multi-line comment

Sarvi

Sarvi Shanmugham

unread,
Dec 14, 2013, 2:02:49 AM12/14/13
to pytho...@googlegroups.com
The hang was case of looped import/ffi.include between 2 python wrapper files.
Fixed that. 
Now I am still seeing crash due to returned "struct token *" pointer changing from  0x2a9a81c010 to 0xffffffff9a81c010 between the return in the C function and cdata returned by the cffi wrapper to the C function.

Any idea what this might be caused by? Is this bug in CFFI or am I doing something wrong.
Interestingly an almost similar function than returns "struct token *" with a similar wrapper seems to be returning cdata/pointer correctly.

Any help.
Sarvi

Sarvi Shanmugham

unread,
Dec 14, 2013, 2:50:08 AM12/14/13
to pytho...@googlegroups.com
I went through gdb to walk the path of return and I see the value from the "return ret" to "result=", go from 0x2a9a824010 to 0xffffffff9a824010

add_addr_space_attr_begin: 0x2a9a823f90
118             ret = create_char_token_after(ret, '(');
(gdb) n
119         printf("\nadd_addr_space_attr_begin: %p",ret);
(gdb) n
add_addr_space_attr_begin: 0x2a9a823fd0
120             return ret;
(gdb) print ret
$2 = (struct token *) 0x2a9a824010
(gdb) s
121     }
(gdb) s
_cffi_f_add_addr_space_attr_begin (self=<value optimized out>, arg0=<value optimized out>) at __pycache__/_cffi_api_mod_x382563ffxa7974013.c:404
404       _cffi_save_errno();
(gdb) list
399       }
400
401       Py_BEGIN_ALLOW_THREADS
402       _cffi_restore_errno();
403       { result = add_addr_space_attr_begin(x0); }
404       _cffi_save_errno();
405       Py_END_ALLOW_THREADS
406
407       return _cffi_from_c_pointer((char *)result, _cffi_type(1));
408     }
(gdb) print result
$3 = (struct token *) 0xffffffff9a824010
(gdb) print x0
$4 = (struct token *) 0x2a9a822f50

Tyler Wade

unread,
Dec 14, 2013, 3:03:33 AM12/14/13
to pytho...@googlegroups.com
Hi Sarvi,

Your pointer is being truncated to 32-bits. Are you sure that the signature of the function is passed to ffi.verify (either directly or by #including the appropriate header)? If it isn't, the function won’t be declared in the C code that cffi generates and the compiler will assume it returns an int.

Sarvi Shanmugham

unread,
Dec 14, 2013, 3:05:59 AM12/14/13
to pytho...@googlegroups.com
ok. looks like the problem was the verify and the generated code did not include the header file associated with the API. 
So this had generated a bunch of warnings but still compiled fine.

But the failed during execution.  Well, hopefully this help others.

Sarvi
Reply all
Reply to author
Forward
0 new messages