+issues about caller/calle incompatible function calltypes
[captive.git] / doc / FAQ
1 Q: Latest CVS?
2 A: Access to the latest CVS development tree:
3         cvs -d :pserver:pserver@cvs.jankratochvil.net:/cvs login
4                         Just hit ENTER (empty password)
5         cvs -d :pserver:pserver@cvs.jankratochvil.net:/cvs -z3 checkout -d captive priv/captive
6         cd captive
7         cvs -d :pserver:pserver@cvs.jankratochvil.net:/cvs -z3 checkout -d reactos -r captive -kk priv/reactos
8
9    The final mapping of directories to CVS repository after the step above:
10         captive         -> priv/captive
11         captive/macros  -> macros
12         captive/reactos -> priv/reactos
13
14    Current diff of captive changes against reactos (it may have backlog):
15         cvs -d :pserver:pserver@cvs.jankratochvil.net:/cvs -z3 rdiff -u -R -r bp_captive -r captive priv/reactos
16
17    Official reactos website:
18         http://www.reactos.com/
19
20    Access to the original reactos source tree (mirrored to my "priv/reactos"):
21         cvs -d :pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS login
22                         Type the password "cvsanon" (without quotes)
23         cvs -d :pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS -z3 checkout reactos
24
25    You may also like ViewCVS web access to the repository:
26         http://cvs.jankratochvil.net/viewcvs/priv/captive/
27         http://cvs.jankratochvil.net/viewcvs/priv/reactos/?only_with_tag=captive
28         http://cvs.jankratochvil.net/viewcvs/macros/
29
30    Finally you can compile the package from the "captive" directory:
31         ./autogen.pl
32         make
33
34    The package gets preconfigured with --enable-maintainer-mode option
35    automatically by './autogen.pl'. You can later just update the "captive"
36    subdirectory if it is your currentdir:
37         cvs update
38         ./autogen.pl
39         make
40
41    Although './autogen.pl' should not be needed do not bugreport if it wan't
42    rerun as some dependencies may not be respected by the package.
43
44 Q: Why it is based on static structures, no multithreading possibility?
45 A: reactos has also static design therefore there would be no possibility
46    to use the whole libcaptive in multithreaded way.
47    Another question is whether to support multithreading in the scope of one
48    reactos machine - this would be possible but it is not yet implemented
49    in libcaptive. All such points should be marked by "TODO:thread"
50
51 Q: Prefer during libcaptive coding the GNU stylee or reactos (=W32) coding style?
52 A: Hard decision as captive connects these two worls together.
53    It was decided to prefer coding in GNU style in the Gnome specific way.
54
55    Full function replaces apparently must use the same calling conventions.
56    Coding of the full-replace should be done in GNU style if coded from scratch
57    although it is permitted to leave the original function code if only minor
58    modifications were performed (decide whether it is not better to patch
59    reactos source files instead to keep the code unified).
60    Any other code besides function replace should use GNU style calling conventions.
61
62    All the normal (GNU) world uses 32-bit wchar_t but reactos (=W32) uses
63    16-bit (unsigned short) wchar_t. This would need GCC -fshort-wchar option.
64    It has effect on L"literal-string" or L'c' character-constants.
65    Unfortunately they expect libc-named functions such as wcsrchr(3) to also
66    follow this broken 16-bit way. FIXME: reactos-compat-includes
67
68 Q: How is the libcaptive project trustworthy?
69 A: We have to run foreign native closed-source W32 code which is certainly NOT
70    trust trustworthy in any way. We could close it to some separate address
71    space and give it some safe gateways to invoce our code for the public world
72    (read "UNIX processes") interaction. It would be too much complicated
73    without any gain thus we choose to open our libcaptive-using process to any
74    attacks by W32 but the whole libcaptive process MUST be closed by a sandbox.
75    Of course all the communication with libcaptive must be taken with care by
76    taint-aware code.
77
78 Q: How to check valid input arguments? Invalid processing state assumptions?
79 A: libcaptive uses uses various Gnome macros out of <glib/gmessages.h>.
80    The common final still should run safely when all theses checks are disabled
81    as the bug-free code should never invoce them. This is the same for
82    libcaptive with a little excuse for foreign native closed-source W32 code
83    beign run which can <a href="FIXME:How is the libcaptive project trustworthy?"
84    >never be trusted</a>. As we glued all the libcaptive code
85    together with the W32 code we no longer can protect all the borders to the
86    enemy land inside the one tainted process. Therefore even fully trusted
87    libcaptive code can never safely run with <glib/gmessages.h> checks
88    disabled. YMMV if your trust W32 code. Be aware that W32 code has to also
89    handle correctly all the corrupted disk data structure to comply with such
90    trusted relationship. I really dunno if it complies but I don't care as
91    I don't trust anyway.
92
93 Q: Is it safe to captive_cleanup() the process and captive_init() it again?
94 A: It should be possible to fully cleanup the state of libcaptive even after
95    <a href="FIXME:How to check valid input arguments? Invalid processing state assumptions?"
96    ><glib/gmessages.h> failures</a>. It is currently not implemented and it
97    may get dropped at all in the future as libcaptive process MUST be sandboxed
98    anyway. You are recommended (currently required) to terminate the sandboxed
99    process and initialize the new one instead of captive_cleanup().
100
101 Q: I am missing symbol although it is already implemented by reactos.
102 A: First look at $(top_srcdir)/src/libcaptive/ke/exports.captivesym
103    where it may be enough to add such symbol to the list of exported symbols.
104    If it is not compiled at all find such symbol definition in reactos.
105    If such file or directory isn't listed in captive reactos compilation part
106    you may need to add it to $(top_srcdir)/src/libcaptive/reactos/ according
107    to the template files around - you will really only add the file to
108    Makefile.am, no real file gets copied there (just symlinked automatically).
109
110    Usuaully you will also like to edit the appended file and simplify it by lines
111         #ifndef LIBCAPTIVE
112    and
113         #endif /* LIBCAPTIVE */
114    as too much (useless for you now) code would have dependencies on other
115    missing code and you would get into never ending race of solving missing ones.
116    If the feature being implemented is some feature not needed or not suitable
117    for the emulation by libcaptive you should implement the emulation
118    below $(top_srcdir)/src/libcaptive/ directly. The filename should conform
119    to the reactos directory tree standard if applicable.
120
121    Always rather implement emply emulation function than patching the referring
122    code to prevent code development conflicts with the mainstream reactos.
123
124 Q: Why not Wine instead of ReactOS?
125 A: Although <a href="http://www.winehq.com/">Wine</a> has better development
126    activity than <a href="http://www.reactos.com/">ReactOS</a> the decision
127    is strictly technical. We need to run W32 file system driver which always
128    lives in kernel-land in W32 system design.
129         guest-OS host-OS       implements  core library correspondence
130         Wine     GNU/Linux     user        ntdll
131         ReactOS  i386 hardware kernel+user ntoskrnl
132    (ReactOS is going to reuse Wine for its 'user' part.)
133    FIXME: href in the table title:
134         host-OS: http://www.vmware.com/support/reference/common/glossary/#hostos
135         guest-OS: http://www.vmware.com/support/reference/common/glossary/#guestos
136    While ReactOS is nice to provide us the wanted kernel emulation we also
137    need to run the guest-OS in GNU/Linux. Initially I wanted to extend Wine
138    to the kernel emulation effort but fortunately
139    <a href="mailto:Steven_Ed4153@yahoo.com">Steven Edwards<a> pointed me to
140    the ReactOS project which better suits the needs of captive.
141
142    The host-OS problem unfortunately hits us on ReactOS but this isn't
143    much hard task. W32 is designed to be hardware-independent using its <code>hal.dll</code>.
144    Unfortunately ReactOS doesn't follow this design and thus we have to patch
145    and replace various parts of hardware-dependent code even inside <code>ntoskrnl.exe</code>.
146
147    Some functions are provided both by <code>ntdll.dll</code> and
148    <code>ntoskrnl.exe</code> in W32. Suggested by
149    <a href="mailto:chorns@users.sourceforge.net">Casper Hornstrup</a> this has
150    to be differentiated as <code>ntdll.dll</code> lives in the userland
151    (low addressspace) and <code>ntoskrnl.exe</code> in the kernelland
152    (high addressspace). Unfortunately these two guys are very far each other
153    and they would have serious problems to correctly hear themselves as the
154    addresspace differentiates at 0x80000000. For completeness it may also
155    differentiate at 0xC0000000 address instead depending on whether /3GB was
156    specified during startup. BTW reactos always uses 0xC0000000 point (macro
157    <code>KERNEL_BASE</code>) although this address doesn't apply in any way on
158    reactos running in host-OS GNU/Linux when compiled for libcaptive.
159
160 Q: What is Ordinal in the W32 library symbols import/export scope?
161 A: Each exported symbol has its Ordinal specified - this number specifies the offset
162    in the library symbol table. During the symbol import from a different module
163    it can specify either the full symbol name or just this number - Ordinal.
164    I dunno why it was ever created as the symbol name string size is very tiny.
165    Fortunately the current versions of W32 binaries no longer utilize this Ordinal
166    feature thus captive doesn't provide them in its export.
167
168 Q: Why I don't see debug info for imported binary W32 modules? Do you use W32 symbol files?
169 Q: W32 symbol files have different debugging info structure. We would have to
170    convert it and I am not aware Wine or ReactOS already done this task.
171    Not yet implemented and I currently do not plan to do it.
172
173 Q: Any competiting projects?
174 A: Depending on your demands you may be interested in one of:
175    <a href="http://www.powerquest.com/partitionmagic/">PowerQuest PartitionMagic</a>:
176         proprietary; $70
177    <a href="http://www.acronis.com/products/osselector/">Acronis OS Selector</a>:
178         proprietary; $45
179
180 Q: Do you differentiate cdecl/stdcall/fastcall functions?
181 A: W32 uses
182    <a href="http://msdn.microsoft.com/library/en-us/vclang/html/_core_argument_passing_and_naming_conventions.asp"
183    >three different types of function calls</a>.
184    captive is completely compiled with the GNU standard function calls "cdecl"
185    - _even_ the functions from reactos source files with different design
186    function call type (=calltype) are all compiled as GNU standard "cdecl" ones and thus
187    the whole project uses one unified calltype. There is always one 'relaying'
188    function generated inside libcaptive/ke/exports.c file named
189    functionname_calltype (such as functionname_fastcall etc.).
190
191    Relaying function are generated as:<dl>
192    <dt>cdecl</dt><dd>No relaying function generated, just a simple #define.
193         You won't see this function in the gdb backtrace.
194         We cannot generate it by C code as we do not know the number of its arguments.
195         We require the host OS GCC feature "__attribute__((__cdecl__))"
196         although it should be the default function call.
197         </dd>
198    <dt>stdcall</dt><dd>Relaying stub generated, all arguments passed.
199         We require the host OS GCC feature "__attribute__((__stdcall__))".
200         </dd>
201    <dt>fastcall</dt><dd>W32 puts first two arguments to ECX and EDX.
202         GCC puts the arguments in the order EAX, EDX, ECX, ...
203         Although GCC can change this order by "-mreg-alloc=REGS" commandline
204         option such option is not supported by stock GNU/Linux GCC compilers.
205         Therefore we pretend an unused first argument and swap the order of
206         second vs. third function argument to get the right parameter pass.
207         We require the host OS GCC feature "__attribute__((__regparm__(3)))".
208         We require the host OS GCC feature "__attribute__((__stdcall__))".
209    </dl>
210    32-bit return value in EAX is always passed back although it may be vain.
211    Please see the documentation of "captivesym.pl" for more information.
212
213    Unfortutely the situation is dense during calling from the standard
214    calltype forced captive/reactos to the foreign binary W32 code as this
215    code we cannot recompile in any way. All function prototypes leading
216    to the W32 binary code are declared by CAPTIVE_CDECL or CAPTIVE_STDCALL
217    macros from "captive/calltype_reactos.h". These macros have to be
218    #ifdef-ed inside reactos code as they are not available during standalong
219    reactos compilation. CAPTIVE_FASTCALL is not provided but it should not be
220    needed as the non-core kernel code inside foreign W32 binaries usually
221    does not provide any FASTCALL functions. There may be problem if some
222    internal reactos code gets compiled while being called through
223    CAPTIVE_CDECL or CAPTIVE_STDCALL declared protypes - there is currently
224    no such case known.
225
226 Q: What happens if the calltype differs than expected by caller/callee?
227 A: If it is fastcall/non-fastcall type you will usually find it easily
228    as the passed arguments will differ. stdcall/cdecl types is much harder
229    as the only effect will be unexpected ESP returned which gets automatically
230    corrected during the caller function return from EBP.
231
232    We may do some sanity checking in libcaptive/ps/signal.c SIGSEGV handler
233    but any such solution is just a perusing of expected W32 binary code
234    stack frame handling code. It is not possible to correctly do such sanity
235    check as it would have to be coded directly into the W32 binary code.
236
237 Q: Which registers get saved during various function calltypes?
238 A: EDI, ESI, EBX is always saved on stack. It is stored on the stack
239    in this particular order from bottom to top addresses (therefure
240    it is stored by 'push EBX', 'push ESI', 'push EDI').
241
242    Fortunately GNU/Linux GCC AFAIK has the same register saving behaviour.
243    If you get some register corruption please check
244    <a href="What happens if the calltype differs than expected by caller/callee?">function calltypes</a>.
245
246 Q: Why do you call the platform "W32"?
247 A: http://mail.gnu.org/pipermail/libtool/2000-September/000110.html
248
249 Q: What captive-specific W32 registry entries do you use?
250 A: "\captive\filesystem"    as the base path for the loaded W32 filesystem
251
252 Q: Why do you use reactos instead of native <code>ntoskrnl.exe</code>?
253 A: Although the reactos implementation is not native and therefore it will
254    always have has some incompatibilities with native W32 platform it is
255    much easier to debug anything with source-available code.
256    If there will be serious compatibility problems we may start replacing
257    reactos implementation with native <code>ntoskrnl.exe</code> code
258    but it should be considered as last resort - it is enough pain to debug
259    closed source filesystem drivers behaviour while all the other system
260    components are still opensourced.
261
262    Not using <code>ntoskrnl.exe</code> has no license gain as you already
263    need valid W32 licence to use native filesystem driver. Any known W32
264    license covering W32 native filesystem driver also covers <code>ntoskrnl.exe</code>.
265
266 Eric Kohl, reactos developer:
267         ..." and ReactOS cannot run on Linux!"
268         "ReactOS won't run on Linux because of direct hardware access!"