What it does
- Manifest read straight from the file, before loading
- Search with an explicit policy, against DLL planting
- Lifecycle as a state table
- Exception barrier at the boundary
- Dependency ordering, with cycles reported
- Hot swap with no lock on the reader path
- Image check (SHA-256, signature) before loading
- Registry with lookup by interface
Measured
- Call through the table vs. a direct indirect call: -0.11 ns on Windows and -0.18 ns on Linux (target under 2 ns)
- Load: 0.016 ms of ours plus 4.4 ms of the Windows loader, 0.002 ms plus 2.3 ms on Linux
- 10,000 hot swaps and 50 real reloads: 0 wrong reads
Known limits
- The exception barrier does not protect against a plugin built with a different C++ runtime
Status of spec 06. This document covers the first block: the C contract that is product, and the gate that refuses an incompatible plugin without executing a single line of it.
---
What exists
| piece | where | oracle |
|---|---|---|
| contract with the plugin | ethereal/c/plugin/plugin_abi.h | a plugin in pure C that includes it and nothing else |
| reading the manifest without executing | plugin/manifest.hpp | a plugin that calls **abort()** in a static initializer |
| raw dynamic library | plugin/library.hpp | (existing, predates this spec) |
| search, flags, reference count | plugin/library_load.hpp | a planted impostor with the same file name in another folder |
| lifecycle and barrier | plugin/lifecycle.hpp | a plugin that refuses and reports, and another that throws |
| dependency ordering | plugin/dependency.hpp | planted graphs: chain, diamond, cycle in the middle, a thousand in a row |
| host | plugin/host.hpp | (existing, and the gate now comes before the load) |
Test suite: plugin.manifest, with 89 checks on Linux and 87 on Windows. Green on Linux Debug/Release and Windows x64 Debug. Whole ctest run: Linux 54/54, Windows 50/50.
---
The tension in the spec, and how it was resolved
The spec asks for two things that do not fit together:
The manifest is exported by the plugin through a well-known symbol (
ETLC_Plugin_GetManifest), returning a pointer to a static POD.
ABI mismatch | plugin code provably never runs, verified by a plugin that aborts in a static initializer.
Why they do not fit (the reason comes from the operating system, not from the design):
- to call a function in a dynamic library, you have to load it.
- loading a dynamic library runs its static initializers.
- therefore, by the time you can call
GetManifest, theabort()from the criterion has already happened.
On POSIX there is no dlopen that does not initialize. On Windows there is (mapping as a resource), but there GetProcAddress does not work on that mapping.
The way out is to export the manifest as data, ETLC_Plugin_Manifest. Data can be read directly from the file, with the same tables the loader would use, without handing control to anyone. The function from the spec still exists (the macro emits both), and a host written to the letter of the spec works.
The mutant that proves this was necessary
I planted the obvious implementation: load the library and call ETLC_Plugin_GetManifest. It is what anyone would write and what the spec describes.
The test process died. No score, no report, nothing. Reading the bad plugin's manifest loaded the plugin, and the static initializer aborted the entire test suite.
This shows that reading the manifest as data is mandatory.
---
The two plugins, and why they are in different languages
| language | what it proves | |
|---|---|---|
plugin_good | pure C, C11, no extensions | that plugin_abi.h really is usable from C |
plugin_abi_old | C++ | that a static initializer kills whoever loads it |
The good one is in C because plugin_abi.h is product: it is the only file that someone writing a plugin for Ethereal includes. A C++ type, a reserved word, or a double-slash comment would go unnoticed while only C++ included it.
The bad one is in C++ because "static initializer" is a C++ thing.
And this found two defects in the product header
GCC warned: "'visibility' attribute ignored". What that meant:
- a
constvariable at global scope in C++ has internal linkage by default, and the symbol would not even be exported. In C the same line has external linkage. That is why the C plugin passed and the C++ one did not. - MSVC decorates the name of a C++ global variable. The symbol would come out as
?ETLC_Plugin_Manifest@@3U...and the host, which looks up the literal name, would find nothing.
Neither would have appeared while only C plugins were built. extern "C" fixes both.
---
Measure the trace, not the death
The first version of the test measured the death: the child process that loaded the bad plugin was supposed to die.
On Linux it dies. On Windows, it does not. Measured: the initializer runs, calls abort(), and the system loader catches that and makes LoadLibrary fail. The process that did the loading stays intact.
The property the spec requires is not "the host dies". It is that no line of the plugin runs. The trap now writes a file before aborting, and the test suite measures that file. It answers the same on both systems.
the child exited with 0x4D [Windows: it survived]
the trap RAN in the child: it is really armed [and it still ran]Without that control, a plugin whose trap did not arm (because the linker discarded the object, or because someone edited the file) would make the main case pass with the gate entirely erased.
---
The defect this found in the existing code
When the aborting plugin was added, the smoke.plugin test suite started to die. It did not fail. It died.
CPluginHost::LoadOne loaded the library and then checked the generation version. The check existed, was well written, and had a correct comment about why the version is the first field of the structure. It just ran too late.
The spec calls this "the classic plugin crash" and requires the order to be tested explicitly. This was exactly that case, and it only appeared because the plugin from the acceptance criterion was actually written.
The gate now comes before the load. A plugin without the new manifest is let through on purpose: the old contract is still valid, and under it there is no way to check without loading. Plugins that carry the new manifest get the guarantee.
the scan returned 0 plugins and 3 failures
and about it, it said: generation contract 3735928559, expected 1 (refused WITHOUT loading)---
What the battery measured
Eight planted defects, seven killed.
| defect | result |
|---|---|
the gate loads the library and calls GetManifest | killed the test process |
| a file that cannot be inspected is now accepted | failed |
| the compatibility check stops comparing the version | failed |
| the ABI version is read from the second field | failed (7 checks) |
| "not an image" becomes "does not export a manifest" | failed¹ |
the ELF reader looks in .symtab instead of .dynsym | failed² |
| an imported symbol passes as defined | survived |
¹ It survived the first round: the test's text file was 50 bytes, and the reader rejects right away anything under 64, too early to be an image. The path never ran.
² It survived the first round: in the build copies both tables exist and both have the symbol. A **copy passed through strip** was added, which is how a plugin reaches the customer: strip deletes .symtab and leaves .dynsym. Without it, the reader would be tested only in the case that never happens in production.
The survivor is documented in the code itself. Catching it would require a third plugin that declared the symbol without defining it, something no real plugin does, because the header macro always defines it.
---
A trap in the test environment
Moving the plugins to their own folder left stale copies in the old folder, because CMake does not delete what it stopped producing. The test suite that scans that folder started to see ghosts and to count extra failures.
Two test suites sharing a folder is a coupling in which adding one test breaks another. They are now separate (plugins/ and plugins6/).
---
Search is a security matter
The spec is explicit, and the reason is an attack known by name:
Search path control: absolute path, explicit directory list, and the platform default, selected explicitly. The platform default is not the default in this API, because implicit search order is a security problem (DLL planting). The caller must opt in.
Planting works like this: the program asks "load network", the system searches a list of folders, and someone puts a file with that name in a folder that comes before the right one. The program loads the attacker's file and runs their code with the privileges of whoever ran the program.
On Windows the list includes, depending on configuration, the executable's folder, the system folders, the current folder and the entire PATH. And the current folder is chosen by whoever opened the program, not by whoever wrote it.
What the code did before
CDynamicLibrary::Load called LoadLibraryA and dlopen with the path as it came. With a full path that is safe. With a simple name, that is the system search order, which means the library already did by default what the spec forbids.
Planting, staged
The test suite has two libraries with the same file name in different folders: the good one, version 2.7.13, and an impostor, 9.9.9. The different version is what makes the attack observable: the test asks by name and looks at the manifest of what was resolved.
requesting 'libplugin_good.so' in default mode: the name does not say where the file is
[good, impostor] resolved to: 2.7.13
[impostor, good] resolved to: 9.9.9 <- the order decidesWithout the two, "it loaded the right one" would be a claim impossible to fail.
What the API does now
| policy | behavior |
|---|---|
eExactPath (default) | uses the path as it came. A name without a separator is refused |
eDirectories | only the listed folders, in order. The first one that has the file wins |
ePlatformDefault | the system order. The caller chooses this option by name |
In addition:
- flags (
LAZY/NOW,GLOBAL/LOCAL,NODELETE,ALREADY_LOADED,SEARCH_OWN_DIR), with refusal of contradictory combinations instead of choosing one of them behind the caller's back. - a reference count that can be queried.
- the system error text copied into the caller's buffer, with the two-pass protocol.
ALREADY_LOADED is the only way to ask "is this already here?" without the side effect of bringing the library into the process.
---
The search battery
Eight planted defects on Linux, seven killed. The eighth died on Windows.
| defect | result |
|---|---|
| the default stops refusing the bare name | failed |
| the folder list is walked in reverse | failed (in both directions) |
| contradictory flags are now accepted | failed |
| the reference count always stays at one | failed |
| the system text is no longer terminated when truncated | failed |
| returns the copied size instead of the full size | failed |
| asking for folders without listing any is no longer refused | failed |
| on Windows only the Unix slash counts as a separator | survived on Linux, died on Windows |
The last one deserves a note. It is unreachable on Linux by construction, because there the backslash is not a separator. But it also survived the first round on Windows: the path the test built was sGood + "\\" + sName, and sGood comes from CMake with forward slashes. The path had both kinds, and the defect passed at the first forward slash.
The case now converts the entire path to backslashes. If the backslash is not recognized, no separator is, and the call has to be refused.
One mutant was left out because it is not measurable here: removing the LocalFree after FormatMessage produces one leak per call. In a folder scan with two hundred files that are not plugins, that is two hundred leaks. Only a sanitizer catches that, and the spec asks for sanitizers.
---
A hung process holds on to the executable
The build on Windows started failing with LNK1168: cannot open the executable for writing. The culprit was found with forty minutes of uptime: a child process of the test itself, the one that loads the aborting library on purpose.
The test waited thirty seconds and moved on without killing anyone. Waiting is right. Giving up without killing is not. Now the child that does not die is terminated, and the error mode is also configured inside it. Inheriting from the parent works as long as there is a parent, and there is no parent when someone runs the executable by hand.
plugin.manifest hanging under the MSVC ctest (2026-09-28)
Sometimes the test hung under ctest on Windows, and passed when run by hand. With the scan instrumented, it always stopped at the same file: plugin_good.recarga1_3668_0.dll, with 0 bytes, from 2026-09-25. It was a reload copy left behind by a run that died in the middle of the copy. These were two defects in the library, and both are fixed:
- **
LoadLibraryon an invalid image opened the system error dialog** ("is not a valid Win32 application") and waited for a click. Underctest, in a service or in a folder scan, nobody clicks. Both loads (CDynamicLibrary::LoadandLoadLibraryEx) now set the thread error mode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX) only for the duration of the call, and the invalid image returns the error, as on POSIX. Checked in reverse: without this, the new test hangs undercteston the first of six runs. With it, it passes in fifteen. - The scan loaded the reload copies as plugins.
<name>.recarga<g>_<pid>_<s>is the host's own leftover when it remains on disk (a second "good" with the same name, or an empty file).LoadDirectoryskips these copies without counting them as failures.
The new test (JunkDirectory, in plugin.manifest) plants in its own folder an empty .dll, a text .dll and a reload copy: two failures, zero loaded, the copy ignored, and all in 14 ms.
---
The lifecycle
Init -> Start -> Stop -> ShutdownThe rule that corrupts the most memory when it is broken
If
Initfails,Shutdownis not called, and the library is unloaded. Document this contract loudly because it is the most common source of plugin memory corruption.
It looks arbitrary and it is not. An Init that failed may have failed on its first line, before building what Shutdown would destroy. Calling Shutdown in that case hands a destructor pointers that nobody initialized. And the damage does not show up there. It shows up when the freed memory is reused by something else.
On the other side, this means that **Init has to clean up what it built itself before returning an error**. Nobody will do it for it.
How this is measured: the plugin_refuse plugin returns an error from Init, and its Shutdown writes a file before anything else. If the file appears, the host broke the contract. Without that report, "shutdown was not called" would be a claim impossible to fail, because a host that called it would produce exactly the same output.
An invalid transition is an error
State machine transitions are strictly enforced and a bad transition is a hard error, not a no-op.
Calling Start twice, or Stop without having started, is a defect in the caller. Swallowing that silently makes the program appear to work and postpones the discovery to the day the plugin keeps state.
And a refused transition does not change the state. It is a wrong request from the caller, not a failure of the plugin, which is not even called.
The table is data, and that is why it can be tested
The rules are in a vector of 6 states × 4 operations, not in a ladder of ifs. The test suite has a second table, written by hand from the contract, and compares the two across all 24 pairs.
A ladder of conditions can only be tested by repeating the same logic, and a test that repeats the logic agrees with its defect.
And the guard against a trivial oracle caught me making a mistake. Besides the 24 pairs, the case requires the number of allowed transitions to be exactly 6, because a table where everything (or nothing) was allowed would agree with an equally degenerate implementation. I had written 7. The two tables agreed on all 24 pairs and the test suite failed anyway: the guard checked my arithmetic, not my logic.
No exception crosses the boundary
Every callback into the plugin is wrapped so a thrown exception (from a plugin built by someone else, with exceptions on) cannot cross the boundary.
The plugin_throw plugin throws from two different places: in Stop, a standard library type. In Start, a type defined inside it, whose type information disappears when the library is unloaded.
That is why the safety net uses catch( ... ) and **does not call what()** on what it caught. Touching the object would require its type to be known on this side, and that type does not exist outside the library.
Throwing and refusing are different things, and the host distinguishes them. Refusing is expected behavior. Throwing is a defect in the plugin. Confusing the two would hide from whoever operates the program that there is a broken library, and not a missing dependency.
The honest limit of the barrier
catch( ... ) catches what the same runtime threw. A plugin built with another C++ library (another compiler, another version, another configuration on Windows) throws through a mechanism this side does not recognize, and then the process terminates before reaching the catch.
There is no fix for this on the host side. What exists is the rule in plugin_abi.h: the entry points are extern "C" and return int. The catch is the net for whoever wrote bad code. It guarantees nothing against whoever compiled differently. That is stated in the header.
---
The lifecycle battery
Nine planted defects, nine killed.
| defect | result |
|---|---|
a failed Init now allows Shutdown | failed (3 checks) |
| the boundary stops catching the exception | killed the test process |
| throwing is no longer distinct from refusing | failed |
| an invalid transition becomes a silent no-op | failed (5 checks) |
| an invalid transition quarantines the plugin | failed |
| the result no longer triggers unloading | failed |
| restarting after stopping is no longer allowed | failed |
| shutting down while the plugin is running is now allowed | failed |
| a successful operation leaves the state where it was | failed |
The second has the same result as in the previous block, and for the same reason. When the planted defect kills the test suite, the result is conclusive: the path it erased was the only thing preventing the catastrophe.
---
In what order the plugins come up
Dependency resolution is a topological sort with cycle detection reporting the cycle members, not just "cycle detected".
The difference between the two is a day's work for whoever has to fix it. In an installation with forty plugins, "there is a cycle" sends the person to read forty manifests by hand. With "the cycle is A → B → C → A", they solve it in a minute.
And it is harder than it looks
Detection by color marking discovers that there is a cycle the instant an edge points to a gray vertex, and at that instant it does not know who the members are. To name them you have to keep the traversal path and cut it from the vertex that repeated.
That is also why the implementation is a depth-first traversal, and not Kahn's algorithm. Kahn's is simpler and easier to make deterministic, but it cannot say who is in the cycle. When it finishes, what remains is a set that contains the cycle plus everything that depends on it. In a graph of forty, that can be thirty names for a cycle of three.
the cycle: ciclo_a -> ciclo_b -> ciclo_c -> ciclo_aExactly the three. aaa_usa, which depends on the cycle, is left out.
The case tested almost nothing, and the mutant showed it
The test graph had the external node named usa. The visit starts with the first name in alphabetical order, and ciclo_a comes before usa. Therefore the traversal started inside the cycle. In that case the whole path is the cycle, and a defect that returned the whole path instead of the right piece passed without a scratch. That is what happened: the ciclo-do-comeco mutant survived.
With the node renamed to aaa_usa, the traversal enters the cycle from outside, the path gains a prefix, and cutting from the wrong place becomes visible. The mutant dies with three failures.
The order is deterministic, and there is a reason
A graph with slack admits several valid orders. Choosing among them by arrival order would make the same installation come up one way on one machine and another way on another, because the order in which the file system lists a folder is not stable. And a plugin that only works in one of the orders has a defect that shows up on some machine once in a while, the worst kind.
Ties are broken by name, and the test suite shuffles the input and requires the same output.
Saying the missing name is not enough
*"network is missing"* in an installation of forty sends the person looking for who asked for it. The result carries both: what is missing and who needed it.
A thousand in a row
In practice, a plugin graph is shallow. But "in practice" is not a guarantee, and a manifest generated by mistake chains thousands. The traversal is written without recursion, with an explicit stack, because a stack overflow in a plugin loader looks like a plugin defect.
---
The ordering battery
Nine planted defects, nine killed.
| defect | result |
|---|---|
| detection now says only that there is a cycle | failed |
| the members become the entire path | failed (3 checks)¹ |
| an optional dependency becomes mandatory | failed |
| the missing one no longer says who asked for it | failed |
| two plugins with the same name pass | failed |
| the visit follows arrival order | failed (determinism) |
| the vertex enters the order before its dependencies | failed |
| the cycle list no longer closes | failed |
| an already resolved vertex is visited again | failed (4 checks) |
¹ It survived the first round. See above.
---
Swapping the table with calls in flight
The spec calls this "the single hardest correctness requirement in the module", and the sentence that explains why is this one: "Unloading a module with a live call in flight is a guaranteed crash." The memory where the code was stops being mapped, and the call that was inside it returns to an address that does not exist. No try catches that.
The reader side does five things and nothing more: it announces that it entered (an odd number in its slot), executes a barrier, reads the pointer, uses it, and announces that it left (the next even number). No loop, no condition, no lock, as the spec requires: "No reader-side lock, no reader-side branch beyond the pointer load."
The writer side swaps the pointer and then waits. For each slot, even means that reader is outside, and when it enters it will enter the new table. Odd means it is inside right now, and the writer waits until the number changes. It does not wait until it becomes even: a reader in a tight loop enters and leaves several times between two reads by the writer, and waiting for a specific value would be waiting for an instant that may never be seen.
In Release, Enter is nine instructions
mov (%rax),%rdx ; reads the sequence
add $0x1,%rdx
mov %rdx,(%rax) ; announces: odd. plain store, "relaxed"
lock orq $0x0,(%rsp) ; THE BARRIER
mov (%rdi),%rax ; reads the table
retNo branch, no lock, no shared write. This is what supports the target of under 2 ns.
The barrier cannot be optimized away, and that was measured
The slot write and the pointer read form a store-load pair, and that pair is the only one an x86 is allowed to reorder. Neither release on the write nor acquire on the read prevents this: neither of them orders store-load.
Without the barrier, the reader can read the pointer before announcing that it entered. The writer looks at the slot, sees that it is idle, concludes that nobody is inside, and unloads the library underneath a call in flight.
Removing the barrier and requiring the test to break took three discoveries.
1. The allocator hid the defect
The cycle did delete pVelha and right after that new SPluginEntries() of the same size. The allocator returns the same block, and the block comes back initialized with the correct function pointer again. A late reader, still holding a table it should already have released, read a perfectly valid table and called the right function. The defect happened and the test saw everything as correct.
Now the retired table is pointed at a poison function before being freed, and it is only freed one cycle later. Whoever arrives late calls the poison, and the error is counted.
2. -O0 compiles the relaxed write as if it had a lock
This was the hardest discovery to see. In Debug, Enter comes out like this:
xchg %rdx,(%rax) ; the "relaxed" write to the slot
lock orq $0x0,(%rsp) ; the barrierXCHG with a memory operand has an implicit lock on x86. At -O0, std::atomic::store dispatches the memory order at run time and falls into the locked path. So the relaxed write already orders store-load by itself. The barrier becomes redundant, and removing it changes nothing. The Debug configuration is structurally incapable of measuring this barrier.
In Release the write is a plain mov and the barrier is the only thing that orders. The mutant dies there, three rounds out of three.
3. Both windows have to be open at the same time
Even in Release, it is not enough to remove the barrier and run. Two things are needed at the same time:
- the slot write has to stay invisible for long enough. On its own, it leaves the store buffer in a few dozen cycles. The writer, between publishing the new table and going to look at that slot, spends more than that on its own barrier alone. By the time it arrives, the write has already left and the window closes by itself. That is why there are readers that clog the queue: 512 writes spread over 4 MiB before entering. An x86's writes leave in order, so the slot write stays behind all of them. It is also what a real host does: it writes a lot of things and only then calls the plugin.
- between reading the pointer and using the pointer, the entire writer has to fit: swap, drain and poison. By default this takes a few nanoseconds, and that is why the readers that clog the queue also linger after entering.
With one window open and the other closed, the mutant survives. That was measured in both forms before combining the two.
What the thread sanitizer catches, and what it does not
There is a linux-tsan configuration (CMakePresets.json). The good code passes clean: 107 checks, zero warnings.
| planted defect | did TSan see it? |
|---|---|
| the reader reads the table before announcing | yes: 4 data races, and the first is the delete of the table on the main thread against a live read on another |
| the drain does not wait | yes: data races, in addition to the test's own failure |
| the reader's barrier disappears | no |
registration replaces compare_exchange with read-then-write | no |
The two absences have an explanation, and the explanation is worth more than the number:
- store-load reordering is not a data race, and TSan only sees data races. A reader that reads the table before the announcement goes out leaves no trace in its model. The instrument cannot see this defect. In addition, it does not model barriers (GCC warned:
'atomic_thread_fence' is not supported with '-fsanitize=thread'). That is why, since 2026-09-30, **under TSan the pair is written withseq_cstoperations**, which it models and which C++ guarantees: the announcement write and the table read on the reader, the swap and the slot reads on the writer. The warning disappeared. The product keeps the barrier. Theseq_cstwrite comes out asxchg, and it was measured: 9.95 to 10.4 ns per enter and leave on MSVC, against 6.8 to 8.9 ns with the barrier. On GCC the difference does not show. The "unordered pair" mutant (relaxed write,acquireread) still dies in the poisoned cycle in Release, three rounds out of three (measured again on 2026-09-30). - there is no data race at all for it to see in the registration: all accesses are atomic. The defect is one of atomicity (two operations that had to be one), and it knows nothing about that.
Both were then killed by their own oracles. The barrier, by the poisoned cycle in Release. The registration, by a new case in which eight threads reach RegisterReader at the same instant, thanks to a synchronization barrier before each round, and the test requires that no two ever come out with the same slot.
A clean TSan run, therefore, does not prove that these defects do not exist. The tool's declared limitations say what it does not see, and they are worth reading.
A note on the read without a range check
Enter does not check whether the slot number is in range, and that is on purpose. The spec forbids branches on the reader path, and RegisterReader has already done that check. A caller that invents a number has a defect of its own, and it is not a case to handle here.
The hot swap battery
Nine planted defects, eight killed, all in Release and in three rounds each.
| defect | result |
|---|---|
| the drain does not wait for whoever is inside | failed (2 checks) |
| the reader's barrier disappears | failed |
| the reader reads the table before announcing | failed |
registration replaces compare_exchange with read-then-write | failed |
| slot thirty-three becomes slot zero | failed |
| leaving no longer changes the slot number | hung: the drain waits forever¹ |
| the swap no longer publishes the new table | failed (2 checks) |
| the drain waits for it to become even instead of to change | failed (3 checks) |
| the writer's barrier disappears | survived, for the reason explained below |
¹ Hanging counts as dying. The report distinguishes a file without a score (the program did not reach the end) from a missing file, which is an instrument failure and is not classified.
The survivor, and why it stays
The barrier on the writer side is redundant, and that was measured before it was claimed.
Swap swaps the pointer with exchange in acq_rel. The acquire half of that operation already prevents the following reads (those of the slots) from moving above the swap. On x86 it is even more direct: exchange comes out as xchg with a memory operand, which has an implicit lock and is therefore already a full barrier.
The barrier stays because Drain is public and should not depend, without warning, on its caller having just done a swap in acq_rel. It costs one instruction on a cold path. But it stays documented as redundant. The comment that was there said it existed "for the same reason as the one over there", and that was false: on the reader side the write is relaxed, and only the barrier orders the pair. The comment was corrected, because a false necessity written in the code discourages whoever comes later from questioning it.
---
Checking the image before loading it
Three checks, applied from cheapest to most expensive: the folder, the digest, the signature. It is not worth reading an entire file to compute the digest if it sits in a place where anyone can swap it between the check and the load.
The injection point, and why not a call into Platform
The spec is explicit: *"through an injected IImageVerifier seam (Platform provides an implementation). The Plugin module does not link Platform."* Only the question exists here. The answer comes from outside. A consumer of the library may want plugins without also taking the system signature reading, and someone who wants signatures may have a policy different from the system's.
**eUnavailable is not approval.** It is the case that most often leads to a mistake. Whoever injected a verifier wants the signature checked, and a verifier that says "I don't know" checked nothing. Treating that as "passed" would turn the absence of a mechanism into permission. The test requires all four verdicts other than ok, including that one.
The measure is the folder, not the file
A read-only file in a world-writable folder can be deleted and recreated by anyone. The file's permissions do not protect it from that. The folder's do. And it is no use checking and then loading: between the two there is a window, and in an open folder that window is exploitable. It is the classic time-of-check to time-of-use race condition.
On POSIX, world-writable means the write bit for others set and the sticky bit cleared. The sticky bit is what makes /tmp an acceptable place.
On Windows the question is different, and what is measured is written down
The code walks the access control list looking for an entry that allows writing to Everyone (S-1-1-0) or to Authenticated Users (S-1-5-11). What is not measured, and matters just as much:
- deny entries are ignored. A folder that allows
Everyoneand then denies is considered writable here. The error is on the safe side: refusing too much, never too little. - groups are not expanded.
- inheritance is not re-evaluated. The code reads the effective list.
That is why the default on Windows is off, and on POSIX it is on, as the spec requires.
And the path was proven, not presumed. Granting write access to Everyone on the test folder with icacls, the answer becomes "world-writable" and the check fails. With the permission restored, it goes back to green. Without that test, the branch could always return "restricted" and the test would agree with code that never worked.
A limit of the test environment that serves as a warning to integrators
This tree is on a Windows disk mounted through 9p/DrvFs **without the metadata option**. That disk does not store modes: everything appears as 0777 and chmod has no effect. The test measures this before requiring anything, and skips the two cases that would need a restricted folder. A test that failed here would be failing the mount, not the code.
The real consequence is printed in the output: on a disk mounted this way, every folder looks world-writable, and the refusal that is on by default would refuse every load.
The digest oracle comes from the standard
The SHA-256 of "abc" is published in FIPS 180-4 and is ba7816bf…f20015ad. Computing the digest with my function and checking it with my function would be asking the defect to agree with itself.
An empty digest list does not refuse everything. It means "there is no list", and in that case the file is not even read. The comparison ignores case, because a hand-written list comes in whatever form it comes.
---
Reloading without stopping the host
The spec describes the sequence as "stop, drain, swap, start". Writing it down precisely reveals a problem: draining before swapping is useless. The drain waits for the readers of the current table. If the swap has not happened yet, new readers keep arriving at the old table while the writer waits. The drain only completes after the swap.
The real sequence:
- export the old plugin's state.
- copy the file, load the copy, resolve symbols,
Init, import the state. All of this is preparation: if it fails, the reload is aborted and the old plugin keeps serving as if nothing had happened. Stopon the old plugin.Starton the new one, which is ready but not yet published.- the swap: one atomic write, and the only instant at which anything changes for callers.
- the drain.
Shutdownon the old plugin, unload it, delete the old copy.- free the state block.
The window that remains: between 3 and 5, callers still see the old table, and the old plugin has already received Stop. There is no way to close it without having both working at the same time, which breaks any plugin that holds an exclusive resource. The choice is between a short window of "stopped but still visible" and one of "two owners of the same resource".
The defect the thread sanitizer found
The first version had one flat table and always published the same address. The drain guarantees that nobody is in the previous table. But if the previous one and the new one are the same object, the next reload writes over the structure the readers have in hand at that instant.
TSan pointed to the exact line: the main thread writing in reload.cpp against a live read on another thread, at the same stack address. Not even the test with 50 reloads and 10 million calls in flight had caught this. On x86 the write is of aligned pointers and the reader sees either the old value or the new one, both valid.
Now there are two tables, alternating. The write always goes to the one that is not published, and the drain that runs after each swap guarantees that it has already been released by everyone. Generation N+2 reuses generation N's table, and two drains ran between them.
The temporary copy, and why in the same folder
On Windows a loaded DLL is locked. By always loading from a copy, the original file is never locked and can be replaced at any moment. That is what makes hot reload possible. The test proves this by opening the original for writing with the plugin running.
The copy lives in the same folder as the original, not in a temporary folder. A library usually depends on others next to it, and changing folders changes how those dependencies are resolved.
When a copy cannot be deleted (on Windows the antivirus holds on to what was just unmapped), the reload moves on and reports it in SReloadReport::bTempLeaked.
Who owns the state memory, the question that brings everything down
The sequence has the unload in the middle. When it is time to free the state block, the library that allocated it is no longer mapped. There is no pfnFreeState that can be called, because there is no code to jump to.
That is why the contract is: the block is allocated with the host allocator, the one that came in the Init context. The host frees it with the same allocator, after handing it to the new copy. This is not a style preference. It is the only ownership order that survives unloading.
This is also what the spec asks for in "Cross-allocator: test plugin that tries to free host memory with a different allocator, must be caught by the API shape or the debug allocator". Here the shape of the API does not let the case happen, and the test requires the host allocator's count to close at zero after two reloads.
---
The host C layer
plugin_abi.h is for whoever writes a plugin. plugin_host.h is for whoever loads one, from a C program. The descriptor is opaque, because on this side there are std::string, std::atomic and a cache-line-aligned class, and none of that has a shape in C.
The hot path gains nothing in between: ETLC_PluginHost_Enter returns the same address that the C++ side publishes, and the implementation asserts this field by field at compile time. If someone adds a member in the middle of the C++ structure, the build stops there. The reason for checking each field, and not only the sizeof, is that in a table of function pointers two swapped fields do not give a wrong number. They give a jump to the wrong function.
tests/plugin_c_abi.c compiles as C11 and C17, without extensions, and loads a real plugin and calls it, with 31 checks. Compiling without running would prove that the header is valid C, and nothing more.
---
The sidecar manifest
The spec asks for the sidecar manifest for one thing only: "discovery without loading, which matters when scanning a directory of 200 plugins to find the three you want".
It is not authoritative about anything. It is a text file next to the library. Anyone who can write to the folder can write whatever they want in it, including a generation the host accepts, pointing at a library that does not have it. It serves to choose which of the two hundred are worth looking at. The ABI gate still reads the manifest from inside the library file, before loading anything.
Format: key=value, one per line, integers only. A format with nesting would need a parser, and a parser running over a file that anyone can edit is attack surface with no gain at all. There is a 1 KiB cap. Without it, a one-gigabyte file with the right name turns a discovery scan into a denial of service.
---
The measured targets
| spec target | measured | where |
|---|---|---|
| call indistinguishable from a direct indirect call, < 2 ns | −0.11 ns (1.47 → 1.36) | Windows x64 Release |
| same | −0.18 ns (1.77 → 1.59) | Linux Release |
| load < 1 ms, excluding the OS loader | 0.016 ms ours + 4.4 ms OS | Windows Release |
| same | 0.002 ms ours + 2.3 ms OS | Linux Release |
| 200 sidecar manifests < 5 ms | 0.067 ms to parse (0.3 µs each) | Windows Release |
| 10,000 reload cycles under concurrent calls | 10,000 swaps + 50 real reloads, 0 wrong reads | both |
| ASan, UBSan, LSan clean | 167 checks, 0 warnings | Linux |
| TSan clean | 166 checks, 0 data races | Linux |
Three things these numbers hide if they are not stated
1. The cost per operation is not the cost per call, and the two are measured separately. The spec says the reader takes the pointer once at the top of its operation. The 2 ns target is about the call. Entering and leaving the slot costs 6.35 ns (Windows Release) and 13 ns (Linux Release), and most of that is the barrier. It is a locked instruction, which is not free and which is the only thing between a reload and a guaranteed crash. Charging it on every call would measure a usage the spec does not describe.
2. Time targets are not enforced in debug builds or under sanitizers. The Windows debug configuration carries /Od /RTC1, which inserts checks around every call: a 2.40 ns difference there, against −0.11 ns in Release. Under TSan, entering and leaving goes from 13 ns to 320 ns. The number is printed in both. Only the gate changes.
3. The 5 ms scan target is not reachable in either of these two test environments, and the code is not to blame. Measured: under WSL, on 9p, opening and reading a 27-byte file costs ~3 ms. 200 files exceed half a second, and that happens just the same with a program that parses nothing. On Windows on this machine, with antivirus, it is ~0.12 ms per file. The target requires a machine where opening a file costs about 25 µs.
That is why the gate is on what this library controls, which is parsing, and it costs 0.067 ms for the two hundred. The total is printed with the per-open cost next to it. The first version of this case measured the total and subtracted a disk control. On Windows, the subtraction of two noisy numbers gave minus ten milliseconds. The error was in the instrument, not in the code.
---
---
The audit against spec 06, and what it found
Until this batch, this README said:
## The status of spec 06 Done.
What was done were the pieces, and all of them are good. What was not done was the class the spec asks for to bring them together, and without it the pieces did nothing.
| what the spec asks for | was it there? |
|---|---|
CPluginRegistry::Scan( sDirectory, SScanOptions, pFound ): discovery without loading | did not exist |
CPluginRegistry::LoadAll(): ordered by dependency | did not exist |
CPluginRegistry::Query( sInterfaceName, iVersion, ppOut ): by interface | did not exist |
CPluginRegistry::EnumPlugins( callback ) | did not exist |
What that meant, concretely
TopologicalOrder (dependency ordering with reporting of the cycle members) was written, had twelve dedicated checks, and was not called by anyone outside its own test. The same was true of ProbeSidecar, the sidecar manifest reader, which exists to discover without loading.
The existing host loaded a folder in alphabetical order of file name. And its comment explained, at length, that the order returned by the file system is not stable across machines or across systems. Without sorting, the plugins' initialization order would change from one machine to another, and a dependency defect between them would appear on one and not on the other, the worst kind of defect there is.
The comment describes exactly the problem that the ordering next to it solved, and it solves it by sorting by file name, which is the wrong order for another reason. Sorting by name makes the order stable. It does not make it correct.
And the query by interface was not the one the spec asks for
The existing one is Query( sPluginName, sInterfaceName, uVersion ). With the plugin name as an argument, whoever wants a renderer has to know which plugin offers it, and then the plugins stop being replaceable, which was the reason they existed.
The manifest's pProvidedInterfaces field, where each plugin declares what it offers, was not read by anyone. Now it is the query filter.
There were two contracts, and the host only spoke the old one
SPluginInfo, in host.hpp: name, version, description, Initialize, Shutdown, Query. No dependencies and no interface list. That was what CPluginHost loaded.
ETLC_PluginManifest, in c/plugin/plugin_abi.h: the spec's contract, with declared dependencies and interfaces. It was read by ProbeManifest and by nobody else.
The new registry speaks the second one. CPluginHost stays as it is, because it is what the old plugins use. That is stated here, instead of being resolved by deleting one of them.
---
The order of the steps, and why the third one is surprising
- Scan reads the sidecar manifest, or inspects the library file. Nothing is mapped.
- The ABI gate runs here, on the file. A plugin from another generation is never mapped, and that is why the
abort()in one of its static initializers never runs, which is the spec's criterion. - Map, without initializing.
- Order by dependency. With a cycle, nothing comes up.
- Initialize in order. A failure quarantines that plugin, and the others continue.
Step 3 is the surprising one. Ordering by dependency requires knowing what each one depends on, and that is text inside the image: the manifest strings are pointers into the plugin's static array. There is no way to read them without mapping.
This does not mean the plugin's code runs out of order. What runs on mapping are the static initializers, and the module's contract is that they do nothing. That is why the spec's criterion talks about a plugin that aborts in a static initializer as a test of the gate, not of the order. What runs in the right order is ETLC_Plugin_Init, which is where the plugin is entitled to do something.
And the ABI version is checked again, now on the mapped image. The two gates should say the same thing. When they do not, it is because the file changed between the scan and the load, which is exactly the window through which someone swaps the library after it has been approved.
---
The order oracle is a file, not what the registry says about itself
Asking the registry in what order it loaded would be asking it to confirm itself. Each test plugin writes a letter to a file when its ETLC_Plugin_Init runs. The file records the order in which the code really ran.
And the file names are in the reverse of the correct order, on purpose. dep_a needs dep_b, which needs dep_c:
| alphabetical order of the files | A B C |
| dependency order | C B A |
| what the witness file says | **CBA** |
With names in the correct order, the test would pass with the old implementation and measure nothing.
And the scan proves the opposite through the same file: after Scan, the witness is empty, because no plugin ran. That is what the spec means by "scanning a directory of 200 plugins to find the three you want": a scan that loaded would be loading two hundred.
---
The cycle, and the missing dependency
With a cycle, nothing comes up. Bringing up "the ones that can" would leave a set that depends on things that are not there, and the failure would appear on the first call instead of at load time. The report carries the members, not "there is a cycle". With twenty plugins, the second phrase sends the integrator searching a twenty-node graph.
And a missing dependency says what is missing and who asked for it. The test builds an incomplete installation (it copies dep_a and dep_b to a folder and leaves dep_c out) and requires both halves:
missing "dep_c", requested by "dep_b"---
The allocator cannot be null, and the test plugin refuses when it is
The C contract tells the plugin author to use the host allocator, and only that one, for memory the host will see. A host that does not provide it pushes the plugin back to the malloc of its own copy of the C library. On Windows, where each library can bring its own copy, that corrupts the heap without anyone switching languages.
That is why the registry carries a default allocator, and the test plugin **returns ETL_PLUGIN_ERR_CONFIG when pAllocator is null**. That way the test fails if someone ever stops passing it.
---
---
The defect that two tests running at the same time found
Reloading copies the plugin file before loading it, so that the original can be replaced on disk while the old one stays mapped. The copy's name was **original.recarga<generation>.dll, in the original's folder**, made only from the generation number.
Two processes, the same name
Two instances that load the same plugin for the first time ask for the same file. On Windows the second cannot even open it for writing while the first has it mapped, and the load fails with eCopyFailed.
This is not a test-only scenario. It is what happens with two instances of the same application pointing at the same plugin folder: the normal case of an editor opened twice, or of a service with two copies running.
How it appeared
ctest starts plugin.cabi.c11 and plugin.cabi.c17 at the same time, and both load the same plugin. Seven checks failed at once: loads from a temporary copy and everything that comes after it, because the copy failure brings down the rest of the case. It looked like a disaster and it was one line.
It appeared in about half of the full Debug runs with --parallel 8, and never with the test running alone, which is exactly what you would expect from a collision between two processes.
The name now
original.recarga<generation>_<pid>_<serial>.dll. The pid separates processes. The serial, a process-wide atomic counter, separates two hosts within the same process: the case of someone who has two CHotPlugin instances pointing at the same file. The generation stays because it helps to read the name and understand where it came from.
Nothing rebuilds the name from the generation. It is stored in m_sCopy, and the cleanup reads it from there. That is why the change has no effect anywhere else.
Zero failures in eighteen Debug runs after this, against three in six before.
How this was verified
tests/plugin_registry.cpp (plugin.registry), 41 checks, with five test libraries generated from the same source: the chain dep_a → dep_b → dep_c and the pair cic_x ⇄ cic_y.
Whole ctest run 79/79 on Windows x64, Debug and Release.
The registry has not been compiled on Linux. The rest of the module was, in the previous batch. This part uses
std::filesystemandCDynamicLibrary, which already compiled on both systems, but it has not been through any compiler outside Windows. This is recorded.
---
What is still missing from spec 06
- **
Reloadis not in the registry.**CHotPluginreloads a plugin with draining, and it works well. Wiring it to the registry (stopping, swapping and restarting one of the loaded plugins, keeping the dependency order) is not done. - **The registry does not consult the
IImageVerifier.** The injection point exists inverification.hppand has a test with a fake verifier.Scandoes not call it yet. - **
SScanOptionsdoes not filter by name or by interface.** The spec talks about finding "the three you want" among two hundred. Today the scan returns all the ones that pass the gate, and the caller does the choosing. - **
CPluginHoststill loads in alphabetical order.** It speaks the old contract, which has no dependencies, so there is nothing to order by. Whoever wants ordering uses the registry. - The remaining acceptance targets are still remaining: the 200-manifest scan in 5 ms needs a test environment without antivirus intercepting every open, and wiring up the image verifier is the integrator's choice.
The status of spec 06, as it was written in the previous batch
This section said "Done", and it is kept here as it was. The section above explains what was missing. Deleting the wrong claim would hide that it was ever made, and it is useful to know that the pieces passed their tests while the thing they served did not happen.
Done. The C contract (product), the ABI gate without executing, search with an explicit policy, the lifecycle with the exception barrier, dependency ordering with reporting of the cycle members, the flat table with atomic swap and draining, the three image checks, reloading with a temporary copy and state handoff, the sidecar manifest, the host C layer, and the measured targets.
| configuration | test suite | ctest |
|---|---|---|
| Linux Debug | 157 | 56/56 |
| Linux Release | 168 | (no number) |
| Linux ASan/UBSan/LSan | 167, 0 warnings | (no number) |
| Linux TSan | 166, 0 data races | (no number) |
| Windows x64 Debug | 161 | 52/52 |
| Windows x64 Release | 161 | (no number) |
What remains to be done depends on decisions, not on code:
- the 5 ms scan needs a test environment with a local disk, without antivirus intercepting every open. The number this library controls is measured and fits with 70× headroom.
- the
IImageVerifierhas the injection point and the test with a fake verifier. The real implementation is provided by the Platform module, and wiring the two together is the integrator's choice. The spec is explicit that Plugin does not link Platform. - the refusal for world-writable folders is on by default on POSIX and off on Windows, and the reason is written down. Changing the Windows default is a policy decision, not an implementation decision.