Assembly - Johannes Kepler University Linz

Download Report

Transcript Assembly - Johannes Kepler University Linz

Assembly
Where it all gets physical
Objectives
 Introduce
 Discuss
 Show
concepts of assemblies
elements of assemblies
how to build assemblies
 Runtime
aspects
Contents
 Section
1: Overview
 Section
2: Concepts and Elements
 Section
3: Assemblies at Buildtime
 Section
4: Assemblies at Runtime
Section 1: Overview
 Versioning
and DLL conflicts must be resolved

Windows 2000 partially fixed DLL conflicts

New shared version still replaces the old
 Physical
 Easy
units instead of logical
installation/deinstallation procedures required

xcopy installation

Just delete the files!
What‘s an Assembly
 Runtime

executable code = IL
Single point of entry
 Fundamental
unit

Version control

Reuse

Scoping

Identity

Security permissions
 Runtime
metadata
Static and Dynamic Assemblies
 Static

Generated by compilers

Loaded from disk or downloaded from the net
 Dynamic

Built "on-the-fly"

From scripts or via Reflection.Emit

Can be transient
Assembly vs. Module
 Module
is compiled unit
 Modules
contain types and global methods
 Assemblies
 An
contain modules
assembly consists of modules and resources
 Assembly
manifest references files
Dependencies
 Viewed

as collection of exported resources
Independent of implementation
 Assembly

may depend on other assemblies
May import any public resource of other assembly

Types

Resource files

Etc.
 Dependencies
 Resolved
are recorded in the manifest
at runtime – no static linking
Type Referencing
Public Types
Assembly Types
Public Types
External
Refs
External
Refs
Assembly Types
Module3
Public Types
Module2
Module1
Module Types
Public
PublicTypes
Types
Assembly
ModuleTypes
Types
ref: null, TypeA, module1
ref: null, TypeB, module2
ref: AssemblyB, TypeC, module3
AssemblyA
AssemblyB
Assembly vs. Namespace
 Namespaces
 Assemblies
can contain several namespaces
 Namespaces

can be partitioned across assemblies
Types implemented twice are different!
 Both

are used to group names
must be included into project independently:
Namespaces are imported in the source code
using System.Runtime.Remoting.Services;

Assemblies are referenced by compiler switch
csc /r:System.Runtime.Remoting.DLL ...
Section 2: Concepts and Elements
 Elements
of an assembly

Manifest

Versioning

Security
 Physical
 What's
representation
in it's name
Manifest: Standard Elements
 Manifest
is table with info records
 Manifest
contains info about:

Assembly name

Version information

Strong name information

Culture information

Processor and OS

Files that make up this assembly

References to types and resources

Exported and local types
Manifest: Custom Elements
 AssemblyCompany
 AssemblyConfiguration
 AssemblyCopyright
 AssemblyDefaultAlias
 AssemblyDescription
 AssemblyInformationalVersion
 AssemblyProduct
 AssemblyTitle
 AssemblyTrademark
Multi-File Assemblies
 Association

based on metadata within assembly
Not linked by the file system
Module1
Module2
Assembly
Manifest
Module
Manifest
Assembly.exe
Module2.dll
Graph
Graph.jpg
Versioning
 Manifest
carries version information
 Compatibility

Major, minor, build, revision: 2.1.1254.0
 Informational

version
version
String stored in AssemblyInformationalVersion
 References
to other assemblies carry version info
Side-by-side execution
 Run
different versions simultaneously

Per machine

Or even per process
 Requires
special coding considerations

Issues with dependencies on machine resources

Process-wide resources
Security Considerations
 Integrity
of files is guaranteed by hash verification
 Assembly
 Security
carries permission requests
policy is applied to requests at load time
 AuthentiCode
 Strong
names
digital signing
Strong Names
 Simple
name accompanied by:

Public key

Digital signature
 Generated
 Prevent
 Protect
from assembly and private key
others from „taking over your namespace“
version lineage
 Assemblies
 Versioning
with same strong name are identical
only works with strong named assemblies
Strong Name Utility

sn.exe provides options for:

