C Extension API

Native modules

Implement trusted native filters, join planners and deferred domain installers.

Add trusted native filter and join-planning callbacks, or install domain declarations when a policy loads. The Modules guide applies these callbacks to document-access decisions and shows the required startup order.

Handle: domain_builder_t

C · handleSince v0.10.0
domain_builder_t

#include <maelys/datalog_module.h>

typedef struct maelys_datalog_domain_builder maelys_datalog_domain_builder_t;
Borrowed, bounded predicate-declaration builder supplied only during a domain installer callback. Do not retain it or attempt to create or free it yourself.

Creation

domain_register_advanced()

C · functionSince v0.10.0
domain_register_advanced()

#include <maelys/datalog_module.h>

maelys_datalog_status_t
maelys_datalog_domain_register_advanced(
const maelys_datalog_domain_t *domain,
const char *description,
maelys_datalog_domain_installer_t installer
);
Register a named domain whose predicate declarations are installed when a policy loads.
Arguments
domainconst maelys_datalog_domain_t *in
Named domain and optional atom declarations. For deferred installation, set predicates to NULL and predicate_count to zero, then supply a non-NULL installer. With a direct nonempty predicates table, pass NULL as installer.
descriptionconst char *in
Optional descriptive text copied by registration; at most 255 bytes before its terminating NUL.
installermaelys_datalog_domain_installer_tin
Callback invoked at policy loading to add predicates through the supplied builder, or NULL when the domain supplies a predicate table. Its code must remain available for every policy load.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after registration, including compatible repeat registration. Invalid argument combinations return INVALID_ARGUMENT; incompatible repeat registration returns INVALID_FIELD. Invalid declarations or a full bounded registry fail. Domains are process-global and are not isolated by explicit contexts.

Example
maelys_datalog_status_t status = maelys_datalog_domain_register_advanced(&domain,
    "Document access with a native file-suffix filter", install_document_predicates);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);

Functions

domain_builder_add()

C · functionSince v0.10.0
domain_builder_add()

#include <maelys/datalog_module.h>

maelys_datalog_status_t
maelys_datalog_domain_builder_add(
maelys_datalog_domain_builder_t *builder,
const maelys_datalog_predicate_t *predicate
);
Add one predicate declaration to the candidate domain being installed.
Arguments
buildermaelys_datalog_domain_builder_t *handlemodified
Borrowed builder supplied to the current installer callback; valid only during that callback.
predicateconst maelys_datalog_predicate_t *in
Predicate name, arity and public flags to copy into the bounded candidate registry. The declaration need not outlive this call.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after adding the declaration. A NULL builder or predicate, or invalid flag bits, returns INVALID_ARGUMENT; registry validation and capacity errors are propagated. The first builder failure remains sticky, so policy loading rejects the candidate even if the installer ignores that failure.

Example
const maelys_datalog_predicate_t predicate = MAELYS_DATALOG_IDB_QUERY("allow", 2);
maelys_datalog_status_t status = maelys_datalog_domain_builder_add(builder, &predicate);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
return status;

Type: domain_installer_t

C · typeSince v0.10.0
domain_installer_t

#include <maelys/datalog_module.h>

Install a domain into the candidate builder supplied during policy loading. Add declarations with domain_builder_add and return an error on failure; a builder failure rejects the candidate even if the callback returns OK.
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_domain_installer_t)(maelys_datalog_domain_builder_t *builder);

Type: filter_cost_fn

C · typeSince v0.1.0-alpha.4
filter_cost_fn

#include <maelys/datalog_module.h>

Compute a conservative work bound from the value and pattern byte lengths, writing it through out_cost. The bound must be at least one for an external filter; reject overflow or unsupported inputs instead of wrapping the count.
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_filter_cost_fn)(size_t value_length,
                                                                 size_t pattern_length,
                                                                 size_t *out_cost);

Type: filter_evaluate_fn

C · typeSince v0.1.0-alpha.4
filter_evaluate_fn

#include <maelys/datalog_module.h>

Test callback-scoped value and pattern bytes, writing exactly zero or one through out_matched. The engine charges the previously computed cost before calling this deterministic evaluator; return an error if the inputs cannot be evaluated.
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_filter_evaluate_fn)(const unsigned char *value,
                                                                     size_t value_length,
                                                                     const unsigned char *pattern,
                                                                     size_t pattern_length,
                                                                     int *out_matched);

Struct: filter_module_t

C · structSince v0.1.0-alpha.4
filter_module_t

#include <maelys/datalog_module.h>

