Java Card RE 3.20 chapter5 Memory Model
The Java Card Virtual Machine conforms to the relevant subset of the Java Language Specification memory model. In particular, it provides a strong guarantee about the visibility and ordering of all individual actions (such as read and write) in an execution of a program that must be sequentially consistent with the order of the program.
Besides these rules, the Java Card Virtual Machine lifetime (see section 2) implies that the Java Card Virtual Machine uses non-volatile memory to persistently store object heap. Multiple memory layout and strategies are possible and depend on the capability and characteristics of the underlying hardware platform. Note that objects referenced from another persistent object must be persistent and a Java Card Virtual Machine implementation may use volatile memory for short-lived objects only referenced from the stack or volatile memory. In any case, specific implementation strategies using cache or storing such temporary objects in volatile memory must not affect their behavior. These objects shall behave like other objects and therefore conform to the rules defined in section 7 Transactions and Atomicity.
This has implications on the application programming model. An application must expect that operations performed on a persistent object will imply non-volatile memory updates (see also section 7 Transactions and Atomicity). Applications sometimes require objects that contain temporary (transient) data that need not be persistent or data that, for security reasons or because it is frequently updated, must not be stored in non-volatile memory. In this case, it must use transient objects.
This chapter describes special kinds of objects and how they are treated within a Java Card runtime environment.
Java Card 虚拟机符合 Java 语言规范内存模型的相关子集。具体而言,它对程序执行过程中所有操作(例如读取和写入)的可见性和顺序提供了强有力的保证,这些操作必须与程序的执行顺序保持一致。
除了这些规则之外,Java Card 虚拟机的生命周期(参见第 2 节)意味着它使用非易失性内存来持久存储对象堆。内存布局和策略多种多样,具体取决于底层硬件平台的性能和特性。请注意,从其他持久对象引用的对象必须是持久的,并且 Java Card 虚拟机实现可以使用易失性内存来存储仅从栈或易失性内存中引用的短生命周期对象。无论如何,使用缓存或将此类临时对象存储在易失性内存中的特定实现策略不得影响其行为。这些对象的行为应与其他对象相同,因此应符合第 7 节“事务和原子性”中定义的规则。
这会对应用程序编程模型产生影响。应用程序必须预期对持久对象执行的操作会涉及非易失性内存的更新(另请参阅第 7 节“事务和原子性”)。应用程序有时需要包含临时(瞬态)数据的对象,这些数据无需持久化,或者出于安全原因或频繁更新,不应存储在非易失性内存中。在这种情况下,必须使用瞬态对象。
本章介绍特殊类型的对象以及它们在 Java Card 运行时环境中的处理方式。
5.1 Transient Objects
The Java Card platform does not support the Java programming language keyword transient. However, Java Card technology provides methods to create transient arrays with primitive components or references to Object.
Note: In this section, the term field is used to refer to the component of an array object also.
The term “transient object” is a misnomer. It can be incorrectly interpreted to mean that the object itself is transient. However, only the contents of the fields of the object (except for the length field) have a transient nature. As with any other object in the Java programming language, transient objects within the Java Card platform exist as long as they are referenced from:
- The stack
- Local variables
- A class static field
- A field in another existing object
Java Card 平台不支持 Java 编程语言关键字 transient。但是,Java Card 技术提供了创建包含基本类型组件或对象引用的瞬态数组的方法。
注意:在本节中,术语 field 也用于指代数组对象的组件(数组值)。
术语“瞬态对象”并不准确。它可能被错误地理解为对象本身是瞬态的。然而,只有对象字段的内容(长度字段除外)才具有瞬态性质。与 Java 编程语言中的任何其他对象一样,Java Card 平台中的瞬态对象只要被以下位置引用就会存在:
- 堆栈
- 局部变量
- 类静态字段
- 另一个现有对象中的字段
5.1.1 Transient Objects Characteristics
A transient object within the Java Card platform has the following required behavior:
- The fields of a transient object shall be cleared to the field’s default value (zero, false, or null) at the occurrence of certain events (see Section 5.1.2 Events That Clear Transient Objects).
- For security reasons, the fields of a transient object shall never be stored in a “persistent memory technology.” Using current smart card technology as an example, the contents of transient objects can be stored in RAM, but never in EEPROM. The purpose of this requirement is to allow transient objects to be used to store session keys.
- Writes to the fields of a transient object shall not have a performance penalty. Using current smart card technology as an example, the contents of transient objects can be stored in RAM, while the contents of persistent objects can be stored in EEPROM. Typically, RAM technology has a much faster write cycle time than EEPROM.
- Writes to the fields of a transient object shall not be affected by “transactions.” That is, an abortTransaction never causes a field in a transient object to be restored to a previous value.
This behavior makes transient objects ideal for small amounts of temporary applet data that is frequently modified, but that need not be preserved across CAD or select sessions.
Java Card 平台中的瞬态对象必须具备以下行为:
- 当某些事件发生时(参见 5.1.2 节“清除瞬态对象的事件”),瞬态对象的字段应被清除为字段的默认值(零、false 或 null)。
- 出于安全考虑,瞬态对象的字段(组件)绝不能存储在“持久性存储器”中。以当前的智能卡技术为例,瞬态对象的内容可以存储在 RAM 中,但绝不能存储在 EEPROM 中。此要求的目的是允许使用瞬态对象来存储会话密钥。
- 写入瞬态对象的字段不应造成性能损失。以当前的智能卡技术为例,瞬态对象的内容可以存储在 RAM 中,而持久性对象的内容可以存储在 EEPROM 中。通常,RAM 技术的写入周期比 EEPROM 快得多。
- 写入瞬态对象的字段不应受“事务”的影响。也就是说,中止事务永远不会将瞬态对象中的字段恢复到先前的值。
这种行为使得瞬态对象非常适合用于存储少量频繁修改的临时小程序数据,这些数据无需在 CAD 或特定会话之间保留。
5.1.2 Events That Clear Transient Objects
Persistent objects are used for maintaining states that shall be preserved across card resets. When a transient object is created, one of two events is specified that causes its fields to be cleared. CLEAR_ON_RESET transient objects are used for maintaining states that shall be preserved across applet selections, but not across card resets. CLEAR_ON_DESELECT transient objects are used for maintaining states that must be preserved while an applet is selected, but not across applet selections or card resets.
持久对象用于维护需要在卡片重置后保留的状态。创建瞬态对象时,会指定两个事件之一来清除其字段。CLEAR_ON_RESET 瞬态对象用于维护需要在小程序选择后保留但不需要在卡片重置后保留的状态。CLEAR_ON_DESELECT 瞬态对象用于维护在小程序选中期间必须保留但不需要在小程序选择或卡片重置后保留的状态。
Details of the two clear events are as follows:
- CLEAR_ON_RESET - The object’s fields (except for the length field) are cleared when the card is reset. When a card is powered on, this also causes a card reset.
Note: It is not necessary to clear the fields of transient objects before power is removed from a card. However, it is necessary to guarantee that the previous contents of such fields cannot be recovered once power is lost.- CLEAR_ON_DESELECT - The object’s fields (except for the length field) are cleared whenever the applet is deselected and no other applets from the same context are active on the card. Because a card reset implicitly deselects the currently selected applet, the fields of CLEAR_ON_DESELECT objects are also cleared by the same events specified for CLEAR_ON_RESET.
两个清除事件的详细信息如下:
- CLEAR_ON_RESET - 当卡片复位时,对象的所有字段(长度字段除外)将被清除。卡片上电时,也会触发卡片复位。
注意:无需在卡片断电前清除瞬态对象的字段。但是,必须确保断电后无法恢复这些字段的先前内容。
- CLEAR_ON_DESELECT - 当小程序被取消选择且卡片上没有来自同一上下文的其他小程序处于活动状态时,对象的所有字段(长度字段除外)将被清除。由于卡片复位会隐式地取消选择当前选定的小程序,因此 CLEAR_ON_DESELECT 对象的字段也会通过与 CLEAR_ON_RESET 相同的事件进行清除。
The currently selected applet is explicitly deselected (its deselect method is called) only when a SELECT FILE command or MANAGE CHANNEL CLOSE command is processed. The currently selected applet is deselected and then the fields of all CLEAR_ON_DESELECT transient objects owned by the applet are cleared if no other applets from the same context are active on the card, regardless of whether the SELECT FILE command:
- Fails to select an applet
- Selects a different applet
- Reselects the same applet
仅当执行 SELECT FILE 命令或 MANAGE CHANNEL CLOSE 命令时,才会显式取消选择当前选定的小程序(调用其取消选择方法)。如果卡片上没有来自同一上下文的其他小程序处于活动状态,则无论 SELECT FILE 命令是否出现以下情况,都会取消选择当前选定的小程序,并清除该小程序拥有的所有 CLEAR_ON_DESELECT 瞬态对象的字段:
- 选择小程序失败
- 选择其他小程序
- 重新选择同一小程序
5.2 Temporary Objects
Temporary objects are short lived objects intended to be used only by current execution flow for computations on temporary data and can only be referenced from the execution stack as local variables or method parameters. The Java Card Runtime Environment detects and restricts attempts to store references to these objects as part of the firewall functionality to prevent unauthorized reuse of the object instance. See section 6.2.8 Class and Object Access Behavior for more details.
An application has no direct control on designating temporary objects and the temporary attribute is assigned by the Java Card Runtime Environment in a limited number of cases:
- Some JCRE Entry Point Objects are temporary objects: the APDU object and all Java Card RE owned exception objects are examples of temporary objects.
- All global arrays are temporary objects: the APDU buffer is an example of global array
- All array views are temporary objects.
临时对象是生命周期很短的对象,仅供当前执行流程用于对临时数据进行计算,并且只能作为局部变量或方法参数从执行堆栈中引用。Java Card 运行时环境 (JCRE) 会检测并限制对这些对象的引用存储尝试,这是防火墙功能的一部分,旨在防止未经授权重用对象实例。有关更多详细信息,请参阅 6.2.8 节“类和对象访问行为”。
应用程序无法直接控制临时对象的指定,临时属性由 Java Card 运行时环境在有限的情况下分配:
-
某些 JCRE 入口点对象是临时对象:APDU 对象和所有 Java Card RE 拥有的异常对象都是临时对象的示例。
-
所有全局数组都是临时对象:APDU 缓冲区就是一个全局数组的示例。
-
所有数组视图都是临时对象。
5.3 Array views
In certain scenarios an application may need to extract, process or share only a subset of an array. An application may accomplish this by creating a defensive copy of the data in a separate array. However, this requires additional memory and data synchronization between the original array and the copy of its subset. Java Card platform provides an alternative mechanism to accomplish this by creating a temporary array object, called “Array View”, which is a view on all or a subset of the array elements of an actual parent array. In this chapter we are using indistinctly array view or view to refer to it.
在某些情况下,应用程序可能只需要提取、处理或共享数组的子集。应用程序可以通过在单独的数组中创建数据的备份来实现这一点。然而,这需要额外的内存,并且需要在原始数组和其子集副本之间进行数据同步。Java Card 平台提供了一种替代机制,即创建一个名为“数组视图”的临时数组对象。数组视图是对实际父数组中所有元素或部分元素的视图。在本章中,我们将不区分使用“数组视图”或“视图”来指代它。
5.3.1 Characteristics of an Array View
An array view must have the following characteristics.
From Java language perspective, an array view is an array and is accessed via the same set of array-access byte-codes. In particular, an array view can be used as parameter of any method that accept an array with same type.
- The elements of a view are mapped to the selected subset of the actual parent array and any modification in the parent array is visible in the view and vice versa. Also note that a view can map all the elements of its parent array. By construction, a view is a different object from its parent, with its own attributes (type, firewall context, length) but with same memory location for its elements.
- The elements of a view have the exact same type as the elements of the parent array.
- A view has configurable read/write access rights that can be specified at its creation time. These access rights apply only to accessing the elements of the array (bytecodes aload, astore), not to invoking methods, reading the length or checking the type.
- A view is a temporary object. Consequently, it is not possible to store a reference to a view in class variables, instance variables or array components.
- A parent array cannot be deleted (garbage collected) if an array view mapping its elements still exist.
数组视图必须具备以下特性。
-
从 Java 语言的角度来看,数组视图本身就是一个数组,并且使用相同的数组访问字节码进行访问。具体来说,数组视图可以作为任何接受相同类型数组的方法的参数。
-
视图的元素映射到实际父数组的选定子集,父数组的任何修改都会反映到视图中,反之亦然。另请注意,视图可以映射其父数组的所有元素。从构造上看,视图与其父对象不同,具有自己的属性(类型、防火墙上下文、长度),但其元素存储在相同的内存位置。
-
视图的元素与父数组的元素具有完全相同的类型。
-
视图具有可配置的读/写访问权限,可以在创建时指定。这些访问权限仅适用于访问数组元素(字节码 aload、astore),不适用于调用方法、读取长度或检查类型。
-
视图是临时对象。因此,无法将视图的引用存储在类变量、实例变量或数组元素中。
-
如果映射其元素的数组视图仍然存在,则无法删除(垃圾回收)父数组。

