From 263e37aeb115e30ddff997db4e59167c9c42388c Mon Sep 17 00:00:00 2001 From: Pension_qe <97943633+AbyssGG@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:08:07 +0800 Subject: [PATCH 1/2] fix: prepare 0.2.1 correctness and safety fixes Validate shape dimensions and C strings, prevent unsafe tensor copies and avoid completion queue timeout overflow. Add regression tests and prepare patch release notes. --- CHANGELOG.md | 13 +++++ DEVLOG.md | 29 ++++++++++ README.md | 3 + README_zh-CN.md | 2 + RELEASE_NOTES.md | 76 +++++--------------------- docs/api-overview.md | 5 +- openvino.nimble | 2 +- src/openvino/compiled_model.nim | 2 + src/openvino/core.nim | 9 +++ src/openvino/infer_request.nim | 8 ++- src/openvino/model.nim | 2 + src/openvino/private/conversions.nim | 13 ++++- src/openvino/properties.nim | 3 + src/openvino/tensor.nim | 23 ++++++-- src/openvino/version.nim | 2 +- tests/integration/tasync_inference.nim | 14 +++++ tests/unit/tcstring_safety.nim | 34 ++++++++++++ tests/unit/tshape.nim | 13 +++++ tests/unit/ttensor_copy_safety.nim | 39 +++++++++++++ 19 files changed, 218 insertions(+), 74 deletions(-) create mode 100644 tests/unit/tcstring_safety.nim create mode 100644 tests/unit/ttensor_copy_safety.nim diff --git a/CHANGELOG.md b/CHANGELOG.md index d2ea01b..9ee5d88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,19 @@ and the OpenVINO runtime baseline evolve independently: ## Unreleased +## 0.2.1 + +Release in preparation. + +### Nim API + +- Reject tensor copies involving types that contain Nim-managed references. + Raw byte copies of these values can corrupt ownership and memory. +- Reject negative shape dimensions even when another dimension is zero. +- Reject embedded NUL characters in C-bound names, properties, device names + and paths instead of silently truncating them. +- Avoid a deadline overflow when a completion queue has a long timeout. + ## 0.2.0 Released on 2026-10-18 as diff --git a/DEVLOG.md b/DEVLOG.md index f4dc771..34e1746 100644 --- a/DEVLOG.md +++ b/DEVLOG.md @@ -31,6 +31,22 @@ Phase numbers refer to ## English +### 2026-11-15 0.2.1 correctness fixes + +Validate all static dimensions before applying the zero-size shortcut; reject +embedded NULs at C string boundaries; and measure completion queue timeouts +using elapsed monotonic time rather than constructing an overflowing deadline. +Regression tests cover each reproduced failure, including a long timeout and +valid zero-sized shapes whose other dimensions would otherwise overflow. + +### 2026-11-15 Tensor copy ownership safety + +The typed tensor copy functions checked element widths but also accepted Nim +types containing managed references. Copying raw tensor bytes over such a +value can corrupt reference ownership. They now reject types for which Nim's +`supportsCopyMem` is false, before accessing the tensor or runtime. A +runtime-free regression covers all four entry points and a nested string. + ### 2026-09-24 Phase 0: audit the prototype and freeze the scope Audited the Resonance prototype that this package replaces, and verified the @@ -686,6 +702,19 @@ ThreadSanitizer cannot run because OpenVINO loads `libtbbbind` with ## 中文 +### 2026-11-15 0.2.1 正确性修复 + +先校验全部静态维度再处理零元素形状;拒绝 C 字符串边界中的 NUL;完成队列用单调时间 +的已耗时计算超时,避免构造溢出的截止时间。回归测试覆盖每个复现的问题,包括超长超时 +和其他维度乘积会溢出的合法零元素形状。 + +### 2026-11-15 张量复制的引用安全 + +张量类型化复制原先只检查元素宽度,也会接受包含 Nim 托管引用的类型。将张量字节直接 +覆盖这些值可能破坏引用的所有权。现在四个入口在访问张量或运行时之前,拒绝 Nim 的 +`supportsCopyMem` 判定不能按字节复制的类型;无需运行时的回归测试覆盖了四个入口和 +嵌套字符串。 + ### 2026-09-24 Phase 0:审计原型并冻结范围 审计了本包要取代的 Resonance 原型,并且没有采信计划里的 ABI 论断,而是逐项 diff --git a/README.md b/README.md index 216278a..0ab027a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ Corporation.** OpenVINO is a trademark of Intel Corporation. ## Status +Version `0.2.1` is in development and contains correctness and memory safety +fixes. The latest published release remains `0.2.0`. + Version `0.2.0` was released on 2026-10-18 and adds asynchronous inference, dynamic and partial shapes with reshape, and declarative layout and preprocessing. Download the source archives from the diff --git a/README_zh-CN.md b/README_zh-CN.md index 73f558f..5b9d1e3 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -10,6 +10,8 @@ Corporation 的认可或附属关系。** OpenVINO 是 Intel Corporation 的商 ## 项目状态 +版本 `0.2.1` 正在开发,包含正确性和内存安全修复;最新已发布版本仍为 `0.2.0`。 + 版本 `0.2.0` 已于 2026-10-18 发布([GitHub Release](https://github.com/AbyssGG/OpenVINO-Nim-API/releases/tag/v0.2.0)),新增异步推理(`startAsync`、`wait`、 `waitFor`、`cancel` 与 `CompletionQueue`,不在 OpenVINO 线程上运行任何 Nim 代码, 因此不提供用户回调)、动态与部分形状(`PartialShape`、`Port.partialShape`、 diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index c2abca9..ffc889a 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,68 +1,22 @@ -# OpenVINO Nim API 0.2.0 +# OpenVINO Nim API 0.2.1 (in development) -OpenVINO Nim API 0.2.0 adds asynchronous inference, dynamic shapes and -declarative preprocessing to the community-maintained Nim bindings for the -OpenVINO Runtime C API. It is not an official Intel or OpenVINO project. +This patch release is being prepared. It fixes correctness and memory safety +issues in the community-maintained Nim bindings for the OpenVINO Runtime C API. +It has not been published. -Published as [`v0.2.0`](https://github.com/AbyssGG/OpenVINO-Nim-API/releases/tag/v0.2.0) -on 2026-10-18. +## Fixes -## Highlights - -- **Asynchronous inference.** `startAsync`, `wait`, `waitFor` and `cancel` on - `InferRequest`, with `OpenVinoBusyError` and `OpenVinoCancelledError`. - `CompletionQueue` tells one Nim thread which of many requests has finished. - No Nim code runs on OpenVINO's threads: the package installs one internal C - callback that only records the completion, so user callbacks are - deliberately not offered. Closing a running request is safe. -- **Dynamic shapes.** `PartialShape` and `Dimension` describe dynamic, bounded - and lower-bounded dimensions; `Port.partialShape` reads them; `Model.reshape` - changes them by input, index, name or several names atomically. One compiled - dynamic model infers different batch sizes. -- **Layout and preprocessing.** A `PreprocessPlan` describes element type, - layout, color format, spatial size, resize, mean, scale, crop, pad and - channel reversal as a plain Nim value, and `applyPreprocessing` builds the - prepared model in one call that never lets an OpenVINO child object escape. -- **Measured, not assumed.** Ten upstream behaviours were measured before the - design was fixed, and two corrected the plan. All are pinned by smoke tests - and recorded in ADRs 0003 to 0005. +- Reject tensor byte copies of Nim types containing managed references. +- Validate every shape dimension even when a zero dimension is present. +- Reject embedded NUL characters in C-bound names, properties, devices and + file paths rather than silently truncating them. +- Avoid overflowing a completion queue's deadline for very long timeouts. ## Compatibility -Targets OpenVINO `2026.4.1` (and supports `2026.4.0`) and requires Nim `2.0.0` -or newer; `0.1.x` code compiles and behaves as before. Verified on Windows 11 -x86_64 (CPU, Intel iGPU, NVIDIA dGPU, Intel NPU) and Ubuntu 26.04 x86_64 (CPU, -valgrind). The Intel NPU plugin needs bounded or static shapes and does not -support `cancel`. See [the compatibility matrix](docs/compatibility.md). - -## Installation - -Install OpenVINO Runtime `2026.4.x` separately and configure its library search -path, then install the Nim package: - -```shell -nimble refresh -nimble install openvino -``` - -## Release assets - -The published `v0.2.0` assets are the four canonical, checksummed files the -release workflow builds from the tagged commit: - -- `openvino-nim-0-2-0-2026-10-18-ov2026-4-1.zip` -- `openvino-nim-0-2-0-2026-10-18-ov2026-4-1.tar.gz` -- `openvino-nim-0-2-0-2026-10-18-ov2026-4-1.zip.sha256` -- `openvino-nim-0-2-0-2026-10-18-ov2026-4-1.tar.gz.sha256` - -Each archive has one top-level directory named after the base name and contains -no OpenVINO runtime, model weights or credentials. Verify a download with the -sidecar from the same Release: - -```shell -sha256sum -c openvino-nim-0-2-0-2026-10-18-ov2026-4-1.zip.sha256 -sha256sum -c openvino-nim-0-2-0-2026-10-18-ov2026-4-1.tar.gz.sha256 -``` +The package targets OpenVINO `2026.4.1` and requires Nim `2.0.0` or newer. +The changes reject inputs that could previously cause incorrect behaviour. +The release's full platform and hardware validation has not yet been completed. +See [the compatibility matrix](docs/compatibility.md) for previous releases. -For the complete change list, see [CHANGELOG.md](CHANGELOG.md). For problems, -see [the troubleshooting guide](docs/troubleshooting.md). +For the complete change list, see [CHANGELOG.md](CHANGELOG.md). diff --git a/docs/api-overview.md b/docs/api-overview.md index 79cb8ad..ea44827 100644 --- a/docs/api-overview.md +++ b/docs/api-overview.md @@ -143,8 +143,9 @@ completed. Tensors created by OpenVINO, including by `tensorFrom`, may be closed once bound: the request keeps its own reference. Tensor getters return new handles that you must close, even when their storage belongs to the request. `tensorFrom` copies input data; `toSeq` returns an independent Nim -sequence. Typed access checks element width and size, but does not convert -data or distinguish integer and floating-point types of the same width. +sequence. Typed access rejects types containing Nim-managed references and +checks element width and size, but does not convert data or distinguish +integer and floating-point types of the same width. Match the Nim data type to the model's declared element type. For repeated inference, keep the compiled model and request, update input diff --git a/openvino.nimble b/openvino.nimble index d958aa2..8eabe8a 100644 --- a/openvino.nimble +++ b/openvino.nimble @@ -66,7 +66,7 @@ proc stringMetadata(name: string): string = # # `nimble releaseCheck` closes the loop by asserting each literal against # src/openvino/version.nim, so the single source of truth still wins. -version = "0.2.0" +version = "0.2.1" author = "WANG" description = "Community-maintained Nim bindings for the OpenVINO Runtime " & "C API. Not an official Intel or OpenVINO project." diff --git a/src/openvino/compiled_model.nim b/src/openvino/compiled_model.nim index 9152c00..d1aeff8 100644 --- a/src/openvino/compiled_model.nim +++ b/src/openvino/compiled_model.nim @@ -136,6 +136,7 @@ proc getProperty*(model: CompiledModel; key: string): string = ## ## Raises `OpenVinoError` when the property is unknown to the device, and ## `OpenVinoArgumentError` if the compiled model is closed. + checkCString(key, "property key") var native: cstring = nil checkStatus(ov_compiled_model_get_property(model.handle.native(), key.cstring, addr native), @@ -151,6 +152,7 @@ proc exportTo*(model: CompiledModel; path: string) = ## ## Raises `OpenVinoError` when the path cannot be written or the device does ## not support export. + checkCString(path, "export path") checkStatus(ov_compiled_model_export_model(model.handle.native(), path.cstring), "export compiled model", path) diff --git a/src/openvino/core.nim b/src/openvino/core.nim index 91fcde3..069ad5a 100644 --- a/src/openvino/core.nim +++ b/src/openvino/core.nim @@ -181,6 +181,8 @@ proc getProperty*(core: Core; device, key: string): string = ## Copies the value and releases OpenVINO's allocation before returning. ## ## Raises `OpenVinoError` for an unknown device or an unsupported property. + checkCString(device, "device name") + checkCString(key, "property key") var native: cstring = nil checkStatus(ov_core_get_property(core.handle.native(), device.cstring, key.cstring, addr native), @@ -193,6 +195,7 @@ proc setProperties*(core: Core; device: string; ## ## Raises `OpenVinoError` when the device rejects a property, and ## `OpenVinoArgumentError` if the core is closed. + checkCString(device, "device name") let native = core.handle.native() withRawProperties(values, rawProperties, count): checkStatus(ov_core_set_properties(native, device.cstring, count, @@ -210,6 +213,8 @@ proc readModel*(core: Core; modelPath: string; weightsPath = ""): Model = ## ## Raises `OpenVinoArgumentError` when the path does not exist, ## `OpenVinoError` when OpenVINO cannot parse the model. + checkCString(modelPath, "model path") + checkCString(weightsPath, "weights path") if not fileExists(modelPath): raiseArgumentError("model file does not exist: " & modelPath) if weightsPath.len > 0 and not fileExists(weightsPath): @@ -259,6 +264,7 @@ proc compileModel*(core: Core; source: Model; device: string; ## ## Raises `OpenVinoError` for an unknown device or a model the device cannot ## take, and `OpenVinoArgumentError` if either object is closed. + checkCString(device, "device name") let coreNative = core.handle.native() modelNative = source.unsafeRawHandle() @@ -278,6 +284,8 @@ proc compileModel*(core: Core; modelPath: string; device: string; ## `compileModel`, but without producing a `Model` you would then close. ## ## Raises `OpenVinoArgumentError` when the path does not exist. + checkCString(modelPath, "model path") + checkCString(device, "device name") if not fileExists(modelPath): raiseArgumentError("model file does not exist: " & modelPath) let coreNative = core.handle.native() @@ -313,6 +321,7 @@ proc importModel*(core: Core; blob: string; device: string): CompiledModel = ## ## Raises `OpenVinoArgumentError` for an empty blob, `OpenVinoError` when the ## blob is not usable. + checkCString(device, "device name") if blob.len == 0: raiseArgumentError("cannot import a compiled model from an empty blob") var native: ptr ov_compiled_model_t = nil diff --git a/src/openvino/infer_request.nim b/src/openvino/infer_request.nim index 70f25aa..7426140 100644 --- a/src/openvino/infer_request.nim +++ b/src/openvino/infer_request.nim @@ -172,6 +172,7 @@ proc setInputTensor*(request: InferRequest; name: string; value: Tensor) = ## ## Raises `OpenVinoError` when no port has that name, and ## `OpenVinoArgumentError` if either object is closed. + checkCString(name, "tensor name") checkStatus(ov_infer_request_set_tensor(request.nativeRequest(), name.cstring, value.unsafeRawHandle()), @@ -450,13 +451,14 @@ proc next*(queue: CompletionQueue; timeout: Duration): Option[int] = ## closed queue. let milliseconds = timeoutMilliseconds(timeout) let shared = queue.handle.native() - let deadline = getMonoTime() + initDuration(milliseconds = milliseconds) + let started = getMonoTime() acquireQueue(shared) try: while pendingCount(shared) == 0: - let remaining = (deadline - getMonoTime()).inMilliseconds - if remaining <= 0: + let elapsed = (getMonoTime() - started).inMilliseconds + if elapsed >= milliseconds: return none(int) + let remaining = milliseconds - elapsed # Waits in chunks of at most one hour, which the platform waits accept. waitForCompletion(shared, min(remaining, 3_600_000'i64)) result = some(popCompletion(shared)) diff --git a/src/openvino/model.nim b/src/openvino/model.nim index a3c8b10..4297419 100644 --- a/src/openvino/model.nim +++ b/src/openvino/model.nim @@ -102,6 +102,7 @@ proc input*(model: Model; name: string): Port = ## Returns the input port named `name`. ## ## Raises `OpenVinoError` when no input has that name. + checkCString(name, "input name") var native: ptr ov_output_const_port_t = nil checkStatus(ov_model_const_input_by_name(model.handle.native(), name.cstring, addr native), @@ -112,6 +113,7 @@ proc output*(model: Model; name: string): Port = ## Returns the output port named `name`. ## ## Raises `OpenVinoError` when no output has that name. + checkCString(name, "output name") var native: ptr ov_output_const_port_t = nil checkStatus(ov_model_const_output_by_name(model.handle.native(), name.cstring, addr native), diff --git a/src/openvino/private/conversions.nim b/src/openvino/private/conversions.nim index 45e9a62..f5cea5b 100644 --- a/src/openvino/private/conversions.nim +++ b/src/openvino/private/conversions.nim @@ -17,6 +17,11 @@ import ../raw/core import ../raw/error import ../raw/shape +proc checkCString*(value, what: string) = + ## Reject embedded terminators before passing a Nim string to C. + if '\0' in value: + raiseArgumentError(what & " contains a NUL character") + proc takeString*(native: var cstring): string = ## Copies an OpenVINO-allocated string and releases the original. ## @@ -98,13 +103,17 @@ proc checkedElementCount*(dimensions: openArray[int64]): int64 = ## Raises `OpenVinoArgumentError` on a negative dimension or on overflow. ## Overflow is checked before it happens rather than detected afterwards, ## because a wrapped count would be used to size a buffer. - result = 1 + var hasZero = false for index, dimension in dimensions: if dimension < 0: raiseArgumentError("dimension " & $index & " is negative: " & $dimension) if dimension == 0: - return 0 + hasZero = true + if hasZero: + return 0 + result = 1 + for dimension in dimensions: if result > high(int64) div dimension: raiseArgumentError("shape " & $(@dimensions) & " overflows a signed 64-bit element count") diff --git a/src/openvino/properties.nim b/src/openvino/properties.nim index 8f925d6..c306005 100644 --- a/src/openvino/properties.nim +++ b/src/openvino/properties.nim @@ -13,6 +13,7 @@ ## the kind of thing that silently stops matching after an upstream rename. import errors +import private/conversions import raw/property type Property* = object @@ -31,6 +32,8 @@ proc initProperty*(key, value: string): Property = ## otherwise reject with a less specific message. if key.len == 0: raiseArgumentError("a property key cannot be empty") + checkCString(key, "property key") + checkCString(value, "property value") result = Property(key: key, value: value) proc key*(property: Property): string = diff --git a/src/openvino/tensor.nim b/src/openvino/tensor.nim index 1c4ff94..044339e 100644 --- a/src/openvino/tensor.nim +++ b/src/openvino/tensor.nim @@ -12,6 +12,8 @@ ## guarantee. There is no unnamed middle ground, because a borrowing entry point ## that looked safe would be the most dangerous thing in this package. +import std/typetraits + import errors import private/conversions import private/handles @@ -188,6 +190,12 @@ proc requireTypedAccess(tensor: Tensor; elementSize: int; " elements of " & $elementSize & " bytes; refusing typed access") result = count +proc requireCopyableElement[T]() = + ## Never overwrite or duplicate Nim-managed references with raw bytes. + when not supportsCopyMem(T): + raiseArgumentError("tensor access requires a type safe for byte copying: " & + $T) + proc copyFrom*[T](tensor: Tensor; source: openArray[T]) = ## Copies `source` into the tensor's storage. ## @@ -195,9 +203,11 @@ proc copyFrom*[T](tensor: Tensor; source: openArray[T]) = ## nothing of the caller's is borrowed afterwards. ## ## Raises `OpenVinoArgumentError` when the tensor is closed, when `T` does not - ## match the element type's width, or when the lengths differ. Requiring an + ## match the element type's width, when `T` contains managed references, or + ## when the lengths differ. Requiring an ## exact length rather than accepting a shorter one is deliberate: a partial ## fill would leave the rest holding whatever was there before. + requireCopyableElement[T]() let count = requireTypedAccess(tensor, sizeof(T), $T) if source.len != count: raiseArgumentError("tensor holds " & $count & " elements but " & @@ -211,7 +221,9 @@ proc copyTo*[T](tensor: Tensor; destination: var openArray[T]) = ## Copies the tensor's contents into `destination`. ## ## Raises `OpenVinoArgumentError` when the tensor is closed, when `T` does not - ## match the element type's width, or when the lengths differ. + ## match the element type's width, when `T` contains managed references, or + ## when the lengths differ. + requireCopyableElement[T]() let count = requireTypedAccess(tensor, sizeof(T), $T) if destination.len != count: raiseArgumentError("tensor holds " & $count & " elements but " & @@ -228,7 +240,8 @@ proc toSeq*[T](tensor: Tensor; _: typedesc[T]): seq[T] = ## lifetime obligation. ## ## Raises `OpenVinoArgumentError` when the tensor is closed or `T` does not - ## match the element type. + ## match the element type's width or contains managed references. + requireCopyableElement[T]() let count = requireTypedAccess(tensor, sizeof(T), $T) result = newSeq[T](count) if count > 0: @@ -243,7 +256,9 @@ proc tensorFrom*[T](elementType: ElementType; tensorShape: Shape; ## collected freely. ## ## Raises `OpenVinoArgumentError` when `T` does not match `elementType`, or - ## when `source.len` does not equal the shape's element count. + ## when `T` contains managed references, or when `source.len` does not equal + ## the shape's element count. + requireCopyableElement[T]() result = newTensor(elementType, tensorShape) try: result.copyFrom(source) diff --git a/src/openvino/version.nim b/src/openvino/version.nim index 1dacd52..84adfd6 100644 --- a/src/openvino/version.nim +++ b/src/openvino/version.nim @@ -39,7 +39,7 @@ const ## `RepositoryName`; the Nimble package identifier and Nim import root are ## both `openvino`, because Nimble identifiers may not contain a hyphen. - PackageVersion* = "0.2.0" + PackageVersion* = "0.2.1" ## SemVer version of this Nim package. Versioned independently of the ## OpenVINO runtime: see `TargetOpenVinoVersion`. diff --git a/tests/integration/tasync_inference.nim b/tests/integration/tasync_inference.nim index 49218aa..eb15dc7 100644 --- a/tests/integration/tasync_inference.nim +++ b/tests/integration/tasync_inference.nim @@ -201,6 +201,20 @@ suite "busy and cancelled": request.wait() suite "completion queue": + test "a very long timeout can collect a completion without overflow": + let core = newCore() + defer: core.close() + let compiled = core.compileSmall() + defer: compiled.close() + let request = compiled.createInferRequest() + defer: request.close() + let queue = newCompletionQueue(1) + defer: queue.close() + queue.attach(request, 42) + request.startAsync() + check queue.next(initDuration(seconds = 10_000_000_000'i64)).get == 42 + request.wait() + test "next returns the id of the request that finished": let core = newCore() defer: core.close() diff --git a/tests/unit/tcstring_safety.nim b/tests/unit/tcstring_safety.nim new file mode 100644 index 0000000..5878172 --- /dev/null +++ b/tests/unit/tcstring_safety.nim @@ -0,0 +1,34 @@ +# SPDX-License-Identifier: Apache-2.0 + +## C strings must not silently truncate caller-supplied names or paths. + +import std/[strutils, unittest] + +import openvino + +suite "C string safety": + test "public entry points reject embedded NUL before native calls": + var model: Model + var compiled: CompiledModel + var request: InferRequest + var core: Core + var tensor: Tensor + for operation in 0 .. 12: + try: + case operation + of 0: discard model.input("input\0bad") + of 1: discard model.output("output\0bad") + of 2: request.setInputTensor("input\0bad", tensor) + of 3: discard core.getProperty("CPU\0bad", "key") + of 4: discard core.getProperty("CPU", "key\0bad") + of 5: core.setProperties("CPU\0bad", []) + of 6: discard core.readModel("model\0bad") + of 7: discard core.compileModel(model, "CPU\0bad") + of 8: discard core.compileModel("model", "CPU\0bad") + of 9: discard compiled.getProperty("key\0bad") + of 10: compiled.exportTo("path\0bad") + of 11: discard core.readModel("model", "weights\0bad") + else: discard core.importModel("blob", "CPU\0bad") + check false + except OpenVinoArgumentError as err: + check "NUL" in err.msg diff --git a/tests/unit/tshape.nim b/tests/unit/tshape.nim index f2f74cc..33b85b2 100644 --- a/tests/unit/tshape.nim +++ b/tests/unit/tshape.nim @@ -49,6 +49,13 @@ suite "building a shape": except OpenVinoArgumentError as err: check "dimension 1" in err.msg + test "zero does not hide a negative dimension or create overflow": + expect OpenVinoArgumentError: + discard initShape(0, -1) + expect OpenVinoArgumentError: + discard initShape(0, 1, -1) + check initShape([high(int64), 2'i64, 0'i64]).elementCount == 0 + test "an element count that would overflow is refused": expect OpenVinoArgumentError: discard initShape([high(int64), int64(8)]) @@ -91,6 +98,12 @@ suite "element types": discard initShape(1, 4).byteSize(etDynamic) suite "properties": + test "embedded NUL in a key or value is rejected": + expect OpenVinoArgumentError: + discard initProperty("key\0suffix", "value") + expect OpenVinoArgumentError: + discard initProperty("key", "value\0suffix") + test "a key and value are kept verbatim": let property = initProperty("CACHE_DIR", "/tmp/cache") check property.key == "CACHE_DIR" diff --git a/tests/unit/ttensor_copy_safety.nim b/tests/unit/ttensor_copy_safety.nim new file mode 100644 index 0000000..706c881 --- /dev/null +++ b/tests/unit/ttensor_copy_safety.nim @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: Apache-2.0 + +## Managed values must never be overwritten or duplicated by tensor byte copies. +## The check runs before accessing a tensor or loading the OpenVINO runtime. + +import std/[strutils, unittest] + +import openvino + +type ContainsString = object + value: string + +suite "tensor copy safety": + test "managed values are rejected before accessing the runtime": + var tensor: Tensor + var strings = @["owned"] + var records = @[ContainsString(value: "owned")] + + for operation in 0 .. 3: + try: + case operation + of 0: tensor.copyFrom(strings) + of 1: tensor.copyTo(strings) + of 2: discard tensor.toSeq(ContainsString) + else: discard tensorFrom(etU64, initShape(1), records) + check false + except OpenVinoArgumentError as err: + check "safe for byte copying" in err.msg + + check strings == @["owned"] + check records[0].value == "owned" + + test "ordinary numeric values still reach the tensor state check": + var tensor: Tensor + try: + tensor.copyFrom([float32(1)]) + check false + except OpenVinoArgumentError as err: + check "closed" in err.msg From 475e2b35f70458c9419f73ac6eded1929797dee0 Mon Sep 17 00:00:00 2001 From: Pension_qe <97943633+AbyssGG@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:09:55 +0800 Subject: [PATCH 2/2] docs: finalize 0.2.1 release notes --- CHANGELOG.md | 2 +- README.md | 4 ++-- README_zh-CN.md | 2 +- RELEASE_NOTES.md | 9 ++++----- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee5d88..2eaa0f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and the OpenVINO runtime baseline evolve independently: ## 0.2.1 -Release in preparation. +Patch release with correctness and memory safety fixes. ### Nim API diff --git a/README.md b/README.md index 0ab027a..9a1e22d 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Corporation.** OpenVINO is a trademark of Intel Corporation. ## Status -Version `0.2.1` is in development and contains correctness and memory safety -fixes. The latest published release remains `0.2.0`. +Version `0.2.1` contains correctness and memory safety fixes; see the +[release notes](RELEASE_NOTES.md). Version `0.2.0` was released on 2026-10-18 and adds asynchronous inference, dynamic and partial shapes with reshape, and declarative layout and diff --git a/README_zh-CN.md b/README_zh-CN.md index 5b9d1e3..c2ed0c1 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -10,7 +10,7 @@ Corporation 的认可或附属关系。** OpenVINO 是 Intel Corporation 的商 ## 项目状态 -版本 `0.2.1` 正在开发,包含正确性和内存安全修复;最新已发布版本仍为 `0.2.0`。 +版本 `0.2.1` 包含正确性和内存安全修复,详见[发布说明](RELEASE_NOTES.md)。 版本 `0.2.0` 已于 2026-10-18 发布([GitHub Release](https://github.com/AbyssGG/OpenVINO-Nim-API/releases/tag/v0.2.0)),新增异步推理(`startAsync`、`wait`、 `waitFor`、`cancel` 与 `CompletionQueue`,不在 OpenVINO 线程上运行任何 Nim 代码, diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index ffc889a..09c3f1b 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,8 +1,7 @@ -# OpenVINO Nim API 0.2.1 (in development) +# OpenVINO Nim API 0.2.1 -This patch release is being prepared. It fixes correctness and memory safety -issues in the community-maintained Nim bindings for the OpenVINO Runtime C API. -It has not been published. +This patch release fixes correctness and memory safety issues in the +community-maintained Nim bindings for the OpenVINO Runtime C API. ## Fixes @@ -16,7 +15,7 @@ It has not been published. The package targets OpenVINO `2026.4.1` and requires Nim `2.0.0` or newer. The changes reject inputs that could previously cause incorrect behaviour. -The release's full platform and hardware validation has not yet been completed. +The 0.2.1 changes have not been tested on GPU or NPU hardware. See [the compatibility matrix](docs/compatibility.md) for previous releases. For the complete change list, see [CHANGELOG.md](CHANGELOG.md).