# US 2021/0232441 A1 · Data race detection with per-thread memory protection

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

- Number: US 2021/0232441 A1
- Document ID: US20210232441A1
- Status: Published application
- Kind: A1
- Application number: 16/752,470
- Filed: 2020-01-24
- Published: 2021-07-29
- Earliest claimed: 2020-01-24
- Assignees: Microsoft Technology Licensing, LLC
- Applicants: Microsoft Technology Licensing, LLC
- Inventors: Sangho Lee; Adil Ahmad
- Web page: https://patentgrep.com/patent/US20210232441A1

## CPC

- G06F 9/526: {Mutual exclusion algorithms}
- G06F 9/544: {Buffers; Shared memory; Pipes}
- G06F 11/3624: {by performing operations on the source code, e.g. via a compiler}
- G06F 11/3632: {of specific synchronisation aspects}
- G06F 12/084: with a shared cache
- G06F 11/3636: {by tracing the execution of the program}
- G06F 12/10: Address translation
- G06F 2212/657: Virtual address space management
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

Data race detection in multi-threaded programs can be achieved by leveraging per-thread memory protection technology in conjunction with a custom dynamic memory allocator to protect shared memory objects with unique memory protection keys, allowing data races to be turned into inter-thread memory access violations. In various embodiments, threads acquire or release the keys used for accessing protected memory objects at the entry and exit points of critical sections within the program. An attempt by a thread to access a protected memory object within a critical section without the associated key triggers a protection fault, which may be indicative of a data race.

## Claims

### Claim 1

A non-transitory machine-readable medium storing instructions for execution by a computer processor, the instructions causing the processor to perform operations for detecting data races in a multi-threaded program comprising one or more critical sections and a plurality of shared memory objects, the operations comprising:
protecting each of the plurality of shared memory objects by a respective access key selected from a set of keys associated with a critical domain;
upon entry of any of multiple threads of the program into any of the one or more critical sections, providing to the thread, for each of the plurality of shared memory objects that is accessed within the section, the respective access key if the access key is not held by any other of the multiple threads; and
upon any attempt, by any of the multiple threads, to access any of the shared memory objects without holding the respective access key, detecting a protection fault.

### Claim 2 (depends on 1)

The machine-readable medium of claim 1, wherein protecting each of the plurality of shared memory objects by a respective access key comprises assigning the plurality of shared memory objects to respective virtual memory pages each storing only one of the plurality of shared memory objects, and assigning the access keys to the respective virtual memory pages storing the respective memory objects.

### Claim 3 (depends on 2)

The machine-readable medium of claim 2, wherein the set of keys associated with the critical domain comprises a plurality of memory protection keys supported by the processor.

### Claim 4 (depends on 3)

The machine-readable medium of claim 3, wherein the access keys are assigned to the respective virtual memory pages by the program via a system call.

### Claim 5 (depends on 2)

The machine-readable medium of claim 2, the operations further comprising consolidating multiple of the virtual memory pages in a single physical memory page, wherein the respective memory objects are stored within the multiple virtual memory pages at multiple respective offsets.

### Claim 6 (depends on 1)

The machine-readable medium of claim 1, the operations further comprising, during compilation of the program, instrumenting the shared memory objects with a memory allocator configured to assign the plurality of shared memory objects to the respective virtual memory pages, and instrumenting synchronization functions defining the one or more critical sections with wrapper functions configured to provide access keys to threads upon entry into the one or more critical sections.

### Claim 7 (depends on 1)

The machine-readable medium of claim 1, wherein providing an access key to one of the multiple threads comprises setting access rights to the access key in a thread-local processor register associated with the thread.

### Claim 8 (depends on 1)

The machine-readable medium of claim 1, wherein each of the plurality of shared memory objects is protected by the respective access key upon first access by one of the multiple threads from within one of the one or more critical sections.

### Claim 9 (depends on 8)

The machine-readable medium of claim 8, wherein each of the plurality of shared memory objects is initialized as part of a non-critical domain by assignment of a key associated with the non-critical domain, and migrates to the critical domain by assignment of the respective access key in response to a protection fault caused by an attempt, by any of the multiple threads, to execute the respective shared memory object without holding the key associated with the critical domain.