5.3.2 Creating and Using an Array View
To create an Array View, an application must specify a source array and the range of elements that
should be mapped by this view. The source can be an actual array or a view and will be used to retrieve
the array elements to be mapped. The following checks are performed before creating the view:
- The specified source must be an array accessible from the context creating the view. Particularly, when the source is a CLEAR_ON_DESELECT transient array, the currently active context must be the context of the currently selected applet.
- The range of elements specified must not go beyond the boundaries of the specified source array. If the source array is a view, the array view being created shall map the actual array elements and the elements mapped shall remain in the range of elements accessible to the source view.
- The created array view shall not be granted access rights that are not allowed on the source array. For example, it is not possible to create a writable view mapping the elements from a read-only view.
要创建数组视图,应用程序必须指定源数组以及该视图应映射的元素范围。源可以是实际数组或视图,用于检索要映射的数组元素。创建视图之前会执行以下检查:
-
指定的源必须是创建视图的上下文可访问的数组。特别是,当源是 CLEAR_ON_DESELECT 瞬态数组时,当前活动上下文必须是当前选定小程序的上下文。
-
指定的元素范围不得超出指定源数组的边界。如果源数组是视图,则创建的数组视图应映射实际数组元素,并且映射的元素应保持在源视图可访问的元素范围内。
-
创建的数组视图不得被授予源数组不允许的访问权限。例如,无法创建映射只读视图元素的可写视图。
A view can be created in the context of the application creating the view, or in the context of a provided Sharable Interface Object of a server application. In the latter case, the main purpose is to share a subset of the elements of an array as parameter with a server application and the array view will only be accessible in the context of the server application and not in the context of the application creating it. For more details, see section 6.2.2.2 Sharing using Array Views and see the JCSystem.makeArrayView() method definition in the Application Programming Interface, Java Card Platform, Version 3.2.
A view can be used as parameter of any method of the Java Card API that accept an array with same type. The parameters used as input buffer(s) must be readable and API implementation must support read-only views for these parameters. The parameters used as output buffer(s) must be writable and API implementation must support write-only views for these parameters.
视图可以在创建该视图的应用程序上下文中创建,也可以在服务器应用程序提供的共享接口对象 (SIO) 的上下文中创建。在后一种情况下,主要目的是将数组元素的子集作为参数共享给服务器应用程序,并且该数组视图只能在服务器应用程序的上下文中访问,而不能在创建它的应用程序的上下文中访问。更多详细信息,请参阅 6.2.2.2 节“使用数组视图进行共享”,并参阅 Java Card 平台应用程序编程接口 (API) 3.2 版中的 JCSystem.makeArrayView() 方法定义。
视图可以用作 Java Card API 中任何接受相同类型数组的方法的参数。用作输入缓冲区的参数必须是可读的,并且 API 实现必须支持这些参数的只读视图。用作输出缓冲区的参数必须是可写的,并且 API 实现必须支持这些参数的只写视图。
An application can use the following methods to get details about a view
- JCSystem.isArrayView(Object) to check if an object is an array view
- JCSystem.getAttributes(Object) to get the access attributes of a view ( ATTR_READABLE_VIEW , ATTR_WRITABLE_VIEW )
- JCSystem.isTransient(Object) to get the memory type where the elements mapped by the view are located.
- SensitiveArrays.isIntegritySensitive(Object) to check whether the view is mapping elements that belong to a sensitive array.
For that purpose, a call to JCSystem.isTransient(Object) or SensitiveArrays.isIntegritySensitive(Object) on a view returns the same value as if called on the parent array.
应用程序可以使用以下方法获取视图的详细信息:
JCSystem.isArrayView(Object)用于检查对象是否为数组视图。JCSystem.getAttributes(Object)用于获取视图的访问属性(ATTR_READABLE_VIEW、ATTR_WRITABLE_VIEW)。JCSystem.isTransient(Object)用于获取视图映射的元素所在的内存类型。SensitiveArrays.isIntegritySensitive(Object)用于检查视图是否映射了属于敏感数组的元素。
为此,对视图调用 JCSystem.isTransient(Object) 或 SensitiveArrays.isIntegritySensitive(Object) 返回的值与对父数组调用时返回的值相同。
更多推荐
所有评论(0)