debuggers.hg

view tools/libxl/libxltypes.py @ 22855:1d1eec7e1fb4

xl: Perform minimal validation of virtual disk file while parsing config file

This patch performs some very basic validation on the virtual disk
file passed through the config file. This validation ensures that we
don't go too far with the initialization like spawn qemu and more
while there could be some potentially fundamental issues.

[ Patch fixed up to work with PHYSTYPE_EMPTY 22808:6ec61438713a -iwj ]

Signed-off-by: Kamala Narasimhan <kamala.narasimhan@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Kamala Narasimhan <kamala.narasimhan@gmail.com>
date Tue Jan 25 18:09:49 2011 +0000 (2011-01-25)
parents 063927551e9c
children
line source
1 import sys
3 PASS_BY_VALUE = 1
4 PASS_BY_REFERENCE = 2
6 class Type(object):
7 def __init__(self, typename, **kwargs):
8 self.comment = kwargs.setdefault('comment', None)
9 self.namespace = kwargs.setdefault('namespace', "libxl_")
11 self.passby = kwargs.setdefault('passby', PASS_BY_VALUE)
12 if self.passby not in [PASS_BY_VALUE, PASS_BY_REFERENCE]:
13 raise ValueError
15 if typename is None: # Anonymous type
16 self.typename = None
17 self.rawname = None
18 elif self.namespace is None: # e.g. system provided types
19 self.typename = typename
20 self.rawname = typename
21 else:
22 self.typename = self.namespace + typename
23 self.rawname = typename
25 if self.typename is not None:
26 self.destructor_fn = kwargs.setdefault('destructor_fn', self.typename + "_destroy")
27 else:
28 self.destructor_fn = kwargs.setdefault('destructor_fn', None)
30 self.autogenerate_destructor = kwargs.setdefault('autogenerate_destructor', True)
32 class Builtin(Type):
33 """Builtin type"""
34 def __init__(self, typename, **kwargs):
35 kwargs.setdefault('destructor_fn', None)
36 kwargs.setdefault('autogenerate_destructor', False)
37 Type.__init__(self, typename, **kwargs)
39 class Number(Builtin):
40 def __init__(self, ctype, **kwargs):
41 kwargs.setdefault('namespace', None)
42 kwargs.setdefault('destructor_fn', None)
43 kwargs.setdefault('signed', False)
44 self.signed = kwargs['signed']
45 Builtin.__init__(self, ctype, **kwargs)
47 class UInt(Number):
48 def __init__(self, w, **kwargs):
49 kwargs.setdefault('namespace', None)
50 kwargs.setdefault('destructor_fn', None)
51 Number.__init__(self, "uint%d_t" % w, **kwargs)
53 self.width = w
55 class BitField(Type):
56 def __init__(self, ty, w, **kwargs):
57 kwargs.setdefault('namespace', None)
58 kwargs.setdefault('destructor_fn', None)
59 Type.__init__(self, ty.typename, **kwargs)
61 self.width = w
63 class Field(object):
64 """An element of an Aggregate type"""
65 def __init__(self, type, name, **kwargs):
66 self.type = type
67 self.name = name
68 self.const = kwargs.setdefault('const', False)
69 self.comment = kwargs.setdefault('comment', None)
70 self.keyvar_expr = kwargs.setdefault('keyvar_expr', None)
72 class Aggregate(Type):
73 """A type containing a collection of other types"""
74 def __init__(self, kind, typename, fields, **kwargs):
75 Type.__init__(self, typename, **kwargs)
77 self.kind = kind
79 self.fields = []
80 for f in fields:
81 # (name, type[, const=False[, comment=None]])
82 if len(f) == 2:
83 n,t = f
84 const = False
85 comment = None
86 elif len(f) == 3:
87 n,t,const = f
88 comment = None
89 else:
90 n,t,const,comment = f
91 self.fields.append(Field(t,n,const=const,comment=comment))
93 class Struct(Aggregate):
94 def __init__(self, name, fields, **kwargs):
95 kwargs.setdefault('passby', PASS_BY_REFERENCE)
96 Aggregate.__init__(self, "struct", name, fields, **kwargs)
98 class Union(Aggregate):
99 def __init__(self, name, fields, **kwargs):
100 # Generally speaking some intelligence is required to free a
101 # union therefore any specific instance of this class will
102 # need to provide an explicit destructor function.
103 kwargs.setdefault('passby', PASS_BY_REFERENCE)
104 kwargs.setdefault('destructor_fn', None)
105 Aggregate.__init__(self, "union", name, fields, **kwargs)
107 class KeyedUnion(Aggregate):
108 """A union which is keyed of another variable in the parent structure"""
109 def __init__(self, name, keyvar_name, fields, **kwargs):
110 Aggregate.__init__(self, "union", name, [], **kwargs)
112 self.keyvar_name = keyvar_name
114 for f in fields:
115 # (name, keyvar_expr, type)
117 # keyvar_expr must contain exactly one %s which will be replaced with the keyvar_name
119 n, kve, ty = f
120 self.fields.append(Field(ty, n, keyvar_expr=kve))
122 class Reference(Type):
123 """A reference to another type"""
124 def __init__(self, ty, **kwargs):
125 self.ref_type = ty
127 # Ugh
129 kwargs.setdefault('destructor_fn', "free")
130 kwargs.setdefault('autogenerate_destructor', False)
131 kwargs.setdefault('passby', PASS_BY_VALUE)
133 kwargs.setdefault('namespace', ty.namespace)
135 typename = ty.typename
136 if ty.namespace:
137 typename = typename[len(kwargs['namespace']):]
138 Type.__init__(self, typename + " *", **kwargs)
140 #
141 # Standard Types
142 #
144 void = Builtin("void *", namespace = None)
145 bool = Builtin("bool", namespace = None)
146 size_t = Number("size_t", namespace = None)
148 integer = Number("int", namespace = None, signed = True)
149 unsigned_integer = Number("unsigned int", namespace = None)
150 unsigned = Number("unsigned int", namespace = None)
151 unsigned_long = Number("unsigned long", namespace = None)
153 uint8 = UInt(8)
154 uint16 = UInt(16)
155 uint32 = UInt(32)
156 uint64 = UInt(64)
158 domid = UInt(32)
160 string = Builtin("char *", namespace = None, destructor_fn = "free")
162 inaddr_ip = Builtin("struct in_addr", namespace = None)
164 class OrderedDict(dict):
165 """A dictionary which remembers insertion order.
167 push to back on duplicate insertion"""
169 def __init__(self):
170 dict.__init__(self)
171 self.__ordered = []
173 def __setitem__(self, key, value):
174 try:
175 self.__ordered.remove(key)
176 except ValueError:
177 pass
179 self.__ordered.append(key)
180 dict.__setitem__(self, key, value)
182 def ordered_keys(self):
183 return self.__ordered
184 def ordered_values(self):
185 return [self[x] for x in self.__ordered]
186 def ordered_items(self):
187 return [(x,self[x]) for x in self.__ordered]
189 def parse(f):
190 print >>sys.stderr, "Parsing %s" % f
192 globs = {}
193 locs = OrderedDict()
195 for n,t in globals().items():
196 if isinstance(t, Type):
197 globs[n] = t
198 elif isinstance(t,type(object)) and issubclass(t, Type):
199 globs[n] = t
200 elif n in ['PASS_BY_REFERENCE', 'PASS_BY_VALUE']:
201 globs[n] = t
203 try:
204 execfile(f, globs, locs)
205 except SyntaxError,e:
206 raise SyntaxError, \
207 "Errors were found at line %d while processing %s:\n\t%s"\
208 %(e.lineno,f,e.text)
210 types = [t for t in locs.ordered_values() if isinstance(t,Type)]
212 builtins = [t for t in types if isinstance(t,Builtin)]
213 types = [t for t in types if not isinstance(t,Builtin)]
215 return (builtins,types)