2 * reactos lookaside list functions emulation of libcaptive
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "reactos/ddk/exfuncs.h"
23 #include <glib/gmessages.h>
24 #include "reactos/internal/module.h" /* for MODULE_OBJECT */
25 #include "reactos/internal/ldr.h" /* for LdrGetModuleObject() */
26 #include "captive/unicode.h"
27 #include "captive/ldr_exports.h" /* for captive_ModuleList_patchpoint */
30 static G_CONST_RETURN guint8 *binary_defined_find(gconstpointer binary_base,gsize binary_length,
31 const guint8 *code,const guint8 *code_defined,gsize code_length,gssize call_offset)
33 const guint8 *base,*r;
36 g_return_val_if_fail(binary_base!=NULL,NULL);
37 g_return_val_if_fail(binary_length>=code_length,NULL);
38 g_return_val_if_fail(code!=NULL,NULL);
39 g_return_val_if_fail(code_defined!=NULL,NULL);
40 g_return_val_if_fail(code_length>0,NULL);
42 g_return_val_if_fail(code_defined[0],NULL); /* NOT SUPPORTED */
45 for (base=binary_base;(char *)base<((char *)binary_base)+binary_length-code_length;base++) {
46 gpointer call_orig=NULL; /* Prevent: ... might be used uninitialized in this function */
48 base=memchr(base,code[0],(((char *)binary_base)+binary_length-code_length)-((char *)base));
52 g_assert(call_offset+1+4<=(gssize)code_length);
53 g_assert(code[call_offset]==0xE8); /* call $quad-immediate */
54 g_assert(code_defined[call_offset+0]
55 && code_defined[call_offset+1]
56 && code_defined[call_offset+2]
57 && code_defined[call_offset+3]
58 && code_defined[call_offset+4]);
59 call_orig=*(gpointer *)(code+call_offset+1);
60 *(gint32 *)(code+call_offset+1)=((char *)call_orig)-(((char *)base)+call_offset+1+4); /* make it relative */
62 for (resti=1;resti<code_length;resti++) {
63 if (code_defined[resti] && code[resti]!=base[resti])
67 *(gpointer *)(code+call_offset+1)=call_orig;
68 if (resti<code_length)
70 g_return_val_if_fail(r==NULL,NULL); /* FATAL: found a duplicate */
79 /* Declared only inside libcaptive/ke/exports.c scope. */
80 extern struct captive_ModuleList_patchpoint ExInitializeNPagedLookasideList_patchpoint;
81 extern struct captive_ModuleList_patchpoint ExInitializePagedLookasideList_patchpoint;
85 static gboolean ExAllocateFromPagedLookasideList_findinit
86 (gpointer Lookaside,const struct captive_ModuleList_patchpoint *patchpoint)
90 /* + 0 */ 0x6A,D, /* push $guint8 ;Depth */
91 /* + 2 */ 0x68,D,D,D,D, /* push $guint32 ;Tag */
92 /* + 7 */ 0x6A,D, /* push $guint8 ;Size */
93 /* + 9 */ 0x6A,D, /* push $guint8 ;Flags */
94 /* +11 */ 0x6A,0, /* push $guint8 ;Free (gpointer size) */
95 /* +13 */ 0x6A,0, /* push $guint8 ;Allocate (gpointer size) */
96 /* +15 */ 0x68,D,D,D,D, /* push $guint32 ;Lookaside */
97 /* +20 */ 0xE8,D,D,D,D, /* call $ExInitializeNPagedLookasideList */
101 guint8 initcode_defined[]={
102 /* + 0 */ 1,0, /* push $guint8 ;Depth */
103 /* + 2 */ 1,0,0,0,0, /* push $guint32 ;Tag */
104 /* + 7 */ 1,0, /* push $guint8 ;Size */
105 /* + 9 */ 1,0, /* push $guint8 ;Flags */
106 /* +11 */ 1,1, /* push $guint8 ;Free */
107 /* +13 */ 1,1, /* push $guint8 ;Allocate */
108 /* +15 */ 1,1,1,1,1, /* push $guint32 ;Lookaside */
109 /* +20 */ 1,1,1,1,1, /* call $ExInitializeNPagedLookasideList */
112 const guint8 *initcode_found;
113 MODULE_OBJECT *ntoskrnl_exe_ModuleObject;
115 g_return_val_if_fail(Lookaside!=NULL,FALSE);
117 g_assert(sizeof(initcode)==25);
118 g_assert(sizeof(initcode)==sizeof(initcode_defined));
120 g_assert(initcode[15]==0x68); *(void **)(initcode+15+1)=Lookaside;
122 ntoskrnl_exe_ModuleObject=LdrGetModuleObject(captive_utf8_to_UnicodeString_alloca("ntoskrnl.exe"));
123 g_assert(ntoskrnl_exe_ModuleObject!=NULL);
124 g_assert(ntoskrnl_exe_ModuleObject->TextSection!=NULL);
126 g_assert(patchpoint->orig_w32_func!=NULL);
127 g_assert(initcode[20]==0xE8); *(void **)(initcode+20+1)=patchpoint->orig_w32_func;
129 if (!(initcode_found=binary_defined_find(
130 (void *)ntoskrnl_exe_ModuleObject->TextSection->Base, /* binary_base */
131 ntoskrnl_exe_ModuleObject->TextSection->Length, /* binary_length */
133 initcode_defined, /* code_defined */
134 sizeof(initcode), /* code_length */
135 20))) /* call_offset */
138 g_assert(Lookaside==*(gpointer *)(initcode_found+15+1));
140 /**/ if (patchpoint==&ExInitializeNPagedLookasideList_patchpoint)
141 ExInitializeNPagedLookasideList(
142 (NPAGED_LOOKASIDE_LIST *)Lookaside, /* Lookaside */
143 NULL, /* Allocate; NULL as there is only guint8 in the code */
144 NULL, /* Free; NULL as there is only guint8 in the code */
145 *(guint8 *)(initcode_found+9+1), /* Flags; undocumented by W32 doc (reserved) */
146 *(guint8 *)(initcode_found+7+1), /* Size; FIXME: ? node size */
147 *(guint32 *)(initcode_found+2+1), /* Tag; 'FSfm' */
148 *(guint8 *)(initcode_found+0+1)); /* Depth; undocumented by W32 doc (reserved) */
149 else if (patchpoint==&ExInitializePagedLookasideList_patchpoint)
150 ExInitializePagedLookasideList(
151 (PAGED_LOOKASIDE_LIST *)Lookaside, /* Lookaside */
152 NULL, /* Allocate; NULL as there is only guint8 in the code */
153 NULL, /* Free; NULL as there is only guint8 in the code */
154 *(guint8 *)(initcode_found+9+1), /* Flags; undocumented by W32 doc (reserved) */
155 *(guint8 *)(initcode_found+7+1), /* Size; FIXME: ? node size */
156 *(guint32 *)(initcode_found+2+1), /* Tag; 'FSfm' */
157 *(guint8 *)(initcode_found+0+1)); /* Depth; undocumented by W32 doc (reserved) */
158 else g_assert_not_reached();
164 PVOID ExAllocateFromPagedLookasideList_orig(PPAGED_LOOKASIDE_LIST Lookaside);
166 PVOID ExAllocateFromPagedLookasideList_wrap(PAGED_LOOKASIDE_LIST *Lookaside)
168 static PAGED_LOOKASIDE_LIST Lookaside_zero;
170 g_return_val_if_fail(Lookaside!=NULL,NULL);
172 if (!memcmp(Lookaside,&Lookaside_zero,sizeof(Lookaside_zero))) {
174 && !ExAllocateFromPagedLookasideList_findinit(Lookaside,&ExInitializeNPagedLookasideList_patchpoint)
175 && !ExAllocateFromPagedLookasideList_findinit(Lookaside,&ExInitializePagedLookasideList_patchpoint))
176 g_assert_not_reached(); /* failed to patch it */
179 return ExAllocateFromPagedLookasideList_orig(Lookaside);