VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack.py@ 78076

Last change on this file since 78076 was 78076, checked in by vboxsync, 6 years ago

GuestHost/OpenGL,HostServices/SharedOpenGL: Fixed parameter validation, bugref:9433

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.4 KB
Line 
1# Copyright (c) 2001, Stanford University
2# All rights reserved.
3#
4# See the file LICENSE.txt for information on redistributing this software.
5
6from __future__ import print_function
7import sys
8
9import apiutil
10
11
12apiutil.CopyrightC()
13
14print("""/* DO NOT EDIT! THIS CODE IS AUTOGENERATED BY unpack.py */
15
16#include "unpacker.h"
17#include "cr_opcodes.h"
18#include "cr_error.h"
19#include "cr_mem.h"
20#include "cr_spu.h"
21#include "unpack_extend.h"
22#include <stdio.h>
23#include <memory.h>
24
25#include <iprt/cdefs.h>
26
27DECLEXPORT(const unsigned char *) cr_unpackData = NULL;
28DECLEXPORT(const unsigned char *) cr_unpackDataEnd = NULL;
29SPUDispatchTable cr_unpackDispatch;
30
31static void crUnpackExtend(void);
32static void crUnpackExtendDbg(void);
33
34#if 0 //def DEBUG_misha
35//# define CR_UNPACK_DEBUG_OPCODES
36# define CR_UNPACK_DEBUG_LAST_OPCODES
37# define CR_UNPACK_DEBUG_PREV_OPCODES
38#endif
39
40#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
41static GLenum g_VBoxDbgCrPrevOpcode = 0;
42static GLenum g_VBoxDbgCrPrevExtendOpcode = 0;
43#endif
44""")
45
46nodebug_opcodes = [
47 "CR_MULTITEXCOORD2FARB_OPCODE",
48 "CR_VERTEX3F_OPCODE",
49 "CR_NORMAL3F_OPCODE",
50 "CR_COLOR4UB_OPCODE",
51 "CR_LOADIDENTITY_OPCODE",
52 "CR_MATRIXMODE_OPCODE",
53 "CR_LOADMATRIXF_OPCODE",
54 "CR_DISABLE_OPCODE",
55 "CR_COLOR4F_OPCODE",
56 "CR_ENABLE_OPCODE",
57 "CR_BEGIN_OPCODE",
58 "CR_END_OPCODE",
59 "CR_SECONDARYCOLOR3FEXT_OPCODE"
60]
61
62nodebug_extopcodes = [
63 "CR_ACTIVETEXTUREARB_EXTEND_OPCODE"
64]
65
66#
67# Useful functions
68#
69
70def ReadData( offset, arg_type ):
71 """Emit a READ_DOUBLE or READ_DATA call for pulling a GL function
72 argument out of the buffer's operand area."""
73 if arg_type == "GLdouble" or arg_type == "GLclampd":
74 retval = "READ_DOUBLE(%d)" % offset
75 else:
76 retval = "READ_DATA(%d, %s)" % (offset, arg_type)
77 return retval
78
79
80def FindReturnPointer( return_type, params ):
81 """For GL functions that return values (either as the return value or
82 through a pointer parameter) emit a SET_RETURN_PTR call."""
83 arg_len = apiutil.PacketLength( params )
84 if (return_type != 'void'):
85 print('\tSET_RETURN_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length
86 else:
87 paramList = [ ('foo', 'void *', 0) ]
88 print('\tSET_RETURN_PTR(%d);' % (arg_len + 8 - apiutil.PacketLength(paramList)))
89
90
91def FindWritebackPointer( return_type, params ):
92 """Emit a SET_WRITEBACK_PTR call."""
93 arg_len = apiutil.PacketLength( params )
94 if return_type != 'void':
95 paramList = [ ('foo', 'void *', 0) ]
96 arg_len += apiutil.PacketLength( paramList )
97
98 print('\tSET_WRITEBACK_PTR(%d);' % (arg_len + 8)) # extended opcode plus packet length
99
100
101def MakeNormalCall( return_type, func_name, params, counter_init = 0 ):
102 counter = counter_init
103 copy_of_params = params[:]
104
105 for i in range( 0, len(params) ):
106 (name, type, vecSize) = params[i]
107 if apiutil.IsPointer(copy_of_params[i][1]):
108 params[i] = ('NULL', type, vecSize)
109 copy_of_params[i] = (copy_of_params[i][0], 'void', 0)
110 if not "get" in apiutil.Properties(func_name):
111 print('\tcrError( "%s needs to be special cased!" );' % func_name)
112 else:
113 print("\t%s %s = %s;" % ( copy_of_params[i][1], name, ReadData( counter, copy_of_params[i][1] ) ))
114 counter += apiutil.sizeof(copy_of_params[i][1])
115
116 if ("get" in apiutil.Properties(func_name)):
117 FindReturnPointer( return_type, params )
118 FindWritebackPointer( return_type, params )
119
120 if return_type != "void":
121 print("\t(void)", end=" ")
122 else:
123 print("\t", end="")
124 print("cr_unpackDispatch.%s(%s);" % (func_name, apiutil.MakeCallString(params)))
125
126
127def MakeVectorCall( return_type, func_name, arg_type ):
128 """Convert a call like glVertex3f to glVertex3fv."""
129 vec_func = apiutil.VectorFunction(func_name)
130 params = apiutil.Parameters(vec_func)
131 assert len(params) == 1
132 (arg_name, vecType, vecSize) = params[0]
133
134 if arg_type == "GLdouble" or arg_type == "GLclampd":
135 print("#ifdef CR_UNALIGNED_ACCESS_OKAY")
136 print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))
137 print("#else")
138 for index in range(0, vecSize):
139 print("\tGLdouble v" + repr(index) + " = READ_DOUBLE(" + repr(index * 8) + ");")
140 if return_type != "void":
141 print("\t(void) cr_unpackDispatch.%s(" % func_name, end="")
142 else:
143 print("\tcr_unpackDispatch.%s(" % func_name, end="")
144 for index in range(0, vecSize):
145 print("v" + repr(index), end="")
146 if index != vecSize - 1:
147 print(",", end=" ")
148 print(");")
149 print("#endif")
150 else:
151 print("\tcr_unpackDispatch.%s((%s) cr_unpackData);" % (vec_func, vecType))
152
153
154
155keys = apiutil.GetDispatchedFunctions(sys.argv[1]+"/APIspec.txt")
156
157
158#
159# Generate unpack functions for all the simple functions.
160#
161for func_name in keys:
162 if (not "pack" in apiutil.ChromiumProps(func_name) or
163 apiutil.FindSpecial( "unpacker", func_name )):
164 continue
165
166 params = apiutil.Parameters(func_name)
167 return_type = apiutil.ReturnType(func_name)
168
169 print("static void crUnpack%s(void)" % func_name)
170 print("{")
171
172 # Verify that the provided buffer length is what we expect.
173 packet_length = apiutil.PacketLength( params )
174 print("\tif(!DATA_POINTER_CHECK(%d))" % packet_length);
175 print("\t{");
176 print("\t\tcrError(\"crUnpack%s: parameters out of range\");" % func_name);
177 print("\t\treturn;");
178 print("\t}");
179
180 vector_func = apiutil.VectorFunction(func_name)
181 if (vector_func and len(apiutil.Parameters(vector_func)) == 1):
182 MakeVectorCall( return_type, func_name, params[0][1] )
183 else:
184 MakeNormalCall( return_type, func_name, params )
185 if packet_length == 0:
186 print("\tINCR_DATA_PTR_NO_ARGS( );")
187 else:
188 print("\tINCR_DATA_PTR(%d);" % packet_length)
189 print("}\n")
190
191
192#
193# Emit some code
194#
195print("""
196typedef struct __dispatchNode {
197 const unsigned char *unpackData;
198 struct __dispatchNode *next;
199} DispatchNode;
200
201static DispatchNode *unpackStack = NULL;
202
203static SPUDispatchTable *cr_lastDispatch = NULL;
204
205void crUnpackPush(void)
206{
207 DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
208 node->next = unpackStack;
209 unpackStack = node;
210 node->unpackData = cr_unpackData;
211}
212
213void crUnpackPop(void)
214{
215 DispatchNode *node = unpackStack;
216
217 if (!node)
218 {
219 crError( "crUnpackPop called with an empty stack!" );
220 }
221 unpackStack = node->next;
222 cr_unpackData = node->unpackData;
223 crFree( node );
224}
225
226CR_UNPACK_BUFFER_TYPE crUnpackGetBufferType(const void *opcodes, unsigned int num_opcodes)
227{
228 const uint8_t *pu8Codes = (const uint8_t *)opcodes;
229
230 uint8_t first;
231 uint8_t last;
232
233 if (!num_opcodes)
234 return CR_UNPACK_BUFFER_TYPE_GENERIC;
235
236 first = pu8Codes[0];
237 last = pu8Codes[1-(int)num_opcodes];
238
239 switch (last)
240 {
241 case CR_CMDBLOCKFLUSH_OPCODE:
242 return CR_UNPACK_BUFFER_TYPE_CMDBLOCK_FLUSH;
243 case CR_CMDBLOCKEND_OPCODE:
244 return (first == CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_END;
245 default:
246 return (first != CR_CMDBLOCKBEGIN_OPCODE) ? CR_UNPACK_BUFFER_TYPE_GENERIC : CR_UNPACK_BUFFER_TYPE_CMDBLOCK_BEGIN;
247 }
248}
249
250void crUnpack( const void *data, const void *data_end, const void *opcodes,
251 unsigned int num_opcodes, SPUDispatchTable *table )
252{
253 unsigned int i;
254 const unsigned char *unpack_opcodes;
255 if (table != cr_lastDispatch)
256 {
257 crSPUCopyDispatchTable( &cr_unpackDispatch, table );
258 cr_lastDispatch = table;
259 }
260
261 unpack_opcodes = (const unsigned char *)opcodes;
262 cr_unpackData = (const unsigned char *)data;
263 cr_unpackDataEnd = (const unsigned char *)data_end;
264
265#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
266 crDebug("crUnpack: %d opcodes", num_opcodes);
267#endif
268
269 for (i = 0; i < num_opcodes; i++)
270 {
271
272 CRDBGPTR_CHECKZ(writeback_ptr);
273 CRDBGPTR_CHECKZ(return_ptr);
274
275 /*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
276#ifdef CR_UNPACK_DEBUG_PREV_OPCODES
277 g_VBoxDbgCrPrevOpcode = *unpack_opcodes;
278#endif
279 switch( *unpack_opcodes )
280 {""")
281
282#
283# Emit switch cases for all unextended opcodes
284#
285for func_name in keys:
286 if "pack" in apiutil.ChromiumProps(func_name):
287 print('\t\t\tcase %s:' % apiutil.OpcodeName( func_name ))
288 if not apiutil.OpcodeName(func_name) in nodebug_opcodes:
289 print("""
290#ifdef CR_UNPACK_DEBUG_LAST_OPCODES
291 if (i==(num_opcodes-1))
292#endif
293#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
294 crDebug("Unpack: %s");
295#endif """ % apiutil.OpcodeName(func_name))
296 print('\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name)
297
298print("""
299 case CR_EXTEND_OPCODE:
300 #ifdef CR_UNPACK_DEBUG_OPCODES
301 crUnpackExtendDbg();
302 #else
303 # ifdef CR_UNPACK_DEBUG_LAST_OPCODES
304 if (i==(num_opcodes-1)) crUnpackExtendDbg();
305 else
306 # endif
307 crUnpackExtend();
308 #endif
309 break;
310 case CR_CMDBLOCKBEGIN_OPCODE:
311 case CR_CMDBLOCKEND_OPCODE:
312 case CR_CMDBLOCKFLUSH_OPCODE:
313 case CR_NOP_OPCODE:
314 INCR_DATA_PTR_NO_ARGS( );
315 break;
316 default:
317 crError( "Unknown opcode: %d", *unpack_opcodes );
318 break;
319 }
320
321 CRDBGPTR_CHECKZ(writeback_ptr);
322 CRDBGPTR_CHECKZ(return_ptr);
323
324 unpack_opcodes--;
325 }
326}""")
327
328
329#
330# Emit unpack functions for extended opcodes, non-special functions only.
331#
332for func_name in keys:
333 if ("extpack" in apiutil.ChromiumProps(func_name)
334 and not apiutil.FindSpecial("unpacker", func_name)):
335 return_type = apiutil.ReturnType(func_name)
336 params = apiutil.Parameters(func_name)
337 print('static void crUnpackExtend%s(void)' % func_name)
338 print('{')
339
340 # Verify that the provided buffer length is what we expect.
341 packet_length = apiutil.PacketLength( params )
342 print("\tif(!DATA_POINTER_CHECK(%d))" % packet_length);
343 print("\t{");
344 print("\t\tcrError(\"crUnpack%s: parameters out of range\");" % func_name);
345 print("\t\treturn;");
346 print("\t}");
347
348 MakeNormalCall( return_type, func_name, params, 8 )
349 print('}\n')
350
351print('static void crUnpackExtend(void)')
352print('{')
353print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
354print('')
355print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
356print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
357print('#endif')
358print('')
359print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
360print('\tswitch( extend_opcode )')
361print('\t{')
362
363
364#
365# Emit switch statement for extended opcodes
366#
367for func_name in keys:
368 if "extpack" in apiutil.ChromiumProps(func_name):
369 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
370# print('\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )))
371 print('\t\t\tcrUnpackExtend%s( );' % func_name)
372 print('\t\t\tbreak;')
373
374print(""" default:
375 crError( "Unknown extended opcode: %d", (int) extend_opcode );
376 break;
377 }
378 INCR_VAR_PTR();
379}""")
380
381print('static void crUnpackExtendDbg(void)')
382print('{')
383print('\tGLenum extend_opcode = %s;' % ReadData( 4, 'GLenum' ))
384print('')
385print('#ifdef CR_UNPACK_DEBUG_PREV_OPCODES')
386print('\tg_VBoxDbgCrPrevExtendOpcode = extend_opcode;')
387print('#endif')
388print('')
389print('\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/')
390print('\tswitch( extend_opcode )')
391print('\t{')
392
393
394#
395# Emit switch statement for extended opcodes
396#
397for func_name in keys:
398 if "extpack" in apiutil.ChromiumProps(func_name):
399 print('\t\tcase %s:' % apiutil.ExtendedOpcodeName( func_name ))
400 if not apiutil.ExtendedOpcodeName(func_name) in nodebug_extopcodes:
401 print('\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name ))
402 print('\t\t\tcrUnpackExtend%s( );' % func_name)
403 print('\t\t\tbreak;')
404
405print(""" default:
406 crError( "Unknown extended opcode: %d", (int) extend_opcode );
407 break;
408 }
409 INCR_VAR_PTR();
410}""")
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette