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