Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

6571 lines (5771 sloc) 186.713 kB
/*
* metadata.c: Routines for accessing the metadata
*
* Authors:
* Miguel de Icaza (miguel@ximian.com)
* Paolo Molaro (lupus@ximian.com)
*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
* Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "metadata.h"
#include "tabledefs.h"
#include "mono-endian.h"
#include "cil-coff.h"
#include "tokentype.h"
#include "class-internals.h"
#include "metadata-internals.h"
#include "verify-internals.h"
#include "class.h"
#include "marshal.h"
#include "debug-helpers.h"
#include "abi-details.h"
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/bsearch.h>
/* Auxiliary structure used for caching inflated signatures */
typedef struct {
MonoMethodSignature *sig;
MonoGenericContext context;
} MonoInflatedMethodSignature;
static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container, gboolean transient,
const char *ptr, const char **rptr);
static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
gboolean signature_only);
static void free_generic_inst (MonoGenericInst *ginst);
static void free_generic_class (MonoGenericClass *ginst);
static void free_inflated_method (MonoMethodInflated *method);
static void free_inflated_signature (MonoInflatedMethodSignature *sig);
static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
/*
* This enumeration is used to describe the data types in the metadata
* tables
*/
enum {
MONO_MT_END,
/* Sized elements */
MONO_MT_UINT32,
MONO_MT_UINT16,
MONO_MT_UINT8,
/* Index into Blob heap */
MONO_MT_BLOB_IDX,
/* Index into String heap */
MONO_MT_STRING_IDX,
/* GUID index */
MONO_MT_GUID_IDX,
/* Pointer into a table */
MONO_MT_TABLE_IDX,
/* HasConstant:Parent pointer (Param, Field or Property) */
MONO_MT_CONST_IDX,
/* HasCustomAttribute index. Indexes any table except CustomAttribute */
MONO_MT_HASCAT_IDX,
/* CustomAttributeType encoded index */
MONO_MT_CAT_IDX,
/* HasDeclSecurity index: TypeDef Method or Assembly */
MONO_MT_HASDEC_IDX,
/* Implementation coded index: File, Export AssemblyRef */
MONO_MT_IMPL_IDX,
/* HasFieldMarshal coded index: Field or Param table */
MONO_MT_HFM_IDX,
/* MemberForwardedIndex: Field or Method */
MONO_MT_MF_IDX,
/* TypeDefOrRef coded index: typedef, typeref, typespec */
MONO_MT_TDOR_IDX,
/* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
MONO_MT_MRP_IDX,
/* MethodDefOrRef coded index: Method or Member Ref table */
MONO_MT_MDOR_IDX,
/* HasSemantic coded index: Event or Property */
MONO_MT_HS_IDX,
/* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
MONO_MT_RS_IDX
};
const static unsigned char TableSchemas [] = {
#define ASSEMBLY_SCHEMA_OFFSET 0
MONO_MT_UINT32, /* "HashId" }, */
MONO_MT_UINT16, /* "Major" }, */
MONO_MT_UINT16, /* "Minor" }, */
MONO_MT_UINT16, /* "BuildNumber" }, */
MONO_MT_UINT16, /* "RevisionNumber" }, */
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_BLOB_IDX, /* "PublicKey" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_STRING_IDX, /* "Culture" }, */
MONO_MT_END,
#define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
MONO_MT_UINT32, /* "OSPlatformID" }, */
MONO_MT_UINT32, /* "OSMajor" }, */
MONO_MT_UINT32, /* "OSMinor" }, */
MONO_MT_END,
#define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
MONO_MT_UINT32, /* "Processor" }, */
MONO_MT_END,
#define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
MONO_MT_UINT16, /* "Major" }, */
MONO_MT_UINT16, /* "Minor" }, */
MONO_MT_UINT16, /* "Build" }, */
MONO_MT_UINT16, /* "Revision" }, */
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_STRING_IDX, /* "Culture" }, */
MONO_MT_BLOB_IDX, /* "HashValue" }, */
MONO_MT_END,
#define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
MONO_MT_UINT32, /* "OSPlatformID" }, */
MONO_MT_UINT32, /* "OSMajorVersion" }, */
MONO_MT_UINT32, /* "OSMinorVersion" }, */
MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
MONO_MT_END,
#define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
MONO_MT_UINT32, /* "Processor" }, */
MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
MONO_MT_END,
#define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
MONO_MT_UINT16, /* "PackingSize" }, */
MONO_MT_UINT32, /* "ClassSize" }, */
MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
MONO_MT_END,
#define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
MONO_MT_UINT8, /* "Type" }, */
MONO_MT_UINT8, /* "PaddingZero" }, */
MONO_MT_CONST_IDX, /* "Parent" }, */
MONO_MT_BLOB_IDX, /* "Value" }, */
MONO_MT_END,
#define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
MONO_MT_HASCAT_IDX, /* "Parent" }, */
MONO_MT_CAT_IDX, /* "Type" }, */
MONO_MT_BLOB_IDX, /* "Value" }, */
MONO_MT_END,
#define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
MONO_MT_UINT16, /* "Action" }, */
MONO_MT_HASDEC_IDX, /* "Parent" }, */
MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
MONO_MT_END,
#define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
MONO_MT_END,
#define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_TDOR_IDX, /* "EventType" }, TypeDef or TypeRef or TypeSpec */
MONO_MT_END,
#define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
MONO_MT_TABLE_IDX, /* "Event" }, */
MONO_MT_END,
#define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
MONO_MT_STRING_IDX, /* "TypeName" }, */
MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
MONO_MT_IMPL_IDX, /* "Implementation" }, */
MONO_MT_END,
#define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
MONO_MT_UINT16, /* "Flags" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_END,
#define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
MONO_MT_UINT32, /* "Offset" }, */
MONO_MT_TABLE_IDX, /* "Field:Field" }, */
MONO_MT_END,
#define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
MONO_MT_HFM_IDX, /* "Parent" }, */
MONO_MT_BLOB_IDX, /* "NativeType" }, */
MONO_MT_END,
#define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
MONO_MT_UINT32, /* "RVA" }, */
MONO_MT_TABLE_IDX, /* "Field:Field" }, */
MONO_MT_END,
#define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
MONO_MT_TABLE_IDX, /* "Field" }, */
MONO_MT_END,
#define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_BLOB_IDX, /* "Value" }, */
MONO_MT_END,
#define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
MONO_MT_UINT16, /* "MappingFlag" }, */
MONO_MT_MF_IDX, /* "MemberForwarded" }, */
MONO_MT_STRING_IDX, /* "ImportName" }, */
MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
MONO_MT_END,
#define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
MONO_MT_END,
#define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
MONO_MT_UINT32, /* "Offset" }, */
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_IMPL_IDX, /* "Implementation" }, */
MONO_MT_END,
#define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
MONO_MT_MRP_IDX, /* "Class" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_END,
#define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
MONO_MT_UINT32, /* "RVA" }, */
MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
MONO_MT_END,
#define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
MONO_MT_MDOR_IDX, /* "MethodBody" }, */
MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
MONO_MT_END,
#define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
MONO_MT_UINT16, /* "MethodSemantic" }, */
MONO_MT_TABLE_IDX, /* "Method:Method" }, */
MONO_MT_HS_IDX, /* "Association" }, */
MONO_MT_END,
#define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
MONO_MT_TABLE_IDX, /* "Method" }, */
MONO_MT_END,
#define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
MONO_MT_UINT16, /* "Generation" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_GUID_IDX, /* "MVID" }, */
MONO_MT_GUID_IDX, /* "EncID" }, */
MONO_MT_GUID_IDX, /* "EncBaseID" }, */
MONO_MT_END,
#define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_END,
#define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
MONO_MT_END,
#define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
MONO_MT_UINT16, /* "Flags" }, */
MONO_MT_UINT16, /* "Sequence" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_END,
#define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
MONO_MT_TABLE_IDX, /* "Param" }, */
MONO_MT_END,
#define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
MONO_MT_UINT16, /* "Flags" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_BLOB_IDX, /* "Type" }, */
MONO_MT_END,
#define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
MONO_MT_TABLE_IDX, /* "Property" }, */
MONO_MT_END,
#define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
MONO_MT_END,
#define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_END,
#define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
MONO_MT_UINT32, /* "Flags" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_STRING_IDX, /* "Namespace" }, */
MONO_MT_TDOR_IDX, /* "Extends" }, */
MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
MONO_MT_END,
#define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_STRING_IDX, /* "Namespace" }, */
MONO_MT_END,
#define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_END,
#define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
MONO_MT_UINT16, /* "Number" }, */
MONO_MT_UINT16, /* "Flags" }, */
MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
MONO_MT_STRING_IDX, /* "Name" }, */
MONO_MT_END,
#define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
MONO_MT_MDOR_IDX, /* "Method" }, */
MONO_MT_BLOB_IDX, /* "Signature" }, */
MONO_MT_END,
#define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
MONO_MT_TABLE_IDX, /* "GenericParam" }, */
MONO_MT_TDOR_IDX, /* "Constraint" }, */
MONO_MT_END,
#define DOCUMENT_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
MONO_MT_BLOB_IDX, /* Name */
MONO_MT_GUID_IDX, /* HashAlgorithm */
MONO_MT_BLOB_IDX, /* Hash */
MONO_MT_GUID_IDX, /* Language */
MONO_MT_END,
#define METHODBODY_SCHEMA_OFFSET DOCUMENT_SCHEMA_OFFSET + 5
MONO_MT_BLOB_IDX, /* SequencePoints */
MONO_MT_END,
#define LOCALSCOPE_SCHEMA_OFFSET METHODBODY_SCHEMA_OFFSET + 2
MONO_MT_TABLE_IDX, /* Method */
MONO_MT_TABLE_IDX, /* ImportScope */
MONO_MT_TABLE_IDX, /* VariableList */
MONO_MT_TABLE_IDX, /* ConstantList */
MONO_MT_UINT32, /* StartOffset */
MONO_MT_UINT32, /* Length */
MONO_MT_END,
#define LOCALVARIABLE_SCHEMA_OFFSET LOCALSCOPE_SCHEMA_OFFSET + 7
MONO_MT_UINT16, /* Attributes */
MONO_MT_UINT16, /* Index */
MONO_MT_STRING_IDX, /* Name */
MONO_MT_END,
#define NULL_SCHEMA_OFFSET LOCALVARIABLE_SCHEMA_OFFSET + 4
MONO_MT_END
};
/* Must be the same order as MONO_TABLE_* */
const static unsigned char
table_description [] = {
MODULE_SCHEMA_OFFSET,
TYPEREF_SCHEMA_OFFSET,
TYPEDEF_SCHEMA_OFFSET,
FIELD_POINTER_SCHEMA_OFFSET,
FIELD_SCHEMA_OFFSET,
METHOD_POINTER_SCHEMA_OFFSET,
METHOD_SCHEMA_OFFSET,
PARAM_POINTER_SCHEMA_OFFSET,
PARAM_SCHEMA_OFFSET,
IFACEMAP_SCHEMA_OFFSET,
MEMBERREF_SCHEMA_OFFSET, /* 0xa */
CONSTANT_SCHEMA_OFFSET,
CUSTOM_ATTR_SCHEMA_OFFSET,
FIELD_MARSHAL_SCHEMA_OFFSET,
DECL_SEC_SCHEMA_OFFSET,
CLASS_LAYOUT_SCHEMA_OFFSET,
FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
STDALON_SIG_SCHEMA_OFFSET,
EVENTMAP_SCHEMA_OFFSET,
EVENT_POINTER_SCHEMA_OFFSET,
EVENT_SCHEMA_OFFSET,
PROPERTY_MAP_SCHEMA_OFFSET,
PROPERTY_POINTER_SCHEMA_OFFSET,
PROPERTY_SCHEMA_OFFSET,
METHOD_SEMA_SCHEMA_OFFSET,
METHOD_IMPL_SCHEMA_OFFSET,
MODULEREF_SCHEMA_OFFSET, /* 0x1a */
TYPESPEC_SCHEMA_OFFSET,
IMPLMAP_SCHEMA_OFFSET,
FIELD_RVA_SCHEMA_OFFSET,
NULL_SCHEMA_OFFSET,
NULL_SCHEMA_OFFSET,
ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
ASSEMBLYPROC_SCHEMA_OFFSET,
ASSEMBLYOS_SCHEMA_OFFSET,
ASSEMBLYREF_SCHEMA_OFFSET,
ASSEMBLYREFPROC_SCHEMA_OFFSET,
ASSEMBLYREFOS_SCHEMA_OFFSET,
FILE_SCHEMA_OFFSET,
EXPORTED_TYPE_SCHEMA_OFFSET,
MANIFEST_SCHEMA_OFFSET,
NESTED_CLASS_SCHEMA_OFFSET,
GENPARAM_SCHEMA_OFFSET, /* 0x2a */
METHOD_SPEC_SCHEMA_OFFSET,
GEN_CONSTRAINT_SCHEMA_OFFSET,
NULL_SCHEMA_OFFSET,
NULL_SCHEMA_OFFSET,
NULL_SCHEMA_OFFSET,
DOCUMENT_SCHEMA_OFFSET, /* 0x30 */
METHODBODY_SCHEMA_OFFSET,
LOCALSCOPE_SCHEMA_OFFSET,
LOCALVARIABLE_SCHEMA_OFFSET
};
#ifdef HAVE_ARRAY_ELEM_INIT
#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
#define MSGSTRFIELD1(line) str##line
static const struct msgstr_t {
#define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
#include "mono/cil/tables.def"
#undef TABLEDEF
} tablestr = {
#define TABLEDEF(a,b) b,
#include "mono/cil/tables.def"
#undef TABLEDEF
};
static const gint16 tableidx [] = {
#define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
#include "mono/cil/tables.def"
#undef TABLEDEF
};
#else
#define TABLEDEF(a,b) b,
static const char* const
mono_tables_names [] = {
#include "mono/cil/tables.def"
NULL
};
#endif
/**
* mono_meta_table_name:
* @table: table index
*
* Returns the name of the given ECMA metadata logical format table
* as described in ECMA 335, Partition II, Section 22.
*
* Returns: the name for the @table index
*/
const char *
mono_meta_table_name (int table)
{
if ((table < 0) || (table > MONO_TABLE_LAST))
return "";
#ifdef HAVE_ARRAY_ELEM_INIT
return (const char*)&tablestr + tableidx [table];
#else
return mono_tables_names [table];
#endif
}
/* The guy who wrote the spec for this should not be allowed near a
* computer again.
If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
inverse of this mapping.
*/
#define rtsize(meta,s,b) (((s) < (1 << (b)) ? 2 : 4))
#define idx_size(meta,tableidx) ((meta)->tables [(tableidx)].rows < 65536 ? 2 : 4)
/* Reference: Partition II - 23.2.6 */
/*
* mono_metadata_compute_size:
* @meta: metadata context
* @tableindex: metadata table number
* @result_bitfield: pointer to guint32 where to store additional info
*
* mono_metadata_compute_size() computes the lenght in bytes of a single
* row in a metadata table. The size of each column is encoded in the
* @result_bitfield return value along with the number of columns in the table.
* the resulting bitfield should be handed to the mono_metadata_table_size()
* and mono_metadata_table_count() macros.
* This is a Mono runtime internal only function.
*/
int
mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
{
guint32 bitfield = 0;
int size = 0, field_size = 0;
int i, n, code;
int shift = 0;
const unsigned char *description = TableSchemas + table_description [tableindex];
for (i = 0; (code = description [i]) != MONO_MT_END; i++){
switch (code){
case MONO_MT_UINT32:
field_size = 4; break;
case MONO_MT_UINT16:
field_size = 2; break;
case MONO_MT_UINT8:
field_size = 1; break;
case MONO_MT_BLOB_IDX:
field_size = meta->idx_blob_wide ? 4 : 2; break;
case MONO_MT_STRING_IDX:
field_size = meta->idx_string_wide ? 4 : 2; break;
case MONO_MT_GUID_IDX:
field_size = meta->idx_guid_wide ? 4 : 2; break;
case MONO_MT_TABLE_IDX:
/* Uhm, a table index can point to other tables besides the current one
* so, it's not correct to use the rowcount of the current table to
* get the size for this column - lupus
*/
switch (tableindex) {
case MONO_TABLE_ASSEMBLYREFOS:
g_assert (i == 3);
field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
case MONO_TABLE_ASSEMBLYREFPROCESSOR:
g_assert (i == 1);
field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
case MONO_TABLE_CLASSLAYOUT:
g_assert (i == 2);
field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
case MONO_TABLE_EVENTMAP:
g_assert (i == 0 || i == 1);
field_size = i ? idx_size (meta, MONO_TABLE_EVENT):
idx_size (meta, MONO_TABLE_TYPEDEF);
break;
case MONO_TABLE_EVENT_POINTER:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_EVENT); break;
case MONO_TABLE_EXPORTEDTYPE:
g_assert (i == 1);
/* the index is in another metadata file, so it must be 4 */
field_size = 4; break;
case MONO_TABLE_FIELDLAYOUT:
g_assert (i == 1);
field_size = idx_size (meta, MONO_TABLE_FIELD); break;
case MONO_TABLE_FIELDRVA:
g_assert (i == 1);
field_size = idx_size (meta, MONO_TABLE_FIELD); break;
case MONO_TABLE_FIELD_POINTER:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_FIELD); break;
case MONO_TABLE_IMPLMAP:
g_assert (i == 3);
field_size = idx_size (meta, MONO_TABLE_MODULEREF); break;
case MONO_TABLE_INTERFACEIMPL:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
case MONO_TABLE_METHOD:
g_assert (i == 5);
field_size = idx_size (meta, MONO_TABLE_PARAM); break;
case MONO_TABLE_METHODIMPL:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
case MONO_TABLE_METHODSEMANTICS:
g_assert (i == 1);
field_size = idx_size (meta, MONO_TABLE_METHOD); break;
case MONO_TABLE_METHOD_POINTER:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_METHOD); break;
case MONO_TABLE_NESTEDCLASS:
g_assert (i == 0 || i == 1);
field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
case MONO_TABLE_PARAM_POINTER:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_PARAM); break;
case MONO_TABLE_PROPERTYMAP:
g_assert (i == 0 || i == 1);
field_size = i ? idx_size (meta, MONO_TABLE_PROPERTY):
idx_size (meta, MONO_TABLE_TYPEDEF);
break;
case MONO_TABLE_PROPERTY_POINTER:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_PROPERTY); break;
case MONO_TABLE_TYPEDEF:
g_assert (i == 4 || i == 5);
field_size = i == 4 ? idx_size (meta, MONO_TABLE_FIELD):
idx_size (meta, MONO_TABLE_METHOD);
break;
case MONO_TABLE_GENERICPARAM:
g_assert (i == 2);
n = MAX (meta->tables [MONO_TABLE_METHOD].rows, meta->tables [MONO_TABLE_TYPEDEF].rows);
/*This is a coded token for 2 tables, so takes 1 bit */
field_size = rtsize (meta, n, 16 - MONO_TYPEORMETHOD_BITS);
break;
case MONO_TABLE_GENERICPARAMCONSTRAINT:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_GENERICPARAM);
break;
case MONO_TABLE_LOCALSCOPE:
switch (i) {
case 0:
// FIXME: This table is in another file
field_size = idx_size (meta, MONO_TABLE_METHOD);
break;
case 1:
field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE);
break;
case 2:
field_size = idx_size (meta, MONO_TABLE_LOCALVARIABLE);
break;
case 3:
field_size = idx_size (meta, MONO_TABLE_LOCALCONSTANT);
break;
default:
g_assert_not_reached ();
break;
}
break;
default:
g_error ("Can't handle MONO_MT_TABLE_IDX for table %d element %d", tableindex, i);
}
break;
/*
* HasConstant: ParamDef, FieldDef, Property
*/
case MONO_MT_CONST_IDX:
n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
meta->tables [MONO_TABLE_FIELD].rows);
n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
/* 2 bits to encode tag */
field_size = rtsize (meta, n, 16-2);
break;
/*
* HasCustomAttribute: points to any table but
* itself.
*/
case MONO_MT_HASCAT_IDX:
/*
* We believe that since the signature and
* permission are indexing the Blob heap,
* we should consider the blob size first
*/
/* I'm not a believer - lupus
if (meta->idx_blob_wide){
field_size = 4;
break;
}*/
n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
meta->tables [MONO_TABLE_FIELD].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
/* 5 bits to encode */
field_size = rtsize (meta, n, 16-5);
break;
/*
* CustomAttributeType: TypeDef, TypeRef, MethodDef,
* MemberRef and String.
*/
case MONO_MT_CAT_IDX:
/* String is a heap, if it is wide, we know the size */
/* See above, nope.
if (meta->idx_string_wide){
field_size = 4;
break;
}*/
n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
meta->tables [MONO_TABLE_TYPEDEF].rows);
n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
/* 3 bits to encode */
field_size = rtsize (meta, n, 16-3);
break;
/*
* HasDeclSecurity: Typedef, MethodDef, Assembly
*/
case MONO_MT_HASDEC_IDX:
n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
meta->tables [MONO_TABLE_METHOD].rows);
n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
/* 2 bits to encode */
field_size = rtsize (meta, n, 16-2);
break;
/*
* Implementation: File, AssemblyRef, ExportedType
*/
case MONO_MT_IMPL_IDX:
n = MAX (meta->tables [MONO_TABLE_FILE].rows,
meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
/* 2 bits to encode tag */
field_size = rtsize (meta, n, 16-2);
break;
/*
* HasFieldMarshall: FieldDef, ParamDef
*/
case MONO_MT_HFM_IDX:
n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
meta->tables [MONO_TABLE_PARAM].rows);
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* MemberForwarded: FieldDef, MethodDef
*/
case MONO_MT_MF_IDX:
n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
meta->tables [MONO_TABLE_METHOD].rows);
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* TypeDefOrRef: TypeDef, ParamDef, TypeSpec
* LAMESPEC
* It is TypeDef, _TypeRef_, TypeSpec, instead.
*/
case MONO_MT_TDOR_IDX:
n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
meta->tables [MONO_TABLE_TYPEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
/* 2 bits to encode */
field_size = rtsize (meta, n, 16-2);
break;
/*
* MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
*/
case MONO_MT_MRP_IDX:
n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
meta->tables [MONO_TABLE_TYPEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
/* 3 bits to encode */
field_size = rtsize (meta, n, 16 - 3);
break;
/*
* MethodDefOrRef: MethodDef, MemberRef
*/
case MONO_MT_MDOR_IDX:
n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
meta->tables [MONO_TABLE_MEMBERREF].rows);
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* HasSemantics: Property, Event
*/
case MONO_MT_HS_IDX:
n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
meta->tables [MONO_TABLE_EVENT].rows);
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
*/
case MONO_MT_RS_IDX:
n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
meta->tables [MONO_TABLE_MODULEREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
/* 2 bits used to encode tag (ECMA spec claims 3) */
field_size = rtsize (meta, n, 16 - 2);
break;
}
/*
* encode field size as follows (we just need to
* distinguish them).
*
* 4 -> 3
* 2 -> 1
* 1 -> 0
*/
bitfield |= (field_size-1) << shift;
shift += 2;
size += field_size;
/*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
}
*result_bitfield = (i << 24) | bitfield;
return size;
}
/**
* mono_metadata_compute_table_bases:
* @meta: metadata context to compute table values
*
* Computes the table bases for the metadata structure.
* This is an internal function used by the image loader code.
*/
void
mono_metadata_compute_table_bases (MonoImage *meta)
{
int i;
const char *base = meta->tables_base;
for (i = 0; i < MONO_TABLE_NUM; i++) {
MonoTableInfo *table = &meta->tables [i];
if (table->rows == 0)
continue;
table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
table->base = base;
base += table->rows * table->row_size;
}
}
/**
* mono_metadata_locate:
* @meta: metadata context
* @table: table code.
* @idx: index of element to retrieve from @table.
*
* Returns: a pointer to the @idx element in the metadata table
* whose code is @table.
*/
const char *
mono_metadata_locate (MonoImage *meta, int table, int idx)
{
/* idx == 0 refers always to NULL */
g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, ""); /*FIXME shouldn't we return NULL here?*/
return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
}
/**
* mono_metadata_locate_token:
* @meta: metadata context
* @token: metadata token
*
* Returns: a pointer to the data in the metadata represented by the
* token #token.
*/
const char *
mono_metadata_locate_token (MonoImage *meta, guint32 token)
{
return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
}
/**
* mono_metadata_string_heap:
* @meta: metadata context
* @index: index into the string heap.
*
* Returns: an in-memory pointer to the @index in the string heap.
*/
const char *
mono_metadata_string_heap (MonoImage *meta, guint32 index)
{
g_assert (index < meta->heap_strings.size);
g_return_val_if_fail (index < meta->heap_strings.size, "");
return meta->heap_strings.data + index;
}
/**
* mono_metadata_user_string:
* @meta: metadata context
* @index: index into the user string heap.
*
* Returns: an in-memory pointer to the @index in the user string heap ("#US").
*/
const char *
mono_metadata_user_string (MonoImage *meta, guint32 index)
{
g_assert (index < meta->heap_us.size);
g_return_val_if_fail (index < meta->heap_us.size, "");
return meta->heap_us.data + index;
}
/**
* mono_metadata_blob_heap:
* @meta: metadata context
* @index: index into the blob.
*
* Returns: an in-memory pointer to the @index in the Blob heap.
*/
const char *
mono_metadata_blob_heap (MonoImage *meta, guint32 index)
{
g_assert (index < meta->heap_blob.size);
g_return_val_if_fail (index < meta->heap_blob.size, "");/*FIXME shouldn't we return NULL and check for index == 0?*/
return meta->heap_blob.data + index;
}
/**
* mono_metadata_guid_heap:
* @meta: metadata context
* @index: index into the guid heap.
*
* Returns: an in-memory pointer to the @index in the guid heap.
*/
const char *
mono_metadata_guid_heap (MonoImage *meta, guint32 index)
{
--index;
index *= 16; /* adjust for guid size and 1-based index */
g_return_val_if_fail (index < meta->heap_guid.size, "");
return meta->heap_guid.data + index;
}
static const unsigned char *
dword_align (const unsigned char *ptr)
{
#if SIZEOF_VOID_P == 8
return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
#else
return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
#endif
}
/**
* mono_metadata_decode_row:
* @t: table to extract information from.
* @idx: index in table.
* @res: array of @res_size cols to store the results in
*
* This decompresses the metadata element @idx in table @t
* into the guint32 @res array that has res_size elements
*/
void
mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
{
guint32 bitfield = t->size_bitfield;
int i, count = mono_metadata_table_count (bitfield);
const char *data;
g_assert (idx < t->rows);
g_assert (idx >= 0);
data = t->base + idx * t->row_size;
g_assert (res_size == count);
for (i = 0; i < count; i++) {
int n = mono_metadata_table_size (bitfield, i);
switch (n){
case 1:
res [i] = *data; break;
case 2:
res [i] = read16 (data); break;
case 4:
res [i] = read32 (data); break;
default:
g_assert_not_reached ();
}
data += n;
}
}
/**
* mono_metadata_decode_row_col:
* @t: table to extract information from.
* @idx: index for row in table.
* @col: column in the row.
*
* This function returns the value of column @col from the @idx
* row in the table @t.
*/
guint32
mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
{
guint32 bitfield = t->size_bitfield;
int i;
register const char *data;
register int n;
g_assert (idx < t->rows);
g_assert (col < mono_metadata_table_count (bitfield));
data = t->base + idx * t->row_size;
n = mono_metadata_table_size (bitfield, 0);
for (i = 0; i < col; ++i) {
data += n;
n = mono_metadata_table_size (bitfield, i + 1);
}
switch (n) {
case 1:
return *data;
case 2:
return read16 (data);
case 4:
return read32 (data);
default:
g_assert_not_reached ();
}
return 0;
}
/**
* mono_metadata_decode_blob_size:
* @ptr: pointer to a blob object
* @rptr: the new position of the pointer
*
* This decodes a compressed size as described by 23.1.4 (a blob or user string object)
*
* Returns: the size of the blob object
*/
guint32
mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
{
const unsigned char *ptr = (const unsigned char *)xptr;
guint32 size;
if ((*ptr & 0x80) == 0){
size = ptr [0] & 0x7f;
ptr++;
} else if ((*ptr & 0x40) == 0){
size = ((ptr [0] & 0x3f) << 8) + ptr [1];
ptr += 2;
} else {
size = ((ptr [0] & 0x1f) << 24) +
(ptr [1] << 16) +
(ptr [2] << 8) +
ptr [3];
ptr += 4;
}
if (rptr)
*rptr = (char*)ptr;
return size;
}
/**
* mono_metadata_decode_value:
* @ptr: pointer to decode from
* @rptr: the new position of the pointer
*
* This routine decompresses 32-bit values as specified in the "Blob and
* Signature" section (22.2)
*
* Returns: the decoded value
*/
guint32
mono_metadata_decode_value (const char *_ptr, const char **rptr)
{
const unsigned char *ptr = (const unsigned char *) _ptr;
unsigned char b = *ptr;
guint32 len;
if ((b & 0x80) == 0){
len = b;
++ptr;
} else if ((b & 0x40) == 0){
len = ((b & 0x3f) << 8 | ptr [1]);
ptr += 2;
} else {
len = ((b & 0x1f) << 24) |
(ptr [1] << 16) |
(ptr [2] << 8) |
ptr [3];
ptr += 4;
}
if (rptr)
*rptr = (char*)ptr;
return len;
}
/**
* mono_metadata_decode_signed_value:
* @ptr: pointer to decode from
* @rptr: the new position of the pointer
*
* This routine decompresses 32-bit signed values
* (not specified in the spec)
*
* Returns: the decoded value
*/
gint32
mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
{
guint32 uval = mono_metadata_decode_value (ptr, rptr);
gint32 ival = uval >> 1;
if (!(uval & 1))
return ival;
/* ival is a truncated 2's complement negative number. */
if (ival < 0x40)
/* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
return ival - 0x40;
if (ival < 0x2000)
/* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
return ival - 0x2000;
if (ival < 0x10000000)
/* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
return ival - 0x10000000;
g_assert (ival < 0x20000000);
g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
return ival - 0x20000000;
}
/*
* Translates the given 1-based index into the Method, Field, Event, or Param tables
* using the *Ptr tables in uncompressed metadata, if they are available.
*
* FIXME: The caller is not forced to call this function, which is error-prone, since
* forgetting to call it would only show up as a bug on uncompressed metadata.
*/
guint32
mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
{
if (!image->uncompressed_metadata)
return idx;
switch (table) {
case MONO_TABLE_METHOD:
if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
else
return idx;
case MONO_TABLE_FIELD:
if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
else
return idx;
case MONO_TABLE_EVENT:
if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
else
return idx;
case MONO_TABLE_PROPERTY:
if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
else
return idx;
case MONO_TABLE_PARAM:
if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
else
return idx;
default:
return idx;
}
}
/**
* mono_metadata_decode_table_row:
*
* Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
* uncompressed metadata into account, so it should be used to access the
* Method, Field, Param and Event tables when the access is made from metadata, i.e.
* IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
*/
void
mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
{
if (image->uncompressed_metadata)
idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
}
/**
* mono_metadata_decode_table_row_col:
*
* Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
* uncompressed metadata into account, so it should be used to access the
* Method, Field, Param and Event tables.
*/
guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
{
if (image->uncompressed_metadata)
idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
return mono_metadata_decode_row_col (&image->tables [table], idx, col);
}
/*
* mono_metadata_parse_typedef_or_ref:
* @m: a metadata context.
* @ptr: a pointer to an encoded TypedefOrRef in @m
* @rptr: pointer updated to match the end of the decoded stream
*
* Returns: a token valid in the @m metadata decoded from
* the compressed representation.
*/
guint32
mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
{
guint32 token;
token = mono_metadata_decode_value (ptr, &ptr);
if (rptr)
*rptr = ptr;
return mono_metadata_token_from_dor (token);
}
/*
* mono_metadata_parse_custom_mod:
* @m: a metadata context.
* @dest: storage where the info about the custom modifier is stored (may be NULL)
* @ptr: a pointer to (possibly) the start of a custom modifier list
* @rptr: pointer updated to match the end of the decoded stream
*
* Checks if @ptr points to a type custom modifier compressed representation.
*
* Returns: #TRUE if a custom modifier was found, #FALSE if not.
*/
int
mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
{
MonoCustomMod local;
if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
if (!dest)
dest = &local;
dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
return TRUE;
}
return FALSE;
}
/*
* mono_metadata_parse_array_internal:
* @m: a metadata context.
* @transient: whenever to allocate data from the heap
* @ptr: a pointer to an encoded array description.
* @rptr: pointer updated to match the end of the decoded stream
*
* Decodes the compressed array description found in the metadata @m at @ptr.
*
* Returns: a #MonoArrayType structure describing the array type
* and dimensions. Memory is allocated from the heap or from the image mempool, depending
* on the value of @transient.
*
* LOCKING: Acquires the loader lock
*/
static MonoArrayType *
mono_metadata_parse_array_internal (MonoImage *m, MonoGenericContainer *container,
gboolean transient, const char *ptr, const char **rptr)
{
int i;
MonoArrayType *array;
MonoType *etype;
array = transient ? g_malloc0 (sizeof (MonoArrayType)) : mono_image_alloc0 (m, sizeof (MonoArrayType));
etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
if (!etype)
return NULL;
array->eklass = mono_class_from_mono_type (etype);
array->rank = mono_metadata_decode_value (ptr, &ptr);
array->numsizes = mono_metadata_decode_value (ptr, &ptr);
if (array->numsizes)
array->sizes = transient ? g_malloc0 (sizeof (int) * array->numsizes) : mono_image_alloc0 (m, sizeof (int) * array->numsizes);
for (i = 0; i < array->numsizes; ++i)
array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
if (array->numlobounds)
array->lobounds = transient ? g_malloc0 (sizeof (int) * array->numlobounds) : mono_image_alloc0 (m, sizeof (int) * array->numlobounds);
for (i = 0; i < array->numlobounds; ++i)
array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
if (rptr)
*rptr = ptr;
return array;
}
MonoArrayType *
mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
const char *ptr, const char **rptr)
{
return mono_metadata_parse_array_internal (m, container, FALSE, ptr, rptr);
}
MonoArrayType *
mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
{
return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
}
/*
* mono_metadata_free_array:
* @array: array description
*
* Frees the array description returned from mono_metadata_parse_array().
*/
void
mono_metadata_free_array (MonoArrayType *array)
{
g_free (array->sizes);
g_free (array->lobounds);
g_free (array);
}
/*
* need to add common field and param attributes combinations:
* [out] param
* public static
* public static literal
* private
* private static
* private static literal
*/
static const MonoType
builtin_types[] = {
/* data, attrs, type, nmods, byref, pinned */
{{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
{{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
{{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
};
#define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
static GHashTable *type_cache = NULL;
static int next_generic_inst_id = 0;
/* Protected by image_sets_mutex */
static MonoImageSet *mscorlib_image_set;
/* Protected by image_sets_mutex */
static GPtrArray *image_sets;
static mono_mutex_t image_sets_mutex;
static guint mono_generic_class_hash (gconstpointer data);
/*
* MonoTypes with modifies are never cached, so we never check or use that field.
*/
static guint
mono_type_hash (gconstpointer data)
{
const MonoType *type = (const MonoType *) data;
if (type->type == MONO_TYPE_GENERICINST)
return mono_generic_class_hash (type->data.generic_class);
else
return type->type | (type->byref << 8) | (type->attrs << 9);
}
static gint
mono_type_equal (gconstpointer ka, gconstpointer kb)
{
const MonoType *a = (const MonoType *) ka;
const MonoType *b = (const MonoType *) kb;
if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
return 0;
/* need other checks */
return 1;
}
guint
mono_metadata_generic_inst_hash (gconstpointer data)
{
const MonoGenericInst *ginst = (const MonoGenericInst *) data;
guint hash = 0;
int i;
for (i = 0; i < ginst->type_argc; ++i) {
hash *= 13;
hash += mono_metadata_type_hash (ginst->type_argv [i]);
}
return hash ^ (ginst->is_open << 8);
}
static gboolean
mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
{
int i;
#ifndef MONO_SMALL_CONFIG
if (a->id && b->id) {
if (a->id == b->id)
return TRUE;
if (!signature_only)
return FALSE;
}
#endif
if (a->is_open != b->is_open || a->type_argc != b->type_argc)
return FALSE;
for (i = 0; i < a->type_argc; ++i) {
if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
return FALSE;
}
return TRUE;
}
gboolean
mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
{
const MonoGenericInst *a = (const MonoGenericInst *) ka;
const MonoGenericInst *b = (const MonoGenericInst *) kb;
return mono_generic_inst_equal_full (a, b, FALSE);
}
static guint
mono_generic_class_hash (gconstpointer data)
{
const MonoGenericClass *gclass = (const MonoGenericClass *) data;
guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
hash *= 13;
hash += gclass->is_tb_open;
hash += mono_metadata_generic_context_hash (&gclass->context);
return hash;
}
static gboolean
mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
{
const MonoGenericClass *a = (const MonoGenericClass *) ka;
const MonoGenericClass *b = (const MonoGenericClass *) kb;
return _mono_metadata_generic_class_equal (a, b, FALSE);
}
/**
* mono_metadata_init:
*
* Initialize the global variables of this module.
* This is a Mono runtime internal function.
*/
void
mono_metadata_init (void)
{
int i;
type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
for (i = 0; i < NBUILTIN_TYPES (); ++i)
g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
mono_mutex_init_recursive (&image_sets_mutex);
}
/**
* mono_metadata_cleanup:
*
* Free all resources used by this module.
* This is a Mono runtime internal function.
*/
void
mono_metadata_cleanup (void)
{
g_hash_table_destroy (type_cache);
type_cache = NULL;
g_ptr_array_free (image_sets, TRUE);
image_sets = NULL;
mono_mutex_destroy (&image_sets_mutex);
}
/**
* mono_metadata_parse_type:
* @m: metadata context
* @mode: king of type that may be found at @ptr
* @opt_attrs: optional attributes to store in the returned type
* @ptr: pointer to the type representation
* @rptr: pointer updated to match the end of the decoded stream
* @transient: whenever to allocate the result from the heap or from a mempool
*
* Decode a compressed type description found at @ptr in @m.
* @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
* MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
* This function can be used to decode type descriptions in method signatures,
* field signatures, locals signatures etc.
*
* To parse a generic type, `generic_container' points to the current class'es
* (the `generic_container' field in the MonoClass) or the current generic method's
* (stored in image->property_hash) generic container.
* When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
* this MonoGenericContainer.
*
* LOCKING: Acquires the loader lock.
*
* Returns: a #MonoType structure representing the decoded type.
*/
static MonoType*
mono_metadata_parse_type_internal (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
short opt_attrs, gboolean transient, const char *ptr, const char **rptr)
{
MonoType *type, *cached;
MonoType stype;
gboolean byref = FALSE;
gboolean pinned = FALSE;
const char *tmp_ptr;
int count = 0;
gboolean found;
/*
* According to the spec, custom modifiers should come before the byref
* flag, but the IL produced by ilasm from the following signature:
* object modopt(...) &
* starts with a byref flag, followed by the modifiers. (bug #49802)
* Also, this type seems to be different from 'object & modopt(...)'. Maybe
* it would be better to treat byref as real type constructor instead of
* a modifier...
* Also, pinned should come before anything else, but some MSV++ produced
* assemblies violate this (#bug 61990).
*/
/* Count the modifiers first */
tmp_ptr = ptr;
found = TRUE;
while (found) {
switch (*tmp_ptr) {
case MONO_TYPE_PINNED:
case MONO_TYPE_BYREF:
++tmp_ptr;
break;
case MONO_TYPE_CMOD_REQD:
case MONO_TYPE_CMOD_OPT:
count ++;
mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
break;
default:
found = FALSE;
}
}
if (count) {
int size;
size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
type = transient ? g_malloc0 (size) : mono_image_alloc0 (m, size);
type->num_mods = count;
if (count > 64)
g_warning ("got more than 64 modifiers in type");
} else {
type = &stype;
memset (type, 0, MONO_SIZEOF_TYPE);
}
/* Parse pinned, byref and custom modifiers */
found = TRUE;
count = 0;
while (found) {
switch (*ptr) {
case MONO_TYPE_PINNED:
pinned = TRUE;
++ptr;
break;
case MONO_TYPE_BYREF:
byref = TRUE;
++ptr;
break;
case MONO_TYPE_CMOD_REQD:
case MONO_TYPE_CMOD_OPT:
mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
count ++;
break;
default:
found = FALSE;
}
}
type->attrs = opt_attrs;
type->byref = byref;
type->pinned = pinned ? 1 : 0;
if (!do_mono_metadata_parse_type (type, m, container, transient, ptr, &ptr)) {
return NULL;
}
if (rptr)
*rptr = ptr;
if (!type->num_mods && !transient) {
/* no need to free type here, because it is on the stack */
if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
/* Consider the case:
class Foo<T> { class Bar {} }
class Test : Foo<Test>.Bar {}
When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
We ensure that the MonoClass is in a state that we can canonicalize to:
klass->byval_arg.data.klass == klass
klass->this_arg.data.klass == klass
If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
of a MonoClass which currently holds the loader lock. 'type' is local.
*/
if (ret->data.klass == type->data.klass) {
return ret;
}
}
/* No need to use locking since nobody is modifying the hash table */
if ((cached = g_hash_table_lookup (type_cache, type))) {
return cached;
}
}
/* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
if (type == &stype) {
type = transient ? g_malloc (MONO_SIZEOF_TYPE) : mono_image_alloc (m, MONO_SIZEOF_TYPE);
memcpy (type, &stype, MONO_SIZEOF_TYPE);
}
return type;
}
MonoType*
mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
short opt_attrs, const char *ptr, const char **rptr)
{
return mono_metadata_parse_type_internal (m, container, mode, opt_attrs, FALSE, ptr, rptr);
}
/*
* LOCKING: Acquires the loader lock.
*/
MonoType*
mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
const char *ptr, const char **rptr)
{
return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
}
gboolean
mono_metadata_method_has_param_attrs (MonoImage *m, int def)
{
MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
if (def < methodt->rows)
lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
else
lastp = m->tables [MONO_TABLE_PARAM].rows + 1;
for (i = param_index; i < lastp; ++i) {
guint32 flags = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_FLAGS);
if (flags)
return TRUE;
}
return FALSE;
}
/*
* mono_metadata_get_param_attrs:
*
* @m The image to loader parameter attributes from
* @def method def token (one based)
* @param_count number of params to decode including the return value
*
* Return the parameter attributes for the method whose MethodDef index is DEF. The
* returned memory needs to be freed by the caller. If all the param attributes are
* 0, then NULL is returned.
*/
int*
mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count)
{
MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
guint32 cols [MONO_PARAM_SIZE];
guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
int *pattrs = NULL;
if (def < methodt->rows)
lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
else
lastp = paramt->rows + 1;
for (i = param_index; i < lastp; ++i) {
mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
if (cols [MONO_PARAM_FLAGS]) {
if (!pattrs)
pattrs = g_new0 (int, param_count);
/* at runtime we just ignore this kind of malformed file:
* the verifier can signal the error to the user
*/
if (cols [MONO_PARAM_SEQUENCE] >= param_count)
continue;
pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
}
}
return pattrs;
}
/*
* mono_metadata_parse_signature:
* @image: metadata context
* @toke: metadata token
*
* Decode a method signature stored in the STANDALONESIG table
*
* Returns: a MonoMethodSignature describing the signature.
*/
MonoMethodSignature*
mono_metadata_parse_signature (MonoImage *image, guint32 token)
{
MonoError error;
MonoMethodSignature *ret;
MonoTableInfo *tables = image->tables;
guint32 idx = mono_metadata_token_index (token);
guint32 sig;
const char *ptr;
if (image_is_dynamic (image))
return mono_lookup_dynamic_token (image, token, NULL);
g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
ptr = mono_metadata_blob_heap (image, sig);
mono_metadata_decode_blob_size (ptr, &ptr);
ret = mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, &error);
mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
return ret;
}
/*
* mono_metadata_signature_alloc:
* @image: metadata context
* @nparmas: number of parameters in the signature
*
* Allocate a MonoMethodSignature structure with the specified number of params.
* The return type and the params types need to be filled later.
* This is a Mono runtime internal function.
*
* LOCKING: Assumes the loader lock is held.
*
* Returns: the new MonoMethodSignature structure.
*/
MonoMethodSignature*
mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
{
MonoMethodSignature *sig;
sig = mono_image_alloc0 (m, MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)nparams) * sizeof (MonoType*));
sig->param_count = nparams;
sig->sentinelpos = -1;
return sig;
}
static MonoMethodSignature*
mono_metadata_signature_dup_internal_with_padding (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig, size_t padding)
{
int sigsize, sig_header_size;
MonoMethodSignature *ret;
sigsize = sig_header_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *) + padding;
if (sig->ret)
sigsize += MONO_SIZEOF_TYPE;
if (image) {
ret = mono_image_alloc (image, sigsize);
} else if (mp) {
ret = mono_mempool_alloc (mp, sigsize);
} else {
ret = g_malloc (sigsize);
}
memcpy (ret, sig, sig_header_size - padding);
// Copy return value because of ownership semantics.
if (sig->ret) {
// Danger! Do not alter padding use without changing the dup_add_this below
intptr_t end_of_header = (intptr_t)( (char*)(ret) + sig_header_size);
ret->ret = (MonoType *)end_of_header;
memcpy (ret->ret, sig->ret, MONO_SIZEOF_TYPE);
}
return ret;
}
static MonoMethodSignature*
mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
{
return mono_metadata_signature_dup_internal_with_padding (image, mp, sig, 0);
}
/*
* signature_dup_add_this:
*
* Make a copy of @sig, adding an explicit this argument.
*/
MonoMethodSignature*
mono_metadata_signature_dup_add_this (MonoImage *image, MonoMethodSignature *sig, MonoClass *klass)
{
MonoMethodSignature *ret;
ret = mono_metadata_signature_dup_internal_with_padding (image, NULL, sig, sizeof (MonoType *));
ret->param_count = sig->param_count + 1;
ret->hasthis = FALSE;
for (int i = sig->param_count - 1; i >= 0; i --)
ret->params [i + 1] = sig->params [i];
ret->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
for (int i = sig->param_count - 1; i >= 0; i --)
g_assert(ret->params [i + 1]->type == sig->params [i]->type && ret->params [i+1]->type != MONO_TYPE_END);
g_assert (ret->ret->type == sig->ret->type && ret->ret->type != MONO_TYPE_END);
return ret;
}
MonoMethodSignature*
mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
{
MonoMethodSignature *ret = mono_metadata_signature_dup_internal (image, NULL, sig);
for (int i = 0 ; i < sig->param_count; i ++)
g_assert(ret->params [i]->type == sig->params [i]->type);
g_assert (ret->ret->type == sig->ret->type);
return ret;
}
/*The mempool is accessed without synchronization*/
MonoMethodSignature*
mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
{
return mono_metadata_signature_dup_internal (NULL, mp, sig);
}
/*
* mono_metadata_signature_dup:
* @sig: method signature
*
* Duplicate an existing MonoMethodSignature so it can be modified.
* This is a Mono runtime internal function.
*
* Returns: the new MonoMethodSignature structure.
*/
MonoMethodSignature*
mono_metadata_signature_dup (MonoMethodSignature *sig)
{
return mono_metadata_signature_dup_full (NULL, sig);
}
/*
* mono_metadata_signature_size:
*
* Return the amount of memory allocated to SIG.
*/
guint32
mono_metadata_signature_size (MonoMethodSignature *sig)
{
return MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
}
/*
* mono_metadata_parse_method_signature:
* @m: metadata context
* @generic_container: generics container
* @def: the MethodDef index or 0 for Ref signatures.
* @ptr: pointer to the signature metadata representation
* @rptr: pointer updated to match the end of the decoded stream
*
* Decode a method signature stored at @ptr.
* This is a Mono runtime internal function.
*
* LOCKING: Assumes the loader lock is held.
*
* Returns: a MonoMethodSignature describing the signature.
*/
MonoMethodSignature *
mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
int def, const char *ptr, const char **rptr, MonoError *error)
{
MonoMethodSignature *method;
int i, *pattrs = NULL;
guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
guint32 gen_param_count = 0;
gboolean is_open = FALSE;
mono_error_init (error);
if (*ptr & 0x10)
gen_param_count = 1;
if (*ptr & 0x20)
hasthis = 1;
if (*ptr & 0x40)
explicit_this = 1;
call_convention = *ptr & 0x0F;
ptr++;
if (gen_param_count)
gen_param_count = mono_metadata_decode_value (ptr, &ptr);
param_count = mono_metadata_decode_value (ptr, &ptr);
if (def)
pattrs = mono_metadata_get_param_attrs (m, def, param_count + 1); /*Must be + 1 since signature's param count doesn't account for the return value */
method = mono_metadata_signature_alloc (m, param_count);
method->hasthis = hasthis;
method->explicit_this = explicit_this;
method->call_convention = call_convention;
method->generic_param_count = gen_param_count;
if (call_convention != 0xa) {
method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
if (!method->ret) {
mono_metadata_free_method_signature (method);
g_free (pattrs);
if (mono_loader_get_last_error ())
mono_error_set_from_loader_error (error);
else
mono_error_set_bad_image (error, m, "Could not parse return type signature");
return NULL;
}
is_open = mono_class_is_open_constructed_type (method->ret);
}
for (i = 0; i < method->param_count; ++i) {
if (*ptr == MONO_TYPE_SENTINEL) {
if (method->call_convention != MONO_CALL_VARARG || def) {
mono_loader_assert_no_error ();
mono_error_set_bad_image (error, m, "Found sentinel for methoddef or no vararg");
g_free (pattrs);
return NULL;
}
if (method->sentinelpos >= 0) {
mono_loader_assert_no_error ();
mono_error_set_bad_image (error, m, "Found sentinel twice in the same signature.");
g_free (pattrs);
return NULL;
}
method->sentinelpos = i;
ptr++;
}
method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
if (!method->params [i]) {
if (mono_loader_get_last_error ())
mono_error_set_from_loader_error (error);
else
mono_error_set_bad_image (error, m, "Could not parse type argument %d on method signature", i);
mono_metadata_free_method_signature (method);
g_free (pattrs);
return NULL;
}
if (!is_open)
is_open = mono_class_is_open_constructed_type (method->params [i]);
}
/* The sentinel could be missing if the caller does not pass any additional arguments */
if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
method->sentinelpos = method->param_count;
method->has_type_parameters = is_open;
if (def && (method->call_convention == MONO_CALL_VARARG))
method->sentinelpos = method->param_count;
g_free (pattrs);
if (rptr)
*rptr = ptr;
/*
* Add signature to a cache and increase ref count...
*/
mono_loader_assert_no_error ();
return method;
}
/*
* mono_metadata_parse_method_signature:
* @m: metadata context
* @def: the MethodDef index or 0 for Ref signatures.
* @ptr: pointer to the signature metadata representation
* @rptr: pointer updated to match the end of the decoded stream
*
* Decode a method signature stored at @ptr.
* This is a Mono runtime internal function.
*
* LOCKING: Assumes the loader lock is held.
*
* Returns: a MonoMethodSignature describing the signature.
*/
MonoMethodSignature *
mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
{
MonoError error;
MonoMethodSignature *ret;
ret = mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr, &error);
if (!ret) {
mono_loader_set_error_from_mono_error (&error);
mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
}
return ret;
}
/*
* mono_metadata_free_method_signature:
* @sig: signature to destroy
*
* Free the memory allocated in the signature @sig.
* This method needs to be robust and work also on partially-built
* signatures, so it does extra checks.
*/
void
mono_metadata_free_method_signature (MonoMethodSignature *sig)
{
/* Everything is allocated from mempools */
/*
int i;
if (sig->ret)
mono_metadata_free_type (sig->ret);
for (i = 0; i < sig->param_count; ++i) {
if (sig->params [i])
mono_metadata_free_type (sig->params [i]);
}
*/
}
void
mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
{
int i;
/* Allocated in inflate_generic_signature () */
if (sig->ret)
mono_metadata_free_type (sig->ret);
for (i = 0; i < sig->param_count; ++i) {
if (sig->params [i])
mono_metadata_free_type (sig->params [i]);
}
g_free (sig);
}
static gboolean
inflated_method_equal (gconstpointer a, gconstpointer b)
{
const MonoMethodInflated *ma = a;
const MonoMethodInflated *mb = b;
if (ma->declaring != mb->declaring)
return FALSE;
return mono_metadata_generic_context_equal (&ma->context, &mb->context);
}
static guint
inflated_method_hash (gconstpointer a)
{
const MonoMethodInflated *ma = a;
return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring));
}
static gboolean
inflated_signature_equal (gconstpointer a, gconstpointer b)
{
const MonoInflatedMethodSignature *sig1 = a;
const MonoInflatedMethodSignature *sig2 = b;
/* sig->sig is assumed to be canonized */
if (sig1->sig != sig2->sig)
return FALSE;
/* The generic instances are canonized */
return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
}
static guint
inflated_signature_hash (gconstpointer a)
{
const MonoInflatedMethodSignature *sig = a;
/* sig->sig is assumed to be canonized */
return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
}
/*static void
dump_ginst (MonoGenericInst *ginst)
{
int i;
char *name;
g_print ("Ginst: <");
for (i = 0; i < ginst->type_argc; ++i) {
if (i != 0)
g_print (", ");
name = mono_type_get_name (ginst->type_argv [i]);
g_print ("%s", name);
g_free (name);
}
g_print (">");
}*/
static gboolean type_in_image (MonoType *type, MonoImage *image);
static gboolean
signature_in_image (MonoMethodSignature *sig, MonoImage *image)
{
gpointer iter = NULL;
MonoType *p;
while ((p = mono_signature_get_params (sig, &iter)) != NULL)
if (type_in_image (p, image))
return TRUE;
return type_in_image (mono_signature_get_return_type (sig), image);
}
static gboolean
ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
{
int i;
for (i = 0; i < ginst->type_argc; ++i) {
if (type_in_image (ginst->type_argv [i], image))
return TRUE;
}
return FALSE;
}
static gboolean
gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
{
return gclass->container_class->image == image ||
ginst_in_image (gclass->context.class_inst, image);
}
static gboolean
type_in_image (MonoType *type, MonoImage *image)
{
retry:
switch (type->type) {
case MONO_TYPE_GENERICINST:
return gclass_in_image (type->data.generic_class, image);
case MONO_TYPE_PTR:
type = type->data.type;
goto retry;
case MONO_TYPE_SZARRAY:
type = &type->data.klass->byval_arg;
goto retry;
case MONO_TYPE_ARRAY:
type = &type->data.array->eklass->byval_arg;
goto retry;
case MONO_TYPE_FNPTR:
return signature_in_image (type->data.method, image);
case MONO_TYPE_VAR: {
MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
if (container) {
g_assert (!container->is_method);
/*
* FIXME: The following check is here solely
* for monodis, which uses the internal
* function
* mono_metadata_load_generic_params(). The
* caller of that function needs to fill in
* owner->klass or owner->method of the
* returned struct, but monodis doesn't do
* that. The image unloading depends on that,
* however, so a crash results without this
* check.
*/
if (!container->owner.klass)
return container->image == image;
return container->owner.klass->image == image;
} else {
return type->data.generic_param->image == image;
}
}
case MONO_TYPE_MVAR: {
MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
if (type->data.generic_param->image == image)
return TRUE;
if (container) {
g_assert (container->is_method);
if (!container->owner.method)
/* RefEmit created generic param whose method is not finished */
return container->image == image;
return container->owner.method->klass->image == image;
} else {
return type->data.generic_param->image == image;
}
}
default:
/* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
return image == mono_class_from_mono_type (type)->image;
}
}
static inline void
image_sets_lock (void)
{
mono_mutex_lock (&image_sets_mutex);
}
static inline void
image_sets_unlock (void)
{
mono_mutex_unlock (&image_sets_mutex);
}
/*
* get_image_set:
*
* Return a MonoImageSet representing the set of images in IMAGES.
*/
static MonoImageSet*
get_image_set (MonoImage **images, int nimages)
{
int i, j, k;
MonoImageSet *set;
GSList *l;
/* Common case */
if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
return mscorlib_image_set;
/* Happens with empty generic instances */
if (nimages == 0)
return mscorlib_image_set;
image_sets_lock ();
if (!image_sets)
image_sets = g_ptr_array_new ();
if (images [0] == mono_defaults.corlib && nimages > 1)
l = images [1]->image_sets;
else
l = images [0]->image_sets;
set = NULL;
for (; l; l = l->next) {
set = l->data;
if (set->nimages == nimages) {
for (j = 0; j < nimages; ++j) {
for (k = 0; k < nimages; ++k)
if (set->images [k] == images [j])
break;
if (k == nimages)
/* Not found */
break;
}
if (j == nimages)
/* Found */
break;
}
}
if (!l) {
/* Not found */
set = g_new0 (MonoImageSet, 1);
set->nimages = nimages;
set->images = g_new0 (MonoImage*, nimages);
mono_mutex_init_recursive (&set->lock);
for (i = 0; i < nimages; ++i)
set->images [i] = images [i];
set->gclass_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
set->ginst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
set->gmethod_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
set->gsignature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
for (i = 0; i < nimages; ++i)
set->images [i]->image_sets = g_slist_prepend (set->images [i]->image_sets, set);
g_ptr_array_add (image_sets, set);
}
if (nimages == 1 && images [0] == mono_defaults.corlib) {
mono_memory_barrier ();
mscorlib_image_set = set;
}
image_sets_unlock ();
return set;
}
static void
delete_image_set (MonoImageSet *set)
{
int i;
g_hash_table_destroy (set->gclass_cache);
g_hash_table_destroy (set->ginst_cache);
g_hash_table_destroy (set->gmethod_cache);
g_hash_table_destroy (set->gsignature_cache);
mono_wrapper_caches_free (&set->wrapper_caches);
image_sets_lock ();
for (i = 0; i < set->nimages; ++i)
set->images [i]->image_sets = g_slist_remove (set->images [i]->image_sets, set);
g_ptr_array_remove (image_sets, set);
image_sets_unlock ();
if (set->mempool)
mono_mempool_destroy (set->mempool);
g_free (set->images);
mono_mutex_destroy (&set->lock);
g_free (set);
}
void
mono_image_set_lock (MonoImageSet *set)
{
mono_mutex_lock (&set->lock);
}
void
mono_image_set_unlock (MonoImageSet *set)
{
mono_mutex_unlock (&set->lock);
}
gpointer
mono_image_set_alloc (MonoImageSet *set, guint size)
{
gpointer res;
mono_image_set_lock (set);
if (!set->mempool)
set->mempool = mono_mempool_new_size (1024);
res = mono_mempool_alloc (set->mempool, size);
mono_image_set_unlock (set);
return res;
}
gpointer
mono_image_set_alloc0 (MonoImageSet *set, guint size)
{
gpointer res;
mono_image_set_lock (set);
if (!set->mempool)
set->mempool = mono_mempool_new_size (1024);
res = mono_mempool_alloc0 (set->mempool, size);
mono_image_set_unlock (set);
return res;
}
char*
mono_image_set_strdup (MonoImageSet *set, const char *s)
{
char *res;
mono_image_set_lock (set);
if (!set->mempool)
set->mempool = mono_mempool_new_size (1024);
res = mono_mempool_strdup (set->mempool, s);
mono_image_set_unlock (set);
return res;
}
/*
* Structure used by the collect_..._images functions to store the image list.
*/
typedef struct {
MonoImage *image_buf [64];
MonoImage **images;
int nimages, images_len;
} CollectData;
static void
collect_data_init (CollectData *data)
{
data->images = data->image_buf;
data->images_len = 64;
data->nimages = 0;
}
static void
collect_data_free (CollectData *data)
{
if (data->images != data->image_buf)
g_free (data->images);
}
static void
enlarge_data (CollectData *data)
{
int new_len = data->images_len < 16 ? 16 : data->images_len * 2;
MonoImage **d = g_new (MonoImage *, new_len);
// FIXME: test this
g_assert_not_reached ();
memcpy (d, data->images, data->images_len);
if (data->images != data->image_buf)
g_free (data->images);
data->images = d;
data->images_len = new_len;
}
static inline void
add_image (MonoImage *image, CollectData *data)
{
int i;
/* The arrays are small, so use a linear search instead of a hash table */
for (i = 0; i < data->nimages; ++i)
if (data->images [i] == image)
return;
if (data->nimages == data->images_len)
enlarge_data (data);
data->images [data->nimages ++] = image;
}
static void
collect_type_images (MonoType *type, CollectData *data);
static void
collect_ginst_images (MonoGenericInst *ginst, CollectData *data)
{
int i;
for (i = 0; i < ginst->type_argc; ++i) {
collect_type_images (ginst->type_argv [i], data);
}
}
static void
collect_gclass_images (MonoGenericClass *gclass, CollectData *data)
{
add_image (gclass->container_class->image, data);
if (gclass->context.class_inst)
collect_ginst_images (gclass->context.class_inst, data);
}
static void
collect_signature_images (MonoMethodSignature *sig, CollectData *data)
{
gpointer iter = NULL;
MonoType *p;
collect_type_images (mono_signature_get_return_type (sig), data);
while ((p = mono_signature_get_params (sig, &iter)) != NULL)
collect_type_images (p, data);
}
static void
collect_inflated_signature_images (MonoInflatedMethodSignature *sig, CollectData *data)
{
collect_signature_images (sig->sig, data);
if (sig->context.class_inst)
collect_ginst_images (sig->context.class_inst, data);
if (sig->context.method_inst)
collect_ginst_images (sig->context.method_inst, data);
}
static void
collect_method_images (MonoMethodInflated *method, CollectData *data)
{
MonoMethod *m = method->declaring;
add_image (method->declaring->klass->image, data);
if (method->context.class_inst)
collect_ginst_images (method->context.class_inst, data);
if (method->context.method_inst)
collect_ginst_images (method->context.method_inst, data);
/*
* Dynamic assemblies have no references, so the images they depend on can be unloaded before them.
*/
if (image_is_dynamic (m->klass->image))
collect_signature_images (mono_method_signature (m), data);
}
static void
collect_type_images (MonoType *type, CollectData *data)
{
retry:
switch (type->type) {
case MONO_TYPE_GENERICINST:
collect_gclass_images (type->data.generic_class, data);
break;
case MONO_TYPE_PTR:
type = type->data.type;
goto retry;
case MONO_TYPE_SZARRAY:
type = &type->data.klass->byval_arg;
goto retry;
case MONO_TYPE_ARRAY:
type = &type->data.array->eklass->byval_arg;
goto retry;
case MONO_TYPE_FNPTR:
//return signature_in_image (type->data.method, image);
g_assert_not_reached ();
case MONO_TYPE_VAR: {
MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
if (container) {
g_assert (!container->is_method);
/*
* FIXME: The following check is here solely
* for monodis, which uses the internal
* function
* mono_metadata_load_generic_params(). The
* caller of that function needs to fill in
* owner->klass or owner->method of the
* returned struct, but monodis doesn't do
* that. The image unloading depends on that,
* however, so a crash results without this
* check.
*/
if (!container->owner.klass)
add_image (container->image, data);
else
add_image (container->owner.klass->image, data);
} else {
add_image (type->data.generic_param->image, data);
}
}
break;
case MONO_TYPE_MVAR: {
MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
if (type->data.generic_param->image)
add_image (type->data.generic_param->image, data);
if (container) {
if (!container->owner.method) {
/* RefEmit created generic param whose method is not finished */
add_image (container->image, data);
} else {
g_assert (container->is_method);
add_image (container->owner.method->klass->image, data);
}
} else {
add_image (type->data.generic_param->image, data);
}
}
break;
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
add_image (mono_class_from_mono_type (type)->image, data);
break;
default:
add_image (mono_defaults.corlib, data);
}
}
typedef struct {
MonoImage *image;
GSList *list;
} CleanForImageUserData;
static gboolean
steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
{
MonoGenericClass *gclass = key;
CleanForImageUserData *user_data = data;
g_assert (gclass_in_image (gclass, user_data->image));
user_data->list = g_slist_prepend (user_data->list, gclass);
return TRUE;
}
static gboolean
steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
{
MonoGenericInst *ginst = key;
CleanForImageUserData *user_data = data;
// This doesn't work during corlib compilation
//g_assert (ginst_in_image (ginst, user_data->image));
user_data->list = g_slist_prepend (user_data->list, ginst);
return TRUE;
}
static gboolean
inflated_method_in_image (gpointer key, gpointer value, gpointer data)
{
MonoImage *image = data;
MonoMethodInflated *method = key;
// FIXME:
// https://bugzilla.novell.com/show_bug.cgi?id=458168
g_assert (method->declaring->klass->image == image ||
(method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
(method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image)));
return TRUE;
}
static gboolean
inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
{
MonoImage *image = data;
MonoInflatedMethodSignature *sig = key;
return signature_in_image (sig->sig, image) ||
(sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
(sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
}
static void
check_gmethod (gpointer key, gpointer value, gpointer data)
{
MonoMethodInflated *method = key;
MonoImage *image = data;
if (method->context.class_inst)
g_assert (!ginst_in_image (method->context.class_inst, image));
if (method->context.method_inst)
g_assert (!ginst_in_image (method->context.method_inst, image));
if (((MonoMethod*)method)->signature)
g_assert (!signature_in_image (mono_method_signature ((MonoMethod*)method), image));
}
/*
* check_image_sets:
*
* Run a consistency check on the image set data structures.
*/
static G_GNUC_UNUSED void
check_image_sets (MonoImage *image)
{
int i;
GSList *l = image->image_sets;
if (!image_sets)
return;
for (i = 0; i < image_sets->len; ++i) {
MonoImageSet *set = g_ptr_array_index (image_sets, i);
if (!g_slist_find (l, set)) {
g_hash_table_foreach (set->gmethod_cache, check_gmethod, image);
}
}
}
void
mono_metadata_clean_for_image (MonoImage *image)
{
CleanForImageUserData ginst_data, gclass_data;
GSList *l, *set_list;
//check_image_sets (image);
/*
* The data structures could reference each other so we delete them in two phases.
* This is required because of the hashing functions in gclass/ginst_cache.
*/
ginst_data.image = gclass_data.image = image;
ginst_data.list = gclass_data.list = NULL;
/* Collect the items to delete */
/* delete_image_set () modifies the lists so make a copy */
for (l = image->image_sets; l; l = l->next) {
MonoImageSet *set = l->data;
mono_image_set_lock (set);
g_hash_table_foreach_steal (set->gclass_cache, steal_gclass_in_image, &gclass_data);
g_hash_table_foreach_steal (set->ginst_cache, steal_ginst_in_image, &ginst_data);
g_hash_table_foreach_remove (set->gmethod_cache, inflated_method_in_image, image);
g_hash_table_foreach_remove (set->gsignature_cache, inflated_signature_in_image, image);
mono_image_set_unlock (set);
}
/* Delete the removed items */
for (l = ginst_data.list; l; l = l->next)
free_generic_inst (l->data);
for (l = gclass_data.list; l; l = l->next)
free_generic_class (l->data);
g_slist_free (ginst_data.list);
g_slist_free (gclass_data.list);
/* delete_image_set () modifies the lists so make a copy */
set_list = g_slist_copy (image->image_sets);
for (l = set_list; l; l = l->next) {
MonoImageSet *set = l->data;
delete_image_set (set);
}
g_slist_free (set_list);
}
static void
free_inflated_method (MonoMethodInflated *imethod)
{
int i;
MonoMethod *method = (MonoMethod*)imethod;
if (method->signature)
mono_metadata_free_inflated_signature (method->signature);
if (!((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))) {
MonoMethodHeader *header = imethod->header;
if (header) {
/* Allocated in inflate_generic_header () */
for (i = 0; i < header->num_locals; ++i)
mono_metadata_free_type (header->locals [i]);
g_free (header->clauses);
g_free (header);
}
}
g_free (method);
}
static void
free_generic_inst (MonoGenericInst *ginst)
{
int i;
/* The ginst itself is allocated from the image set mempool */
for (i = 0; i < ginst->type_argc; ++i)
mono_metadata_free_type (ginst->type_argv [i]);
}
static void
free_generic_class (MonoGenericClass *gclass)
{
/* The gclass itself is allocated from the image set mempool */
if (gclass->is_dynamic)
mono_reflection_free_dynamic_generic_class (gclass);
if (gclass->cached_class && gclass->cached_class->interface_id)
mono_unload_interface_id (gclass->cached_class);
}
static void
free_inflated_signature (MonoInflatedMethodSignature *sig)
{
mono_metadata_free_inflated_signature (sig->sig);
g_free (sig);
}
/*
* mono_metadata_get_inflated_signature:
*
* Given an inflated signature and a generic context, return a canonical copy of the
* signature. The returned signature might be equal to SIG or it might be a cached copy.
*/
MonoMethodSignature *
mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
{
MonoInflatedMethodSignature helper;
MonoInflatedMethodSignature *res;
CollectData data;
MonoImageSet *set;
helper.sig = sig;
helper.context.class_inst = context->class_inst;
helper.context.method_inst = context->method_inst;
collect_data_init (&data);
collect_inflated_signature_images (&helper, &data);
set = get_image_set (data.images, data.nimages);
collect_data_free (&data);
mono_image_set_lock (set);
res = g_hash_table_lookup (set->gsignature_cache, &helper);
if (!res) {
res = g_new0 (MonoInflatedMethodSignature, 1);
res->sig = sig;
res->context.class_inst = context->class_inst;
res->context.method_inst = context->method_inst;
g_hash_table_insert (set->gsignature_cache, res, res);
}
mono_image_set_unlock (set);
return res->sig;
}
MonoImageSet *
mono_metadata_get_image_set_for_method (MonoMethodInflated *method)
{
MonoImageSet *set;
CollectData image_set_data;
collect_data_init (&image_set_data);
collect_method_images (method, &image_set_data);
set = get_image_set (image_set_data.images, image_set_data.nimages);
collect_data_free (&image_set_data);
return set;
}
/*
* mono_metadata_get_generic_inst:
*
* Given a list of types, return a MonoGenericInst that represents that list.
* The returned MonoGenericInst has its own copy of the list of types. The list
* passed in the argument can be freed, modified or disposed of.
*
*/
MonoGenericInst *
mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
{
MonoGenericInst *ginst;
gboolean is_open;
int i;
int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
CollectData data;
MonoImageSet *set;
for (i = 0; i < type_argc; ++i)
if (mono_class_is_open_constructed_type (type_argv [i]))
break;
is_open = (i < type_argc);
ginst = g_alloca (size);
memset (ginst, 0, sizeof (MonoGenericInst));
ginst->is_open = is_open;
ginst->type_argc = type_argc;
memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
collect_data_init (&data);
collect_ginst_images (ginst, &data);
set = get_image_set (data.images, data.nimages);
collect_data_free (&data);
mono_image_set_lock (set);
ginst = g_hash_table_lookup (set->ginst_cache, ginst);
if (!ginst) {
ginst = mono_image_set_alloc0 (set, size);
#ifndef MONO_SMALL_CONFIG
ginst->id = ++next_generic_inst_id;
#endif
ginst->is_open = is_open;
ginst->type_argc = type_argc;
for (i = 0; i < type_argc; ++i)
ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
g_hash_table_insert (set->ginst_cache, ginst, ginst);
}
mono_image_set_unlock (set);
return ginst;
}
static gboolean
mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
{
MonoGenericContainer *container = container_class->generic_container;
if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
return FALSE;
return inst == container->context.class_inst;
}
/*
* mono_metadata_lookup_generic_class:
*
* Returns a MonoGenericClass with the given properties.
*
*/
MonoGenericClass *
mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
{
MonoGenericClass *gclass;
MonoGenericClass helper;
gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
MonoImageSet *set;
CollectData data;
helper.container_class = container_class;
helper.context.class_inst = inst;
helper.context.method_inst = NULL;
helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
helper.is_tb_open = is_tb_open;
helper.cached_class = NULL;
collect_data_init (&data);
collect_gclass_images (&helper, &data);
set = get_image_set (data.images, data.nimages);
collect_data_free (&data);
mono_image_set_lock (set);
gclass = g_hash_table_lookup (set->gclass_cache, &helper);
/* A tripwire just to keep us honest */
g_assert (!helper.cached_class);
if (gclass) {
mono_image_set_unlock (set);
return gclass;
}
if (is_dynamic) {
MonoDynamicGenericClass *dgclass = mono_image_set_new0 (set, MonoDynamicGenericClass, 1);
gclass = &dgclass->generic_class;
gclass->is_dynamic = 1;
} else {
gclass = mono_image_set_new0 (set, MonoGenericClass, 1);
}
gclass->is_tb_open = is_tb_open;
gclass->container_class = container_class;
gclass->context.class_inst = inst;
gclass->context.method_inst = NULL;
gclass->owner = set;
if (inst == container_class->generic_container->context.class_inst && !is_tb_open)
gclass->cached_class = container_class;
g_hash_table_insert (set->gclass_cache, gclass, gclass);
mono_image_set_unlock (set);
return gclass;
}
/*
* mono_metadata_inflate_generic_inst:
*
* Instantiate the generic instance @ginst with the context @context.
* Check @error for success.
*
*/
MonoGenericInst *
mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error)
{
MonoType **type_argv;
MonoGenericInst *nginst = NULL;
int i, count = 0;
mono_error_init (error);
if (!ginst->is_open)
return ginst;
type_argv = g_new0 (MonoType*, ginst->type_argc);
for (i = 0; i < ginst->type_argc; i++) {
type_argv [i] = mono_class_inflate_generic_type_checked (ginst->type_argv [i], context, error);
if (!mono_error_ok (error))
goto cleanup;
++count;
}
nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
cleanup:
for (i = 0; i < count; i++)
mono_metadata_free_type (type_argv [i]);
g_free (type_argv);
return nginst;
}
MonoGenericInst *
mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
int count, const char *ptr, const char **rptr)
{
MonoType **type_argv;
MonoGenericInst *ginst;
int i;
type_argv = g_new0 (MonoType*, count);
for (i = 0; i < count; i++) {
MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
if (!t) {
g_free (type_argv);
return NULL;
}
type_argv [i] = t;
}
if (rptr)
*rptr = ptr;
ginst = mono_metadata_get_generic_inst (count, type_argv);
g_free (type_argv);
return ginst;
}
static gboolean
do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
const char *ptr, const char **rptr)
{
MonoGenericInst *inst;
MonoClass *gklass;
MonoType *gtype;
int count;
gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
if (gtype == NULL)
return FALSE;
gklass = mono_class_from_mono_type (gtype);
if (!gklass->generic_container)
return FALSE;
count = mono_metadata_decode_value (ptr, &ptr);
inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
if (inst == NULL)
return FALSE;
if (rptr)
*rptr = ptr;
type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
return TRUE;
}
/*
* select_container:
* @gc: The generic container to normalize
* @type: The kind of generic parameters the resulting generic-container should contain
*/
static MonoGenericContainer *
select_container (MonoGenericContainer *gc, MonoTypeEnum type)
{
gboolean is_var = (type == MONO_TYPE_VAR);
if (!gc)
return NULL;
g_assert (is_var || type == MONO_TYPE_MVAR);
if (is_var) {
if (gc->is_method || gc->parent)
/*
* The current MonoGenericContainer is a generic method -> its `parent'
* points to the containing class'es container.
*/
return gc->parent;
}
return gc;
}
/*
* mono_metadata_parse_generic_param:
* @generic_container: Our MonoClass's or MonoMethod's MonoGenericContainer;
* see mono_metadata_parse_type_full() for details.
* Internal routine to parse a generic type parameter.
* LOCKING: Acquires the loader lock
*/
static MonoGenericParam *
mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
MonoTypeEnum type, const char *ptr, const char **rptr)
{
int index = mono_metadata_decode_value (ptr, &ptr);
if (rptr)
*rptr = ptr;
generic_container = select_container (generic_container, type);
if (!generic_container) {
/* Create dummy MonoGenericParam */
MonoGenericParam *param;
param = mono_image_alloc0 (m, sizeof (MonoGenericParam));
param->num = index;
param->image = m;
return param;
}
if (index >= generic_container->type_argc)
return NULL;
return mono_generic_container_get_param (generic_container, index);
}
/*
* mono_metadata_get_shared_type:
*
* Return a shared instance of TYPE, if available, NULL otherwise.
* Shared MonoType instances help save memory. Their contents should not be modified
* by the caller. They do not need to be freed as their lifetime is bound by either
* the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
* instance they are embedded in. If they are freed, they should be freed using
* mono_metadata_free_type () instead of g_free ().
*/
MonoType*
mono_metadata_get_shared_type (MonoType *type)
{
MonoType *cached;
/* No need to use locking since nobody is modifying the hash table */
if ((cached = g_hash_table_lookup (type_cache, type)))
return cached;
switch (type->type){
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
if (type == &type->data.klass->byval_arg)
return type;
if (type == &type->data.klass->this_arg)
return type;
break;
default:
break;
}
return NULL;
}
static gboolean
compare_type_literals (int class_type, int type_type)
{
/* byval_arg.type can be zero if we're decoding a type that references a class been loading.
* See mcs/test/gtest-440. and #650936.
* FIXME This better be moved to the metadata verifier as it can catch more cases.
*/
if (!class_type)
return TRUE;
/* NET 1.1 assemblies might encode string and object in a denormalized way.
* See #675464.
*/
if (class_type == type_type)
return TRUE;
if (type_type == MONO_TYPE_CLASS)
return class_type == MONO_TYPE_STRING || class_type == MONO_TYPE_OBJECT;
g_assert (type_type == MONO_TYPE_VALUETYPE);
switch (class_type) {
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_CHAR:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_R4:
case MONO_TYPE_R8:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_CLASS:
return TRUE;
default:
return FALSE;
}
}
/*
* do_mono_metadata_parse_type:
* @type: MonoType to be filled in with the return value
* @m: image context
* @generic_context: generics_context
* @transient: whenever to allocate data from the heap
* @ptr: pointer to the encoded type
* @rptr: pointer where the end of the encoded type is saved
*
* Internal routine used to "fill" the contents of @type from an
* allocated pointer. This is done this way to avoid doing too
* many mini-allocations (particularly for the MonoFieldType which
* most of the time is just a MonoType, but sometimes might be augmented).
*
* This routine is used by mono_metadata_parse_type and
* mono_metadata_parse_field_type
*
* This extracts a Type as specified in Partition II (22.2.12)
*
* Returns: FALSE if the type could not be loaded
*/
static gboolean
do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
gboolean transient, const char *ptr, const char **rptr)
{
gboolean ok = TRUE;
type->type = mono_metadata_decode_value (ptr, &ptr);
switch (type->type){
case MONO_TYPE_VOID:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_CHAR:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I2:
case MONO_TYPE_U2:
case MONO_TYPE_I4:
case MONO_TYPE_U4:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_R4:
case MONO_TYPE_R8:
case MONO_TYPE_I:
case MONO_TYPE_U:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_TYPEDBYREF:
break;
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_CLASS: {
guint32 token;
MonoClass *class;
MonoError error;
token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
class = mono_class_get_checked (m, token, &error);
type->data.klass = class;
if (!class) {
mono_loader_set_error_from_mono_error (&error);
mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
return FALSE;
}
if (!compare_type_literals (class->byval_arg.type, type->type))
return FALSE;
break;
}
case MONO_TYPE_SZARRAY: {
MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
if (!etype)
return FALSE;
type->data.klass = mono_class_from_mono_type (etype);
if (!type->data.klass)
return FALSE;
break;
}
case MONO_TYPE_PTR:
type->data.type = mono_metadata_parse_type_internal (m, container, MONO_PARSE_MOD_TYPE, 0, transient, ptr, &ptr);
if (!type->data.type)
return FALSE;
break;
case MONO_TYPE_FNPTR: {
MonoError error;
type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr, &error);
if (!type->data.method) {
mono_loader_set_error_from_mono_error (&error);
mono_error_cleanup (&error); /*FIXME don't swallow the error message*/
return FALSE;
}
break;
}
case MONO_TYPE_ARRAY:
type->data.array = mono_metadata_parse_array_internal (m, container, transient, ptr, &ptr);
if (!type->data.array)
return FALSE;
break;
case MONO_TYPE_MVAR:
if (container && !container->is_method)
return FALSE;
case MONO_TYPE_VAR:
type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
if (!type->data.generic_param)
return FALSE;
break;
case MONO_TYPE_GENERICINST:
ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
break;
default:
g_warning ("type 0x%02x not handled in do_mono_metadata_parse_type on image %s", type->type, m->name);
return FALSE;
}
if (rptr)
*rptr = ptr;
return ok;
}
/*
* mono_metadata_free_type:
* @type: type to free
*
* Free the memory allocated for type @type which is allocated on the heap.
*/
void
mono_metadata_free_type (MonoType *type)
{
if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
return;
switch (type->type){
case MONO_TYPE_OBJECT:
case MONO_TYPE_STRING:
if (!type->data.klass)
break;
/* fall through */
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
return;
break;
case MONO_TYPE_PTR:
mono_metadata_free_type (type->data.type);
break;
case MONO_TYPE_FNPTR:
mono_metadata_free_method_signature (type->data.method);
break;
case MONO_TYPE_ARRAY:
mono_metadata_free_array (type->data.array);
break;
default:
break;
}
g_free (type);
}
#if 0
static void
hex_dump (const char *buffer, int base, int count)
{
int show_header = 1;
int i;
if (count < 0){
count = -count;
show_header = 0;
}
for (i = 0; i < count; i++){
if (show_header)
if ((i % 16) == 0)
printf ("\n0x%08x: ", (unsigned char) base + i);
printf ("%02x ", (unsigned char) (buffer [i]));
}
fflush (stdout);
}
#endif
/**
* @ptr: Points to the beginning of the Section Data (25.3)
*/
static MonoExceptionClause*
parse_section_data (MonoImage *m, int *num_clauses, const unsigned char *ptr)
{
unsigned char sect_data_flags;
int is_fat;
guint32 sect_data_len;
MonoExceptionClause* clauses = NULL;
while (1) {
/* align on 32-bit boundary */
ptr = dword_align (ptr);
sect_data_flags = *ptr;
ptr++;
is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
if (is_fat) {
sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
ptr += 3;
} else {
sect_data_len = ptr [0];
++ptr;
}
if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
const unsigned char *p = dword_align (ptr);
int i;
*num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
/* we could just store a pointer if we don't need to byteswap */
clauses = g_malloc0 (sizeof (MonoExceptionClause) * (*num_clauses));
for (i = 0; i < *num_clauses; ++i) {
MonoExceptionClause *ec = &clauses [i];
guint32 tof_value;
if (is_fat) {
ec->flags = read32 (p);
ec->try_offset = read32 (p + 4);
ec->try_len = read32 (p + 8);
ec->handler_offset = read32 (p + 12);
ec->handler_len = read32 (p + 16);
tof_value = read32 (p + 20);
p += 24;
} else {
ec->flags = read16 (p);
ec->try_offset = read16 (p + 2);
ec->try_len = *(p + 4);
ec->handler_offset = read16 (p + 5);
ec->handler_len = *(p + 7);
tof_value = read32 (p + 8);
p += 12;
}
if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
ec->data.filter_offset = tof_value;
} else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
ec->data.catch_class = NULL;
if (tof_value) {
MonoError error;
ec->data.catch_class = mono_class_get_checked (m, tof_value, &error);
if (!mono_error_ok (&error)) {
mono_error_cleanup (&error); /* FIXME don't swallow the error */
g_free (clauses);
return NULL;
}
}
} else {
ec->data.catch_class = NULL;
}
/* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
}
}
if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
else
return clauses;
}
}
/*
* mono_method_get_header_summary:
* @method: The method to get the header.
* @summary: Where to store the header
*
*
* Returns: true if the header was properly decoded.
*/
gboolean
mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary)
{
int idx;
guint32 rva;
MonoImage* img;
const char *ptr;
unsigned char flags, format;
guint16 fat_flags;
/*Only the GMD has a pointer to the metadata.*/
while (method->is_inflated)
method = ((MonoMethodInflated*)method)->declaring;
summary->code_size = 0;
summary->has_clauses = FALSE;
/*FIXME extract this into a MACRO and share it with mono_method_get_header*/
if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
return FALSE;
if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
MonoMethodHeader *header = ((MonoMethodWrapper *)method)->header;
if (!header)
return FALSE;
summary->code_size = header->code_size;
summary->has_clauses = header->num_clauses > 0;
return TRUE;
}
idx = mono_metadata_token_index (method->token);
img = method->klass->image;
rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
/*We must run the verifier since we'll be decoding it.*/
if (!mono_verifier_verify_method_header (img, rva, NULL))
return FALSE;
ptr = mono_image_rva_map (img, rva);
g_assert (ptr);
flags = *(const unsigned char *)ptr;
format = flags & METHOD_HEADER_FORMAT_MASK;
switch (format) {
case METHOD_HEADER_TINY_FORMAT:
ptr++;
summary->code_size = flags >> 2;
break;
case METHOD_HEADER_FAT_FORMAT:
fat_flags = read16 (ptr);
ptr += 4;
summary->code_size = read32 (ptr);
if (fat_flags & METHOD_HEADER_MORE_SECTS)
summary->has_clauses = TRUE;
break;
default:
return FALSE;
}
return TRUE;
}
/*
* mono_metadata_parse_mh_full:
* @m: metadata context
* @generic_context: generics context
* @ptr: pointer to the method header.
*
* Decode the method header at @ptr, including pointer to the IL code,
* info about local variables and optional exception tables.
* This is a Mono runtime internal function.
*
* LOCKING: Acquires the loader lock.
*
* Returns: a transient MonoMethodHeader allocated from the heap.
*/
MonoMethodHeader *
mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
{
MonoMethodHeader *mh = NULL;
unsigned char flags = *(const unsigned char *) ptr;
unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
guint16 fat_flags;
guint32 local_var_sig_tok, max_stack, code_size, init_locals;
const unsigned char *code;
MonoExceptionClause* clauses = NULL;
int num_clauses = 0;
MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
g_return_val_if_fail (ptr != NULL, NULL);
switch (format) {
case METHOD_HEADER_TINY_FORMAT:
mh = g_malloc0 (MONO_SIZEOF_METHOD_HEADER);
ptr++;
mh->max_stack = 8;
mh->is_transient = TRUE;
local_var_sig_tok = 0;
mh->code_size = flags >> 2;
mh->code = (unsigned char*)ptr;
return mh;
case METHOD_HEADER_FAT_FORMAT:
fat_flags = read16 (ptr);
ptr += 2;
max_stack = read16 (ptr);
ptr += 2;
code_size = read32 (ptr);
ptr += 4;
local_var_sig_tok = read32 (ptr);
ptr += 4;
if (fat_flags & METHOD_HEADER_INIT_LOCALS)
init_locals = 1;
else
init_locals = 0;
code = (unsigned char*)ptr;
if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
break;
/*
* There are more sections
*/
ptr = (char*)code + code_size;
break;
default:
return NULL;
}
if (local_var_sig_tok) {
int idx = (local_var_sig_tok & 0xffffff)-1;
if (idx >= t->rows || idx < 0)
goto fail;
mono_metadata_decode_row (t, idx, cols, 1);
if (!mono_verifier_verify_standalone_signature (m, cols [MONO_STAND_ALONE_SIGNATURE], NULL))
goto fail;
}
if (fat_flags & METHOD_HEADER_MORE_SECTS)
clauses = parse_section_data (m, &num_clauses, (const unsigned char*)ptr);
if (local_var_sig_tok) {
const char *locals_ptr;
int len=0, i;
locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
if (*locals_ptr != 0x07)
g_warning ("wrong signature for locals blob");
locals_ptr++;
len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
mh = g_malloc0 (MONO_SIZEOF_METHOD_HEADER + len * sizeof (MonoType*) + num_clauses * sizeof (MonoExceptionClause));
mh->num_locals = len;
for (i = 0; i < len; ++i) {
mh->locals [i] = mono_metadata_parse_type_internal (m, container,
MONO_PARSE_LOCAL, 0, TRUE, locals_ptr, &locals_ptr);
if (!mh->locals [i])
goto fail;
}
} else {
mh = g_malloc0 (MONO_SIZEOF_METHOD_HEADER + num_clauses * sizeof (MonoExceptionClause));
}
mh->code = code;
mh->code_size = code_size;
mh->max_stack = max_stack;
mh->is_transient = TRUE;
mh->init_locals = init_locals;
if (clauses) {
MonoExceptionClause* clausesp = (MonoExceptionClause*)&mh->locals [mh->num_locals];
memcpy (clausesp, clauses, num_clauses * sizeof (MonoExceptionClause));
g_free (clauses);
mh->clauses = clausesp;
mh->num_clauses = num_clauses;