winapi_local.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #
  2. # Copyright 1999, 2000, 2001 Patrik Stridvall
  3. #
  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.
  8. #
  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.
  13. #
  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
  17. #
  18. package winapi_local;
  19. use strict;
  20. use warnings 'all';
  21. use nativeapi qw($nativeapi);
  22. use options qw($options);
  23. use output qw($output);
  24. use winapi qw($win16api $win32api @winapis);
  25. sub _check_function($$$$$$) {
  26. my $return_type = shift;
  27. my $calling_convention = shift;
  28. my $external_name = shift;
  29. my $internal_name = shift;
  30. my $refargument_types = shift;
  31. my @argument_types = @$refargument_types;
  32. my $winapi = shift;
  33. my $module = $winapi->function_internal_module($internal_name);
  34. if($winapi->name eq "win16") {
  35. if($winapi->is_function_stub_in_module($module, $internal_name)) {
  36. if($options->implemented) {
  37. $output->write("function implemented but declared as stub in .spec file\n");
  38. }
  39. return;
  40. } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
  41. if($options->implemented_win32) {
  42. $output->write("32-bit variant of function implemented but declared as stub in .spec file\n");
  43. }
  44. return;
  45. }
  46. } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
  47. if($options->implemented) {
  48. $output->write("function implemented but declared as stub in .spec file\n");
  49. }
  50. return;
  51. }
  52. my $forbidden_return_type = 0;
  53. my $implemented_return_kind;
  54. $winapi->type_used_in_module($return_type,$module);
  55. if(!defined($implemented_return_kind = $winapi->translate_argument($return_type))) {
  56. $winapi->declare_argument($return_type, "unknown");
  57. if($return_type ne "") {
  58. $output->write("no win*.api translation defined: " . $return_type . "\n");
  59. }
  60. } elsif(!$winapi->is_allowed_kind($implemented_return_kind) ||
  61. !$winapi->is_allowed_type_in_module($return_type, $module))
  62. {
  63. $forbidden_return_type = 1;
  64. $winapi->allow_kind($implemented_return_kind);
  65. $winapi->allow_type_in_module($return_type, $module);
  66. if($options->report_argument_forbidden($return_type)) {
  67. $output->write("return type is forbidden: $return_type ($implemented_return_kind)\n");
  68. }
  69. }
  70. my $segmented = 0;
  71. if(defined($implemented_return_kind) && $implemented_return_kind =~ /^seg[sp]tr$/) {
  72. $segmented = 1;
  73. }
  74. my $implemented_calling_convention;
  75. if($winapi->name eq "win16") {
  76. if($calling_convention eq "__cdecl") {
  77. $implemented_calling_convention = "cdecl";
  78. } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
  79. $implemented_calling_convention = "varargs";
  80. } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|NET_API_FUNCTION|RPC_ENTRY|SEC_ENTRY|VFWAPI|WINGDIPAPI|WMIAPI|WINAPI|CALLBACK)$/) {
  81. if(defined($implemented_return_kind) && $implemented_return_kind =~ /^(?:s_word|word|void)$/) {
  82. $implemented_calling_convention = "pascal16";
  83. } else {
  84. $implemented_calling_convention = "pascal";
  85. }
  86. } elsif($calling_convention eq "__asm") {
  87. $implemented_calling_convention = "asm";
  88. } else {
  89. $implemented_calling_convention = "cdecl";
  90. }
  91. } elsif($winapi->name eq "win32") {
  92. if($calling_convention eq "__cdecl") {
  93. $implemented_calling_convention = "cdecl";
  94. } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
  95. $implemented_calling_convention = "varargs";
  96. } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|APIENTRY|NET_API_FUNCTION|RPC_ENTRY|SEC_ENTRY|VFWAPI|WINGDIPAPI|WMIAPI|WINAPI|CALLBACK)$/) {
  97. if(defined($implemented_return_kind) && $implemented_return_kind eq "longlong") {
  98. $implemented_calling_convention = "stdcall"; # FIXME: Check entry flags
  99. } else {
  100. $implemented_calling_convention = "stdcall";
  101. }
  102. } elsif($calling_convention eq "__asm") {
  103. $implemented_calling_convention = "asm";
  104. } else {
  105. $implemented_calling_convention = "cdecl";
  106. }
  107. }
  108. my $declared_calling_convention = $winapi->function_internal_calling_convention($internal_name) || "";
  109. my @declared_argument_kinds = split(/\s+/, $winapi->function_internal_arguments($internal_name));
  110. my $declared_register = ($declared_calling_convention =~ / -register\b/);
  111. my $declared_i386 = ($declared_calling_convention =~ /(?:^pascal| -i386)\b/);
  112. $declared_calling_convention =~ s/ .*$//;
  113. if(!$declared_register &&
  114. $implemented_calling_convention ne $declared_calling_convention &&
  115. $implemented_calling_convention ne "asm" &&
  116. !($declared_calling_convention =~ /^pascal/ && $forbidden_return_type) &&
  117. !($implemented_calling_convention =~ /^(?:cdecl|varargs)$/ && $declared_calling_convention =~ /^(?:cdecl|varargs)$/))
  118. {
  119. if($options->calling_convention && (
  120. ($options->calling_convention_win16 && $winapi->name eq "win16") ||
  121. ($options->calling_convention_win32 && $winapi->name eq "win32")) &&
  122. !$nativeapi->is_function($internal_name))
  123. {
  124. $output->write("calling convention mismatch: $implemented_calling_convention != $declared_calling_convention\n");
  125. }
  126. }
  127. if($declared_calling_convention eq "varargs") {
  128. if ($#argument_types != -1 &&
  129. (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
  130. ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
  131. {
  132. pop @argument_types;
  133. } else {
  134. $output->write("function not implemented as varargs\n");
  135. }
  136. } elsif ($#argument_types != -1 &&
  137. (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
  138. ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
  139. {
  140. if($#argument_types == 0) {
  141. pop @argument_types;
  142. } else {
  143. $output->write("function not declared as varargs\n");
  144. }
  145. }
  146. if($internal_name =~ /^(?:NTDLL__ftol|NTDLL__CIpow)$/) { # FIXME: Kludge
  147. # ignore
  148. } else {
  149. my $n = 0;
  150. my @argument_kinds = map {
  151. my $type = $_;
  152. my $kind = "unknown";
  153. $winapi->type_used_in_module($type,$module);
  154. if($type eq "CONTEXT *") {
  155. $kind = "context";
  156. } elsif($type eq "CONTEXT86 *") {
  157. $kind = "context86";
  158. } elsif(!defined($kind = $winapi->translate_argument($type))) {
  159. $winapi->declare_argument($type, "unknown");
  160. $output->write("no win*.api translation defined: " . $type . "\n");
  161. } elsif(!$winapi->is_allowed_kind($kind) ||
  162. !$winapi->is_allowed_type_in_module($type, $module))
  163. {
  164. $winapi->allow_kind($kind);
  165. $winapi->allow_type_in_module($type, $module);
  166. if($options->report_argument_forbidden($type)) {
  167. $output->write("argument " . ($n + 1) . " type is forbidden: " . $type . " (" . $kind . ")\n");
  168. }
  169. }
  170. # FIXME: Kludge
  171. if(defined($kind) && $kind eq "struct16") {
  172. $n+=2;
  173. ("double", "double");
  174. } elsif(defined($kind) && $kind eq "longlong") {
  175. $n+=1;
  176. "longlong";
  177. } else {
  178. $n++;
  179. $kind;
  180. }
  181. } @argument_types;
  182. if ($declared_register)
  183. {
  184. if (!$declared_i386 &&
  185. $argument_kinds[$#argument_kinds] ne "context") {
  186. $output->write("function declared as register, but CONTEXT * is not last argument\n");
  187. } elsif ($declared_i386 &&
  188. $argument_kinds[$#argument_kinds] ne "context86") {
  189. $output->write("function declared as register, but CONTEXT86 * is not last argument\n");
  190. }
  191. }
  192. for my $n (0..$#argument_kinds) {
  193. if(!defined($argument_kinds[$n]) || !defined($declared_argument_kinds[$n])) { next; }
  194. if($argument_kinds[$n] =~ /^seg[ps]tr$/ ||
  195. $declared_argument_kinds[$n] =~ /^seg[ps]tr$/)
  196. {
  197. $segmented = 1;
  198. }
  199. # FIXME: Kludge
  200. if(!defined($argument_types[$n])) {
  201. $argument_types[$n] = "";
  202. }
  203. if($argument_kinds[$n] =~ /^context(?:86)?$/) {
  204. # Nothing
  205. } elsif(!$winapi->is_allowed_kind($argument_kinds[$n]) ||
  206. !$winapi->is_allowed_type_in_module($argument_types[$n], $module))
  207. {
  208. $winapi->allow_kind($argument_kinds[$n]);
  209. $winapi->allow_type_in_module($argument_types[$n],, $module);
  210. if($options->report_argument_forbidden($argument_types[$n])) {
  211. $output->write("argument " . ($n + 1) . " type is forbidden: " .
  212. "$argument_types[$n] ($argument_kinds[$n])\n");
  213. }
  214. } elsif($argument_kinds[$n] ne $declared_argument_kinds[$n] &&
  215. !($argument_kinds[$n] eq "longlong" && $declared_argument_kinds[$n] eq "double")) {
  216. if($options->report_argument_kind($argument_kinds[$n]) ||
  217. $options->report_argument_kind($declared_argument_kinds[$n]))
  218. {
  219. $output->write("argument " . ($n + 1) . " type mismatch: " .
  220. $argument_types[$n] . " ($argument_kinds[$n]) != " .
  221. $declared_argument_kinds[$n] . "\n");
  222. }
  223. }
  224. }
  225. if ($options->argument_count &&
  226. $implemented_calling_convention ne "asm")
  227. {
  228. if ($#argument_kinds != $#declared_argument_kinds and
  229. $#argument_types != $#declared_argument_kinds) {
  230. $output->write("argument count differs: " .
  231. ($#argument_kinds + 1) . " != " .
  232. ($#declared_argument_kinds + 1) . "\n");
  233. } elsif ($#argument_kinds != $#declared_argument_kinds or
  234. $#argument_types != $#declared_argument_kinds) {
  235. $output->write("argument count differs: " .
  236. ($#argument_kinds + 1) . "/" . ($#argument_types + 1) .
  237. " != " . ($#declared_argument_kinds + 1) .
  238. " (long vs. long long problem?)\n");
  239. }
  240. }
  241. }
  242. if($segmented && $options->shared_segmented && $winapi->is_shared_internal_function($internal_name)) {
  243. $output->write("function using segmented pointers shared between Win16 and Win32\n");
  244. }
  245. }
  246. sub check_function($) {
  247. my $function = shift;
  248. my $return_type = $function->return_type;
  249. my $calling_convention = $function->calling_convention;
  250. my $calling_convention16 = $function->calling_convention16;
  251. my $calling_convention32 = $function->calling_convention32;
  252. my $internal_name = $function->internal_name;
  253. my $external_name16 = $function->external_name16;
  254. my $external_name32 = $function->external_name32;
  255. my $module16 = $function->module16;
  256. my $module32 = $function->module32;
  257. my $refargument_types = $function->argument_types;
  258. if(!defined($refargument_types)) {
  259. return;
  260. }
  261. if($options->win16 && $options->report_module($module16)) {
  262. _check_function($return_type,
  263. $calling_convention, $external_name16,
  264. $internal_name, $refargument_types,
  265. $win16api);
  266. }
  267. if($options->win32 && $options->report_module($module32)) {
  268. _check_function($return_type,
  269. $calling_convention, $external_name32,
  270. $internal_name, $refargument_types,
  271. $win32api);
  272. }
  273. }
  274. sub _check_statements($$$) {
  275. my $winapi = shift;
  276. my $functions = shift;
  277. my $function = shift;
  278. my $module = $function->module;
  279. my $internal_name = $function->internal_name;
  280. my $first_debug_message = 1;
  281. local $_ = $function->statements;
  282. while(defined($_)) {
  283. if(s/(\w+)\s*(?:\(\s*(\w+)\s*\))?\s*\(\s*((?:\"[^\"]*\"|\([^\)]*\)|[^\)])*?)\s*\)//) {
  284. my $called_name = $1;
  285. my $channel = $2;
  286. my $called_arguments = $3;
  287. if($called_name =~ /^(?:if|for|while|switch|sizeof)$/) {
  288. # Nothing
  289. } elsif($called_name =~ /^(?:ERR|FIXME|MSG|TRACE|WARN)$/) {
  290. if($first_debug_message && $called_name =~ /^(?:FIXME|TRACE)$/) {
  291. $first_debug_message = 0;
  292. if($called_arguments =~ /^\"\((.*?)\)(.*?)\"\s*,\s*(.*?)$/) {
  293. my $formatting = $1;
  294. my $extra = $2;
  295. my $arguments = $3;
  296. my $format;
  297. my $argument;
  298. my $n = 0;
  299. while($formatting && ($formatting =~ s/^([^,]*),?//, $format = $1, $format =~ s/^\s*(.*?)\s*$/$1/) &&
  300. $arguments && ($arguments =~ s/^([^,]*),?//, $argument = $1, $argument =~ s/^\s*(.*?)\s*$/$1/))
  301. {
  302. my $type = @{$function->argument_types}[$n];
  303. my $name = @{$function->argument_names}[$n];
  304. $n++;
  305. if(!defined($type)) { last; }
  306. $format =~ s/^\w+\s*[:=]?\s*//;
  307. $format =~ s/\s*\{[^\{\}]*\}$//;
  308. $format =~ s/\s*\[[^\[\]]*\]$//;
  309. $format =~ s/^\'(.*?)\'$/$1/;
  310. $format =~ s/^\\\"(.*?)\\\"$/$1/;
  311. if($options->debug_messages) {
  312. if($argument !~ /$name/) {
  313. $output->write("$called_name: argument $n is wrong ($name != '$argument')\n");
  314. } elsif(!$winapi->is_allowed_type_format($module, $type, $format)) {
  315. $output->write("$called_name: argument $n ($type $name) has illegal format ($format)\n");
  316. }
  317. }
  318. }
  319. if($options->debug_messages) {
  320. my $count = $#{$function->argument_types} + 1;
  321. if($n != $count) {
  322. $output->write("$called_name: argument count mismatch ($n != $count)\n");
  323. }
  324. }
  325. }
  326. }
  327. } elsif($options->cross_call) {
  328. # $output->write("$internal_name: called $called_name\n");
  329. $$functions{$internal_name}->function_called($called_name);
  330. if(!defined($$functions{$called_name})) {
  331. my $called_function = 'winapi_function'->new;
  332. $called_function->internal_name($called_name);
  333. $$functions{$called_name} = $called_function;
  334. }
  335. $$functions{$called_name}->function_called_by($internal_name);
  336. }
  337. } else {
  338. undef $_;
  339. }
  340. }
  341. }
  342. sub check_statements($$) {
  343. my $functions = shift;
  344. my $function = shift;
  345. my $module16 = $function->module16;
  346. my $module32 = $function->module32;
  347. if($options->win16 && $options->report_module($module16)) {
  348. _check_statements($win16api, $functions, $function);
  349. }
  350. if($options->win32 && $options->report_module($module32)) {
  351. _check_statements($win32api, $functions, $function);
  352. }
  353. }
  354. sub check_file($$) {
  355. my $file = shift;
  356. my $functions = shift;
  357. if($options->cross_call) {
  358. my @names = sort(keys(%$functions));
  359. for my $name (@names) {
  360. my $function = $$functions{$name};
  361. my @called_names = $function->called_function_names;
  362. my @called_by_names = $function->called_by_function_names;
  363. my $module = $function->module;
  364. if($options->cross_call_win32_win16) {
  365. my $module16 = $function->module16;
  366. my $module32 = $function->module32;
  367. if($#called_names >= 0 && (defined($module16) || defined($module32)) ) {
  368. for my $called_name (@called_names) {
  369. my $called_function = $$functions{$called_name};
  370. my $called_module16 = $called_function->module16;
  371. my $called_module32 = $called_function->module32;
  372. if(defined($module32) &&
  373. defined($called_module16) && !defined($called_module32) &&
  374. $name ne $called_name)
  375. {
  376. $output->write("$file: $module: $name: illegal call to $called_name (Win32 -> Win16)\n");
  377. }
  378. }
  379. }
  380. }
  381. if($options->cross_call_unicode_ascii) {
  382. if($name =~ /(?<!A)W$/) {
  383. for my $called_name (@called_names) {
  384. if($called_name =~ /A$/) {
  385. $output->write("$file: $module: $name: illegal call to $called_name (Unicode -> ASCII)\n");
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. 1;