1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: CDDL 1.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the COMMON DEVELOPMENT AND
|
---|
5 | * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
|
---|
6 | * this file except in compliance with the License. You may obtain a copy of
|
---|
7 | * the License at http://www.sun.com/cddl/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is "NOM" Netlabs Object Model
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * netlabs.org: Chris Wohlgemuth <cinc-ml@netlabs.org>.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2005-2006
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | *
|
---|
23 | * Alternatively, the contents of this file may be used under the terms of
|
---|
24 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
|
---|
25 | * case the provisions of the LGPL are applicable instead of those above. If
|
---|
26 | * you wish to allow use of your version of this file only under the terms of
|
---|
27 | * the LGPL, and not to allow others to use your version of this file under
|
---|
28 | * the terms of the CDDL, indicate your decision by deleting the provisions
|
---|
29 | * above and replace them with the notice and other provisions required by the
|
---|
30 | * LGPL. If you do not delete the provisions above, a recipient may use your
|
---|
31 | * version of this file under the terms of any one of the CDDL or the LGPL.
|
---|
32 | *
|
---|
33 | * ***** END LICENSE BLOCK ***** */
|
---|
34 |
|
---|
35 | /*
|
---|
36 | This file contains functions for working with the garbage collector.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #define INCL_DOS
|
---|
40 | #define INCL_DOSERRORS
|
---|
41 | #define INCL_DOSMEMMGR
|
---|
42 | #include <os2.h>
|
---|
43 |
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 |
|
---|
48 | /* For nomToken etc. */
|
---|
49 | #include <nom.h>
|
---|
50 | #include "nomtk.h"
|
---|
51 | #include "nomgc.h"
|
---|
52 |
|
---|
53 | /* Garbage collector */
|
---|
54 | #include <gc.h>
|
---|
55 |
|
---|
56 | gboolean bUseGC=FALSE; /* Mark if we use the garbage collector */
|
---|
57 | GTree* treeRegisteredDLL;
|
---|
58 |
|
---|
59 | static gpointer gcMalloc(gulong ulBytes)
|
---|
60 | {
|
---|
61 | //printf("Hi there...\n");
|
---|
62 | // return malloc(ulBytes);
|
---|
63 | return (gpointer) GC_malloc(ulBytes);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static gpointer gcRealloc(gpointer mem, gulong ulBytes)
|
---|
67 | {
|
---|
68 | // printf("...and here\n");
|
---|
69 | // return realloc(mem, ulBytes);
|
---|
70 | return (gpointer) GC_realloc(mem, ulBytes);
|
---|
71 | }
|
---|
72 |
|
---|
73 | static void gcFree(gpointer mem)
|
---|
74 | {
|
---|
75 | // printf("free(): %x\n", mem);
|
---|
76 | return;
|
---|
77 | GC_free(mem);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /*
|
---|
82 | This is called from the EMX wrapper to set the garbage collector
|
---|
83 | memory functions as the GLIB default allocation function.
|
---|
84 | */
|
---|
85 | void _System nomInitGarbageCollection(void* pMemInExe)
|
---|
86 | {
|
---|
87 | GMemVTable vtbl={0};
|
---|
88 |
|
---|
89 | /* Init the garbage collector */
|
---|
90 | GC_init();
|
---|
91 |
|
---|
92 | vtbl.malloc=(gpointer)gcMalloc;
|
---|
93 | vtbl.realloc=(gpointer)gcRealloc;
|
---|
94 | vtbl.free=(gpointer)gcFree;
|
---|
95 |
|
---|
96 | g_mem_set_vtable(&vtbl);
|
---|
97 | /* fprintf(stderr, " GC memory functions set for GLIB. (%s: %d)\n", __FILE__, __LINE__); */
|
---|
98 |
|
---|
99 | /* Cretea tree holding the already registered DLLs */
|
---|
100 | treeRegisteredDLL=g_tree_new((GCompareFunc)stricmp);
|
---|
101 |
|
---|
102 | bUseGC=TRUE;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | void test()
|
---|
107 | {
|
---|
108 | gulong ulObj, ulOffset;
|
---|
109 | gchar thePath[CCHMAXPATH];
|
---|
110 | HMODULE hModule;
|
---|
111 |
|
---|
112 | if(DosQueryModFromEIP( &hModule, &ulObj, CCHMAXPATH, thePath, &ulOffset, (ULONG)test)!=0) {
|
---|
113 | hModule=0;
|
---|
114 | return ; /* Error */
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | NOMEXTERN void NOMLINK nomRegisterDataAreaForGC(char* pStart, char* pEnd)
|
---|
120 | {
|
---|
121 | GC_add_roots(pStart, pEnd);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static void qsAddDLLToList(HREGDLL hReg, qsLrec_t* rec)
|
---|
125 | {
|
---|
126 | if(NULLHANDLE==g_slist_find(hReg->dllList, rec))
|
---|
127 | hReg->dllList=g_slist_append(hReg->dllList, rec);
|
---|
128 | }
|
---|
129 |
|
---|
130 | #if 0
|
---|
131 | static void qsPrintDLLList(HREGDLL hReg)
|
---|
132 | {
|
---|
133 | GSList* lTemp;
|
---|
134 | int a=0;
|
---|
135 |
|
---|
136 | lTemp=hReg->dllList;
|
---|
137 | while(lTemp)
|
---|
138 | {
|
---|
139 | qsLrec_t* rec;
|
---|
140 | rec=(qsLrec_t*)lTemp->data;
|
---|
141 | a++;
|
---|
142 | g_message(" %d: %s", a, rec->pName);
|
---|
143 | lTemp=g_slist_next(lTemp);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | /*
|
---|
149 | Find a library record in the buffer filled by DosQuerySysState().
|
---|
150 | */
|
---|
151 | static qsLrec_t* qsFindModuleRec(const qsPtrRec_t * hRegisterDLL, USHORT hMod){
|
---|
152 | qsLrec_t * pModRec;
|
---|
153 | int a=0;
|
---|
154 |
|
---|
155 | pModRec=hRegisterDLL->pLibRec;
|
---|
156 | while(NULL!=pModRec)
|
---|
157 | {
|
---|
158 | a++;
|
---|
159 | /* printf("%d Checking: %x -> %04X (%s)\n", a, pModRec, pModRec->hmte, pModRec->pName); */
|
---|
160 |
|
---|
161 | if (NULLHANDLE==pModRec->pObjInfo && pModRec->ctObj > 0)
|
---|
162 | {
|
---|
163 | pModRec->pObjInfo = (qsLObjrec_t*)((char*)pModRec
|
---|
164 | + ((sizeof(qsLrec_t)
|
---|
165 | + pModRec->ctImpMod * sizeof(short)
|
---|
166 | + strlen((char*)pModRec->pName) + 1 /* filename */
|
---|
167 | + 3) & ~3));
|
---|
168 | pModRec->pNextRec = (void*)((char*)pModRec->pObjInfo
|
---|
169 | + sizeof(qsLObjrec_t) * pModRec->ctObj);
|
---|
170 | }
|
---|
171 | if(pModRec->hmte==hMod)
|
---|
172 | break;
|
---|
173 |
|
---|
174 | pModRec=(qsLrec_t *)pModRec->pNextRec;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return pModRec;
|
---|
178 | }
|
---|
179 |
|
---|
180 | #define BUFSIZE 1024*1024
|
---|
181 | NOMEXTERN HREGDLL NOMLINK nomBeginRegisterDLLWithGC(void)
|
---|
182 | {
|
---|
183 | ULONG rc;
|
---|
184 | HREGDLL hReg=NULLHANDLE;
|
---|
185 | PTIB ptib;
|
---|
186 | PPIB ppib;
|
---|
187 | char * buf;
|
---|
188 | HREGDLL pRegDLL=NULLHANDLE;
|
---|
189 |
|
---|
190 | rc = DosGetInfoBlocks(&ptib, &ppib);
|
---|
191 | if (rc!=NO_ERROR)
|
---|
192 | return NULLHANDLE;
|
---|
193 |
|
---|
194 | buf = malloc(BUFSIZE);
|
---|
195 | if(!buf)
|
---|
196 | return NULLHANDLE;
|
---|
197 |
|
---|
198 | pRegDLL =(HREGDLL) malloc(sizeof(REGDLL));
|
---|
199 | if(!pRegDLL){
|
---|
200 | free(buf);
|
---|
201 | return NULLHANDLE;
|
---|
202 | }
|
---|
203 | pRegDLL->dllList=NULLHANDLE;
|
---|
204 |
|
---|
205 | memset(buf,0,BUFSIZE);
|
---|
206 |
|
---|
207 | rc = DosQuerySysState(QS_PROCESS | QS_SEMAPHORE | QS_MTE | QS_FILESYS | QS_SHMEMORY ,
|
---|
208 | QS_MTE, /*0x96*/ ppib->pib_ulpid , 1UL, (PCHAR)buf, BUFSIZE);
|
---|
209 | if (rc==NO_ERROR) {
|
---|
210 | qsPrec_t * p;
|
---|
211 | GSList* lTemp;
|
---|
212 |
|
---|
213 | pRegDLL->pMainAnchor=(qsPtrRec_t*) buf;
|
---|
214 |
|
---|
215 | p=pRegDLL->pMainAnchor->pProcRec;
|
---|
216 |
|
---|
217 | while(p && p->RecType == 1)
|
---|
218 | {
|
---|
219 |
|
---|
220 | if (p->cLib) {
|
---|
221 | int i;
|
---|
222 |
|
---|
223 | for (i=0; i<p->cLib; i++){
|
---|
224 | qsLrec_t * pModRec;
|
---|
225 |
|
---|
226 | pModRec=qsFindModuleRec(pRegDLL->pMainAnchor, p->pLibRec[i]);
|
---|
227 |
|
---|
228 | if(pModRec){
|
---|
229 | // if(pModRec->pName)
|
---|
230 | //g_message("%s", pModRec->pName);
|
---|
231 | qsAddDLLToList(pRegDLL, pModRec);
|
---|
232 | }
|
---|
233 | }/* for() */
|
---|
234 | }/* if(p->clib) */
|
---|
235 | break;
|
---|
236 | };/* while() */
|
---|
237 |
|
---|
238 | /* Ok, got directly imported DLLs. Now go over these and check them for additional imports.
|
---|
239 | Every import is added to the end of the list (except duplicates). So while going over
|
---|
240 | the list we touch every DLL and check every import. Import cycles are no problem, because
|
---|
241 | later duplicates are ignored. */
|
---|
242 | //g_message("\n\n");
|
---|
243 | lTemp=pRegDLL->dllList;
|
---|
244 | while(lTemp)
|
---|
245 | {
|
---|
246 | qsLrec_t* rec;
|
---|
247 |
|
---|
248 | rec=(qsLrec_t*)lTemp->data;
|
---|
249 |
|
---|
250 | /* Check the imports of this DLL if any */
|
---|
251 | if(rec->ctImpMod >0)
|
---|
252 | {
|
---|
253 | int iImps;
|
---|
254 | PUSHORT pImpHmte;
|
---|
255 |
|
---|
256 | pImpHmte=(PUSHORT)((void*)rec + sizeof(qsLrec_t));
|
---|
257 | for(iImps=0; iImps < rec->ctImpMod; iImps++)
|
---|
258 | {
|
---|
259 | qsLrec_t * pModImp;
|
---|
260 |
|
---|
261 | pModImp=qsFindModuleRec(pRegDLL->pMainAnchor, pImpHmte[iImps]);
|
---|
262 | if(pModImp){
|
---|
263 | //if(pModImp->pName)
|
---|
264 | // g_message("%s", pModImp->pName);
|
---|
265 | qsAddDLLToList(pRegDLL, pModImp);
|
---|
266 | }
|
---|
267 | }/* for()*/
|
---|
268 | }/* if() */
|
---|
269 | lTemp=g_slist_next(lTemp);
|
---|
270 | };/* while() */
|
---|
271 | // qsPrintDLLList();
|
---|
272 | hReg=pRegDLL;
|
---|
273 | }
|
---|
274 | else{
|
---|
275 | free(pRegDLL);
|
---|
276 | free(buf);
|
---|
277 | }
|
---|
278 | return hReg;
|
---|
279 | }
|
---|
280 |
|
---|
281 | NOMEXTERN void NOMLINK nomEndRegisterDLLWithGC(const HREGDLL hRegisterDLL )
|
---|
282 | {
|
---|
283 | g_slist_free(hRegisterDLL->dllList);
|
---|
284 | free((char*)hRegisterDLL->pMainAnchor);
|
---|
285 | free((char*)hRegisterDLL);
|
---|
286 | }
|
---|
287 |
|
---|
288 | #define OBJREAD 0x0001L
|
---|
289 | #define OBJWRITE 0x0002L
|
---|
290 | #define OBJINVALID 0x0080L
|
---|
291 | NOMEXTERN BOOL NOMLINK nomRegisterDLLByName(const HREGDLL hRegisterDLL, const char* chrDLLName)
|
---|
292 | {
|
---|
293 | GSList* lTemp;
|
---|
294 |
|
---|
295 | //g_message("Trying to register DLL %s\n", chrDLLName);
|
---|
296 | lTemp=hRegisterDLL->dllList;
|
---|
297 | while(lTemp)
|
---|
298 | {
|
---|
299 | qsLrec_t* pModRec;
|
---|
300 |
|
---|
301 | pModRec=(qsLrec_t*)lTemp->data;
|
---|
302 | if(pModRec){
|
---|
303 | //g_message("DLL name: %s\n", pModRec->pName);
|
---|
304 | if(pModRec->pName && (NULLHANDLE!=strstr( pModRec->pName, chrDLLName)))
|
---|
305 | {
|
---|
306 | qsLObjrec_t *pObjInfo;
|
---|
307 | //g_message(" --> Found DLL %s", pModRec->pName);
|
---|
308 | pObjInfo=pModRec->pObjInfo;
|
---|
309 | if(NULLHANDLE!=pObjInfo)
|
---|
310 | {
|
---|
311 | int iObj;
|
---|
312 | for(iObj=0; iObj<pModRec->ctObj ;iObj++)
|
---|
313 | {
|
---|
314 | if (!(pObjInfo[iObj].oflags & OBJWRITE)) continue;
|
---|
315 | if (!(pObjInfo[iObj].oflags & OBJREAD)) continue;
|
---|
316 | if ((pObjInfo[iObj].oflags & OBJINVALID)) continue;
|
---|
317 | //g_message(" #%d: %04lX, size: %04lX %04lX",
|
---|
318 | // iObj, pObjInfo[iObj].oaddr, pObjInfo[iObj].osize, pObjInfo[iObj].oflags);
|
---|
319 | nomRegisterDataAreaForGC((char*)pObjInfo[iObj].oaddr,
|
---|
320 | (char*)(pObjInfo[iObj].oaddr+pObjInfo[iObj].osize));
|
---|
321 | g_tree_insert(treeRegisteredDLL, g_strdup(chrDLLName), GUINT_TO_POINTER((guint)pModRec->hmte));
|
---|
322 | }
|
---|
323 | }
|
---|
324 | return TRUE;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | lTemp=g_slist_next(lTemp);
|
---|
328 | };/* while() */
|
---|
329 | return FALSE;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | NOMEXTERN BOOL NOMLINK nomQueryUsingNameIsDLLRegistered(const gchar *chrName)
|
---|
334 | {
|
---|
335 | if(NULLHANDLE!=g_tree_lookup(treeRegisteredDLL, chrName))
|
---|
336 | return TRUE;
|
---|
337 |
|
---|
338 | return FALSE;
|
---|
339 | }
|
---|