Skip to content
Open
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
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@ for Linux, written on the kernel's own system-call interface.

```toml
[dependencies]
openkal = "0.25.0"
openkal = "0.26.0"

[target.'cfg(os = "linux")'.dependencies]
openkal-linux = "0.26.3"
openkal-linux = "0.27.0"
```

## What 0.27 changes (openkal 0.26)

**A start is bounded by the kernel alone** (clause 7.6). An argument vector or an
environment of more than 512 entries was refused and reported as
`kal_err_no_memory`; the constant guarded no buffer, since both vectors are
allocated at their measured size. It is removed: the kernel bounds bytes, a quarter
of the stack limit in all and 128 KiB for one string, and its refusal (`E2BIG`) is
reported as `kal_err_no_space`, where it was `kal_err_io`.
`tests/a_start_is_bounded_by_the_kernel_alone.cpp` starts a shell with 2,000
arguments, with 2,000 variables, and with a string of 200 KiB.

**A unit's reference names its process space exactly** (clause 7.18). A reference
held the text of `/proc/self/ns/pid`, and the kernel gives the number of a
namespace that has ended to a later one (measured: four namespaces made one after
another bore one number), so a reference from one was answered in the next from a
process group that had nothing to do with it. The reference (`L3`) adds the start
of the space's first program. A reference of another boot is `empty`, and so is
one whose number a program of another start now holds: a number is given again
only once no program holds it as its group. A reference of another space of this
boot is refused by `kal_process_job_open` with `kal_err_not_supported`, as is one
in the earlier form (`L2`), which does not say which space it was made in.
`KAL_PROCESS_PROP_JOB_OBSERVE` is claimed only where the boot's name, the space
and the caller's start can be read. `tests/a_unit_reference_names_its_boot_and_space.cpp`.

## What 0.26.3 corrects

**The processor time on i386.** The program's processor time and the monotonic
Expand Down
4 changes: 2 additions & 2 deletions mcpp.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
namespace = "mcpplibs"
name = "openkal-linux"
version = "0.26.3"
version = "0.27.0"
description = "The reference implementation of openkal for Linux, written on the kernel's own system-call interface so that it can be placed beneath a C library as well as above one."
license = "Apache-2.0"

