From 25d6b3ce5055211a0878bafcf04005d6d880bc36 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:48:38 +0000 Subject: [PATCH 1/6] build(deps): bump Microsoft.SemanticKernel Bumps [Microsoft.SemanticKernel](https://github.com/microsoft/semantic-kernel) from 1.0.0-beta1 to 1.0.0-beta4. - [Release notes](https://github.com/microsoft/semantic-kernel/releases) - [Commits](https://github.com/microsoft/semantic-kernel/compare/dotnet-1.0.0-beta1...dotnet-1.0.0-beta4) --- updated-dependencies: - dependency-name: Microsoft.SemanticKernel dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- LLama.Examples/LLama.Examples.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LLama.Examples/LLama.Examples.csproj b/LLama.Examples/LLama.Examples.csproj index 7fea4562..b53ad296 100644 --- a/LLama.Examples/LLama.Examples.csproj +++ b/LLama.Examples/LLama.Examples.csproj @@ -1,4 +1,4 @@ - + Exe @@ -29,7 +29,7 @@ - + From b20c3ecda5d5c71be477ea7d09ed9a72f5c1cc61 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:48:48 +0000 Subject: [PATCH 2/6] build(deps): bump xunit from 2.5.0 to 2.6.1 Bumps [xunit](https://github.com/xunit/xunit) from 2.5.0 to 2.6.1. - [Commits](https://github.com/xunit/xunit/compare/2.5.0...2.6.1) --- updated-dependencies: - dependency-name: xunit dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- LLama.Unittest/LLama.Unittest.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LLama.Unittest/LLama.Unittest.csproj b/LLama.Unittest/LLama.Unittest.csproj index 596e5da9..2c4f0d3d 100644 --- a/LLama.Unittest/LLama.Unittest.csproj +++ b/LLama.Unittest/LLama.Unittest.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -13,7 +13,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all From c9858e873922974a25187bc8134b8c83861f748c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:48:53 +0000 Subject: [PATCH 3/6] build(deps): bump Swashbuckle.AspNetCore from 6.2.3 to 6.5.0 Bumps [Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore) from 6.2.3 to 6.5.0. - [Release notes](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases) - [Commits](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/compare/v6.2.3...v6.5.0) --- updated-dependencies: - dependency-name: Swashbuckle.AspNetCore dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- LLama.WebAPI/LLama.WebAPI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LLama.WebAPI/LLama.WebAPI.csproj b/LLama.WebAPI/LLama.WebAPI.csproj index 2b14d8a4..bdb2ad8a 100644 --- a/LLama.WebAPI/LLama.WebAPI.csproj +++ b/LLama.WebAPI/LLama.WebAPI.csproj @@ -8,7 +8,7 @@ - + From 08c29d52c59cf0be952113f5ef53abdb917f7d32 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Sat, 4 Nov 2023 16:02:33 +0000 Subject: [PATCH 4/6] Slightly refactored `SafeLLamaGrammarHandle.Create` to solve CodeQL warning about pointer arithmetic. --- LLama/Native/SafeLLamaGrammarHandle.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/LLama/Native/SafeLLamaGrammarHandle.cs b/LLama/Native/SafeLLamaGrammarHandle.cs index f430b7c3..2218995b 100644 --- a/LLama/Native/SafeLLamaGrammarHandle.cs +++ b/LLama/Native/SafeLLamaGrammarHandle.cs @@ -49,29 +49,31 @@ namespace LLama.Native // Borrow an array large enough to hold every single element // and another array large enough to hold a pointer to each rule var allElements = ArrayPool.Shared.Rent(totalElements); - var pointers = ArrayPool.Shared.Rent(rules.Count); + var rulePointers = ArrayPool.Shared.Rent(rules.Count); try { fixed (LLamaGrammarElement* allElementsPtr = allElements) { var elementIndex = 0; - var pointerIndex = 0; + var ruleIndex = 0; foreach (var rule in rules) { + Debug.Assert(elementIndex < allElements.Length); + // Save a pointer to the start of this rule - pointers[pointerIndex++] = (IntPtr)(allElementsPtr + elementIndex); + rulePointers[ruleIndex++] = (IntPtr)(allElementsPtr + elementIndex); // Copy all of the rule elements into the flat array foreach (var element in rule.Elements) - allElementsPtr[elementIndex++] = element; + allElements[elementIndex++] = element; } // Sanity check some things that should be true if the copy worked as planned - Debug.Assert((ulong)pointerIndex == nrules); + Debug.Assert((ulong)ruleIndex == nrules); Debug.Assert(elementIndex == totalElements); // Make the actual call through to llama.cpp - fixed (void* ptr = pointers) + fixed (void* ptr = rulePointers) { return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index); } @@ -80,7 +82,7 @@ namespace LLama.Native finally { ArrayPool.Shared.Return(allElements); - ArrayPool.Shared.Return(pointers); + ArrayPool.Shared.Return(rulePointers); } } } From a03fdc4818acaca6d49d5ad027d26bb42c006dc3 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Sat, 4 Nov 2023 16:17:32 +0000 Subject: [PATCH 5/6] Using a reference to an array instead of pointer arithmetic. This means it will benefit from bounds checking on the array. --- LLama/Native/SafeLLamaGrammarHandle.cs | 42 +++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/LLama/Native/SafeLLamaGrammarHandle.cs b/LLama/Native/SafeLLamaGrammarHandle.cs index 2218995b..ee27befd 100644 --- a/LLama/Native/SafeLLamaGrammarHandle.cs +++ b/LLama/Native/SafeLLamaGrammarHandle.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Runtime.CompilerServices; using LLama.Exceptions; using LLama.Grammars; @@ -52,31 +53,30 @@ namespace LLama.Native var rulePointers = ArrayPool.Shared.Rent(rules.Count); try { - fixed (LLamaGrammarElement* allElementsPtr = allElements) - { - var elementIndex = 0; - var ruleIndex = 0; - foreach (var rule in rules) - { - Debug.Assert(elementIndex < allElements.Length); + // We're taking pointers into `allElements` below, so this pin is required to fix + // that memory in place while those pointers are in use! + using var pin = allElements.AsMemory().Pin(); - // Save a pointer to the start of this rule - rulePointers[ruleIndex++] = (IntPtr)(allElementsPtr + elementIndex); + var elementIndex = 0; + var ruleIndex = 0; + foreach (var rule in rules) + { + // Save a pointer to the start of this rule + rulePointers[ruleIndex++] = (IntPtr)Unsafe.AsPointer(ref allElements[elementIndex]); - // Copy all of the rule elements into the flat array - foreach (var element in rule.Elements) - allElements[elementIndex++] = element; - } + // Copy all of the rule elements into the flat array + foreach (var element in rule.Elements) + allElements[elementIndex++] = element; + } - // Sanity check some things that should be true if the copy worked as planned - Debug.Assert((ulong)ruleIndex == nrules); - Debug.Assert(elementIndex == totalElements); + // Sanity check some things that should be true if the copy worked as planned + Debug.Assert((ulong)ruleIndex == nrules); + Debug.Assert(elementIndex == totalElements); - // Make the actual call through to llama.cpp - fixed (void* ptr = rulePointers) - { - return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index); - } + // Make the actual call through to llama.cpp + fixed (void* ptr = rulePointers) + { + return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index); } } finally From b7893317f5195e3727af3a0a0c8a611c569bbead Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:54:42 +0000 Subject: [PATCH 6/6] build(deps): bump xunit.runner.visualstudio from 2.5.0 to 2.5.3 Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.5.0 to 2.5.3. - [Release notes](https://github.com/xunit/visualstudio.xunit/releases) - [Commits](https://github.com/xunit/visualstudio.xunit/compare/2.5.0...2.5.3) --- updated-dependencies: - dependency-name: xunit.runner.visualstudio dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- LLama.Unittest/LLama.Unittest.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LLama.Unittest/LLama.Unittest.csproj b/LLama.Unittest/LLama.Unittest.csproj index 2c4f0d3d..a62c3201 100644 --- a/LLama.Unittest/LLama.Unittest.csproj +++ b/LLama.Unittest/LLama.Unittest.csproj @@ -14,7 +14,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all