Skip to content

Commit

Permalink
ownership proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 3, 2024
1 parent f80cade commit 5b05627
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 6 deletions.
74 changes: 74 additions & 0 deletions ownership_proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# New qualifiers to enable checked lifetime contracts

## Abstract

This proposal introduces new qualifiers to improve
safety regarding the misuse of object lifetimes. These new qualifiers add contracts
that can be checked at compile time.

## Introduction, Motivation

The motivation is to check at compile time the correct usage of patterns that already exist in C.
For instance, how could we check at compile time the correct usage of `fopen` and `fclose`.

```c
#include <stdio.h>
int main()
{
FILE * f = fopen("file.txt", "r");
if (f)
fclose(f);
}
```


## Design

We introduce several qualifiers called ownership qualifiers.
Just like `const` has in its basic principle avoid the object to be modified,
Ownership qualifiers have principles in mind and all the rules serve to keep the objective.

Principles:

1 - Each object has only one owner
2 - Ownership can be transferred between owners.
3 - Before the end of its lifetime, the owner object must transfer the ownership of the object it owns.


Lets introduce the first qualifier `_Owner`.

`_Owner` indicates that an object works as a reference to another object,
managing its lifetime as its unique owner.

So here is the first rule.

Constrain 1 - The result of a function returning a owner object cannot be discarded.

```c
int* f();
int main(){
f();
}
```

This rule is necessary to ensure the principle number 3.
"Before the end of its lifetime, the owner object must transfer the ownership of the object it owns."

Constrain 2 - The result of a function returning a owner object must be transferred
to another owner object.

```c
int* f();
int main(){
int * p = f(); //warning/error
}
```

```c
int* f();
int main(){
int * _Owner p = f(); //ok
}
```

TODO
111 changes: 105 additions & 6 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,105 @@
struct {
int : 0;
long a;
} b;
void c() { b.a; }
static_assert(sizeof(b.a) == sizeof(long));
void putchar(char c);
void* malloc(int s);
void close(int i);
void abort();
#pragma expand __COUNTER__

void
main(typeof
(*malloc(0)))
{
((typeof(&close))
(const struct d
{
typeof("")* x;
})
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ &
__COUNTER__ |
__COUNTER__]);
((typeof(&close))
(const struct d)
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ &
__COUNTER__]);
((typeof(&close))
(const struct d)
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ &
~__COUNTER__]);
((typeof(&close))
(const struct d)
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ %
__COUNTER__ &
__COUNTER__]);
((typeof(&close))
(const struct d)
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ -
__COUNTER__ -
__COUNTER__ &
__COUNTER__ &
__COUNTER__]);
((typeof(&close))
(const struct d)
{
(const struct d)
{
(typeof(abort())*)
(typeof(sizeof~!0))
putchar
}.x
}.x
)(
__func__
[__COUNTER__ -
~__COUNTER__ +
~__COUNTER__ &
__COUNTER__]);
}

0 comments on commit 5b05627

Please sign in to comment.