[Spice-devel] [PATCH spice-common 4/9] codegen: Allows to generate C declarations automatically

Christophe Fergeau cfergeau at redhat.com
Mon Mar 11 13:09:06 UTC 2019


On Fri, Mar 08, 2019 at 04:47:35PM -0500, Frediano Ziglio wrote:
> > 
> > On Sun, Mar 03, 2019 at 07:10:25PM +0000, Frediano Ziglio wrote:
> > > Allows to specify a @declare attribute for messages and structure
> > > that can generate the needed C structures.
> > > 
> > > Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> > > ---
> > >  python_modules/ptypes.py | 64 ++++++++++++++++++++++++++++++++++++++++
> > >  spice_codegen.py         | 47 +++++++++++++++++++++++++++++
> > >  2 files changed, 111 insertions(+)
> > > 
> > > diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
> > > index 7dca78d..64c198e 100644
> > > --- a/python_modules/ptypes.py
> > > +++ b/python_modules/ptypes.py
> > > @@ -72,6 +72,8 @@ valid_attributes=set([
> > >      'zero',
> > >      # this attribute does not exist on the network, fill just structure
> > >      with the value
> > >      'virtual',
> > > +    # generate C structure declarations from protocol definition
> > > +    'declare',
> > >  ])
> > >  
> > >  attributes_with_arguments=set([
> > > @@ -485,6 +487,26 @@ class ArrayType(Type):
> > >      def c_type(self):
> > >          return self.element_type.c_type()
> > >  
> > > +    def generate_c_declaration(self, writer, member):
> > > +        name = member.name
> > > +        if member.has_attr("chunk"):
> > > +            return writer.writeln('SpiceChunks *%s;' % name)
> > > +        if member.has_attr("as_ptr"):
> > > +            len_var = member.attributes["as_ptr"][0]
> > > +            writer.writeln('uint32_t %s;' % len_var)
> > > +            return writer.writeln('%s *%s;' % (self.c_type(), name))
> > > +        if member.has_attr("to_ptr"): # TODO len
> > > +            return writer.writeln('%s *%s;' % (self.c_type(), name))
> > > +        if member.has_attr("ptr_array"): # TODO where the length is
> > > stored? overflow?
> > > +            return writer.writeln('%s *%s[0];' % (self.c_type(), name))
> > > +        if member.has_end_attr() or self.is_remaining_length(): # TODO len
> > > +            return writer.writeln('%s %s[0];' % (self.c_type(), name))
> > 
> > These TODO are worrying, but only the has_end_attr() one seems to be run
> > at the moment, the others seem to be dead code?
> > 
> 
> Indeed. And also is more a "for security we should avoid that in the
> protocol" so it's not much related to code generation.
> The idea is that "if you have an array as output you should also have
> the length in the output otherwise you are probably generating an
> overflow".
> 
> The check should be done parsing the protocol file, not generating
> a specific code from it (surely demarshaller code is affected).
> 
> Maybe the best is to move these TODOs in a separate patch.
> And possibly I'll keep it somewhere when I'll have the time to
> generate the proper checks.

Hmm, ok, might indeed be better to move them to a different commit.

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20190311/01ea6d24/attachment.sig>


More information about the Spice-devel mailing list