### Claim 10 (depends on 8)

The machine-readable medium of claim 8, wherein, for each of the plurality of shared memory objects, protecting the shared memory object by a respective key upon first access within one of the one or more critical sections comprises selecting, from the set of keys associated with the critical domain, an access key not assigned to any of the shared memory objects accessed within any other of the one or more critical sections.

### Claim 11 (depends on 8)

The machine-readable medium of claim 8, wherein, for each of the plurality of shared memory objects, protecting the shared memory object by a respective key upon first access by one of the multiple threads within one of the one or more critical sections comprises selecting, from the set of keys associated with the critical domain, an access key not held by any other of the multiple threads.

### Claim 12 (depends on 1)

The machine-readable medium of claim 1, the operations further comprising, upon exit of any of the multiple threads from any of the one or more critical sections, retracting any one or more access keys provided to the thread upon entry or during execution of the critical section.

### Claim 13 (depends on 1)

The machine-readable medium of claim 1, the operations further comprising automatically analyzing detected protection faults to eliminate redundant faults or false alarms.

### Claim 14

A system for detecting data races in a multi-threaded program comprising one or more critical sections and a plurality of shared memory objects, the system comprising:
a computer processor supporting per-thread memory protection; and
computer memory storing a plurality of software components each comprising instructions for execution by the computer processor, the software components comprising:
a memory allocator configured to assign the plurality of shared memory objects to respective virtual memory pages each storing only one of the plurality of shared memory objects, each shared memory object being protectable by a memory protection key assigned to the respective virtual memory page;
synchronization wrapper functions associated with the one or more critical sections to provide to a thread, upon entry of the thread into one of the one or more critical sections, for each of the plurality of shared memory objects that is accessed within the critical section, the respective memory protection key, provided that no other thread holds the memory protection key, wherein an attempt by the thread to access any of the shared memory objects without holding the respective memory protection key causes a protection fault; and
a fault handler to receive and process any protection fault raised by access to one or the memory objects without the respective memory protection key.

### Claim 15 (depends on 14)

The system of claim 14, wherein the fault handler is configured store at least some of the protection faults to a fault log.

### Claim 16 (depends on 15)

The system of claim 15, wherein the software components further comprise a post-mortem analyzer to prune the protection faults stored in the fault log to remove at least one of redundant faults and false alarms.

### Claim 17 (depends on 14)

The system of claim 14, wherein the software components further comprise a backend code analysis and instrumentation component to locate, in source code of the program, memory allocations to shared memory objects and synchronization functions associated with the one or more critical sections, and to replace the memory allocations with the memory allocator and the synchronization functions with the synchronization wrapper functions.

### Claim 18

A method for data race detection during execution of a multi-threaded program, the method comprising:
maintaining a first set of access keys available for acquisition;
maintaining multiple second sets of access keys held by respective ones of multiple threads, the first set of access keys and the multiple second sets of access keys collectively containing an entirety of keys, wherein an access key is removed from the first set of keys upon acquisition by any of the multiple threads and added back to the first set of keys upon release by that thread;
maintaining one or more third sets of access keys for one or more respective critical sections of the program, each access key within one of the one or more third sets of access keys being one of the keys of the entirety of keys and being associated with a memory object to be accessed within the respective critical section; and
registering a data race upon an attempt of any of the multiple threads to access a memory object whose associated access key is held by another one of the multiple threads.

### Claim 19 (depends on 18)

The method of claim 18, wherein maintaining the multiple second sets of access keys comprises, at a time of entry of one of the multiple threads into one of the one or more critical sections, acquiring, by the thread, any key of the third set of access keys associated with the critical section that is within the first set of access keys at the time of entry.

### Claim 20 (depends on 18)

The method of claim 18, wherein maintaining the one or more third sets of access keys comprises, for each of the one or more critical sections, upon a first attempt by one of the multiple threads to access a memory object within the critical section, assigning one of the keys of the entirety of keys to the memory object.