Native filter identity and callbacks copied by startup registration.
Fields
abi_versionuint32_t
Module ABI version; use MAELYS_DATALOG_MODULE_ABI_VERSION.
struct_sizesize_t
Exact size of this descriptor, set to sizeof(maelys_datalog_filter_module_t).
nameconst char *
Lower-case predicate identifier, at most 63 bytes plus a terminating NUL; reserved language identifiers cannot be registered as filters.
semantic_idconst char *
Nonempty semantic identity using ASCII letters, digits, dot, hyphen or underscore, within MAELYS_DATALOG_MODULE_SEMANTIC_ID_BYTES including the terminating NUL. Change it whenever behavior or cost accounting changes; module identities enter policy fingerprints.
validate_patternmaelys_datalog_filter_validate_fn
Required pattern-validation callback, called on the pattern bytes used by a policy.
costmaelys_datalog_filter_cost_fn
Required callback computing a conservative work bound of at least one before evaluation; overflow and unsupported inputs must fail.
evaluatemaelys_datalog_filter_evaluate_fn
Required deterministic evaluation callback; writes zero for no match or one for a match.
Typedef
typedef struct maelys_datalog_filter_module_t {
    uint32_t abi_version;
    size_t struct_size;
    const char *name;        /* lower-case predicate identifier, max 63 bytes */
    const char *semantic_id; /* ASCII letters/digits/dot/hyphen/underscore */
    maelys_datalog_filter_validate_fn validate_pattern;
    /* Conservative work bound, charged BEFORE evaluate; >= 1 for external
     * filters. Overflow/unsupported inputs must return an error. */
    maelys_datalog_filter_cost_fn cost;
    maelys_datalog_filter_evaluate_fn evaluate;
} maelys_datalog_filter_module_t;

Functions

register_filter_module()

C · functionSince v0.1.0-alpha.4
register_filter_module()

#include <maelys/datalog_module.h>

maelys_datalog_status_t
maelys_datalog_register_filter_module(
const maelys_datalog_filter_module_t *module
);
Make a native filter available to policies loaded after startup registration.
Arguments
moduleconst maelys_datalog_filter_module_t *in
Filter descriptor with the current module ABI, its exact structure size, valid name and semantic identity, and all three callbacks. The descriptor and strings are copied; callback code must remain loaded for the process lifetime.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after copying the descriptor. Invalid descriptors return INVALID_ARGUMENT; a duplicate name or semantic identity returns INVALID_FIELD; exceeding the additional-filter capacity returns PAYLOAD_TOO_LARGE. Registration after the module registry is sealed returns INVALID_STATE.

Example
maelys_datalog_status_t status = maelys_datalog_register_filter_module(&suffix_module);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);

Constants

Module contract

MODULE_ABI_VERSION

C · constantSince v0.1.0-alpha.4
MODULE_ABI_VERSION
#define MAELYS_DATALOG_MODULE_ABI_VERSION 1u

ABI version required by native filter and join-planner descriptors.

Value

1

MODULE_NAME_BYTES

C · constantSince v0.1.0-alpha.4
MODULE_NAME_BYTES
#define MAELYS_DATALOG_MODULE_NAME_BYTES 64u

Storage size for a module name, including its terminating NUL.

Value

64

MODULE_SEMANTIC_ID_BYTES

C · constantSince v0.1.0-alpha.4
MODULE_SEMANTIC_ID_BYTES
#define MAELYS_DATALOG_MODULE_SEMANTIC_ID_BYTES 128u

Storage size for a semantic identity, including its terminating NUL.

Value

128

MODULE_MAX_FILTERS

C · constantSince v0.1.0-alpha.4
MODULE_MAX_FILTERS
#define MAELYS_DATALOG_MODULE_MAX_FILTERS 16u /* additional to standard filters */

Maximum number of external filters in addition to the standard filters.

Value

16

Type: filter_validate_fn

C · typeSince v0.1.0-alpha.4
filter_validate_fn

#include <maelys/datalog_module.h>

Validate the pattern bytes when a policy uses a native filter. The pattern pointer and length describe callback-scoped input; return OK to accept the pattern or an error to reject it.
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_filter_validate_fn)(const unsigned char *pattern,
                                                                     size_t pattern_length);

Struct: join_candidate_t

C · structSince v0.1.0-alpha.4
join_candidate_t

#include <maelys/datalog_module.h>

