// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Runtime.InteropServices; #pragma warning disable CS8500 #pragma warning disable CS8981 namespace Regorus.Internal { /// /// Native FFI method declarations for Regorus. /// This file contains all P/Invoke declarations for the Regorus native library. /// internal static unsafe partial class API { private const string LibraryName = "regorus_ffi"; #region Common Methods /// /// Drop a RegorusResult. /// output and error_message strings are not valid after drop. /// [DllImport(LibraryName, EntryPoint = "regorus_result_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_result_drop(RegorusResult result); /// /// Drop a RegorusBuffer. /// data is not valid after drop. /// [DllImport(LibraryName, EntryPoint = "regorus_buffer_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_buffer_drop(RegorusBuffer* buffer); #endregion #region Memory Limit Methods /// /// Set the global memory limit. Pass hasLimit=false to clear the limit. /// [DllImport(LibraryName, EntryPoint = "regorus_set_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_set_global_memory_limit(ulong limit, [MarshalAs(UnmanagedType.U1)] bool hasLimit); /// /// Get the current global memory limit. bool_value indicates whether a limit is set. /// [DllImport(LibraryName, EntryPoint = "regorus_get_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_get_global_memory_limit(); /// /// Check the global memory limit immediately. /// [DllImport(LibraryName, EntryPoint = "regorus_check_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_check_global_memory_limit(); /// /// Flush the current thread's pending allocation counters into global aggregates. /// [DllImport(LibraryName, EntryPoint = "regorus_flush_thread_memory_counters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_flush_thread_memory_counters(); /// /// Set the per-thread flush threshold override. Pass hasThreshold=false to restore defaults. /// [DllImport(LibraryName, EntryPoint = "regorus_set_thread_flush_threshold_override", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_set_thread_flush_threshold_override(ulong threshold, [MarshalAs(UnmanagedType.U1)] bool hasThreshold); /// /// Get the per-thread flush threshold. bool_value indicates whether a threshold is configured. /// [DllImport(LibraryName, EntryPoint = "regorus_get_thread_memory_flush_threshold", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_get_thread_memory_flush_threshold(); #endregion #region Engine Methods /// /// Construct a new Engine. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html /// [DllImport(LibraryName, EntryPoint = "regorus_engine_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusEngine* regorus_engine_new(); /// /// Clone a RegorusEngine. /// To avoid having to parse same policy again, the engine can be cloned /// after policies and data have been added. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusEngine* regorus_engine_clone(RegorusEngine* engine); /// /// Compile an RVM program from the engine state with entry points. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_program_with_entrypoints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_compile_program_with_entrypoints(RegorusEngine* engine, byte** entryPoints, UIntPtr entryPointsLen); /// /// Drop a RegorusEngine. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_engine_drop(RegorusEngine* engine); /// /// /// Compile an RVM program from data/modules and entry points. /// [DllImport(LibraryName, EntryPoint = "regorus_program_compile_from_modules", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_program_compile_from_modules(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte** entry_points, UIntPtr entry_points_len); /// /// Construct a new empty program. /// [DllImport(LibraryName, EntryPoint = "regorus_program_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusProgram* regorus_program_new(); /// /// Drop a program handle. /// [DllImport(LibraryName, EntryPoint = "regorus_program_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_program_drop(RegorusProgram* program); /// /// Serialize a program to binary format. /// [DllImport(LibraryName, EntryPoint = "regorus_program_serialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_program_serialize_binary(RegorusProgram* program); /// /// Deserialize a program from binary format. /// [DllImport(LibraryName, EntryPoint = "regorus_program_deserialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_program_deserialize_binary(byte* data, UIntPtr len, byte* is_partial); /// /// Generate a readable assembly listing for the program. /// [DllImport(LibraryName, EntryPoint = "regorus_program_generate_listing", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_program_generate_listing(RegorusProgram* program); /// /// Create a new RVM instance. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusRvm* regorus_rvm_new(); /// /// Create a new RVM instance from a compiled policy. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_new_with_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_new_with_policy(RegorusCompiledPolicy* compiled_policy); /// /// Drop an RVM instance. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_rvm_drop(RegorusRvm* vm); /// /// Load a program into the RVM. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_load_program", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_load_program(RegorusRvm* vm, RegorusProgram* program); /// /// Set the data document for the RVM. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_data(RegorusRvm* vm, byte* data_json); /// /// Set the input document for the RVM. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_input(RegorusRvm* vm, byte* input_json); /// /// Execute the program. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_execute(RegorusRvm* vm); /// /// Execute an entry point by name. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_name", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_execute_entry_point_by_name(RegorusRvm* vm, byte* entry_point); /// /// Execute an entry point by index. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_index", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_execute_entry_point_by_index(RegorusRvm* vm, UIntPtr index); /// /// Resume execution. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_resume", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_resume(RegorusRvm* vm, byte* resume_value_json, [MarshalAs(UnmanagedType.I1)] bool has_value); /// /// Get the current execution state. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_get_execution_state", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_get_execution_state(RegorusRvm* vm); /// /// Set the maximum instruction limit. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_max_instructions", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_max_instructions(RegorusRvm* vm, UIntPtr max_instructions); /// /// Set strict builtin error handling. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_strict_builtin_errors(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool strict); /// /// Set execution mode (0 run-to-completion, 1 suspendable). /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_execution_mode(RegorusRvm* vm, byte mode); /// /// Set step mode for suspendable execution. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_step_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_step_mode(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool enabled); /// /// Set execution timer configuration. /// [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rvm_set_execution_timer_config(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool has_config, RegorusExecutionTimerConfig config); /// Add a policy. /// The policy is parsed into AST. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_policy /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_policy(RegorusEngine* engine, byte* path, byte* rego); /// /// Add a policy from file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy_from_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_policy_from_file(RegorusEngine* engine, byte* path); /// /// Add policy data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_data_json(RegorusEngine* engine, byte* data); /// /// Get list of loaded Rego packages as JSON. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_packages", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_packages(RegorusEngine* engine); /// /// Get list of policies as JSON. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_policies /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policies", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policies(RegorusEngine* engine); /// /// Add data from JSON file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_add_data_from_json_file(RegorusEngine* engine, byte* path); /// /// Clear policy data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_data(RegorusEngine* engine); /// /// Set input. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_input /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_input_json(RegorusEngine* engine, byte* input); /// /// Set input from JSON file. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_input_from_json_file(RegorusEngine* engine, byte* path); /// /// Evaluate query. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_query /// [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_query", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_eval_query(RegorusEngine* engine, byte* query); /// /// Evaluate specified rule. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_rule /// [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_rule", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_eval_rule(RegorusEngine* engine, byte* rule); /// /// Enable/disable coverage. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_enable_coverage /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_enable_coverage", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_enable_coverage(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Get coverage report. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_coverage_report /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_coverage_report(RegorusEngine* engine); /// /// Enable/disable strict builtin errors. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_strict_builtin_errors /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_strict_builtin_errors(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool strict); /// /// Get pretty printed coverage report. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Report.html#method.to_string_pretty /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report_pretty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_coverage_report_pretty(RegorusEngine* engine); /// /// Clear coverage data. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_coverage_data /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_coverage_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_coverage_data(RegorusEngine* engine); /// /// Whether to gather output of print statements. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_gather_prints /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_gather_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_gather_prints(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Take all the gathered print statements. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.take_prints /// [DllImport(LibraryName, EntryPoint = "regorus_engine_take_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_take_prints(RegorusEngine* engine); /// /// Get AST of policies. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_ast_as_json /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_ast_as_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_ast_as_json(RegorusEngine* engine); /// /// Gets the package names defined in each policy added to the engine. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_package_names /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_package_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policy_package_names(RegorusEngine* engine); /// /// Gets the parameters defined in each policy added to the engine. /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_parameters /// [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_parameters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_get_policy_parameters(RegorusEngine* engine); /// /// Enable/disable rego v1. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_rego_v0 /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_rego_v0", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_rego_v0(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable); /// /// Compile a target-aware policy from the current engine state. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_for_target /// [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_compile_for_target(RegorusEngine* engine); /// /// Compile a policy with a specific entry point rule. /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_with_entrypoint /// [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_compile_with_entrypoint(RegorusEngine* engine, byte* rule); #if REGORUS_FFI_TEST_HOOKS /// /// Trigger a panic inside the engine for testing purposes. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_test_trigger_panic", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_test_trigger_panic(); /// /// Reset the engine poison flag for testing. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_test_reset_poison", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_engine_test_reset_poison(); #endif /// /// Configure the execution timer for a specific engine instance. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_execution_timer_config(RegorusEngine* engine, RegorusExecutionTimerConfig* config); /// /// Clear the execution timer configuration for a specific engine instance. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_execution_timer_config(RegorusEngine* engine); /// /// Set the policy length limits for a specific engine instance. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_set_policy_length_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_set_policy_length_config(RegorusEngine* engine, RegorusPolicyLengthConfig config); /// /// Clear the policy length configuration for a specific engine instance. /// [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_policy_length_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_engine_clear_policy_length_config(RegorusEngine* engine); #endregion #region Execution Timer Global Methods /// /// Set the process-wide fallback execution timer configuration. /// [DllImport(LibraryName, EntryPoint = "regorus_set_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_set_fallback_execution_timer_config(RegorusExecutionTimerConfig config); /// /// Clear the process-wide fallback execution timer configuration. /// [DllImport(LibraryName, EntryPoint = "regorus_clear_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_clear_fallback_execution_timer_config(); #endregion #region Cache Configuration Global Methods /// /// Configure the global pattern caches used by regex and glob builtins. /// [DllImport(LibraryName, EntryPoint = "regorus_set_cache_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_set_cache_config(RegorusCacheConfig config); /// /// Clear all entries from every pattern cache. /// [DllImport(LibraryName, EntryPoint = "regorus_clear_cache", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_clear_cache(); #endregion #region Compilation Methods /// /// Compiles a policy from data and modules with a specific entry point rule. /// This is a convenience function that wraps regorus::compile_policy_with_entrypoint. /// [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compile_policy_with_entrypoint(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte* entry_point_rule); /// /// Compiles a target-aware policy from data and modules. /// This is a convenience function that wraps regorus::compile_policy_for_target. /// [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compile_policy_for_target(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len); #endregion #region Compiled Policy Methods /// /// Drop a RegorusCompiledPolicy. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void regorus_compiled_policy_drop(RegorusCompiledPolicy* compiled_policy); /// /// Evaluate the compiled policy with the given input. /// For target policies, evaluates the target's effect rule. /// For regular policies, evaluates the originally compiled rule. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_eval_with_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compiled_policy_eval_with_input(RegorusCompiledPolicy* compiled_policy, byte* input); /// /// Get information about the compiled policy including metadata about modules, /// target configuration, and resource types. /// Returns a JSON-encoded PolicyInfo struct containing comprehensive /// information about the compiled policy such as module IDs, target name, /// applicable resource types, entry point rule, and parameters. /// [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_get_policy_info", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_compiled_policy_get_policy_info(RegorusCompiledPolicy* compiled_policy); #endregion #region RBAC Methods /// /// Evaluate an Azure RBAC condition expression against a JSON evaluation context. /// [DllImport(LibraryName, EntryPoint = "regorus_rbac_engine_eval_condition", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_rbac_engine_eval_condition(byte* condition, byte* context_json); #endregion #region Target Registry Methods /// /// Register a target from JSON definition. /// The target JSON should follow the target schema format. /// Once registered, the target can be referenced in Rego policies using __target__ rules. /// [DllImport(LibraryName, EntryPoint = "regorus_register_target_from_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_register_target_from_json(byte* target_json); /// /// Check if a target is registered. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_contains(byte* name); /// /// Get a list of all registered target names as JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_list_names(); /// /// Remove a target from the registry by name. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_remove(byte* name); /// /// Clear all targets from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_clear(); /// /// Get the number of registered targets. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_len(); /// /// Check if the target registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_target_registry_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_target_registry_is_empty(); #endregion #region Resource Schema Registry Methods /// /// Register a resource schema from JSON with a given name. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_register(byte* name, byte* schema_json); /// /// Check if a resource schema with the given name exists. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_contains(byte* name); /// /// Get the number of registered resource schemas. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_len(); /// /// Check if the resource schema registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_is_empty(); /// /// List all registered resource schema names as a JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_list_names(); /// /// Remove a resource schema by name. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_remove(byte* name); /// /// Clear all resource schemas from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_resource_schema_clear(); #endregion #region Effect Schema Registry Methods /// /// Register an effect schema from JSON with a given name. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_register(byte* name, byte* schema_json); /// /// Check if an effect schema with the given name exists. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_contains(byte* name); /// /// Get the number of registered effect schemas. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_len(); /// /// Check if the effect schema registry is empty. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_is_empty(); /// /// List all registered effect schema names as a JSON array. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_list_names(); /// /// Remove an effect schema by name. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_remove(byte* name); /// /// Clear all effect schemas from the registry. /// [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern RegorusResult regorus_effect_schema_clear(); #endregion } #region Native Structures /// /// Type of data contained in RegorusResult. /// internal enum RegorusDataType : uint { /// /// No data / void. /// None, /// /// String data (output field is valid). /// String, /// /// Boolean data (bool_value field is valid). /// Boolean, /// /// Integer data (int_value field is valid). /// Integer, /// /// Pointer data (pointer_value field is valid). /// Pointer, } /// /// Status of a call on RegorusEngine. /// internal enum RegorusStatus : uint { /// /// The operation was successful. /// Ok, /// /// The operation was unsuccessful. /// Error, /// /// Invalid data format provided. /// InvalidDataFormat, /// /// Invalid entrypoint rule specified. /// InvalidEntrypoint, /// /// Compilation failed. /// CompilationFailed, /// /// Invalid argument provided. /// InvalidArgument, /// /// Invalid module ID. /// InvalidModuleId, /// /// Invalid policy content. /// InvalidPolicy, /// /// The engine panicked and cannot be reused until reset. /// Panic, /// /// The engine remains poisoned because a previous panic was detected. /// Poisoned, } /// /// Result of a call on RegorusEngine. /// Must be freed using regorus_result_drop. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusResult { /// /// Status. /// public RegorusStatus status; /// /// Type of data contained in this result. /// public RegorusDataType data_type; /// /// String output produced by the call. /// Valid when data_type is String. Owned by Rust. /// public byte* output; /// /// Boolean value. /// Valid when data_type is Boolean. /// [MarshalAs(UnmanagedType.I1)] public bool bool_value; /// /// Integer value. /// Valid when data_type is Integer. /// public long int_value; /// /// Pointer value. /// Valid when data_type is Pointer. /// public void* pointer_value; /// /// Errors produced by the call. /// Owned by Rust. /// public byte* error_message; } /// /// FFI representation of the execution timer configuration. /// [StructLayout(LayoutKind.Sequential)] internal struct RegorusExecutionTimerConfig { public ulong limit_ns; public uint check_interval; } /// /// FFI representation of the policy length configuration. /// [StructLayout(LayoutKind.Sequential)] internal struct RegorusPolicyLengthConfig { public uint max_col; public UIntPtr max_file_bytes; public UIntPtr max_lines; } /// /// FFI representation of the cache configuration. /// [StructLayout(LayoutKind.Sequential)] internal struct RegorusCacheConfig { public UIntPtr regex; public UIntPtr glob; } /// /// Byte buffer returned from FFI. /// [StructLayout(LayoutKind.Sequential)] internal unsafe struct RegorusBuffer { public byte* data; public UIntPtr len; public UIntPtr capacity; } /// /// Wrapper for regorus::Engine. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusEngine { } /// /// Wrapper for regorus::CompiledPolicy. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusCompiledPolicy { } /// /// Wrapper for regorus::rvm::Program. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusProgram { } /// /// Wrapper for regorus::rvm::RegoVM. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusRvm { } /// /// FFI wrapper for PolicyModule struct. /// [StructLayout(LayoutKind.Sequential)] internal unsafe partial struct RegorusPolicyModule { public byte* id; public byte* content; } #endregion }