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
21 changes: 7 additions & 14 deletions WIP.BugHunt.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,15 @@ Seen on BETA 1042 by the `commonctls` setup (fixture `test/shots/commonctls`).

### tbKMode

tbKMode 0.3.0.6, BETA 1042, met while moving its section to that version. Neither is checked.

- **RtlFailFast** (modWDM.twin line 5837) is a `cdecl naked` Sub whose code is `mov ecx, 7` then
`int 29h`: it loads 7 (`FAST_FAIL_FATAL_APP_EXIT`), not its *code* argument, so
**FatalListEntryError**, which passes `FAST_FAIL_CORRUPT_LIST_ENTRY` (3), raises 7. The WDK's
`RtlFailFast` is `__fastfail(Code)`. Read from the source only: a case would end the process.
**Page affected**: modWDM's sections of **RtlFailFast** and **FatalListEntryError**.
- **SE_SELF_RELATIVE** (`SECURITY_DESCRIPTOR_CONTROL`, modWDM.twin line 4954) is `&H8000&`, a
BETA 1042: met while moving the section to 0.3.0.6, and still in 0.3.0.8, which fixed the other
two leads of 0.3.0.6 (RtlFailFast's fixed reason, MmCopyMemory's 4-byte *NumberOfBytes*).

- **SE_SELF_RELATIVE** (`SECURITY_DESCRIPTOR_CONTROL`, modWDM.twin line 4964) is `&H8000&`, a
Long 32768, which an Integer cannot hold: **SECURITY_DESCRIPTOR.Control** (line 236) and the
*Control* of RtlGetControlSecurityDescriptor and RtlSetControlSecurityDescriptor (lines
5155-5156) are Integers, so assigning the flag to one may raise overflow, and comparing one with
it is never true. Not run.
- **MmCopyMemory** (modWDM.twin line 5354) takes *NumberOfBytes* `ByVal As Long`, where the WDK's
routine takes a `SIZE_T`: 8 bytes in a 64-bit build, passed in a register whose high 32 bits the
call may leave as they were. The verifier's `check` reports it (0.3.0.5 and 0.3.0.6); no case and
no page states it. **Page affected**: modWDM's Declares, if it is a defect.
5164-5165) are Integers, so assigning the flag to one may raise overflow, and comparing one with
it is never true. Not run. By the package's convention for 16-bit values (0.3.0.8's
ImageMachineType) it would be `&H8000`.

### User-defined types (from the GdiPlus work)

Expand Down
2 changes: 1 addition & 1 deletion WIP.Contributed.md

Large diffs are not rendered by default.

39 changes: 26 additions & 13 deletions WIP.tbKMode.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ about the package beyond its pages; the rules for every contributed package are

## What the section decided (owner)

