-
Notifications
You must be signed in to change notification settings - Fork 74
Add per-execution memory budgets to RVM #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Maksym (maksym-mishchenko)
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
maksym-mishchenko:feature/rvm-memory-budget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2f2238c
feat(rvm): add per-execution memory budgets
fbf805c
fix(rvm): address memory budget review feedback
e61aa28
fix(ffi): format conditional import
3ac1c1a
fix(rvm): include evaluation data in memory budgets
e5da647
fix(rvm): restore execute-only memory budgets
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Regorus.Tests; | ||
|
|
||
| [TestClass] | ||
| public sealed class RvmMemoryBudgetTests | ||
| { | ||
| private const ulong TightMemoryBudgetBytes = 64 * 1024; | ||
|
|
||
| private const string Policy = """ | ||
| package limits.memory | ||
| import rego.v1 | ||
|
|
||
| large_array := json.unmarshal(data.large_json) | ||
| """; | ||
|
|
||
| private const string EntryPoint = "data.limits.memory.large_array"; | ||
|
|
||
| private const string PreloadedResultPolicy = """ | ||
| package limits.memory | ||
|
|
||
| large_string := data.large_string | ||
| """; | ||
|
|
||
| private const string PreloadedResultEntryPoint = "data.limits.memory.large_string"; | ||
|
|
||
| [TestMethod] | ||
| public void Memory_budget_must_be_non_zero() | ||
| { | ||
| Assert.ThrowsException<ArgumentOutOfRangeException>(() => new MemoryBudgetConfig(0)); | ||
|
|
||
| using var vm = new Rvm(); | ||
| Assert.ThrowsException<ArgumentOutOfRangeException>(() => vm.SetMemoryBudgetConfig(default)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Execute_exceeding_memory_budget_throws_typed_exception() | ||
| { | ||
| using var program = CreateProgram(); | ||
| using var vm = CreateRvm(program); | ||
| vm.SetMemoryBudgetConfig(new MemoryBudgetConfig(TightMemoryBudgetBytes)); | ||
|
|
||
| Assert.ThrowsException<RegorusMemoryBudgetExceededException>(() => vm.ExecuteEntryPoint(EntryPoint)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Clearing_memory_budget_restores_unlimited_execution() | ||
| { | ||
| using var program = CreateProgram(); | ||
| using var vm = CreateRvm(program); | ||
| vm.SetMemoryBudgetConfig(new MemoryBudgetConfig(TightMemoryBudgetBytes)); | ||
| Assert.ThrowsException<RegorusMemoryBudgetExceededException>(() => vm.ExecuteEntryPoint(EntryPoint)); | ||
|
|
||
| vm.ClearMemoryBudgetConfig(); | ||
|
|
||
| var result = vm.ExecuteEntryPoint(EntryPoint); | ||
| Assert.IsFalse(string.IsNullOrWhiteSpace(result)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Serialization_budget_failure_leaves_error_state() | ||
| { | ||
| var data = JsonSerializer.Serialize(new | ||
| { | ||
| large_string = new string('x', 2 * 1024 * 1024), | ||
| }); | ||
| var modules = new[] { new PolicyModule("memory_budget.rego", PreloadedResultPolicy) }; | ||
| using var program = Program.CompileFromModules(data, modules, new[] { PreloadedResultEntryPoint }); | ||
| using var vm = new Rvm(); | ||
| vm.LoadProgram(program); | ||
| vm.SetDataJson(data); | ||
| vm.SetMemoryBudgetConfig(new MemoryBudgetConfig(512 * 1024)); | ||
|
|
||
| Assert.ThrowsException<RegorusMemoryBudgetExceededException>( | ||
| () => vm.ExecuteEntryPoint(PreloadedResultEntryPoint)); | ||
|
|
||
| var state = vm.GetExecutionState(); | ||
| Assert.IsNotNull(state); | ||
| StringAssert.Contains(state, "Error { error: MemoryBudgetExceeded"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Suspendable_execution_rejects_memory_budget() | ||
| { | ||
| using var vm = new Rvm(); | ||
| vm.SetExecutionMode(ExecutionMode.Suspendable); | ||
| vm.SetMemoryBudgetConfig(new MemoryBudgetConfig(1024)); | ||
|
|
||
| Assert.ThrowsException<RegorusMemoryBudgetUnsupportedException>(() => vm.Execute()); | ||
| } | ||
|
|
||
| private static Program CreateProgram() | ||
| { | ||
| var modules = new[] { new PolicyModule("memory_budget.rego", Policy) }; | ||
| return Program.CompileFromModules(CreateData(), modules, new[] { EntryPoint }); | ||
| } | ||
|
|
||
| private static Rvm CreateRvm(Program program) | ||
| { | ||
| var vm = new Rvm(); | ||
| vm.LoadProgram(program); | ||
| vm.SetDataJson(CreateData()); | ||
| return vm; | ||
| } | ||
|
|
||
| private static string CreateData() | ||
| { | ||
| var values = Enumerable.Range(0, 200_000).ToArray(); | ||
| return JsonSerializer.Serialize(new | ||
| { | ||
| large_json = JsonSerializer.Serialize(values), | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Regorus | ||
| { | ||
| /// <summary> | ||
| /// Configures the additional live-memory budget for one RVM execution. | ||
| /// </summary> | ||
| public readonly struct MemoryBudgetConfig | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MemoryBudgetConfig"/> struct. | ||
| /// </summary> | ||
| /// <param name="limitBytes">Maximum additional live bytes allowed during one execution.</param> | ||
| /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="limitBytes"/> is zero.</exception> | ||
| public MemoryBudgetConfig(ulong limitBytes) | ||
|
maksym-mishchenko marked this conversation as resolved.
|
||
| { | ||
| if (limitBytes == 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(limitBytes), "Memory budget must be non-zero."); | ||
| } | ||
|
|
||
| LimitBytes = limitBytes; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the maximum additional live bytes allowed during one execution. | ||
| /// </summary> | ||
| public ulong LimitBytes { get; } | ||
|
|
||
| internal Regorus.Internal.RegorusMemoryBudgetConfig ToNative() | ||
| { | ||
| if (LimitBytes == 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(LimitBytes), "Memory budget must be non-zero."); | ||
| } | ||
|
|
||
| return new Regorus.Internal.RegorusMemoryBudgetConfig | ||
| { | ||
| limit_bytes = LimitBytes, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low: These benchmark imports/calls use
#[cfg(feature = "allocator-memory-limits")], but the coreMemoryBudgetConfigexport andRegoVM::set_memory_budget_configare gated byall(feature = "allocator-memory-limits", not(miri)). A bench build selected under Miri can therefore fail to compile.Suggested change:
Please use the same predicate for both imports and the configuration block.