Signature generation

Generate public/private key pair
sn –k keyFile.snk

Key management

Signature verification

Setting Cryptographic Service Provider (CSP)
Assigning a Strong Name
 Need
to have a public-private key pair
 Using
attributes
 Using
al (assembly linker)
[assembly:AssemblyKeyFile("sgKey.snk")]
[assembly:AssemblyKeyName("AContainer")]
al myModule.dll /keyfile:sgKey.snk ...
Delaying Strong Name Assignment
 Access
to private key might be restricted
 Delayed

Actual signing is deferred
 Process

(or partial) signing reserves space in file
works as follows:
Developer works with public key file only
[assembly:AssemblyKeyFile(„pubKey.snk")]
[assembly:AssemblyDelaySign(true)]

Verification must be switched off
sn –Vr myAssembly.dll

Full signing must be applied later
sn –R myAssembly.dll fullKey.snk
Using Strong Named Assemblies
 Consumer

of strong named assembly uses token
Token is portion of public key
 Runtime
verifies strong name signature
 Referencing
usually transparent

Compiler inserts token of referenced assembly

Dynamic loading requires explicit notion
> sn –t myDll.DLL
Assembly.Load(“myDll,
Version=1.0.0.1,
Culture=neutral,
PublicKeyToken=9b35aa32c18d4fb1”);
Section 3: More Tools and Deployment
 Assembler
ilasm
 Disassembler
 Global
ildasm
Assembly Cache
 Installation
Assembler: ilasm
 "Assembles"

Generates the metadata
 Output

IL streams into loadable files
can be disassembled by ildasm
No optimizations made
Dis-Assembly: ildasm
 "Disassembles"
 Output
 GUI
assemblies (or modules) into IL
can be reassembled by ilasm
for examining an assembly

Manifest

Metadata

IL code
Global Assembly Cache Advantages
 Using
the GAC has advantages:

Performance improvements

Integrity checking

File security

Versioning

Automatic pickup of Quick Fixes

Additional search location
Installation
 Private
vs. shared assemblies

Private assemblies deployed in local directory

Shared assemblies stored in Global Assembly Cache
gacutil –i myAssembly.DLL

Cache viewer as shell extension (shfusion.dll)

Snap-In for Management Console (mscorcfg.msc)
Section 4: Assemblies at Runtime
 Loading
an assembly
 Concept
of Application Domain
 JITting
an assembly
 PreJITting
an assembly
Loading an Assembly
 Assembly

is Portable Executable (PE) file ...
... with CLR related information added
 Runtime
 Unaware
aware environment loads assembly directly
operating system loads assembly as PE

Entry point: stub that loads and calls CLR

CLR examines addtional header information
Application Domain
 Concept
 Provide
for application isolation
isolation at lower cost than processes
 AppDomains
 AppDomain
 Direct

are created by the runtime host
is created for each application
references between AppDomains disallowed
Requires proxies or copied objects
Loader Optimization
 Assembly

is SingleDomain by default
Each AppDomain loads and compiles assembly
 Assembly
can be marked for MultiDomain use

Assembly is compiled once

Mapped into all referencing AppDomains

A copy is available for each process

References to static data is indirected

Assembly is unloaded when process ends
 MultiDomainHost

Copy of code is hosted in each AppDomain
Just-In-Time Compilation
 MSIL

is made for compilation
Needs some per-method analysis
 Code
is compiled when needed

Compilation on a per-method base

Code that is not called is not compiled

Loader creates stub for each method
 First
step: Verification of type safety
 JITted
code is not persisted
PreJITting with ngen
 Complete

compile at installation time
PreJITted assembly is installed in GAC
 Speeds
up loading time significantly

Both IL and native image are loaded

No verification needed
 Native
image is not used...

...When module version ID of IL is different

...If the same applies to any dependencies

...Assembly binding policy has changed

Fallback to normal JIT process
Summary
 Assemblies
 Anatomy
 Units

as logical DLLs
of an assembly
of versioning
Strong names
 Installation
 Loading
in Global Assembly Cache
and Compiling
Questions?