Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ install(
install(FILES LICENSE DESTINATION ".")

if(BUILD_TESTING)
add_executable(nil_console_input_state_tests tests/console_input_state_tests.cpp)
target_compile_features(nil_console_input_state_tests PRIVATE cxx_std_17)
target_include_directories(nil_console_input_state_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src")
target_link_libraries(nil_console_input_state_tests PRIVATE nilcompiler_core)
add_test(NAME nil.console-input-state COMMAND $<TARGET_FILE:nil_console_input_state_tests>)
set_tests_properties(nil.console-input-state PROPERTIES LABELS "console;runtime;embedding" TIMEOUT 30)
add_executable(nil_durable_file_io_tests
tests/durable_file_io_tests.cpp
nilcompiler/src/secure_file_io.cpp)
target_compile_features(nil_durable_file_io_tests PRIVATE cxx_std_17)
target_include_directories(nil_durable_file_io_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_compile_definitions(nil_durable_file_io_tests PRIVATE NIL_TEST_WRAP_FSYNC=1)
target_link_options(nil_durable_file_io_tests PRIVATE "-Wl,--wrap=fsync")
endif()
add_test(NAME nil.durable-file-io COMMAND $<TARGET_FILE:nil_durable_file_io_tests>)
set_tests_properties(nil.durable-file-io PROPERTIES LABELS "stdlib;io;security;rc" TIMEOUT 30)
add_executable(nil_durable_vm_lifecycle_tests tests/durable_vm_lifecycle_tests.cpp)
target_compile_features(nil_durable_vm_lifecycle_tests PRIVATE cxx_std_17)
target_include_directories(nil_durable_vm_lifecycle_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/nilcompiler/src")
target_link_libraries(nil_durable_vm_lifecycle_tests PRIVATE nilcompiler_core)
add_test(NAME nil.durable-vm-lifecycle COMMAND $<TARGET_FILE:nil_durable_vm_lifecycle_tests>)
set_tests_properties(nil.durable-vm-lifecycle PROPERTIES LABELS "stdlib;io;security;vm;rc" TIMEOUT 30)
foreach(nil_hook_profile IN ITEMS default override)
add_executable(nil_freestanding_hook_${nil_hook_profile} tests/freestanding_hook_contract.cpp)
target_compile_features(nil_freestanding_hook_${nil_hook_profile} PRIVATE cxx_std_17)
Expand Down Expand Up @@ -747,6 +770,12 @@ if(BUILD_TESTING)
# test suite when testing was explicitly enabled.
find_package(Python3 3.8 REQUIRED COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
add_test(NAME nil.durable-file-io-routes
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/durable_file_io_contract.py"
--compiler $<TARGET_FILE:nilcompiler> --interpreter $<TARGET_FILE:nilinterpreter>
--nilstd "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src" --runtime $<TARGET_FILE:nilruntime>
--llvm "${NIL_ENABLE_LLVM}" --output "${CMAKE_BINARY_DIR}/durable-io-evidence")
set_tests_properties(nil.durable-file-io-routes PROPERTIES LABELS "storage;stl;routes;rc" TIMEOUT 300)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_test(NAME nil.darwin-heap-accounting
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/darwin_heap_accounting_contract.py"
Expand Down Expand Up @@ -2417,6 +2446,23 @@ if(BUILD_TESTING)
--expected-exit 0
--routes all
)
foreach(nil_choice_fixture IN ITEMS choice-text-fields choice-text-fields-matrix)
if(nil_choice_fixture MATCHES "-matrix$")
set(nil_choice_suffix matrix)
else()
set(nil_choice_suffix ny)
endif()
add_ny_route_test(
ny.routes.${nil_choice_fixture}
--source "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/${nil_choice_fixture}.${nil_choice_suffix}"
--expected-stdout "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/${nil_choice_fixture}.stdout"
--expected-exit 0
--routes all
--with-stdlib
--compiler-arg=--sdk-path
--compiler-arg=${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src
)
endforeach()
add_ny_route_test(
ny.routes.native-exhaustive-result-return
--source "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/native-exhaustive-result-return.ny"
Expand Down Expand Up @@ -3429,6 +3475,22 @@ if(BUILD_TESTING)
--compiler-arg=${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src
)
endforeach()
foreach(nil_entropy_fixture IN ITEMS secure-hex secure-hex-matrix)
if(nil_entropy_fixture MATCHES "-matrix$")
set(nil_entropy_suffix matrix)
else()
set(nil_entropy_suffix ny)
endif()
add_ny_route_test(
ny.routes.${nil_entropy_fixture}
--source "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/${nil_entropy_fixture}.${nil_entropy_suffix}"
--expected-stdout "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/${nil_entropy_fixture}.stdout"
--expected-exit 0
--routes all
--compiler-arg=--sdk-path
--compiler-arg=${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src
)
endforeach()
add_ny_route_test(
ny.routes.math-runtime
--source "${CMAKE_CURRENT_SOURCE_DIR}/tests/language/math-runtime.ny"
Expand Down
68 changes: 68 additions & 0 deletions docs/design/NIL-0028-durable-files-and-locks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# NIL-0028: Durable publication and context-owned file locks

- **Status:** Implemented additive POSIX source-runtime profile; release SDK
qualification remains separate.

## Motivation

The Matrix-authored Enigma database exposed a general library gap. A checked
atomic write synchronized the candidate inode and published its name, but lacked
parent-directory durability. A stream flush was not a filesystem barrier, and no
supported advisory-lock API existed. Applications had to supply a platform host.

General filesystem mechanisms belong in `nil.std.io`. Database schemas, evidence
semantics, permission policy, and recovery decisions stay in the application.

## Decision

Add `write_bytes_durable`, `sync_directory`, `acquire_file_lock`,
`release_file_lock`, and `file_lock_is_held`. Preserve the existing `write_bytes_atomic` and `File.flush`
contracts. Durable writes reuse the atomic publication helper and synchronize
the exact publication directory descriptor. A post-publication sync failure
returns an explicitly uncertain outcome.

File locks are nonblocking exclusive advisory locks on private regular backing
files. Runtime-owned registries hold the descriptors. Tokens are never recycled,
are checked against their owning execution context, and are closed at invocation
termination and context teardown. An execution checkpoint cannot clone one.
Unsupported platforms fail closed before mutation.

`file_lock_is_held` asks whether the current execution context still retains
the token. Missing, stale, foreign, zero, and negative tokens return `Ok(false)`;
unsupported platforms return `Error`. The query does not inspect a pathname,
release or reacquire a descriptor, or change ownership. An application object
can survive an embedded VM invocation while that invocation's locks have been
retired, so retaining a positive token in an object is not evidence of a live
lock. Applications check current ownership before using that object's state.

Use the existing checked ordinary-I/O bridge for native execution, with exact
parameter/result and enum ownership validation. The VM checks the new result
metadata and concrete enum schema, and validates dynamic arguments before host
effects. The shared helper implements the OS contract once.

## Boundaries and validation

The Enigma consumer also requires the existing `text.byte_at` and `from_byte`
operations on native routes. Their native providers use the ordinary-I/O bridge
and preserve byte/range behavior within the native NUL-free text boundary.

This is a Linux-tested POSIX mechanism, not a production database protocol,
power-loss certification, pathname containment service, credential, or general
rollback defense. Lock participants must cooperate and control the directory
ancestry. Fork-inherited descriptors retain ordinary POSIX lifetime semantics.
An uncertain publication requires caller recovery rather than transparent retry.

Tests cover shared OS helpers, embedded VM invocation/reset/destructor cleanup,
static/shared native context lifetimes, ownership queries before and after
retirement, Ny/Matrix VM text/binary and AOT/JIT
routes, malformed ABI declarations, private file modes, contention, and Linux
file/directory-sync fault injection. Existing secure-byte and ordinary-I/O
regressions remain part of focused validation.

The [public API reference](../standard-library/durable-files.md) defines exact
limits. Existing SDKs do not gain the new provider from source declarations
alone. A development build must ship matching compiler, runtime, and library
sources; release/installed-SDK qualification and the remaining application-host
APIs are independent work. The private-directory, owner/mode, and standalone
SHA-256 surfaces reserved here are decided in
[NIL-0029](NIL-0029-private-entries-and-digests.md).
70 changes: 70 additions & 0 deletions docs/design/NIL-0029-private-entries-and-digests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# NIL-0029: Private directory creation, metadata verdicts, and content digests

- **Status:** Implemented additive POSIX source-runtime profile, with a
portable digest; release SDK qualification remains separate.

## Motivation

NIL-0028 gave applications durable publication and cooperative single-writer
locking, and explicitly left private-directory creation, owner/mode inspection,
and a standalone SHA-256 as separate APIs. A storage application still had to
supply a platform host for them, which is the gap NIL-0028 set out to close.

Each mechanism already existed inside the framework and was already exercised:
owner-only directory creation for toolchain scratch space, the private-regular
predicate used when acquiring a lock, and SHA-256 for module fingerprints. The
gap was a supported language surface, not an algorithm.

## Decision

Add `create_private_directory`, `is_private_regular_file`,
`is_private_directory`, and `sha256_hex`. The filesystem operations join
`nil.std.io`; the digest joins `nil.std.io`'s checksum surface beside
`crc32`, which already draws the line between corruption detection and
authentication.

Directory creation applies its mode at creation rather than through a later
chmod, which would expose a writable directory in between. An existing entry is
reported, never adopted, so success means the caller created the directory.
Parents are neither created nor trusted beyond existing ancestry rules.

The predicates return `Result<bool, text>` so three outcomes stay distinct: the
entry was inspected and matches, it was inspected and does not, or it could not
be inspected. Collapsing absence into a false verdict would let a caller read
"missing" as "unsafe" or the reverse. Inspection does not follow a final
symlink, so a link is judged as itself and never inherits its target's verdict.

`sha256_hex` returns 64 lowercase hexadecimal characters over every supplied
byte, including embedded NULs. It reuses the existing portable implementation,
and on Apple platforms the existing CommonCrypto path, so a digest does not
depend on which route computed it.

## Boundaries and validation

A path predicate describes an entry at inspection time. It is not a safe
substitute for opening the entry under `O_NOFOLLOW`, because it cannot close the
window between inspection and use. Publication and lock acquisition keep their
own checked opens; these surfaces are for preflight and diagnosis. A caller that
needs a guarantee must act on an open descriptor, not on a prior verdict.

A digest detects accidental corruption and identifies content. It is not a
signature, a message authentication code, encryption, or rollback protection:
whoever can replace the bytes can replace the digest beside them. Authenticating
a digest requires a secret these functions do not take.

Mode and owner verdicts describe POSIX ownership and permission bits only. They
say nothing about ACLs, extended attributes, capabilities, or mandatory access
control, and they are not a security boundary against the owner or root.

Tests cover creation mode and exclusivity, cross-kind verdicts, loosened file
and directory modes, symlinks to otherwise-private targets, multiply-linked
files, FIFOs, uninspectable entries, unsupported-platform behavior before any
mutation, and RFC 6234 digest vectors including embedded NUL bytes. The paired
Ny and Matrix fixtures exercise every surface across textual and binary VM, AOT,
and JIT, with an independent host oracle checking directory modes.

Windows returns an explicit unsupported-platform error before filesystem
mutation; the digest is platform-independent. Other POSIX platforms need their
own execution evidence. As with NIL-0028, source declarations alone do not give
an installed SDK the new provider, and release qualification is independent
work. See the [public API reference](../standard-library/durable-files.md).
65 changes: 65 additions & 0 deletions docs/design/NIL-0030-end-of-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# NIL-0030: Reading a line without faulting at end of input

- **Status:** Implemented additive surface on every execution route; release
SDK qualification remains separate.

## Motivation

`read_line` faults at end of input. That suits a program that requires input,
but not a service that reads requests until its caller closes the stream: a
clean shutdown became a fault, so a long-running reader written in Ny or Matrix
could not terminate normally.

The Matrix-authored Enigma durable host surfaced this. Its Python predecessor
exits zero when standard input closes, and its recovery suite closes the
stream to end a session, so a host that faulted there would report failure for
an ordinary shutdown and leave a crash report behind.

## Decision

Add `read_line_option`, which reads a line exactly as `read_line` does but
answers `None` at end of input instead of faulting. `read_line` keeps its
contract; nothing that reads input today changes behavior.

A genuine input error still faults. This is the substance of the decision
rather than a detail: a reader that must fail closed cannot be handed "no more
input" when the truth is "cannot read". Shutting down cleanly on a broken
stream would silently convert an I/O failure into a successful, quiet exit.

## Distinguishing the two is not free

Some host read errors leave an EOF-looking C++ stream state: `eofbit` and
`failbit` set, `badbit` clear. Reading a directory descriptor as standard input,
for example, can resemble an empty pipe by stream state alone. Both providers
therefore clear `errno` immediately before the read and inspect it when the
read fails. A nonzero `errno` is a failure.

Zero `errno` is not sufficient to establish end of input. An embedding host
can supply a stream with `failbit` or `badbit` set without a matching host error.
`None` requires `eofbit` set, `badbit` clear, and zero `errno`; every other failed
read faults. A final line that successfully extracts characters is still
`Some`, including when reaching EOF sets `eofbit` during that read.

Treating the ambiguous case as a failure is deliberate. A reader that stops
early on a healthy stream is a visible bug; a reader that reports success after
an unread request is a silent one.

A final line without a terminator is still a line, as with `read_line`. Native
text cannot contain NUL, so native input rejects that byte explicitly.

## Validation

The surface rides the existing checked ordinary-I/O bridge, so it is lowered
once and reaches the interpreter, AOT and JIT routes without separate codegen.
Paired Ny and Matrix fixtures cover several lines then a clean stop, a final
line without a terminator, and empty input, across those routes. A separate
case feeds a directory descriptor as standard input and requires a fault with
no clean-stop output, which catches an implementation that consults stream
state alone. Embedded VM text/binary and native-provider checks supply failed
streams with zero `errno`, including `badbit` together with `eofbit`, and require
a failure rather than `None`. They also preserve blank lines, final nonempty
lines, repeated clean EOF, and the original required `read_line` EOF failure.

As with NIL-0028 and NIL-0029, source declarations alone do not give an
installed SDK the new provider, and release qualification is independent work.
See the [console reference](../standard-library/index.md).
3 changes: 3 additions & 0 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ not assigned architecture identifiers.
| NIL-0025 | [Matrix desktop UI](NIL-0025-matrix-desktop-ui.md) | Implemented GTK4 on Linux, Win32 on Windows and AppKit on Intel/Apple Silicon macOS; release qualification remains separate |
| NIL-0026 | [Evolving the language without breaking what is written](NIL-0026-language-evolution.md) | Editions implemented in the project manifest |
| NIL-0027 | [Multiline formatted text and structured markup literals](NIL-0027-structured-markup.md) | Proposed; staged, nothing shipped |
| NIL-0028 | [Durable publication and context-owned file locks](NIL-0028-durable-files-and-locks.md) | Implemented additive POSIX source-runtime profile; installed/release SDK qualification separate |
| NIL-0029 | [Private directory creation, metadata verdicts, and content digests](NIL-0029-private-entries-and-digests.md) | Implemented additive POSIX source-runtime profile with a portable digest; installed/release SDK qualification separate |
| NIL-0030 | [Reading a line without faulting at end of input](NIL-0030-end-of-input.md) | Implemented additive surface on every execution route; installed/release SDK qualification separate |
| NIL-0093 | [Executable context is artifact self-description](NIL-0093-executable-context.md) | Implemented within the documented inspection profile |

## Retained Ny feature references
Expand Down
Loading
Loading