ddraw: Validate structure pointers and sizes in IDirect3D3::FindDevice.
[wine/testsucceed.git] / tools / winapi / winapi_local.pm
blob9cea5873a56ab57ab7b7dda3f782a8d528759ba5
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 package winapi_local;
21 use strict;
23 use nativeapi qw($nativeapi);
24 use options qw($options);
25 use output qw($output);
26 use winapi qw($win16api $win32api @winapis);
28 sub _check_function($$$$$$) {
29 my $return_type = shift;
30 my $calling_convention = shift;
31 my $external_name = shift;
32 my $internal_name = shift;
33 my $refargument_types = shift;
34 my @argument_types = @$refargument_types;
35 my $winapi = shift;
37 my $module = $winapi->function_internal_module($internal_name);
39 if($winapi->name eq "win16") {
40 if($winapi->is_function_stub_in_module($module, $internal_name)) {
41 if($options->implemented) {
42 $output->write("function implemented but declared as stub in .spec file\n");
44 return;
45 } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
46 if($options->implemented_win32) {
47 $output->write("32-bit variant of function implemented but declared as stub in .spec file\n");
49 return;
51 } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
52 if($options->implemented) {
53 $output->write("function implemented but declared as stub in .spec file\n");
55 return;
58 my $forbidden_return_type = 0;
59 my $implemented_return_kind;
60 $winapi->type_used_in_module($return_type,$module);
61 if(!defined($implemented_return_kind = $winapi->translate_argument($return_type))) {
62 $winapi->declare_argument($return_type, "unknown");
63 if($return_type ne "") {
64 $output->write("no win*.api translation defined: " . $return_type . "\n");
66 } elsif(!$winapi->is_allowed_kind($implemented_return_kind) ||
67 !$winapi->is_allowed_type_in_module($return_type, $module))
69 $forbidden_return_type = 1;
70 $winapi->allow_kind($implemented_return_kind);
71 $winapi->allow_type_in_module($return_type, $module);
72 if($options->report_argument_forbidden($return_type)) {
73 $output->write("return type is forbidden: $return_type ($implemented_return_kind)\n");
77 my $segmented = 0;
78 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^seg[sp]tr$/) {
79 $segmented = 1;
82 my $implemented_calling_convention;
83 if($winapi->name eq "win16") {
84 if($calling_convention eq "__cdecl") {
85 $implemented_calling_convention = "cdecl";
86 } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
87 $implemented_calling_convention = "varargs";
88 } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|NET_API_FUNCTION|RPC_ENTRY|VFWAPI|WINAPI|CALLBACK)$/) {
89 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^(?:s_word|word|void)$/) {
90 $implemented_calling_convention = "pascal16";
91 } else {
92 $implemented_calling_convention = "pascal";
94 } elsif($calling_convention eq "__asm") {
95 $implemented_calling_convention = "asm";
96 } else {
97 $implemented_calling_convention = "cdecl";
99 } elsif($winapi->name eq "win32") {
100 if($calling_convention eq "__cdecl") {
101 $implemented_calling_convention = "cdecl";
102 } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
103 $implemented_calling_convention = "varargs";
104 } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|APIENTRY|NET_API_FUNCTION|RPC_ENTRY|VFWAPI|WINAPI|CALLBACK)$/) {
105 if(defined($implemented_return_kind) && $implemented_return_kind eq "longlong") {
106 $implemented_calling_convention = "stdcall"; # FIXME: Check entry flags
107 } else {
108 $implemented_calling_convention = "stdcall";
110 } elsif($calling_convention eq "__asm") {
111 $implemented_calling_convention = "asm";
112 } else {
113 $implemented_calling_convention = "cdecl";
117 my $declared_calling_convention = $winapi->function_internal_calling_convention($internal_name) || "";
118 my @declared_argument_kinds = split(/\s+/, $winapi->function_internal_arguments($internal_name));
120 my $declared_register = ($declared_calling_convention =~ / -register\b/);
121 my $declared_i386 = ($declared_calling_convention =~ /(?:^pascal| -i386)\b/);
122 $declared_calling_convention =~ s/ .*$//;
124 if(!$declared_register &&
125 $implemented_calling_convention ne $declared_calling_convention &&
126 $implemented_calling_convention ne "asm" &&
127 !($declared_calling_convention =~ /^pascal/ && $forbidden_return_type) &&
128 !($implemented_calling_convention =~ /^(?:cdecl|varargs)$/ && $declared_calling_convention =~ /^(?:cdecl|varargs)$/))
130 if($options->calling_convention && (
131 ($options->calling_convention_win16 && $winapi->name eq "win16") ||
132 ($options->calling_convention_win32 && $winapi->name eq "win32")) &&
133 !$nativeapi->is_function($internal_name))
135 $output->write("calling convention mismatch: $implemented_calling_convention != $declared_calling_convention\n");
139 if($declared_calling_convention eq "varargs") {
140 if ($#argument_types != -1 &&
141 (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
142 ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
144 pop @argument_types;
145 } else {
146 $output->write("function not implemented as varargs\n");
148 } elsif ($#argument_types != -1 &&
149 (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
150 ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
152 if($#argument_types == 0) {
153 pop @argument_types;
154 } else {
155 $output->write("function not declared as varargs\n");
159 if($internal_name =~ /^(?:NTDLL__ftol|NTDLL__CIpow)$/) { # FIXME: Kludge
160 # ignore
161 } else {
162 my $n = 0;
163 my @argument_kinds = map {
164 my $type = $_;
165 my $kind = "unknown";
166 $winapi->type_used_in_module($type,$module);
167 if($type eq "CONTEXT *") {
168 $kind = "context";
169 } elsif($type eq "CONTEXT86 *") {
170 $kind = "context86";
171 } elsif(!defined($kind = $winapi->translate_argument($type))) {
172 $winapi->declare_argument($type, "unknown");
173 $output->write("no win*.api translation defined: " . $type . "\n");
174 } elsif(!$winapi->is_allowed_kind($kind) ||
175 !$winapi->is_allowed_type_in_module($type, $module))
177 $winapi->allow_kind($kind);
178 $winapi->allow_type_in_module($type, $module);
179 if($options->report_argument_forbidden($type)) {
180 $output->write("argument " . ($n + 1) . " type is forbidden: " . $type . " (" . $kind . ")\n");
184 # FIXME: Kludge
185 if(defined($kind) && $kind eq "struct16") {
186 $n+=2;
187 ("double", "double");
188 } elsif(defined($kind) && $kind eq "longlong") {
189 $n+=1;
190 "longlong";
191 } else {
192 $n++;
193 $kind;
195 } @argument_types;
197 if ($declared_register)
199 if (!$declared_i386 &&
200 $argument_kinds[$#argument_kinds] ne "context") {
201 $output->write("function declared as register, but CONTEXT * is not last argument\n");
202 } elsif ($declared_i386 &&
203 $argument_kinds[$#argument_kinds] ne "context86") {
204 $output->write("function declared as register, but CONTEXT86 * is not last argument\n");
208 for my $n (0..$#argument_kinds) {
209 if(!defined($argument_kinds[$n]) || !defined($declared_argument_kinds[$n])) { next; }
211 if($argument_kinds[$n] =~ /^seg[ps]tr$/ ||
212 $declared_argument_kinds[$n] =~ /^seg[ps]tr$/)
214 $segmented = 1;
217 # FIXME: Kludge
218 if(!defined($argument_types[$n])) {
219 $argument_types[$n] = "";
222 if($argument_kinds[$n] =~ /^context(?:86)?$/) {
223 # Nothing
224 } elsif(!$winapi->is_allowed_kind($argument_kinds[$n]) ||
225 !$winapi->is_allowed_type_in_module($argument_types[$n], $module))
227 $winapi->allow_kind($argument_kinds[$n]);
228 $winapi->allow_type_in_module($argument_types[$n],, $module);
229 if($options->report_argument_forbidden($argument_types[$n])) {
230 $output->write("argument " . ($n + 1) . " type is forbidden: " .
231 "$argument_types[$n] ($argument_kinds[$n])\n");
233 } elsif($argument_kinds[$n] ne $declared_argument_kinds[$n] &&
234 !($argument_kinds[$n] eq "longlong" && $declared_argument_kinds[$n] eq "double")) {
235 if($options->report_argument_kind($argument_kinds[$n]) ||
236 $options->report_argument_kind($declared_argument_kinds[$n]))
238 $output->write("argument " . ($n + 1) . " type mismatch: " .
239 $argument_types[$n] . " ($argument_kinds[$n]) != " .
240 $declared_argument_kinds[$n] . "\n");
245 if ($options->argument_count &&
246 $implemented_calling_convention ne "asm")
248 if ($#argument_kinds != $#declared_argument_kinds and
249 $#argument_types != $#declared_argument_kinds) {
250 $output->write("argument count differs: " .
251 ($#argument_kinds + 1) . " != " .
252 ($#declared_argument_kinds + 1) . "\n");
253 } elsif ($#argument_kinds != $#declared_argument_kinds or
254 $#argument_types != $#declared_argument_kinds) {
255 $output->write("argument count differs: " .
256 ($#argument_kinds + 1) . "/" . ($#argument_types + 1) .
257 " != " . ($#declared_argument_kinds + 1) .
258 " (long vs. long long problem?)\n");
264 if($segmented && $options->shared_segmented && $winapi->is_shared_internal_function($internal_name)) {
265 $output->write("function using segmented pointers shared between Win16 and Win32\n");
269 sub check_function($) {
270 my $function = shift;
272 my $return_type = $function->return_type;
273 my $calling_convention = $function->calling_convention;
274 my $calling_convention16 = $function->calling_convention16;
275 my $calling_convention32 = $function->calling_convention32;
276 my $internal_name = $function->internal_name;
277 my $external_name16 = $function->external_name16;
278 my $external_name32 = $function->external_name32;
279 my $module16 = $function->module16;
280 my $module32 = $function->module32;
281 my $refargument_types = $function->argument_types;
283 if(!defined($refargument_types)) {
284 return;
287 if($options->win16 && $options->report_module($module16)) {
288 _check_function($return_type,
289 $calling_convention, $external_name16,
290 $internal_name, $refargument_types,
291 $win16api);
294 if($options->win32 && $options->report_module($module32)) {
295 _check_function($return_type,
296 $calling_convention, $external_name32,
297 $internal_name, $refargument_types,
298 $win32api);
302 sub _check_statements($$$) {
303 my $winapi = shift;
304 my $functions = shift;
305 my $function = shift;
307 my $module = $function->module;
308 my $internal_name = $function->internal_name;
310 my $first_debug_message = 1;
311 local $_ = $function->statements;
312 while(defined($_)) {
313 if(s/(\w+)\s*(?:\(\s*(\w+)\s*\))?\s*\(\s*((?:\"[^\"]*\"|\([^\)]*\)|[^\)])*?)\s*\)//) {
314 my $called_name = $1;
315 my $channel = $2;
316 my $called_arguments = $3;
317 if($called_name =~ /^(?:if|for|while|switch|sizeof)$/) {
318 # Nothing
319 } elsif($called_name =~ /^(?:ERR|FIXME|MSG|TRACE|WARN)$/) {
320 if($first_debug_message && $called_name =~ /^(?:FIXME|TRACE)$/) {
321 $first_debug_message = 0;
322 if($called_arguments =~ /^\"\((.*?)\)(.*?)\"\s*,\s*(.*?)$/) {
323 my $formating = $1;
324 my $extra = $2;
325 my $arguments = $3;
327 my $format;
328 my $argument;
329 my $n = 0;
330 while($formating && ($formating =~ s/^([^,]*),?//, $format = $1, $format =~ s/^\s*(.*?)\s*$/$1/) &&
331 $arguments && ($arguments =~ s/^([^,]*),?//, $argument = $1, $argument =~ s/^\s*(.*?)\s*$/$1/))
333 my $type = @{$function->argument_types}[$n];
334 my $name = @{$function->argument_names}[$n];
336 $n++;
338 if(!defined($type)) { last; }
340 $format =~ s/^\w+\s*[:=]?\s*//;
341 $format =~ s/\s*\{[^\{\}]*\}$//;
342 $format =~ s/\s*\[[^\[\]]*\]$//;
343 $format =~ s/^\'(.*?)\'$/$1/;
344 $format =~ s/^\\\"(.*?)\\\"$/$1/;
346 if($options->debug_messages) {
347 if($argument !~ /$name/) {
348 $output->write("$called_name: argument $n is wrong ($name != '$argument')\n");
349 } elsif(!$winapi->is_allowed_type_format($module, $type, $format)) {
350 $output->write("$called_name: argument $n ($type $name) has illegal format ($format)\n");
355 if($options->debug_messages) {
356 my $count = $#{$function->argument_types} + 1;
357 if($n != $count) {
358 $output->write("$called_name: argument count mismatch ($n != $count)\n");
363 } elsif($options->cross_call) {
364 # $output->write("$internal_name: called $called_name\n");
365 $$functions{$internal_name}->function_called($called_name);
366 if(!defined($$functions{$called_name})) {
367 my $called_function = 'winapi_function'->new;
369 $called_function->internal_name($called_name);
371 $$functions{$called_name} = $called_function;
373 $$functions{$called_name}->function_called_by($internal_name);
375 } else {
376 undef $_;
381 sub check_statements($$) {
382 my $functions = shift;
383 my $function = shift;
385 my $module16 = $function->module16;
386 my $module32 = $function->module32;
388 if($options->win16 && $options->report_module($module16)) {
389 _check_statements($win16api, $functions, $function);
392 if($options->win32 && $options->report_module($module32)) {
393 _check_statements($win32api, $functions, $function);
397 sub check_file($$) {
398 my $file = shift;
399 my $functions = shift;
401 if($options->cross_call) {
402 my @names = sort(keys(%$functions));
403 for my $name (@names) {
404 my $function = $$functions{$name};
406 my @called_names = $function->called_function_names;
407 my @called_by_names = $function->called_by_function_names;
408 my $module = $function->module;
410 if($options->cross_call_win32_win16) {
411 my $module16 = $function->module16;
412 my $module32 = $function->module32;
414 if($#called_names >= 0 && (defined($module16) || defined($module32)) ) {
415 for my $called_name (@called_names) {
416 my $called_function = $$functions{$called_name};
418 my $called_module16 = $called_function->module16;
419 my $called_module32 = $called_function->module32;
420 if(defined($module32) &&
421 defined($called_module16) && !defined($called_module32) &&
422 $name ne $called_name)
424 $output->write("$file: $module: $name: illegal call to $called_name (Win32 -> Win16)\n");
430 if($options->cross_call_unicode_ascii) {
431 if($name =~ /(?<!A)W$/) {
432 for my $called_name (@called_names) {
433 if($called_name =~ /A$/) {
434 $output->write("$file: $module: $name: illegal call to $called_name (Unicode -> ASCII)\n");