Skip to content

Latest commit

 

History

History
205 lines (166 loc) · 11.6 KB

Architecture.md

File metadata and controls

205 lines (166 loc) · 11.6 KB

Architecture

.NET for Android runtime

Java.Interop provides the JNI bridge between managed code and Android's Java VM. Managed bindings use JniPeerMembers to resolve and invoke Java methods and fields. Java-to-managed calls use generated Java callable wrappers and registered JNI callbacks. CoreCLR and NativeAOT applications use generated trimmable typemaps for peer lookup, constructor activation, and callback registration.

See Java and managed interoperability with the trimmable TypeMap for the build pipeline, generated callbacks, runtime startup, and activation flow.

Values and reference ownership

JniObjectReference is an IntPtr-backed struct representing a JNI local, global, or weak global reference. Its JniObjectReferenceType identifies the reference kind. Ownership transfer is explicit; owned references must be released with JniObjectReference.Dispose (ref reference).

JniMethodInfo and JniFieldInfo represent method and field IDs, which do not require reference disposal. JniPeerMembers caches these IDs for subsequent invocations.

JniValueMarshaler and JniValueMarshalerState provide non-expression value conversion and argument-state creation/cleanup. Primitive, string, and array conversion remain supported. JavaObject and JavaException maintain managed peers and their JNI reference ownership.

Native callback ownership

JniType.RegisterNativeMethods retains the original array and its owner only after successful registration. Callers must not modify the array or its entries through any alias while its callbacks can be invoked. Only the latest successfully registered array is retained, so callers must not start another JNI registration on the same instance before successful unregistration. This safety requirement is not checked at runtime. Failed or partial JNI registration does not retain the supplied array; the caller must keep its delegates rooted until successful unregistration and completion of all callbacks. Registration errors propagate as exceptions without attempting to recover from or roll back partial native registration.

Disposal unregisters callbacks before removing the runtime's owner root or releasing the JNI reference. Failed unregistration preserves those roots and the reference. Prevent new calls and wait for callbacks already running to finish before unregistering or disposing the owner.

Direct JniEnvironment.Types.RegisterNatives calls do not retain callback roots after returning. Their callers must keep delegates or raw callback storage callable and rooted for as long as JNI can invoke them, including after failed or partial registration. Prevent new calls to the registered native methods before unregistration. Release callback storage only after unregistration succeeds and all callbacks already running have finished.

JniNativeMethod name/signature storage must remain readable and NUL-terminated through registration. Its raw callback must match the JNI descriptor's ABI, including the environment and receiver/class arguments.

Borrowed UTF-8 metadata

Retained names/signatures use byte*; JNI handles and method/field IDs remain opaque IntPtr values. ReplacementMethodInfo.TargetJni*Utf8Pointer and the JniTypeManager.GetReplacementTypeInfoUnsafe / GetReplacementMethodInfoUnsafe hooks expose borrowed storage. Owners must retain readable bytes through the NUL terminator, unchanged for the runtime and every cached/diagnostic read, even after a later lookup fails. Read-only aliases may overlap; no concurrent write or free is allowed. Java.Interop neither roots nor frees these buffers.

Peer names and Debug JniMethodInfo strings are converted lazily under that lifetime contract. Managed-string alternatives remain safe; non-null native names take precedence. Complete UTF-8 literals retain their zero-copy path. Remapping equality, hashing and ToString inspect addresses, not their contents.

The .NET 11 RC2 native-int TargetJni*Utf8 accessors and two native-int GetReplacementTypeInfoCore / GetReplacementMethodInfoCore virtual slots stay safe-callable and warning-obsolete. They are explicit compatibility exceptions, not pointer validation. Typed base hooks preserve old overrides and base calls; the distinct type-producer name avoids existing out var ambiguity.

Caller obligations and native implementation scopes are independent; managed wrappers remain safe. Typed pointer address getters are safe-callable; borrowed storage setters and raw hooks require unsafe callers under the updated rules.

Owned JNI implementation memory

Managed alternatives such as JniType.DefineClass with a byte[] and JniEnvironment.Strings.NewString with a string own their pinning. Unsafe operations remain inside their implementations; local scopes do not transfer buffer ownership to callers or change JNI reference cleanup.

JniNativeMethod preserves the sequential layout of JNI's JNINativeMethod. It stores opaque callback addresses as IntPtr. Construction only copies addresses; the constructor is safe-callable and does not establish a pointee-lifetime contract. Registration requires readable NUL-terminated names/signatures and a descriptor-compatible callback rooted until successful unregistration and completion of all in-flight callbacks.

The project uses preview syntax for pointer declarations outside unsafe type contexts and safe explicit-layout scalar fields. It enables the updated C# memory-safety rules. Object-reference control blocks and JavaVM arguments retain their allocation, encoding, and release behavior. JNI object handles and method/field IDs remain opaque IntPtr values.

Native caller contracts

