mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
* fix(ffi): eliminate aliasing UB via to_shared_ref migration Add to_shared_ref() helper that creates &T (shared reference) from raw pointers instead of &mut T. This eliminates undefined behavior caused by violating Rust's aliasing invariant when C# SafeHandle permits concurrent FFI calls on the same handle. With &mut T, the compiler may assume exclusive (noalias) access and reorder or elide reads/writes — a miscompilation risk when another thread holds a reference to the same object. Switching to &T removes that assumption; actual mutation is mediated by the interior RwLock inside Handle<T>, which is the sole synchronization mechanism. Migrated sites: - rvm.rs: 20 non-drop call sites - engine.rs: 30 non-drop call sites + with_unwind_guard for timer fns - compiled_policy.rs: 2 call sites - Fix null-data UB in regorus_program_deserialize_binary Drop paths retain to_ref() where exclusive access is guaranteed by the caller contract (preventing use-after-free). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(ffi): add Azure Policy JSON compilation FFI and C# bindings - AliasRegistry builder pattern: RegorusAliasRegistryBuilder (mutable, single-threaded) + RegorusAliasRegistry (immutable, Arc-wrapped) - Azure Policy JSON compilation: regorus_compile_azure_policy_rule and regorus_compile_azure_policy_definition with alias registry support - regorus_rvm_set_context for host-supplied ambient data - C# AliasRegistryBuilder and AliasRegistry classes with convenience factories (FromJson, FromManifest, Empty) - C# AzurePolicyCompiler static class for policy rule/definition compilation - Compile functions take *const RegorusAliasRegistry (read-only via to_shared_ref for concurrent compilation safety) - Fix pre-existing clippy warnings across multiple crates Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
using Regorus.Internal;
|
|
|
|
#nullable enable
|
|
namespace Regorus
|
|
{
|
|
/// <summary>
|
|
/// Mutable, single-threaded builder for <see cref="AliasRegistry"/>.
|
|
/// Load alias data, then call <see cref="Build"/> to freeze the registry.
|
|
/// </summary>
|
|
public unsafe sealed class AliasRegistryBuilder : SafeHandleWrapper
|
|
{
|
|
/// <summary>
|
|
/// Create an empty alias registry builder.
|
|
/// </summary>
|
|
public AliasRegistryBuilder()
|
|
: base(RegorusAliasRegistryBuilderHandle.Create(), nameof(AliasRegistryBuilder))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load control-plane alias data (array of ProviderAliases) from a JSON string.
|
|
/// </summary>
|
|
public void LoadJson(string json)
|
|
{
|
|
Utf8Marshaller.WithUtf8(json, jsonPtr =>
|
|
{
|
|
UseHandle(builderPtr =>
|
|
{
|
|
ResultHelpers.GetStringResult(API.regorus_alias_registry_builder_load_json(
|
|
(RegorusAliasRegistryBuilder*)builderPtr,
|
|
(byte*)jsonPtr));
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load a data-plane policy manifest from a JSON string.
|
|
/// </summary>
|
|
public void LoadManifest(string json)
|
|
{
|
|
Utf8Marshaller.WithUtf8(json, jsonPtr =>
|
|
{
|
|
UseHandle(builderPtr =>
|
|
{
|
|
ResultHelpers.GetStringResult(API.regorus_alias_registry_builder_load_manifest(
|
|
(RegorusAliasRegistryBuilder*)builderPtr,
|
|
(byte*)jsonPtr));
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Freeze the builder into an immutable, thread-safe alias registry.
|
|
/// </summary>
|
|
public AliasRegistry Build()
|
|
{
|
|
return UseHandle(builderPtr =>
|
|
{
|
|
var registryPtr = ResultHelpers.GetPointerResult(
|
|
API.regorus_alias_registry_builder_build((RegorusAliasRegistryBuilder*)builderPtr));
|
|
return new AliasRegistry(RegorusAliasRegistryHandle.FromPointer(registryPtr));
|
|
});
|
|
}
|
|
}
|
|
}
|