18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
20 |
#include <stdio.h> |
#include <stdio.h> |
21 |
|
#include <gwlog.h> |
22 |
#include "clrCache.h" |
#include "clrCache.h" |
23 |
|
|
24 |
static LIST *image_cache = NULL; |
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 |
void |
35 |
pnetVM_initCache() |
pnetVM_initCache() |
36 |
{ |
{ |
37 |
if( image_cache ) xl_List_end( &image_cache ); |
if( image_cache ) xl_List_end( &image_cache ); |
38 |
xl_List_init( &image_cache, sizeof( DGImage ) ); |
xl_List_init( &image_cache, sizeof( DGImage ) ); |
39 |
} |
} |
40 |
|
|
41 |
/* ------------------------------------------------------------------------- */ |
/* ------------------------------------------------------------------------- */ |
43 |
void |
void |
44 |
pnetVM_addBytecodeToCache( char * name, uchar *bytes, ulong len ) |
pnetVM_addBytecodeToCache( char * name, uchar *bytes, ulong len ) |
45 |
{ |
{ |
46 |
DGImage *img = NULL; |
DGImage *img = NULL; |
47 |
|
|
48 |
xl_List_insert_space( image_cache, ListEnd ); |
xl_List_insert_space( image_cache, ListEnd ); |
49 |
img = (DGImage*)xl_List_get_current_data( image_cache ); |
img = (DGImage*)xl_List_get_current_data( image_cache ); |
50 |
|
|
51 |
strncpy( img->name, name, DGNAMELEN ); |
strncpy( img->name, name, DGNAMELEN ); |
52 |
img->bytes = bytes; |
img->bytes = bytes; |
53 |
img->len = len; |
img->len = len; |
54 |
img->modtime = 0; |
img->modtime = 0; |
55 |
img->flags = 0; |
img->flags = 0; |
56 |
img->object = NULL; |
img->object = NULL; |
57 |
} |
} |
58 |
|
|
59 |
/* ------------------------------------------------------------------------- */ |
/* ------------------------------------------------------------------------- */ |
61 |
DGImage * |
DGImage * |
62 |
pnetVM_findCachedObject( char *name ) |
pnetVM_findCachedObject( char *name ) |
63 |
{ |
{ |
64 |
DGImage *node = NULL; |
DGImage *node = NULL; |
65 |
|
|
66 |
if( xl_List_get_first( image_cache, (uchar**)&node ) == Success ) { |
if( xl_List_get_first( image_cache, (uchar**)&node ) == Success ) { |
67 |
do { |
do { |
68 |
if( strcmp( node->name, name ) == 0 ) return node; |
if( strcmp( node->name, name ) == 0 ) return node; |
69 |
} while( xl_List_get_next( image_cache, (uchar**)&node ) == Success ); |
} while( xl_List_get_next( image_cache, (uchar**)&node ) == Success ); |
70 |
} |
} |
71 |
return NULL; |
return NULL; |
72 |
} |
} |
73 |
|
|
74 |
/* ------------------------------------------------------------------------- */ |
/* ------------------------------------------------------------------------- */ |
75 |
/* |
/* Convert an image into an assembly object. |
76 |
* * Convert an image into an assembly object. |
*/ |
77 |
* */ |
static ILObject * |
78 |
static ILObject *ImageToAssembly(ILExecThread *thread, ILImage *image) |
ImageToAssembly(ILExecThread *thread, ILImage *image) |
79 |
{ |
{ |
80 |
void *item; |
void *item; |
81 |
item = ILImageTokenInfo(image, (IL_META_TOKEN_ASSEMBLY | 1)); |
item = ILImageTokenInfo(image, (IL_META_TOKEN_ASSEMBLY | 1)); |
82 |
if(item) |
if(item) |
83 |
{ |
{ |
84 |
return _ILClrToObject(thread, item, "DotGNU.DGEE.AssemblyContainer"); |
return _ILClrToObject(thread, item, "DotGNU.DGEE.AssemblyContainer"); |
85 |
} |
} |
86 |
|
|
87 |
/* TODO: if the image does not have an assembly manifest, |
return 0; |
|
* then look for the parent assembly */ |
|
|
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 |
ILObject *_IL_AssemblyContainer_LoadFromMemory(ILExecThread *thread, |
|
101 |
ILString *name, |
/* Called via an InternalCall within DotGNU/DGEE/AssemblyContainer.cs |
102 |
ILInt32 *error ) |
*/ |
103 |
|
ILObject * |
104 |
|
_IL_AssemblyContainer_LoadFromMemory(ILExecThread *thread, |
105 |
|
ILString *name, |
106 |
|
ILInt32 *error ) |
107 |
{ |
{ |
108 |
ILImage *image = NULL; |
ILImage *image = NULL; |
109 |
DGImage *dgimg = NULL; |
ILExecProcess *proc = NULL; |
110 |
int loadError = 0; |
ILContext *ctx = NULL; |
111 |
|
DGImage *dgimg = NULL; |
112 |
/* See if this object has already been loaded |
int loadError = 0; |
113 |
*/ |
|
114 |
dgimg = pnetVM_findCachedObject( ILStringToUTF8(thread, name) ); |
/* See if this object has already been loaded |
115 |
|
*/ |
116 |
if( dgimg == NULL ) { |
dgimg = pnetVM_findCachedObject( ILStringToUTF8(thread, name) ); |
117 |
ILExecThreadThrowArgNull(thread, "dgimg"); |
|
118 |
return NULL; |
if( dgimg == NULL ) { |
119 |
} |
/* No entry found. addBytecodeToCache MUST be called before this func! |
120 |
if( dgimg->bytes == NULL || dgimg->len == 0 ) { |
*/ |
121 |
ILExecThreadThrowArgNull(thread, "dgimg->bytes"); |
gw_logf( LOG_DEBUG, "Cannot find bytecode node '%s'", name ); |
122 |
return NULL; |
ILExecThreadThrowArgNull(thread, "dgimg"); |
123 |
} |
return NULL; |
124 |
|
} |
125 |
if( dgimg->object ) return dgimg->object; /* Found object - return it */ |
|
126 |
|
if( dgimg->bytes == NULL || dgimg->len == 0 ) { |
127 |
|
/* Entry found. But no bytecode. addBytecodeToCache MUST be |
128 |
/* Else load object |
* called before this func! |
129 |
*/ |
*/ |
130 |
loadError = ILImageLoadFromMemory( dgimg->bytes, dgimg->len, |
gw_logf( LOG_DEBUG, "No Stored Bytecode for '%s'", name ); |
131 |
thread->process->context, &image, |
ILExecThreadThrowArgNull(thread, "dgimg->bytes"); |
132 |
IL_LOADFLAG_FORCE_32BIT | IL_LOADFLAG_IN_PLACE, 0); |
return NULL; |
133 |
|
} |
134 |
if(loadError == 0) |
|
135 |
{ |
if( dgimg->object ) return dgimg->object; /* Found ILObject - return it */ |
136 |
*error = LoadError_OK; |
|
137 |
|
|
138 |
/* Store object reference and return it */ |
/* Else create an ILObject from the bytecode in dgimg, store it back |
139 |
dgimg->object = ImageToAssembly(thread, image); |
* in dgimg and return it. |
140 |
return dgimg->object; |
*/ |
141 |
} |
proc = ILExecThreadGetProcess ( thread ); |
142 |
|
ctx = ILExecProcessGetContext( proc ); |
143 |
/* Convert the error code into something the C# library knows about */ |
|
144 |
if(loadError == -1) |
gw_logf( LOG_DEBUG, "Got Context %p", ctx ); |
145 |
{ |
|
146 |
*error = LoadError_FileNotFound; |
gw_logf( LOG_DEBUG, "Load bytecode as ILImage (len %d)", dgimg->len ); |
147 |
} |
loadError = ILImageLoadFromMemory( dgimg->bytes, dgimg->len, |
148 |
else if(loadError == IL_LOADERR_MEMORY) |
ctx, |
149 |
{ |
&image, |
150 |
*error = LoadError_FileNotFound; |
IL_LOADFLAG_FORCE_32BIT, 0); |
151 |
ILExecThreadThrowOutOfMemory(thread); |
/* IL_LOADFLAG_FORCE_32BIT | IL_LOADFLAG_IN_PLACE, 0); */ |
152 |
} |
|
153 |
else |
if(loadError == 0) { |
154 |
{ |
*error = LoadError_OK; |
155 |
*error = LoadError_BadImage; |
|
156 |
} |
/* Store object reference and return it */ |
157 |
return 0; |
gw_logf( LOG_DEBUG, "Image to assembly" ); |
158 |
|
dgimg->object = ImageToAssembly(thread, image); |
159 |
|
|
160 |
|
gw_logf( LOG_DEBUG, "Return Assembly" ); |
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 |
/* ------------------------------------------------------------------------- */ |
/* ------------------------------------------------------------------------- */ |