JNI declarations distinguish caller-owned raw memory from implementation-owned buffers. Their <safety> documentation describes buffer bounds, pinning, and lifetimes. Managed alternatives such as JniType.DefineClass with a byte[] and JniEnvironment.Strings.NewString with a string own their pinning and keep unsafe operations inside their implementations.

Caller annotations and implementation regions are audited independently. JNI descriptors select caller-unsafe declarations independently of their documentation. Method-level safety contracts are authored in those descriptors, not inferred from parameter names; raw forwarders reference a shared JNI contract. Each audited unsafe body has a // SAFETY: explanation naming the storage, extent, ABI, or lifetime it relies on. The raw JniNativeMethods boundary requires a live JNIEnv* on its attached thread; that obligation is not implied merely by using opaque object, field, or method handles.

JniEnvironment.InstanceMethods and StaticMethods retain their existing caller-unsafe classification. A nonzero method ID does not verify its descriptor or make a parameterless invocation safe. Managed wrappers with known descriptors discharge that obligation in local implementation blocks or unsafe (...) expressions.

For UTF-8 span lookups, callers must provide readable NUL-terminated backing storage throughout the call. Complete C# UTF-8 literals supply a trailing NUL outside the span length; copying or slicing a span does not preserve that guarantee. These APIs do not add terminators or retain their input spans.

Native registration names and signatures must survive registration. Callbacks must match their JNI descriptors and remain callable and rooted until successful unregistration and completion of all in-flight callbacks, including after failed or partial registration. Declaration-level contracts do not validate callback ABIs or change registration ownership.

An address-only JniNativeMethod constructor stores descriptor addresses; it does not read their contents or invoke a callback. Registration, not address storage, owns the ABI, provenance, and lifetime obligations described above.

Java.Interop sets MemorySafetyRulesVersion=2: modules carry MemorySafetyRulesAttribute (2) and caller-unsafe members carry RequiresUnsafeAttribute. Updated-rule consumers need unsafe call contexts, including for pointer-free APIs with JNI ABI obligations. Legacy consumers retain their existing compiler behavior; this metadata does not validate memory or ABIs. Generated JNI wrappers and T4 inputs keep implementation unsafe contexts local without changing marshaling, dispatch, or cleanup. Single-value operations use unsafe (...) expressions, including return ref unsafe (...) for borrowed refs; pinning, multi-statement operations, and void calls retain unsafe blocks.

Generated managed binding safety

Managed binding methods, properties and ordinary constructors stay caller-safe; JNI operations use local unsafe blocks with concrete // SAFETY: rationale. SourceWriter body/accessor flags do not imply IsUnsafe on the declaration. Constants, guards and managed-only operations stay outside these blocks where possible. Interleaved JNI argument preparation, invocation and cleanup remain together when their temporary lifetimes require it; success/failure cleanup, normal-return copy-back, dispatch and allocations are unchanged.

The same rule applies to hand-written wrappers: JNIEnv.NewString(string) owns its managed-string conversion, JNIEnv.NewArray(bool[]) allocates the matching Java array and pins its managed source for the synchronous copy, and Throwable.Class uses a fixed no-argument descriptor and transfers its returned local reference. These operations do not pass an unchecked caller-owned buffer or descriptor through a safe declaration.

Raw callback entry points and marshal targets require the registered ABI, current-thread JNI frame and live compatible borrowed references without concurrent deletion. Their <safety> contracts do not validate raw addresses. Typed managed targets stay safe. Generated syntax and the existing legacy compiler test harness are preserved. Mono.Android opts into the updated memory-safety rules; customer projects do not opt in implicitly. Public Mono.Android members that shipped before the opt-in remain safe-callable; their <safety> contracts still apply but are not compiler-enforced.

Throwable bindings forward JniObjectReferenceOptions.None through internal, unregistered empty-peer constructors, preserving partial hooks and normal JNI construction without adding externally accessible overloads. This route is available only within the assembly that defines the constructors. Imported bases are checked for an externally accessible safe constructor; their internal constructors do not qualify. Otherwise the generator retains legacy zero-handle initialization. An updated-rules external raw base can therefore still reject a safe derived initializer; internal forwarding does not solve that separate cross-assembly enforcement boundary.

Raw constructors, including Throwable and class/interface invokers, construct JNI peers, not opaque-address containers. Their documented handle type, lifetime, thread, aliasing, ownership and failure duties still apply. RequiresUnsafe enforcement is explicitly deferred to preserve safe zero/DoNotTransfer base initializers, without adding a general initialization API.

Naming conventions

Types such as JavaObject and JavaArray<T> provide managed wrappers for Java objects. Types such as JniObjectReference and JniArgumentValue represent the lower-level JNI references and argument values used by those wrappers.

.NET 12 API retirement

.NET 12 removes the obsolete runtime expression-marshaling builder, context, hooks, and the unused JniValueManager.ActivatePeer contract. These are not part of generated callbacks or trimmable typemap activation. See the breaking-change and migration notes for the removed APIs and migration guidance.