- **Experimental**, with no second-agent reproduction (owner waived it): `parent: Experimental
Packages`, its line in `Experimental.md`, its folder in `_book.yml`'s `left_out:`.
- **Contributed from 0.3.0.8** (owner, 2026-11-22): Experimental up to 0.3.0.6, with no
second-agent reproduction (owner waived it). 0.3.0.8 fixed every defect reported on #2, #3 and #4,
so it is `parent: Contributed Packages`, its line in `Contributed.md`, and its chapter in
`_book.yml` in alphabetical order, after tbFileSysTools's (owner). Its remaining limits are stated in callouts: SiloContext,
KUSER_SHARED_DATA, ExFreePool2, the names shared with VBA, and twinBASIC's 32-bit FastCall defect.
- **No driver is ever loaded, installed, signed or started.** Building one is allowed (the probe
does), and so is reading a built one. Samples are compiled only (`check_build`), never run.
- **Pages**: the landing page and one per module (modWDM, modFltMgr, modSysInfo, modDisk, modImage,
Expand All @@ -22,10 +25,15 @@ about the package beyond its pages; the rules for every contributed package are
writer joins: the snapshot keeps no `#If` of an alias). A name that is a procedure in one build and
a Declare in the other (InterlockedIncrement, KeAcquireSpinLock, KeQuerySystemTime) is a `###`
section in `procedures.md` with a syntax for each build.
- **Callouts** are labelled with the package version (`0.3.0.6`) or `timeless`, each backed by
- **Callouts** are labelled with the package version (`0.3.0.8`) or `timeless`, each backed by
cases or the probe; the one about twinBASIC itself, under *Building a driver*, is labelled `BETA
1042` and names its reproducer, `bugs/declare-fastcall-static-binding-32bit`.
- The owner reviewed the 0.3.0.5 pages, the verifier's included (2026-11-17).
- **A page holds every Module of its file** (0.3.0.8): ImageMachineType moved into a Module
SuppressedWarnings of modImage.twin, under `[IgnoreWarnings(TB0012)]`, so the writer and
`wdk/api.mjs` take a module's names from every Module whose file is `<Module>.twin`, and
modImage's page lists it. Its members above `&H7FFF` are Integer literals from 0.3.0.8, the
16-bit convention, so the `values` of `verifier/config/tbKMode.json` give their negative values.
- **A declaration right for the kit the package was written from is no defect**: modSysInfo follows
the 10.0.22621 SDK (`POLICY_AUDIT_SUBCATEGORY_COUNT` 59, no `FileFsGuidInformation`), and its cases
check that kit's values (`wdk/modSysInfo.json`, `values` and `sizes`). From 0.3.0.6 the load
Expand All @@ -40,7 +48,9 @@ about the package beyond its pages; the rules for every contributed package are
- **IO_DRIVER_CREATE_CONTEXT.SiloContext stays a NOTE** (owner, 2026-11-22): fafalone answered on #4
that it is under `#If NTDDI_VERSION >= NTDDI_WIN10_RS1` as in the SDK, but the package defines no
NTDDI_VERSION and a project's constants do not reach a package, so the field is never compiled;
the NOTE says why.
the NOTE says why. fafalone, on #4 after 0.3.0.6: `NTDDI_VERSION` is the project's to set, and
defining it in the package would be wrong once twinBASIC passes settings through; so the NOTE
also says that a copy of the package built with it set has the field.

## How the pages are written

Expand Down Expand Up @@ -70,11 +80,13 @@ package*). Each folder's README says how.
`tbrun --beside` copies them into the exe's folder, where the loader finds them before System32;
a routine the exe imports that a stand-in lacks stops the exe from starting (0xC0000139), so a
routine a new case reaches is added to the stand-in and its `.def`. A routine called in the wrong
convention gets an x86 naked stand-in that pops what the defective caller pushed; its comment
says what to change once fixed (KfAcquireSpinLock, 0.3.0.6). **A 32-bit exe ends at its first
FastCall case** (twinBASIC BETA 1042, `bugs/declare-fastcall-static-binding-32bit`): those cases
are last in the file and record *no result*; the cases that call no FastCall Declare come before
them.
convention gets an x86 naked stand-in that pops what the defective caller pushed, and a comment
that says what to change once fixed (none from 0.3.0.8, which declares KfAcquireSpinLock
FastCall). **A 32-bit exe ends at its first FastCall case** (twinBASIC BETA 1042,
`bugs/declare-fastcall-static-binding-32bit`): those cases are last in the file and record *no
result*; the cases that call no FastCall Declare come before them. The first of them,
KeAcquireSpinLock_PassesTheLock, passes on 0.3.0.8 (two runs): ECX holds the lock's address at the
call, but the call leaves the stack 4 bytes off, and the next case ends the exe.
- `probe/`: builds small drivers with `tbbuild --build` and judges their import tables with the
verifier's `checkDriver`; it backs the landing page's warning on the package's own settings.
- `verifier/`: published as `tbKMode-verifier.zip` (its page, `Verifier.md`). `check` checks each
Expand Down Expand Up @@ -129,12 +141,13 @@ posts them. #1 (modFltMgr, modDisk) and #2 (modSysInfo) were answered by 0.3.0.5
`tbkmode-issue-2-comment.md`). #3 (the package's Settings) and #4 (the declarations of modWDM and modImage) are filed
(2026-11-17), from `tbkmode-issue-3.md` and `tbkmode-issue-4.md`. 0.3.0.6 answered #3 (its Settings
set the five) and most of #4; fafalone's replies on #2 and #4 decided the conventions above. The
reply to #4 on 0.3.0.6 is drafted in `tbkmode-issue-4-comment.md`.
reply to #4 on 0.3.0.6 is drafted in `tbkmode-issue-4-comment.md`. 0.3.0.8 fixed every item of
its list (1-12); what is left is IO_DRIVER_CREATE_CONTEXT.SiloContext, KUSER_SHARED_DATA (fine as it
is) and the names shared with VBA.

