# US 2014/0208323 A1 · Preventing unsafe sharing through confinement of mutable captured variables

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2014/0208323 A1
- Document ID: US20140208323A1
- Status: Published application
- Kind: A1
- Application number: 14/223,492
- Filed: 2014-03-24
- Published: 2014-07-24
- Earliest claimed: 2011-02-15
- Assignees: Oracle International Corporation
- Applicants: Oracle International Corporation
- Inventors: Brian Goetz; Alexander R. Buckley
- Web page: https://patentgrep.com/patent/US20140208323A1

## CPC

- G06F 9/46: Multiprogramming arrangements
- G06F 8/31: {Programming languages or programming paradigms}
- G06F 9/461: {Saving or restoring of program or task context}
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

The disclosed embodiments provide a system that facilitates the development and execution of a software program. During operation, the system provides a mechanism for restricting a variable to a runtime context in the software program. Next, the system identifies the runtime context during execution of the software program. Finally, the system uses the mechanism to prevent incorrect execution of the software program by ensuring that a closure capturing the variable executes within the identified runtime context.

## Claims

### Claim 1

A computer-implemented method for facilitating the development and execution of a software program, comprising:
during execution of the software program:
receiving a first context identifier that identifies a creation runtime context and a creation thread that are used to create a closure that captures a variable;
receiving a second context identifier that identifies a subsequent-execution runtime context and a subsequent-execution thread for a subsequent execution of the closure; and
preventing unsafe sharing of variables captured by closures by ensuring that the subsequent-execution runtime context corresponds to the creation runtime context by:
in response to determining that the first context identifier does not correspond to the second context identifier, preventing the unsafe sharing of variables captured by closures by modifying the closure to execute within the creation thread.

### Claim 2 (depends on 1)

The computer-implemented method of claim 1, wherein the mechanism declaratively restricts the variable to the runtime context in the software program using a keyword in source code for the software program.

### Claim 3 (depends on 1)

The computer-implemented method of claim 1, wherein the first context identifier is obtained from at least one of:
an argument to a function representing the closure; and
a frame object containing the variable.

### Claim 4 (depends on 1)

The computer-implemented method of claim 1, wherein the comparison includes at least one of:
an equality operation on the first and second context identifiers; and
a bitwise operation using the first or second context identifiers.

### Claim 5 (depends on 1)

The computer-implemented method of claim 1, wherein preventing the unsafe sharing of variables captured by closures comprises raising an exception during the execution of the software program.

### Claim 6 (depends on 1)

The computer-implemented method of claim 1, wherein the mechanism is used to prevent incorrect execution of the software program during at least one of:
entry to the body of the closure;
reading of the variable by the closure; and
writing to the variable by the closure.

### Claim 7 (depends on 1)

The computer-implemented method of claim 1, wherein at least one of the creation runtime context and the subsequent-execution runtime context corresponds to at least one of a thread, a processor, and a processor core.

### Claim 8

A system for facilitating the development and execution of a software program, comprising:
a mechanism for restricting a variable to a runtime context in the software program; and
a runtime system configured to, during execution of the program:
receive a first context identifier that identifies a creation runtime context and a creation thread that are used to create a closure that captures the variable;
receive a second context identifier that identifies a subsequent-execution runtime context and a subsequent-execution thread for a subsequent execution of the closure; and
use the mechanism to prevent the software program from unsafely sharing variables captured by closures by ensuring that the subsequent-execution runtime context corresponds to the creation runtime context by:
in response to determining that the first context identifier does not correspond to the second context identifier, prevent the software program from unsafely sharing variables captured by closures by modifying the closure to execute within the creation thread.

### Claim 9 (depends on 8)

The system of claim 8, wherein the mechanism declaratively restricts the variable to the runtime context in the software program using a keyword in source code for the software program.

### Claim 10 (depends on 8)

The system of claim 8, wherein the comparison includes at least one of:
an equality operation on the first and second context identifiers; and
a bitwise operation using the first or second context identifiers.

### Claim 11 (depends on 8)

The system of claim 8, wherein preventing the software program form unsafely sharing variables captured by closures comprises raising an exception during the execution of the software program.

### Claim 12 (depends on 8)

The system of claim 8, wherein the mechanism is used to prevent incorrect execution of the software program during at least one of:
entry to the body of the closure;
reading of the variable by the closure; and
writing to the variable by the closure.

### Claim 13 (depends on 8)

The system of claim 8, wherein at least one of the runtime context that is used to create the closure and the subsequent-execution runtime context corresponds to at least one of a thread, a processor, and a processor core.

### Claim 14

A non-transitory computer-readable storage medium storing instructions that when executed by a computer cause the computer to perform a method for facilitating the development and execution of a software program, the method comprising:
providing a mechanism for restricting a variable to a runtime context in the software program;
during execution of the software program:
receiving a first context identifier that identifies a creation runtime context and a creation thread that are used to create a closure that captures the variable;
receiving a second context identifier that identifies a subsequent-execution runtime context and a subsequent-execution thread for a subsequent execution of the closure; and
preventing the software program from unsafe sharing of variables captured by closures by ensuring that the subsequent-execution runtime context corresponds to the creation runtime context by:
in response to determining that the first context identifier does not correspond to the second context identifier, preventing the unsafe sharing of variables captured by closures within the subsequent-execution runtime context by modifying the closure to execute within the creation thread.

### Claim 15 (depends on 14)

The computer-readable storage medium of claim 14, wherein preventing the unsafe sharing of variables captured by closures comprises raising an exception during the execution of the software program.

### Claim 16 (depends on 14)

The computer-readable storage medium of claim 14, wherein the mechanism is used to prevent incorrect execution of the software program during at least one of:
entry to the body of the closure;
reading of the variable by the closure; and
writing to the variable by the closure.

### Claim 17 (depends on 14)

The computer-readable storage medium of claim 14, wherein the mechanism declaratively restricts the variable to the runtime context in the software program using a keyword in source code for the software program.