Callback-scoped description of one safe next literal in a rule body.
Fields
body_indexsize_t
Zero-based position of the literal in the normalized rule body, distinct from its index in the supplied candidates array.
kindmaelys_datalog_join_kind_t
Literal kind; predicate metadata applies to positive and negated atoms.
predicate_nameconst char *
Borrowed predicate name for a positive or negated atom; NULL for a comparison or filter.
predicate_flagsunsigned int
Public MAELYS_DATALOG_PREDICATE_* flags for an atom; zero when predicate metadata does not apply.
aritysize_t
Number of terms of a predicate atom; zero when predicate metadata does not apply.
variable_maskuint64_t
Bit mask of variable numbers used by this literal.
bound_variable_maskuint64_t
Bit mask of variable numbers already bound at this planning position.
constant_termssize_t
Number of non-variable terms in a predicate atom; zero for other kinds.
bound_termssize_t
Number of variable-term occurrences already bound in a predicate atom; zero for other kinds.
default_scoreint64_t
Reference heuristic score at this planning position; higher scores are preferred by the reference heuristic.
Typedef
typedef struct {
    size_t body_index;
    maelys_datalog_join_kind_t kind;
    const char *predicate_name;
    unsigned predicate_flags;
    size_t arity;
    uint64_t variable_mask;
    uint64_t bound_variable_mask;
    size_t constant_terms;
    size_t bound_terms;
    int64_t default_score;
} maelys_datalog_join_candidate_t;

Type: join_choose_fn

C · typeSince v0.1.0-alpha.4
join_choose_fn

#include <maelys/datalog_module.h>

Choose one safe next literal by writing a zero-based index into the supplied candidates array. candidate_count bounds that array and every pointer is callback-scoped; return OK with an index strictly below candidate_count, or an error.
Typedef
typedef maelys_datalog_status_t (*maelys_datalog_join_choose_fn)(
    const maelys_datalog_join_candidate_t *candidates, size_t candidate_count,
    size_t *out_candidate_index);

Enum: join_kind_t

C · enumSince v0.1.0-alpha.4
join_kind_t

#include <maelys/datalog_module.h>

Kind of safe, unplanned literal offered to a native join planner.
Members
JOIN_ATOM1
Positive predicate atom offered as a safe next literal.
JOIN_COMPARISON2
Comparison whose required variables are already bound.
JOIN_NEGATED_ATOM3
Negated predicate atom whose required variables are already bound.
JOIN_FILTER4
Filter whose required variables are already bound.
Typedef
typedef enum {
    MAELYS_DATALOG_JOIN_ATOM = 1,
    MAELYS_DATALOG_JOIN_COMPARISON = 2,
    MAELYS_DATALOG_JOIN_NEGATED_ATOM = 3,
    MAELYS_DATALOG_JOIN_FILTER = 4
} maelys_datalog_join_kind_t;

Struct: planner_module_t

C · structSince v0.1.0-alpha.4
planner_module_t

#include <maelys/datalog_module.h>

Native join-planner identity and callback copied by startup registration.
Fields
abi_versionuint32_t
Module ABI version; use MAELYS_DATALOG_MODULE_ABI_VERSION.
struct_sizesize_t
Exact size of this descriptor, set to sizeof(maelys_datalog_planner_module_t).
nameconst char *
Nonempty lower-case identifier naming the planner, within MAELYS_DATALOG_MODULE_NAME_BYTES including the terminating NUL.
semantic_idconst char *
Nonempty semantic identity using ASCII letters, digits, dot, hyphen or underscore, within MAELYS_DATALOG_MODULE_SEMANTIC_ID_BYTES including the terminating NUL. Change it when planning behavior changes; module identities enter policy fingerprints.
choosemaelys_datalog_join_choose_fn
Required callback selecting an index in the safe candidates array; the engine retains binding checks, delta anchoring, execution and ownership.
Typedef
typedef struct maelys_datalog_planner_module_t {
    uint32_t abi_version;
    size_t struct_size;
    const char *name;
    const char *semantic_id;
    maelys_datalog_join_choose_fn choose;
} maelys_datalog_planner_module_t;

Functions

register_planner_module()

C · functionSince v0.1.0-alpha.4
register_planner_module()

#include <maelys/datalog_module.h>

maelys_datalog_status_t
maelys_datalog_register_planner_module(
const maelys_datalog_planner_module_t *module
);
Install the native callback that chooses the next safe rule-body literal.
Arguments
moduleconst maelys_datalog_planner_module_t *in
Planner descriptor with the current module ABI, its exact structure size, valid name and semantic identity, and a choose callback. The descriptor and strings are copied; callback code must remain loaded for the process lifetime.
Return status

maelys_datalog_status_t — OK on success.

Returns OK after copying the descriptor. Invalid descriptors return INVALID_ARGUMENT. A planner already registered, or registration after the module registry is sealed, returns INVALID_STATE.

Example
maelys_datalog_status_t status = maelys_datalog_register_planner_module(&planner_module);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);

Usage guides