1 |
|
/* |
2 |
|
* pnetvm.c - DGEE internal calls |
3 |
|
* |
4 |
|
* Copyright (C) 2002 netFluid Technology Ltd |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
8 |
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* This program is distributed in the hope that it will be useful, |
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU General Public License |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
*/ |
20 |
|
#include <stdio.h> |
21 |
|
#include <gwlog.h> |
22 |
|
#include "clrCache.h" |
23 |
|
|
24 |
|
static LIST *image_cache = NULL; |
25 |
|
|
26 |
|
|
27 |
|
/* Found in libILEngine.a |
28 |
|
*/ |
29 |
|
extern ILObject *_ILClrToObject(ILExecThread *thread, void *item, const char *name); |
30 |
|
|
31 |
|
|
32 |
|
/* ------------------------------------------------------------------------- */ |
33 |
|
|
34 |
|
void |
35 |
|
pnetVM_initCache() |
36 |
|
{ |
37 |
|
if( image_cache ) xl_List_end( &image_cache ); |
38 |
|
xl_List_init( &image_cache, sizeof( DGImage ) ); |
39 |
|
} |
40 |
|
|
41 |
|
/* ------------------------------------------------------------------------- */ |
42 |
|
|
43 |
|
void |
44 |
|
pnetVM_addBytecodeToCache( char * name, uchar *bytes, ulong len ) |
45 |
|
{ |
46 |
|
DGImage *img = NULL; |
47 |
|
|
48 |
|
xl_List_insert_space( image_cache, ListEnd ); |
49 |
|
img = (DGImage*)xl_List_get_current_data( image_cache ); |
50 |
|
|
51 |
|
strncpy( img->name, name, DGNAMELEN ); |
52 |
|
img->bytes = bytes; |
53 |
|
img->len = len; |
54 |
|
img->modtime = 0; |
55 |
|
img->flags = 0; |
56 |
|
img->object = NULL; |
57 |
|
} |
58 |
|
|
59 |
|
/* ------------------------------------------------------------------------- */ |
60 |
|
|
61 |
|
DGImage * |
62 |
|
pnetVM_findCachedObject( char *name ) |
63 |
|
{ |
64 |
|
DGImage *node = NULL; |
65 |
|
|
66 |
|
if( xl_List_get_first( image_cache, (uchar**)&node ) == Success ) { |
67 |
|
do { |
68 |
|
if( strcmp( node->name, name ) == 0 ) return node; |
69 |
|
} while( xl_List_get_next( image_cache, (uchar**)&node ) == Success ); |
70 |
|
} |
71 |
|
return NULL; |
72 |
|
} |
73 |
|
|
74 |
|
/* ------------------------------------------------------------------------- */ |
75 |
|
/* Convert an image into an assembly object. |
76 |
|
*/ |
77 |
|
static ILObject * |
78 |
|
ImageToAssembly(ILExecThread *thread, ILImage *image) |
79 |
|
{ |
80 |
|
void *item; |
81 |
|
item = ILImageTokenInfo(image, (IL_META_TOKEN_ASSEMBLY | 1)); |
82 |
|
if(item) |
83 |
|
{ |
84 |
|
return _ILClrToObject(thread, item, "DotGNU.DGEE.Protocols.AssemblyContainer"); |
85 |
|
} |
86 |
|
|
87 |
|
return 0; |
88 |
|
} |
89 |
|
|
90 |
|
/* ------------------------------------------------------------------------- */ |
91 |
|
/* |
92 |
|
* Load errors. These must be kept in sync with "pnetlib". |
93 |
|
*/ |
94 |
|
#define LoadError_OK 0 |
95 |
|
#define LoadError_InvalidName 1 |
96 |
|
#define LoadError_FileNotFound 2 |
97 |
|
#define LoadError_BadImage 3 |
98 |
|
#define LoadError_Security 4 |
99 |
|
|
100 |
|
|
101 |
|
/* Called via an InternalCall within DotGNU/DGEE/AssemblyContainer.cs |
102 |
|
*/ |
103 |
|
ILObject * |
104 |
|
_IL_AssemblyContainer_LoadFromMemory(ILExecThread *thread, |
105 |
|
ILString *name, |
106 |
|
ILInt32 *error ) |
107 |
|
{ |
108 |
|
ILImage *image = NULL; |
109 |
|
ILExecProcess *proc = NULL; |
110 |
|
ILContext *ctx = NULL; |
111 |
|
DGImage *dgimg = NULL; |
112 |
|
int loadError = 0; |
113 |
|
|
114 |
|
/* See if this object has already been loaded |
115 |
|
*/ |
116 |
|
dgimg = pnetVM_findCachedObject( ILStringToUTF8(thread, name) ); |
117 |
|
|
118 |
|
if( dgimg == NULL ) { |
119 |
|
/* No entry found. addBytecodeToCache MUST be called before this func! |
120 |
|
*/ |
121 |
|
gw_logf( LOG_DEBUG, "Cannot find bytecode node '%s'", name ); |
122 |
|
ILExecThreadThrowArgNull(thread, "dgimg"); |
123 |
|
return NULL; |
124 |
|
} |
125 |
|
|
126 |
|
if( dgimg->bytes == NULL || dgimg->len == 0 ) { |
127 |
|
/* Entry found. But no bytecode. addBytecodeToCache MUST be |
128 |
|
* called before this func! |
129 |
|
*/ |
130 |
|
gw_logf( LOG_DEBUG, "No Stored Bytecode for '%s'", name ); |
131 |
|
ILExecThreadThrowArgNull(thread, "dgimg->bytes"); |
132 |
|
return NULL; |
133 |
|
} |
134 |
|
|
135 |
|
if( dgimg->object ) return dgimg->object; /* Found ILObject - return it */ |
136 |
|
|
137 |
|
|
138 |
|
/* Else create an ILObject from the bytecode in dgimg, store it back |
139 |
|
* in dgimg and return it. |
140 |
|
*/ |
141 |
|
proc = ILExecThreadGetProcess ( thread ); |
142 |
|
ctx = ILExecProcessGetContext( proc ); |
143 |
|
|
144 |
|
gw_logf( LOG_DEBUG, "Got Context %p", ctx ); |
145 |
|
|
146 |
|
gw_logf( LOG_DEBUG, "Load bytecode as ILImage (len %d)", dgimg->len ); |
147 |
|
loadError = ILImageLoadFromMemory( dgimg->bytes, dgimg->len, |
148 |
|
ctx, |
149 |
|
&image, |
150 |
|
IL_LOADFLAG_FORCE_32BIT, 0); |
151 |
|
/* IL_LOADFLAG_FORCE_32BIT | IL_LOADFLAG_IN_PLACE, 0); */ |
152 |
|
|
153 |
|
if(loadError == 0) { |
154 |
|
*error = LoadError_OK; |
155 |
|
|
156 |
|
/* Store object reference and return it */ |
157 |
|
gw_logf( LOG_DEBUG, "Image to assembly" ); |
158 |
|
dgimg->object = ImageToAssembly(thread, image); |
159 |
|
|
160 |
|
gw_logf( LOG_DEBUG, "Return Assembly [%p]", dgimg->object ); |
161 |
|
return dgimg->object; |
162 |
|
} |
163 |
|
|
164 |
|
/* Convert the error code into something the C# library knows about */ |
165 |
|
if(loadError == -1) { |
166 |
|
gw_logf( LOG_DEBUG, "LoadError_FileNotFound" ); |
167 |
|
*error = LoadError_FileNotFound; |
168 |
|
|
169 |
|
} else if(loadError == IL_LOADERR_MEMORY) { |
170 |
|
|
171 |
|
gw_logf( LOG_DEBUG, "Out of Memory?" ); |
172 |
|
*error = LoadError_FileNotFound; |
173 |
|
ILExecThreadThrowOutOfMemory(thread); |
174 |
|
|
175 |
|
} else { |
176 |
|
gw_logf( LOG_DEBUG, "LoadError_BadImage" ); |
177 |
|
*error = LoadError_BadImage; |
178 |
|
} |
179 |
|
return 0; |
180 |
|
} |
181 |
|
|
182 |
|
/* ------------------------------------------------------------------------- */ |
183 |
|
/* end */ |