flang the compiler proves your program cannot hang 0.7.41 GitHub Try in the browserTry it Русский

Memory allocator

docs/examples/allocator/allocator.flang is a memory allocator written as a pure function. The heap is data: a list of free segments and the total size. A request is a value: «Взять» (take) so many, or «Вернуть» (return) an address and a length. The allocator is the function «Шаг кучи», which returns the new heap, an address and the flag «удалось» (succeeded):

тотальная функция «Шаг кучи»
  принимает куча: «Куча», запрос: «Запрос»
  возвращает «Отклик кучи»

The function does not touch memory — it answers what is now to be considered taken. The approach is the same as in the UART driver docs/examples/driver/uart.flang and in the MSI driver.

The program does not replace malloc: the runtime built from C needs memory before the first «Куча» value exists.

What is in the file

1 file, 429 lines .

Types: «Отрезок» (segment: start, length), «Куча» (heap: free segments, total), «Запрос» (request: the variants «Взять» and «Вернуть»), «Отклик кучи» (heap reply: heap, address, succeeded). The heap size is the constant of the function «Размер кучи», 4096.

What the functions do:

How to run

flang test docs/examples/allocator/allocator.flang
flang run docs/examples/allocator/allocator.flang --function '«Отклик первой выдачи»' --args '{}' --trust
flang check docs/examples/allocator/allocator.flang --proof

test runs the examples (37 of 37 pass):

docs/examples/allocator/allocator.flang: примеров 37, прошло 37, не прошло 0

run calls one function. «Отклик первой выдачи» (the reply to the first allocation) takes 64 units from a fresh heap of 4096:

на веру: доказанность не считалась — запуск по ключу --trust
{"куча":{"свободные":[{"начало":64,"длина":4032}],"всего":4096},"адрес":0,"удалось":true}

Without --trust, run refuses with exit code 3 and lists the unproved claims: not everything in the file is proved.

check --proof prints a verdict for every claim. The last lines:

итог:
  функций 25: тотальных 25, обычных 0
  обещание несёт: композиция 25, структура 0, точный шаг 0, постоянный шаг 0, объявленная мера 0
  сторожей в рантайме: 0 мест
  законов на сетке: 0 (значений в сетках 0); на веру: 0
  утверждений 90: доказано 82 (из них индукцией 3) (из них без теоремы 79, объявленным типом 13), сетка 8, объявлено, не доказано 0

25 functions, all total; 90 claims: 82 proved, 8 on the grid. The exit code is 3 — because of the eight claims on the grid. The independent proof checker (FLANG_CHECKER) replayed 42 proofs out of 82; the other 40 rest on the kernel's word.

What is proved and what is not

All 25 functions are total, and each termination is proved by composition. Of the 90 claims 82 are proved and 8 are on the grid.

Proved for all inputs — everything written as an equality to a term of the same branch or as a condition on a flag: honest refusal («ОТКАЗ ЧЕСТЕН: места нет — куча не изменилась», «ОТКАЗ ЧЕСТЕН: нулевой возврат отвергается, куча цела», «ОТКАЗ ЧЕСТЕН: возврат за границу кучи отвергается, куча цела»), issue only from a segment («ВЫДАЁТ ТОЛЬКО ИЗ ОТРЕЗКА: адрес выдачи есть начало отрезка»), the remainder takes the place of the taken segment («НИЧЕГО НЕ ПОТЕРЯНО: вместо взятого отрезка встаёт его остаток»), the returned segment enters the list, the insertion order, the whole closed trace of three steps — including heap validity after every step and "the free space shrank by exactly what was issued".

Proved by induction over the fold: «просят столько же, сколько запросили» and «свободных отрезков остаётся столько же» of «Пройти свободные», «возвращаемый отрезок по дороге не меняется» of «Пройти вставку». For this the fold must run over the list argument itself: «Пройти свободные» takes the list of segments rather than the heap, because with свёртка куча.«свободные» the kernel does not read the induction principle.

On the grid (checked on the function's examples, not proved) remain the postconditions written with an inequality or a subtraction:

postconditionfunction
«конец не левее начала»«Конец отрезка»
«НИЧЕГО НЕ ПОТЕРЯНО: остаток плюс выданное — прежняя длина»«Остаток отрезка»
«ВНУТРИ КУЧИ: адрес не левее начала отрезка»«Выдать из отрезка»
«ВНУТРИ КУЧИ: конец выданного не правее конца отрезка»«Выдать из отрезка»
«НЕ ПЕРЕСЕКАЮТСЯ: конец выданного не правее начала остатка»«Выдать из отрезка»
«НЕ ПЕРЕСЕКАЮТСЯ: выданное и остаток — по проверке»«Выдать из отрезка»
«НИЧЕГО НЕ ПОТЕРЯНО: остаток плюс выданное — прежняя длина отрезка»«Выдать из отрезка»
«ГРАНИЦА НЕ ПЯТИТСЯ: прежняя не правее новой»«Шаг годности»

The reason is one: the kernel has no arithmetic of inequalities — no reflexivity of "not greater", no monotonicity of addition, no law "(a minus b) plus b equals a". So "the issued segment lies inside the heap" and "nothing is lost", written as inequalities, stay on the grid, while the same thought written as an equality ("the remainder starts exactly where the issued segment ends") is proved. The rule for the writer: write segment bounds as equalities.

Separately, about types: the fields «начало» and «длина» have the type число rather than неотрицательное, because the sum of two неотрицательное in the language has the type число. The promise «конец не левее начала» is therefore not merely unproved — with a negative length it is false, and the kernel's refusal here is on the merits.

What is not here

Nearby