Expand Down Expand Up @@ -68,7 +68,7 @@ provides-interfaces = [

[dependencies]
# Every patch release of the specification's minor version (openkal clause 8).
openkal = "^0.25.0"
openkal = "^0.26.0"

# The package contributes definitions and no modules. The interface it
# implements is declared by the specification package, which this package
Expand Down
98 changes: 75 additions & 23 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ bool same_prefix(const char* a, const char* b, okl_uptr n) {
// peak, and in `rest' the counts that follow, the page faults at 3 and 4.
struct k_rusage { okl_long utime_s, utime_us, stime_s, stime_us, maxrss; okl_long rest[13]; };

constexpr okl_uptr kMaxEntries = 512;

constexpr char kPreopens[] = "KAL_PREOPENS=";
constexpr okl_uptr kPreopensLen = sizeof kPreopens - 1;
constexpr okl_uptr kPidDigits = 10;
Expand Down Expand Up @@ -137,7 +135,6 @@ struct vector {
bool ok = true;

bool build(const char** items, const kal_uintptr* lens, kal_uintptr n) {
if (n > kMaxEntries) { ok = false; return false; }
okl_uptr total = 0;
for (kal_uintptr i = 0; i < n; ++i) total += lens[i] + 1;
slots_bytes = (n + 1) * sizeof(char*);
Expand Down Expand Up @@ -181,7 +178,6 @@ struct vector {

bool build_env(const char** items, const kal_uintptr* lens, kal_uintptr n,
const kal_preopen* grants, kal_uintptr g, const conveyance& c) {
if (n > kMaxEntries) { ok = false; return false; }
auto left_out = [&](const char* s, kal_uintptr l) {
return names_preopens(s, l) || starts_with(s, l, kRights, kRightsLen)
|| starts_with(s, l, kResources, kResourcesLen);
Expand Down Expand Up @@ -382,6 +378,8 @@ inline void reap(okl_long child) {

} // namespace

namespace { bool units_observable(); } // defined beside the unit's reference

extern "C" {

// Starting a program. ONE FUNCTION SINCE 0.11, AND THE SAVING IS NOT ONLY IN
Expand Down Expand Up @@ -1665,8 +1663,12 @@ int kal_process_end_as_requested(void) {
for (;;) { }
}

// Observation is claimed where a reference can be made (clause 7.18): an
// environment that hides the boot's name or the space's first program states no
// origin, so a unit declared there could not be told from another's.
kal_uintptr kal_process_props(void) {
return KAL_PROCESS_PROP_JOB_OBSERVE | KAL_PROCESS_PROP_TERMINATE | KAL_PROCESS_PROP_STREAM_PASSING
return (units_observable() ? KAL_PROCESS_PROP_JOB_OBSERVE : 0u)
| KAL_PROCESS_PROP_TERMINATE | KAL_PROCESS_PROP_STREAM_PASSING
| KAL_PROCESS_PROP_EXIT_STATUS
| KAL_PROCESS_PROP_CHANNEL | KAL_PROCESS_PROP_GRANT_DIR
| KAL_PROCESS_PROP_BOUND_LIFETIME
Expand Down Expand Up @@ -2015,14 +2017,37 @@ int kal_process_job_limit(kal_job j, kal_uintptr kind, kal_u64 value) {

// Observable units (openkal 0.24). The unit stays the kernel's own group;
// identity validation does not create a registry or infer quiet from a leader.
//
// A REFERENCE NAMES ITS PROCESS SPACE EXACTLY (openkal 0.26, clause 7.18). The
// space was the text of /proc/self/ns/pid alone until then, and the kernel
// gives a namespace that has ended its number to a later one --- measured: four
// namespaces made one after another, each named pid:[4026533469]. A reference
// from one was read in the next as this space's, and answered from a process
// group there that had nothing to do with it. The space is now that text and
// the start of the space's first program, which no two spaces of one boot share.
//
// WHAT AN OBSERVATION CAN ESTABLISH, AND WHAT IT CANNOT (clause 7.18):
// another boot every program of it has ended EMPTY
// this space, no group the kernel holds no such group EMPTY
// this space, the number is another program's
// a number is given again only once no program
// holds it as its group, so the unit ended EMPTY
// this space, the group OCCUPIED
// another space of this boot
// its programs cannot be enumerated from here:
// `open' refuses, and that answer does not change
#include "unit_reference.h"
namespace {
struct unit_identity {
kal_u64 group = 0, birth = 0;
kal_u64 group = 0, birth = 0, space_birth = 0;
char boot[64]{}, space[64]{};
bool exact = false; // an L3 reference; an L2 one names its space by number only
};
bool unit_birth(okl_long pid, kal_u64& value) { okl_u64 ticks = 0; if (!started_ticks(pid,ticks)) return false; value = ticks; return true; }
bool unit_origin(char* boot, char* space) {
// The boot, the space and the start of the space's first program, or nothing
// where one of them cannot be read: a reference is then not made, and the
// property is not claimed (kal_process_props).
bool unit_origin(char* boot, char* space, kal_u64& space_birth) {
if (self_number() != okl::sys(okl::nr_getpid)) return false;
const auto fd = okl::openat( okl::at_fdcwd,
reinterpret_cast<okl_long>("/proc/sys/kernel/random/boot_id"), okl::o_rdonly | okl::o_cloexec, 0);
Expand All @@ -2035,19 +2060,43 @@ bool unit_origin(char* boot, char* space) {
n = okl::sys(okl::nr_readlinkat, okl::at_fdcwd,
reinterpret_cast<okl_long>("/proc/self/ns/pid"), reinterpret_cast<okl_long>(space), 63);
if (n <= 0 || n >= 63) return false;
space[n] = 0; return true;
space[n] = 0;
return unit_birth(1, space_birth) && space_birth != 0;
}
bool unit_parse(const char* p, kal_uintptr n, unit_identity& u) {
if (!p || n == 0 || n > 256) return false;
unit_reference::reader r{p,n}; char kind[8]{};
return r.token(kind, sizeof kind) && unit_reference::same(kind,"L2")
&& r.number(u.group) && u.group > 0 && u.group <= 0x7fffffffu
&& r.number(u.birth) && u.birth != 0
&& r.token(u.boot,sizeof u.boot) && r.token(u.space,sizeof u.space) && r.at == n;
}
bool unit_same_origin(const unit_identity& u) {
char boot[64]{}, space[64]{};
return unit_origin(boot,space) && unit_reference::same(u.boot,boot) && unit_reference::same(u.space,space);
if (!r.token(kind, sizeof kind)) return false;
u.exact = unit_reference::same(kind,"L3");
if (!u.exact && !unit_reference::same(kind,"L2")) return false;
if (!(r.number(u.group) && u.group > 0 && u.group <= 0x7fffffffu
&& r.number(u.birth) && u.birth != 0
&& r.token(u.boot,sizeof u.boot) && r.token(u.space,sizeof u.space))) return false;
if (u.exact && !(r.number(u.space_birth) && u.space_birth != 0)) return false;
return r.at == n;
}
enum class origin { here, another_boot, another_space, unreadable };
origin unit_origin_of(const unit_identity& u) {
char boot[64]{}, space[64]{}; kal_u64 space_birth = 0;
if (!unit_origin(boot,space,space_birth)) return origin::unreadable;
if (!unit_reference::same(u.boot,boot)) return origin::another_boot;
// An L2 reference does not say which of the spaces that have borne this
// number it was made in, so it is this one's only by assumption.
if (!u.exact || !unit_reference::same(u.space,space) || u.space_birth != space_birth)
return origin::another_space;
return origin::here;
}
// Whether this environment lets a reference be made: read once, since the
// answer is a property of where the program runs.
bool units_observable() {
static int known = -1;
int k = __atomic_load_n(&known, __ATOMIC_ACQUIRE);
if (k < 0) {
char boot[64]{}, space[64]{}; kal_u64 space_birth = 0, own = 0;
k = unit_origin(boot,space,space_birth) && unit_birth(0,own) && own != 0 ? 1 : 0;
__atomic_store_n(&known, k, __ATOMIC_RELEASE);
}
return k == 1;
}
}
extern "C" {
Expand All @@ -2056,9 +2105,9 @@ int kal_process_job_create(kal_job* out, char* reference, kal_uintptr capacity,
if (capacity < 256) return kal_err_no_space;
unit_identity u;
u.group = static_cast<kal_u64>(okl::sys(okl::nr_getpid));
if (!unit_origin(u.boot,u.space)||!unit_birth(0,u.birth)||!u.birth) return kal_err_not_supported;
unit_reference::writer w; w.text("L2;"); w.number(u.group); w.next(); w.number(u.birth); w.next();
w.text(u.boot); w.next(); w.text(u.space);
if (!unit_origin(u.boot,u.space,u.space_birth)||!unit_birth(0,u.birth)||!u.birth) return kal_err_not_supported;
unit_reference::writer w; w.text("L3;"); w.number(u.group); w.next(); w.number(u.birth); w.next();
w.text(u.boot); w.next(); w.text(u.space); w.next(); w.number(u.space_birth);
if (!w.valid) return kal_err_no_space;
kal_job job{}; const int rc = kal_process_job_enter(&job);
if (rc != kal_ok) return rc;
Expand All @@ -2067,26 +2116,29 @@ int kal_process_job_create(kal_job* out, char* reference, kal_uintptr capacity,
int kal_process_job_open(const char* reference, kal_uintptr length, kal_job* out) {
if (!out) return kal_err_invalid;
unit_identity u; if (!unit_parse(reference,length,u)) return kal_err_invalid;
if (!unit_same_origin(u)) return kal_err_not_supported;
const origin o = unit_origin_of(u);
if (o == origin::another_space || o == origin::unreadable) return kal_err_not_supported;
out->h = static_cast<kal_uintptr>(u.group); return kal_ok;
}
int kal_process_job_observe(kal_job job, const char* reference, kal_uintptr length, kal_u32* state) {
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
if (!unit_same_origin(u)) return kal_ok;
const origin o = unit_origin_of(u);
if (o == origin::another_boot) { *state = KAL_JOB_EMPTY; return kal_ok; }
if (o != origin::here) return kal_ok;
const auto status = okl::sys(okl::nr_kill,-static_cast<okl_long>(u.group),0);
if (status == -3 /* ESRCH */) { *state = KAL_JOB_EMPTY; return kal_ok; }
if (okl::failed(status)) return kal_ok;
okl_u64 birth = 0;
if (started_ticks(static_cast<okl_long>(u.group),birth) && birth != u.birth) return kal_ok;
if (started_ticks(static_cast<okl_long>(u.group),birth) && birth != u.birth) { *state = KAL_JOB_EMPTY; return kal_ok; }
*state = KAL_JOB_OCCUPIED; return kal_ok;
}
int kal_process_job_retire(kal_job job, const char* reference, kal_uintptr length,
const kal_program_identity* continuation, kal_u32* state) {
if (!state) return kal_err_invalid; *state = KAL_JOB_UNKNOWN;
unit_identity u; if (!unit_parse(reference,length,u) || job.h != u.group) return kal_err_invalid;
okl_u64 birth = 0;
if (static_cast<kal_u64>(okl::sys(okl::nr_getpid)) != u.group || !unit_same_origin(u)
if (static_cast<kal_u64>(okl::sys(okl::nr_getpid)) != u.group || unit_origin_of(u) != origin::here
|| !started_ticks(0,birth) || birth != u.birth) return kal_err_permission;
if (!continuation || !continuation->id || continuation->id > 0x7fffffffu || !continuation->started_ns)
return kal_ok;
Expand Down
7 changes: 5 additions & 2 deletions src/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ enum : okl_long {
// appear here rather than being taken from a header: the header belongs to a
// C library and this implementation has none.
enum : int {
e_perm = 1, e_noent = 2, e_intr = 4, e_io = 5, e_noexec = 8, e_badf = 9,
e_perm = 1, e_noent = 2, e_intr = 4, e_io = 5, e_2big = 7, e_noexec = 8, e_badf = 9,
e_child = 10,
e_again = 11, e_nomem = 12, e_acces = 13, e_fault = 14, e_busy = 16,
e_exist = 17, e_xdev = 18, e_nodev = 19, e_notdir = 20, e_isdir = 21,
Expand Down Expand Up @@ -458,7 +458,10 @@ inline int translate(okl_long r) {
case e_loop: case e_spipe: return 1; // kal_err_invalid
case e_again: return 2; // kal_err_again
case e_nomem: return 4; // kal_err_no_memory
case e_nospc: case e_fbig: case e_dquot: return 5; // kal_err_no_space
// A start the kernel refuses for the size of its vectors (E2BIG) is a
// request that does not fit, as a write that does not fit is: clause 7.6.
case e_nospc: case e_fbig: case e_dquot:
case e_2big: return 5; // kal_err_no_space
case e_acces: case e_perm: case e_rofs: return 6; // kal_err_permission
case e_nosys: return 7; // kal_err_not_supported
case e_pipe: case e_connreset: return 8; // kal_err_closed
Expand Down
88 changes: 88 additions & 0 deletions tests/a_start_is_bounded_by_the_kernel_alone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// A start takes the vectors its caller gives (SPEC.md clause 7.6, version 0.26).
//
// Until 0.27.0 this implementation refused an argument vector or an environment
// of more than 512 entries and reported it as memory exhausted. Neither vector
// has a fixed buffer, and the kernel bounds bytes, not entries: a quarter of
// the stack limit in all, 128 KiB for one string. A build tool reached the
// bound with a compile command of 520 definitions, and an IDE's build phase
// with 546 variables; both read as a machine out of memory.
//
// Each case has a shell report what it received, since the claim is about the
// started program.
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
import openkal.fs;
import openkal.types;
import openkal.process;
import mcpplibs.mtest;

using namespace mcpplibs::mtest;

namespace {

kal_dir root() {
for (kal_uintptr i = 0; i < kal_fs_preopen_count(); ++i) {
kal_dir d{}; char n[8]; kal_uintptr l = 0;
if (kal_fs_preopen(i, &d, n, sizeof n, &l) == kal_ok && l == 1 && n[0] == '/') return d;
}
return kal_dir{};
}

// Starts /bin/sh with the vectors given; the answer of the start, and the
// status where it started.
int start_shell(const std::vector<std::string>& args, const std::vector<std::string>& env, int& status) {
std::vector<const char*> argv, envp;
std::vector<kal_uintptr> alens, elens;
for (auto const& a : args) { argv.push_back(a.c_str()); alens.push_back(a.size()); }
for (auto const& e : env) { envp.push_back(e.c_str()); elens.push_back(e.size()); }
const kal_spawn how{ root(), kal::fs::working(), nullptr, nullptr, 0, 0 };
kal_process p{};
int rc = kal_err_invalid;
for (const char* path : { "bin/sh", "usr/bin/sh" }) {
rc = kal_process_spawn(&how, path, std::strlen(path), argv.data(), alens.data(), argv.size(),
envp.data(), elens.data(), envp.size(), nullptr, &p);
if (rc != kal_err_not_found) break;
}
if (rc != kal_ok) return rc;
int terminated = -1;
kal_process_wait(p, &status, &terminated);
kal_process_close(p);
if (terminated != 0) status = -1;
return kal_ok;
}

} // namespace

UTest a_start_is_bounded_by_the_kernel_alone_test{"a_start_is_bounded_by_the_kernel_alone", [] {
// 2,000 arguments after the script: the shell counts them. 2000 mod 256 is 208.
{
std::vector<std::string> args{ "sh", "-c", "exit $(($# % 256))", "sh" };
for (int i = 0; i < 2000; ++i) args.push_back("a");
int status = -1;
const int rc = start_shell(args, {}, status);
expect(rc == kal_ok) << "a vector of 2,004 elements starts (answer " << rc << ")";
expect(status == 208) << "the started program counts 2,000 arguments (status " << status << ")";
}
// 2,000 variables, about 200 KiB: the shell reads the first and the last.
{
std::vector<std::string> env;
const std::string value(90, 'v');
for (int i = 0; i < 2000; ++i) env.push_back("OKV" + std::to_string(i) + "=" + value + std::to_string(i));
const std::vector<std::string> args{ "sh", "-c",
"[ \"${OKV0%0}\" = \"" + value + "\" ] && [ \"${OKV1999%1999}\" = \"" + value + "\" ] && exit 77; exit 1" };
int status = -1;
const int rc = start_shell(args, env, status);
expect(rc == kal_ok) << "an environment of 2,000 variables starts (answer " << rc << ")";
expect(status == 77) << "the started program reads the first and the last variable (status " << status << ")";
}
// One string beyond what the kernel takes for a string (128 KiB): the
// request does not fit, which is not a machine without memory.
{
const std::vector<std::string> args{ "sh", "-c", "exit 0", "sh", std::string(200 * 1024, 'x') };
int status = -1;
const int rc = start_shell(args, {}, status);
expect(rc == kal_err_no_space) << "a start the kernel refuses for its size is no space (answer " << rc << ")";
}
}};
Loading
Loading