## Leads, not on the pages

In [WIP.BugHunt.md](WIP.BugHunt.md#tbkmode): RtlFailFast's fixed reason (on the page as a NOTE
marked `none`), `SE_SELF_RELATIVE` `&H8000&` against the Integer `Control` field, and MmCopyMemory's
4-byte *NumberOfBytes* in a 64-bit build, which the verifier reports and no page states yet. Omissions, not defects: newer
In [WIP.BugHunt.md](WIP.BugHunt.md#tbkmode): `SE_SELF_RELATIVE` `&H8000&` against the Integer
`Control` field. Omissions, not defects: newer
members missing from `IMAGE_DEBUG_EX_CH` and `ImageMachineType`, `SYMBOLIC_LINK_SET`; optional
pointers declared `ByRef` (the `ByVal vbNullPtr` paragraph covers them).
1 change: 0 additions & 1 deletion docs/Community/Experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ These packages are published through TWINSERV, as the [Contributed Packages](../
- [**GdiPlus**](../Contributed/GdiPlus/) -- Windows GDI+: lines, shapes, text and images drawn with pens, brushes, fonts, paths and transformations. Version 0.9.45.0 compiles, and has many members that give wrong results, fail or do not return: a colour given as a **Long** has its red and blue swapped and loses its alpha, saving an image with encoder parameters fails, the font collections cannot be created, and several path, metafile and effect members fail.
- [**Generics**](../Contributed/Generics/) -- tuples, read-only objects that hold one to seven values of declared types. Version 1.0.0.0 does not compile in twinBASIC BETA 1042: the **WinDevLib** package it embeds declares six delegates with no return type, which this build reports as TB0003, an error under WinDevLib's own settings.
- [**NewTabCtl**](../Contributed/NewTabCtl/) -- the **NewTab** control, a container with a row of tabs. Version 10.0.289.0 does not compile in twinBASIC BETA 1042: four properties of the control name property pages that the package does not hold, which this build reports as TB5259.
- [**tbKMode**](../Contributed/tbKMode/) -- declarations for writing a Windows kernel-mode driver: the Windows Driver Model, the Filter Manager, system information classes, disk structures and the PE image format. Version 0.3.0.6 compiles, and some of its Windows Driver Model declarations do not match the Windows Driver Kit: a structure with another layout, procedures that lower their caller's IRQL, and in a 32-bit driver a routine called in the wrong convention and routines that the 32-bit kernel does not export. A 32-bit driver also cannot call a `__fastcall` routine of the kernel, because of a defect of twinBASIC BETA 1042.
- [**TBMANLIB**](../Contributed/TBMANLIB/) -- a toolkit of helpers: JSON, an HTTP client, logs, INI and CSV files, timers, strings, bytes, files and dictionaries. Version 1.0.0.29 compiles for 32-bit programs, does not compile in a 64-bit build, and has several members that fail or give wrong results.
- [**TBMANLIB_Aes**](../Contributed/TBMANLIB_Aes/) -- AES encryption of text and byte arrays. Version 1.0.0.4 compiles, and its cipher modes are not the ones their names say: the default mode, CBC, runs ECB, and the output does not match the standard test vectors.
- [**TBMANLIB_ConsoleComponents**](../Contributed/TBMANLIB_ConsoleComponents/) -- text-interface components for console programs: progress bars, charts, tables and panels. Version 1.0.0.1 compiles, and its components cannot use a console object of the published TBMANLIB_ConsoleCore, since the package carries a copy of that package whose types the compiler treats as different.
Expand Down
1 change: 1 addition & 0 deletions docs/Reference/Contributed.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Each package is versioned apart from twinBASIC, and its section says which versi
- [**Fmt**](Fmt/) -- **StringFormat**, a string built from a format string and values, in the style of C#'s **String.Format**
- [**OLEGuids**](OLEGuids/) -- OLE and COM interfaces and structures in twinBASIC source, in place of `OLEGuids.tlb`
- [**tbFileSysTools**](tbFileSysTools/) -- files, folders, drives and text streams, in place of the Scripting Runtime's **FileSystemObject**
- [**tbKMode**](tbKMode/) -- declarations for writing a Windows kernel-mode driver: the Windows Driver Model, the Filter Manager, system information classes, disk structures and the PE image format
- [**TBMANLIB_RES**](TBMANLIB_RES/) -- resources embedded in an executable, as bytes, strings, hex, Base64 or pictures
- [**tbProjPacker**](tbProjPacker/) -- a `.twinproj` or `.twinpack` file unpacked into a folder of source files, and packed again
- [**tbRegExp**](tbRegExp/) -- regular expressions, in place of VBScript's **RegExp** object
Expand Down
23 changes: 6 additions & 17 deletions docs/Reference/Contributed/tbKMode/index.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
---
title: tbKMode Package
parent: Experimental Packages
parent: Contributed Packages
has_toc: false
indexed_from: 0.3.0.6
indexed_from: 0.3.0.8
permalink: /tB/Packages/Contributed/tbKMode/
---

# tbKMode Package
{: .no_toc }

**Author:** fafalone. **Version:** 0.3.0.6. **On GitHub:** [fafalone/tbKMode](https://github.com/fafalone/tbKMode).
**Author:** fafalone. **Version:** 0.3.0.8. **On GitHub:** [fafalone/tbKMode](https://github.com/fafalone/tbKMode).

Declarations for writing a Windows kernel-mode driver in twinBASIC.

The **tbKMode** contributed package declares types, routines, constants and macros of the Windows Driver Kit (WDK), from its headers `wdm.h`, `ntddk.h`, `ntifs.h`, `fltkernel.h`, `ntdddisk.h`, `mountmgr.h` and `ntimage.h`, for a driver built as a `.sys` file. It covers a part of the WDK: its author describes it as a start, to be extended by each project that uses it.

<!-- test: WDMFieldTests.OB_PRE_OPERATION_INFORMATION_ParametersIsAPointer, KernelHelperTests.KeAcquireSpinLock_PassesTheLock, probe x86exports, KernelHelperTests.InterlockedAdd64_LeavesTheIrqlAsItWas, KernelHelperTests.InterlockedIncrement_LeavesTheIrqlAsItWas, ImageFieldTests.MachineTypes_EqualTheMachineField -->
> [!WARNING] 0.3.0.6
> The package compiles, and a driver can be built with it, but some of its declarations do not match the WDK, and fail with no error. The main ones:
>
> - [**OB_PRE_OPERATION_INFORMATION**](modWDM#OB_PRE_OPERATION_INFORMATION) has another layout than the C structure, so that its **Parameters** holds other data: see [modWDM](modWDM#types).
> - [**InterlockedAdd64**](modWDM#InterlockedAdd64), and in a 64-bit driver [**InterlockedIncrement**](modWDM#InterlockedIncrement), release a spin lock that they never acquired, which lowers the IRQL of their caller to `PASSIVE_LEVEL`.
> - In a 32-bit driver, [**KeAcquireSpinLock**](modWDM#KeAcquireSpinLock) calls its routine in another calling convention than the one of `hal.dll`, and [**KeLowerIrql**](modWDM#KeLowerIrql) and the `Rtl...BitsEx` routines name routines that the 32-bit kernel does not export: see [modWDM](modWDM#declares).
> - The members of [**ImageMachineType**](modImage#ImageMachineType) above `&H7FFF` are not equal to the **Machine** field of an image that holds them.
>
> Each page states the defects of its own declarations, in a warning or a note below its table or in the section of the procedure.

* TOC
{:toc}

Expand All @@ -44,7 +33,7 @@ The package's own procedures are compiled with the package's settings, not the d

<!-- test: bug declare-fastcall-static-binding-32bit, KernelHelperTests.IoCallDriver_PassesTheDeviceAndTheIrp, KernelHelperTests.ObDereferenceObject_PassesTheObject, KernelHelperTests.ExAcquireFastMutex_PassesTheMutex, KernelHelperTests.ExReleaseFastMutex_PassesTheMutex, KernelHelperTests.ExTryToAcquireFastMutex_PassesTheMutex, KernelHelperTests.InterlockedPopEntrySList_PassesTheListHead, KernelHelperTests.ExReleaseResource_ReleasesTheResource, KernelHelperTests.InterlockedIncrement_IncrementsTheValue, KernelHelperTests.ExReleaseResourceAndLeaveCriticalRegion_PassesTheResource, KernelHelperTests.ExReleaseResourceLite_PassesTheResource, KernelHelperTests.ExInterlockedAddUlong_PassesTheArguments -->
> [!WARNING] BETA 1042
> In a 32-bit driver no call of a `FastCall` declaration reaches its routine as it should. With **Runtime Binding Of DLL Declares** set to **No**, as a driver has it and as the package sets it for its own declarations, twinBASIC calls a `FastCall` declaration without its arguments in the registers where a `__fastcall` routine reads them: the routine works on other values, and the driver's stack is left wrong. The 32-bit kernel exports many routines as `__fastcall`, among them **IofCallDriver**, which [**IoCallDriver**](modWDM#IoCallDriver) calls, **ObfDereferenceObject**, the fast mutexes, **KfRaiseIrql** and **InterlockedIncrement**, and the package declares each of them `FastCall`. A 64-bit driver is not affected: it has one calling convention.
> In a 32-bit driver no call of a `FastCall` declaration reaches its routine as it should. With **Runtime Binding Of DLL Declares** set to **No**, as a driver has it and as the package sets it for its own declarations, twinBASIC calls a `FastCall` declaration without its arguments in the registers where a `__fastcall` routine reads them: the routine works on other values, and the driver's stack is left wrong. The 32-bit kernel exports many routines as `__fastcall`, among them **IofCallDriver**, which [**IoCallDriver**](modWDM#IoCallDriver) calls, **ObfDereferenceObject**, **KfAcquireSpinLock**, which [**KeAcquireSpinLock**](modWDM#KeAcquireSpinLock) calls, the fast mutexes, **KfRaiseIrql** and **InterlockedIncrement**, and the package declares each of them `FastCall`. A 64-bit driver is not affected: it has one calling convention.

The [Build Output Path](../../../IDE/Project-Settings#build-output-path) ends in `.sys` in place of `.${FileExtension}`. A driver is built for the architecture of the Windows it runs on: the kernel runs no 32-bit code on 64-bit Windows. [Project Types](../../../../Features/Project-Configuration/Project-Types#kernel-mode-drivers) says more about kernel-mode projects.

Expand All @@ -53,7 +42,7 @@ The [Build Output Path](../../../IDE/Project-Settings#build-output-path) ends in
**modWDM** has procedures named [**LCase**](modWDM#LCase), [**UCase**](modWDM#UCase), [**InStr**](modWDM#InStr) and [**MsgBox**](modWDM#MsgBox), as VBA's functions are. They take the addresses of null-terminated strings, since a driver works on the kernel's strings, and the package's changelog calls them overrides of VBA's.

<!-- test: KernelHelperTests.BareNames_CallTheModulesProcedures, compile MsgBoxBareNameIsTheModules -->
> [!WARNING] 0.3.0.6
> [!WARNING] 0.3.0.8
> A call of one of these names without a module name calls VBA's function, not the package's. `LCase(StrPtr(s))` and `UCase(StrPtr(s))` leave the string as it was, `InStr(StrPtr(a), StrPtr(b))` returns 0 when *a* holds *b*, and `MsgBox "text"` compiles as VBA's **MsgBox**. A driver writes the module name to call the package's procedures: `modWDM.LCase`, `modWDM.UCase`, `modWDM.InStr` and `modWDM.MsgBox`.

## Modules
Expand All @@ -71,7 +60,7 @@ The [Build Output Path](../../../IDE/Project-Settings#build-output-path) ends in
- [tbKMode Writer](Writer) -- writes the module pages of this section
- [Project Settings](../../../IDE/Project-Settings) -- the settings of a driver project
- [Project Types](../../../../Features/Project-Configuration/Project-Types#kernel-mode-drivers) -- kernel-mode drivers in twinBASIC
- [Experimental Packages](../../Experimental/) -- the packages from TWINSERV that are not yet ready for use in a program
- [Contributed Packages](..) -- the other packages from TWINSERV

## Test cases

Expand Down
Loading