Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
6922 lines (6047 sloc) 198 KB
/**
* \file
* 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)
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#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>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-counters.h>
static int img_set_cache_hit, img_set_cache_miss, img_set_count;
/* 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, MonoError *error);
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,
/* CustomDebugInformation parent encoded index */
MONO_MT_HASCUSTDEBUG_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_TABLE_IDX, /* Document */
MONO_MT_BLOB_IDX, /* SequencePoints */
MONO_MT_END,
#define LOCALSCOPE_SCHEMA_OFFSET METHODBODY_SCHEMA_OFFSET + 3
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 LOCALCONSTANT_SCHEMA_OFFSET LOCALVARIABLE_SCHEMA_OFFSET + 4
MONO_MT_STRING_IDX, /* Name (String heap index) */
MONO_MT_BLOB_IDX, /* Signature (Blob heap index, LocalConstantSig blob) */
MONO_MT_END,
#define IMPORTSCOPE_SCHEMA_OFFSET LOCALCONSTANT_SCHEMA_OFFSET + 3
MONO_MT_TABLE_IDX, /* Parent (ImportScope row id or nil) */
MONO_MT_BLOB_IDX, /* Imports (Blob index, encoding: Imports blob) */
MONO_MT_END,
#define ASYNCMETHOD_SCHEMA_OFFSET IMPORTSCOPE_SCHEMA_OFFSET + 3
MONO_MT_TABLE_IDX, /* MoveNextMethod (MethodDef row id) */
MONO_MT_TABLE_IDX, /* KickoffMethod (MethodDef row id) */
MONO_MT_END,
#define CUSTOMDEBUGINFORMATION_SCHEMA_OFFSET ASYNCMETHOD_SCHEMA_OFFSET + 3
MONO_MT_HASCUSTDEBUG_IDX, /* Parent (HasCustomDebugInformation coded index) */
MONO_MT_GUID_IDX, /* Kind (Guid heap index) */
MONO_MT_BLOB_IDX, /* Value (Blob heap index) */
MONO_MT_END,
#define NULL_SCHEMA_OFFSET CUSTOMDEBUGINFORMATION_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,
LOCALCONSTANT_SCHEMA_OFFSET,
IMPORTSCOPE_SCHEMA_OFFSET,
ASYNCMETHOD_SCHEMA_OFFSET,
CUSTOMDEBUGINFORMATION_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
// Amount initially reserved in each imageset's mempool.
// FIXME: This number is arbitrary, a more practical number should be found
#define INITIAL_IMAGE_SET_SIZE 1024
/**
* mono_meta_table_name:
* \param 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 \p 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))
static inline int
idx_size (MonoImage *meta, int tableidx)
{
if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
return meta->referenced_table_rows [tableidx] < 65536 ? 2 : 4;
else
return meta->tables [tableidx].rows < 65536 ? 2 : 4;
}
static inline int
get_nrows (MonoImage *meta, int tableidx)
{
if (meta->referenced_tables && (meta->referenced_tables & ((guint64)1 << tableidx)))
return meta->referenced_table_rows [tableidx];
else
return meta->tables [tableidx].rows;
}
/* Reference: Partition II - 23.2.6 */
/**
* mono_metadata_compute_size:
* \param meta metadata context
* \param tableindex metadata table number
* \param result_bitfield pointer to \c guint32 where to store additional info
*
* \c mono_metadata_compute_size computes the length in bytes of a single
* row in a metadata table. The size of each column is encoded in the
* \p result_bitfield return value along with the number of columns in the table.
* the resulting bitfield should be handed to the \c mono_metadata_table_size
* and \c 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 (get_nrows (meta, MONO_TABLE_METHOD), get_nrows (meta, MONO_TABLE_TYPEDEF));
/*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;
case MONO_TABLE_METHODBODY:
g_assert (i == 0);
field_size = idx_size (meta, MONO_TABLE_DOCUMENT); break;
case MONO_TABLE_IMPORTSCOPE:
g_assert(i == 0);
field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE); break;
case MONO_TABLE_STATEMACHINEMETHOD:
g_assert(i == 0 || i == 1);
field_size = idx_size(meta, MONO_TABLE_METHOD); 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 (get_nrows (meta, MONO_TABLE_PARAM),
get_nrows (meta, MONO_TABLE_FIELD));
n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
/* 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 (get_nrows (meta, MONO_TABLE_METHOD),
get_nrows (meta, MONO_TABLE_FIELD));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEDEF));
n = MAX (n, get_nrows (meta, MONO_TABLE_PARAM));
n = MAX (n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
n = MAX (n, get_nrows (meta, MONO_TABLE_MEMBERREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_MODULE));
n = MAX (n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
n = MAX (n, get_nrows (meta, MONO_TABLE_PROPERTY));
n = MAX (n, get_nrows (meta, MONO_TABLE_EVENT));
n = MAX (n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_FILE));
n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
n = MAX (n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
n = MAX (n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
n = MAX (n, get_nrows (meta, MONO_TABLE_METHODSPEC));
/* 5 bits to encode */
field_size = rtsize (meta, n, 16-5);
break;
/*
* HasCustomAttribute: points to any table but
* itself.
*/
case MONO_MT_HASCUSTDEBUG_IDX:
n = MAX(get_nrows (meta, MONO_TABLE_METHOD),
get_nrows (meta, MONO_TABLE_FIELD));
n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEREF));
n = MAX(n, get_nrows (meta, MONO_TABLE_TYPEDEF));
n = MAX(n, get_nrows (meta, MONO_TABLE_PARAM));
n = MAX(n, get_nrows (meta, MONO_TABLE_INTERFACEIMPL));
n = MAX(n, get_nrows (meta, MONO_TABLE_MEMBERREF));
n = MAX(n, get_nrows (meta, MONO_TABLE_MODULE));
n = MAX(n, get_nrows (meta, MONO_TABLE_DECLSECURITY));
n = MAX(n, get_nrows (meta, MONO_TABLE_PROPERTY));
n = MAX(n, get_nrows (meta, MONO_TABLE_EVENT));
n = MAX(n, get_nrows (meta, MONO_TABLE_STANDALONESIG));
n = MAX(n, get_nrows (meta, MONO_TABLE_MODULEREF));
n = MAX(n, get_nrows (meta, MONO_TABLE_TYPESPEC));
n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
n = MAX(n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
n = MAX(n, get_nrows (meta, MONO_TABLE_FILE));
n = MAX(n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
n = MAX(n, get_nrows (meta, MONO_TABLE_MANIFESTRESOURCE));
n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAM));
n = MAX(n, get_nrows (meta, MONO_TABLE_GENERICPARAMCONSTRAINT));
n = MAX(n, get_nrows (meta, MONO_TABLE_METHODSPEC));
n = MAX(n, get_nrows (meta, MONO_TABLE_DOCUMENT));
n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALSCOPE));
n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALVARIABLE));
n = MAX(n, get_nrows (meta, MONO_TABLE_LOCALCONSTANT));
n = MAX(n, get_nrows (meta, MONO_TABLE_IMPORTSCOPE));
/* 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 (get_nrows (meta, MONO_TABLE_TYPEREF),
get_nrows (meta, MONO_TABLE_TYPEDEF));
n = MAX (n, get_nrows (meta, MONO_TABLE_METHOD));
n = MAX (n, get_nrows (meta, MONO_TABLE_MEMBERREF));
/* 3 bits to encode */
field_size = rtsize (meta, n, 16-3);
break;
/*
* HasDeclSecurity: Typedef, MethodDef, Assembly
*/
case MONO_MT_HASDEC_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_TYPEDEF),
get_nrows (meta, MONO_TABLE_METHOD));
n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLY));
/* 2 bits to encode */
field_size = rtsize (meta, n, 16-2);
break;
/*
* Implementation: File, AssemblyRef, ExportedType
*/
case MONO_MT_IMPL_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_FILE),
get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_EXPORTEDTYPE));
/* 2 bits to encode tag */
field_size = rtsize (meta, n, 16-2);
break;
/*
* HasFieldMarshall: FieldDef, ParamDef
*/
case MONO_MT_HFM_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
get_nrows (meta, MONO_TABLE_PARAM));
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* MemberForwarded: FieldDef, MethodDef
*/
case MONO_MT_MF_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_FIELD),
get_nrows (meta, MONO_TABLE_METHOD));
/* 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 (get_nrows (meta, MONO_TABLE_TYPEDEF),
get_nrows (meta, MONO_TABLE_TYPEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
/* 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 (get_nrows (meta, MONO_TABLE_TYPEDEF),
get_nrows (meta, MONO_TABLE_TYPEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_METHOD));
n = MAX (n, get_nrows (meta, MONO_TABLE_MODULEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPESPEC));
/* 3 bits to encode */
field_size = rtsize (meta, n, 16 - 3);
break;
/*
* MethodDefOrRef: MethodDef, MemberRef
*/
case MONO_MT_MDOR_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_METHOD),
get_nrows (meta, MONO_TABLE_MEMBERREF));
/* 1 bit used to encode tag */
field_size = rtsize (meta, n, 16-1);
break;
/*
* HasSemantics: Property, Event
*/
case MONO_MT_HS_IDX:
n = MAX (get_nrows (meta, MONO_TABLE_PROPERTY),
get_nrows (meta, MONO_TABLE_EVENT));
/* 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 (get_nrows (meta, MONO_TABLE_MODULE),
get_nrows (meta, MONO_TABLE_MODULEREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_ASSEMBLYREF));
n = MAX (n, get_nrows (meta, MONO_TABLE_TYPEREF));
/* 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:
* \param 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:
* \param meta metadata context
* \param table table code.
* \param idx index of element to retrieve from \p table.
*
* \returns a pointer to the \p idx element in the metadata table
* whose code is \p 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:
* \param meta metadata context
* \param token metadata token
*
* \returns a pointer to the data in the metadata represented by the
* token \p token .
*/
const char *
mono_metadata_locate_token (MonoImage *meta, guint32 token)
{
return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
}
/**
* mono_metadata_string_heap:
* \param meta metadata context
* \param index index into the string heap.
* \returns an in-memory pointer to the \p 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:
* \param meta metadata context
* \param index index into the user string heap.
* \returns an in-memory pointer to the \p index in the user string heap (<code>#US</code>).
*/
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:
* \param meta metadata context
* \param index index into the blob.
* \returns an in-memory pointer to the \p 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:
* \param meta metadata context
* \param index index into the guid heap.
* \returns an in-memory pointer to the \p 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:
* \param t table to extract information from.
* \param idx index in table.
* \param res array of \p res_size cols to store the results in
*
* This decompresses the metadata element \p idx in table \p t
* into the \c guint32 \p res array that has \p 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:
* \param t table to extract information from.
* \param idx index for row in table.
* \param col column in the row.
*
* This function returns the value of column \p col from the \p idx
* row in the table \p 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:
* \param ptr pointer to a blob object
* \param 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:
* \param ptr pointer to decode from
* \param rptr the new position of the pointer
*
* This routine decompresses 32-bit values as specified in the "Blob and
* Signature" section (23.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:
* \param ptr pointer to decode from
* \param 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;
}
/**
* mono_metadata_translate_token_index:
* Translates the given 1-based index into the \c Method, \c Field, \c Event, or \c Param tables
* using the \c *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 \c mono_metadata_decode_row, but takes an \p image + \p table ID pair, and takes
* uncompressed metadata into account, so it should be used to access the
* \c Method, \c Field, \c Param and \c Event tables when the access is made from metadata, i.e.
* \p idx is retrieved from a metadata table, like \c 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 \c mono_metadata_decode_row_col, but takes an \p image + \p table ID pair, and takes
* uncompressed metadata into account, so it should be used to access the
* \c Method, \c Field, \c Param and \c 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:
* \param m a metadata context.
* \param ptr a pointer to an encoded TypedefOrRef in \p m
* \param rptr pointer updated to match the end of the decoded stream
* \returns a token valid in the \p 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:
* \param m a metadata context.
* \param dest storage where the info about the custom modifier is stored (may be NULL)
* \param ptr a pointer to (possibly) the start of a custom modifier list
* \param rptr pointer updated to match the end of the decoded stream
*
* Checks if \p 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, MonoError *error)
{
int i;
MonoArrayType *array;
MonoType *etype;
etype = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error); //FIXME this doesn't respect @transient
if (!etype)
return NULL;
array = transient ? (MonoArrayType *)g_malloc0 (sizeof (MonoArrayType)) : (MonoArrayType *)mono_image_alloc0 (m, sizeof (MonoArrayType));
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 ? (int *)g_malloc0 (sizeof (int) * array->numsizes) : (int *)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 ? (int *)g_malloc0 (sizeof (int) * array->numlobounds) : (int *)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;
}
/**
* mono_metadata_parse_array:
*/
MonoArrayType *
mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
{
MonoError error;
MonoArrayType *ret = mono_metadata_parse_array_internal (m, NULL, FALSE, ptr, rptr, &error);
mono_error_cleanup (&error);
return ret;
}
/**
* mono_metadata_free_array:
* \param array array description
*
* Frees the array description returned from \c 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;
// An optimization: if the ids of two insts are the same, we know they are the same inst and don't check contents.
// Furthermore, because we perform early de-duping, if the ids differ, we know the contents differ.
#ifndef MONO_SMALL_CONFIG // Optimization does not work in MONO_SMALL_CONFIG: There are no IDs
if (a->id && b->id) { // "id 0" means "object has no id"-- de-duping hasn't been performed yet, must check contents.
if (a->id == b->id)
return TRUE;
// In signature-comparison mode id equality implies object equality, but this is not true for inequality.
// Two separate objects could have signature-equavalent contents.
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_os_mutex_init_recursive (&image_sets_mutex);
mono_counters_register ("ImgSet Cache Hit", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_cache_hit);
mono_counters_register ("ImgSet Cache Miss", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_cache_miss);
mono_counters_register ("ImgSet Count", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &img_set_count);
}
/**
* 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_os_mutex_destroy (&image_sets_mutex);
}
/**
* mono_metadata_parse_type:
* \param m metadata context
* \param mode kind of type that may be found at \p ptr
* \param opt_attrs optional attributes to store in the returned type
* \param ptr pointer to the type representation
* \param rptr pointer updated to match the end of the decoded stream
* \param transient whenever to allocate the result from the heap or from a mempool
*
* Decode a compressed type description found at \p ptr in \p m .
* \p mode can be one of \c MONO_PARSE_MOD_TYPE, \c MONO_PARSE_PARAM, \c MONO_PARSE_RET,
* \c MONO_PARSE_FIELD, \c MONO_PARSE_LOCAL, \c 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, \c generic_container points to the current class'es
* (the \c generic_container field in the <code>MonoClass</code>) or the current generic method's
* (stored in <code>image->property_hash</code>) generic container.
* When we encounter a \c MONO_TYPE_VAR or \c MONO_TYPE_MVAR, it's looked up in
* this \c MonoGenericContainer.
*
* LOCKING: Acquires the loader lock.
*
* \returns a \c MonoType structure representing the decoded type.
*/
static MonoType*
mono_metadata_parse_type_internal (MonoImage *m, MonoGenericContainer *container,
short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
{
MonoType *type, *cached;
MonoType stype;
gboolean byref = FALSE;
gboolean pinned = FALSE;
const char *tmp_ptr;
int count = 0; // Number of mod arguments
gboolean found;
error_init (error);
/*
* 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) { // There are mods, so the MonoType will be of nonstandard size.
int size;
size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
type = transient ? (MonoType *)g_malloc0 (size) : (MonoType *)mono_image_alloc0 (m, size);
type->num_mods = count;
if (count > 64) {
mono_error_set_bad_image (error, m, "Invalid type with more than 64 modifiers");
return NULL;
}
} else { // The type is of standard size, so we can allocate it on the stack.
type = &stype;
memset (type, 0, MONO_SIZEOF_TYPE);
}
/* Iterate again, but now 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, error))
return NULL;
if (rptr)
*rptr = ptr;
// Possibly we can return an already-allocated type instead of the one we decoded
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 = (MonoType *)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 was allocated on the stack, so we need to copy it to safety
type = transient ? (MonoType *)g_malloc (MONO_SIZEOF_TYPE) : (MonoType *)mono_image_alloc (m, MONO_SIZEOF_TYPE);
memcpy (type, &stype, MONO_SIZEOF_TYPE);
}
return type;
}
MonoType*
mono_metadata_parse_type_checked (MonoImage *m, MonoGenericContainer *container,
short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
{
return mono_metadata_parse_type_internal (m, container, opt_attrs, transient, ptr, rptr, error);
}
/*
* LOCKING: Acquires the loader lock.
*/
MonoType*
mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
const char *ptr, const char **rptr)
{
MonoError error;
MonoType * type = mono_metadata_parse_type_internal (m, NULL, opt_attrs, FALSE, ptr, rptr, &error);
mono_error_cleanup (&error);
return type;
}
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:
* \param image metadata context
* \param token metadata token
*
* Decode a method signature stored in the \c StandAloneSig table
*
* \returns a \c MonoMethodSignature describing the signature.
*/
MonoMethodSignature*
mono_metadata_parse_signature (MonoImage *image, guint32 token)
{
MonoError error;
MonoMethodSignature *ret;
ret = mono_metadata_parse_signature_checked (image, token, &error);
mono_error_cleanup (&error);
return ret;
}
/*
* mono_metadata_parse_signature_checked:
* @image: metadata context
* @token: metadata token
* @error: set on error
*
* Decode a method signature stored in the STANDALONESIG table
*
* Returns: a MonoMethodSignature describing the signature. On failure
* returns NULL and sets @error.
*/
MonoMethodSignature*
mono_metadata_parse_signature_checked (MonoImage *image, guint32 token, MonoError *error)
{
error_init (error);
MonoTableInfo *tables = image->tables;
guint32 idx = mono_metadata_token_index (token);
guint32 sig;
const char *ptr;
if (image_is_dynamic (image)) {
return (MonoMethodSignature *)mono_lookup_dynamic_token (image, token, NULL, error);
}
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);
return mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
}
/**
* mono_metadata_signature_alloc:
* \param image metadata context
* \param nparams number of parameters in the signature
*
* Allocate a \c 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 \c MonoMethodSignature structure.
*/
MonoMethodSignature*
mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
{
MonoMethodSignature *sig;
sig = (MonoMethodSignature *)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 = (MonoMethodSignature *)mono_image_alloc (image, sigsize);
} else if (mp) {
ret = (MonoMethodSignature *)mono_mempool_alloc (mp, sigsize);
} else {
ret = (MonoMethodSignature *)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:
* \param sig method signature
*
* Duplicate an existing \c MonoMethodSignature so it can be modified.
* This is a Mono runtime internal function.
*
* \returns the new \c 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;
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_checked (m, container, pattrs ? pattrs [0] : 0, FALSE, ptr, &ptr, error);
if (!method->ret) {
mono_metadata_free_method_signature (method);
g_free (pattrs);
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_error_set_bad_image (error, m, "Found sentinel for methoddef or no vararg");
g_free (pattrs);
return NULL;
}
if (method->sentinelpos >= 0) {
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_checked (m, container, pattrs ? pattrs [i+1] : 0, FALSE, ptr, &ptr, error);
if (!method->params [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...
*/
return method;
}
/**
* mono_metadata_parse_method_signature:
* \param m metadata context
* \param def the \c MethodDef index or 0 for \c Ref signatures.
* \param ptr pointer to the signature metadata representation
* \param rptr pointer updated to match the end of the decoded stream
*
* Decode a method signature stored at \p ptr.
* This is a Mono runtime internal function.
*
* LOCKING: Assumes the loader lock is held.
*
* \returns a \c MonoMethodSignature describing the signature.
*/
MonoMethodSignature *
mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
{
/*
* This function MUST NOT be called by runtime code as it does error handling incorrectly.
* Use mono_metadata_parse_method_signature_full instead.
* It's ok to asser on failure as we no longer use it.
*/
MonoError error;
MonoMethodSignature *ret;
ret = mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr, &error);
g_assert (mono_error_ok (&error));
return ret;
}
/**
* mono_metadata_free_method_signature:
* \param sig signature to destroy
*
* Free the memory allocated in the signature \p 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 = (const MonoMethodInflated *)a;
const MonoMethodInflated *mb = (const MonoMethodInflated *)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 = (const MonoMethodInflated *)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 = (const MonoInflatedMethodSignature *)a;
const MonoInflatedMethodSignature *sig2 = (const MonoInflatedMethodSignature *)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 = (const MonoInflatedMethodSignature *)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:
case MONO_TYPE_MVAR:
return image == get_image_for_generic_param (type->data.generic_param);
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_os_mutex_lock (&image_sets_mutex);
}
static inline void
image_sets_unlock (void)
{
mono_os_mutex_unlock (&image_sets_mutex);
}
static int
compare_pointers (const void *a, const void *b)
{
return (size_t)a - (size_t)b;
}
//1103, 1327, 1597
#define HASH_TABLE_SIZE 1103
static MonoImageSet *img_set_cache [HASH_TABLE_SIZE];
static guint32
mix_hash (uintptr_t source)
{
unsigned int hash = source;
// Actual hash
hash = (((hash * 215497) >> 16) ^ ((hash * 1823231) + hash));
// Mix in highest bits on 64-bit systems only
if (sizeof (source) > 4)
hash = hash ^ (source >> 32);
return hash;
}
static guint32
hash_images (MonoImage **images, int nimages)
{
guint32 res = 0;
int i;
for (i = 0; i < nimages; ++i)
res += mix_hash ((size_t)images [i]);
return res;
}
static gboolean
compare_img_set (MonoImageSet *set, MonoImage **images, int nimages)
{
int j, k;
if (set->nimages != nimages)
return FALSE;
for (j = 0; j < nimages; ++j) {
for (k = 0; k < nimages; ++k)
if (set->images [k] == images [j])
break; // Break on match
// If we iterated all the way through set->images, images[j] was *not* found.
if (k == nimages)
break; // Break on "image not found"
}
// If we iterated all the way through images without breaking, all items in images were found in set->images
return j == nimages;
}
static MonoImageSet*
img_set_cache_get (MonoImage **images, int nimages)
{
guint32 hash_code = hash_images (images, nimages);
int index = hash_code % HASH_TABLE_SIZE;
MonoImageSet *img = img_set_cache [index];
if (!img || !compare_img_set (img, images, nimages)) {
++img_set_cache_miss;
return NULL;
}
++img_set_cache_hit;
return img;
}
static void
img_set_cache_add (MonoImageSet *set)
{
guint32 hash_code = hash_images (set->images, set->nimages);
int index = hash_code % HASH_TABLE_SIZE;
img_set_cache [index] = set;
}
static void
img_set_cache_remove (MonoImageSet *is)
{
guint32 hash_code = hash_images (is->images, is->nimages);
int index = hash_code % HASH_TABLE_SIZE;
if (img_set_cache [index] == is)
img_set_cache [index] = NULL;
}
/*
* 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: Image set contains corlib only. If we've seen that case before, we cached the set. */
if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
return mscorlib_image_set;
/* Happens with empty generic instances */
// FIXME: Is corlib the correct thing to return here? If so, why? This may be an artifact of generic instances previously defaulting to allocating from corlib.
if (nimages == 0)
return mscorlib_image_set;
set = img_set_cache_get (images, nimages);
if (set)
return set;
image_sets_lock ();
if (!image_sets)
image_sets = g_ptr_array_new ();
// Before we go on, we should check to see whether a MonoImageSet with these images already exists.
// We can search the referred-by imagesets of any one of our images to do this. Arbitrarily pick one here:
if (images [0] == mono_defaults.corlib && nimages > 1)
l = images [1]->image_sets; // Prefer not to search the imagesets of corlib-- that will be a long list.
else
l = images [0]->image_sets;
set = NULL;
while (l) // Iterate over selected list, looking for an imageset with members equal to our target one
{
set = (MonoImageSet *)l->data;
if (set->nimages == nimages) { // Member count differs, this can't be it
// Compare all members to all members-- order might be different
for (j = 0; j < nimages; ++j) {
for (k = 0; k < nimages; ++k)
if (set->images [k] == images [j])
break; // Break on match
// If we iterated all the way through set->images, images[j] was *not* found.
if (k == nimages)
break; // Break on "image not found"
}
// If we iterated all the way through images without breaking, all items in images were found in set->images
if (j == nimages)
break; // Break on "found a set with equal members"
}
l = l->next;
}
// If we iterated all the way through l without breaking, the imageset does not already exist and we shuold create it
if (!l) {
set = g_new0 (MonoImageSet, 1);
set->nimages = nimages;
set->images = g_new0 (MonoImage*, nimages);
mono_os_mutex_init_recursive (&set->lock);
for (i = 0; i < nimages; ++i)
set->images [i] = images [i];
set->gclass_cache = mono_conc_hashtable_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);
++img_set_count;
img_set_cache_add (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;
mono_conc_hashtable_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 ();
img_set_cache_remove (set);
if (set->mempool)
mono_mempool_destroy (set->mempool);
g_free (set->images);
mono_os_mutex_destroy (&set->lock);
g_free (set);
}
void
mono_image_set_lock (MonoImageSet *set)
{
mono_os_mutex_lock (&set->lock);
}
void
mono_image_set_unlock (MonoImageSet *set)
{
mono_os_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 (INITIAL_IMAGE_SET_SIZE);
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 (INITIAL_IMAGE_SET_SIZE);
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 (INITIAL_IMAGE_SET_SIZE);
res = mono_mempool_strdup (set->mempool, s);
mono_image_set_unlock (set);
return res;
}
// Get a descriptive string for a MonoImageSet
// Callers are obligated to free buffer with g_free after use
char *
mono_image_set_description (MonoImageSet *set)
{
GString *result = g_string_new (NULL);
int img;
g_string_append (result, "[");
for (img = 0; img < set->nimages; img++)
{
if (img > 0)
g_string_append (result, ", ");
g_string_append (result, set->images[img]->name);
}
g_string_append (result, "]");
return g_string_free (result, FALSE);
}
/*
* 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:
case MONO_TYPE_MVAR:
{
MonoImage *image = get_image_for_generic_param (type->data.generic_param);
add_image (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 = (MonoGenericClass *)key;
CleanForImageUserData *user_data = (CleanForImageUserData *)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 = (MonoGenericInst *)key;
CleanForImageUserData *user_data = (CleanForImageUserData *)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 = (MonoImage *)data;
MonoMethodInflated *method = (MonoMethodInflated *)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 = (MonoImage *)data;
MonoInflatedMethodSignature *sig = (MonoInflatedMethodSignature *)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 = (MonoMethodInflated *)key;
MonoImage *image = (MonoImage *)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 = (MonoImageSet *)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 = (MonoImageSet *)l->data;
mono_image_set_lock (set);
mono_conc_hashtable_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 ((MonoGenericInst *)l->data);
for (l = gclass_data.list; l; l = l->next)
free_generic_class ((MonoGenericClass *)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 = (MonoImageSet *)l->data;
delete_image_set (set);
}
g_slist_free (set_list);
}
static void
free_inflated_method (MonoMethodInflated *imethod)
{
MonoMethod *method = (MonoMethod*)imethod;
if (method->signature)
mono_metadata_free_inflated_signature (method->signature);
if (method->wrapper_type)
g_free (((MonoMethodWrapper*)method)->method_data);
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->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 = (MonoInflatedMethodSignature *)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;
}
static gboolean
type_is_gtd (MonoType *type)
{
switch (type->type) {
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
return mono_class_is_gtd (type->data.klass);
default:
return FALSE;
}
}
/*
* 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 *);
for (i = 0; i < type_argc; ++i)
if (mono_class_is_open_constructed_type (type_argv [i]))
break;
is_open = (i < type_argc);
ginst = (MonoGenericInst *)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 *));
for (i = 0; i < type_argc; ++i) {
MonoType *t = ginst->type_argv [i];
if (type_is_gtd (t)) {
ginst->type_argv [i] = mono_class_gtd_get_canonical_inst (t->data.klass);
}
}
return mono_metadata_get_canonical_generic_inst (ginst);
}
/**
* mono_metadata_get_canonical_generic_inst:
* \param candidate an arbitrary generic instantiation
*
* \returns the canonical generic instantiation that represents the given
* candidate by identifying the image set for the candidate instantiation and
* finding the instance in the image set or adding a copy of the given instance
* to the image set.
*
* The returned MonoGenericInst has its own copy of the list of types. The list