C API

Program

Inspect the compiled program, permitted queries and normalized rules of a loaded policy.

Inspect what a loaded policy compiled before using it: its admitted queries, identity, predicates, static facts, normalized rules and required capabilities. The Program guide shows a complete read-only inspection flow using a program borrowed from a live session.

Handle: program_t

C · handleSince v0.1.0-alpha.4
program_t

#include <maelys/datalog_program.h>

typedef struct maelys_datalog_program maelys_datalog_program_t;
Borrowed, read-only view of the compiled program owned by a session.

Functions

program_fact()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_fact(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_ir_atom_t *out_atom
);
Read a normalized static fact by its zero-based index.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
indexsize_tin
Zero-based index of the static fact to read.
Outputs

Written on success.

out_atommaelys_datalog_ir_atom_t *out
Receives one normalized static fact.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with a normalized static fact. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.

Example
maelys_datalog_ir_atom_t fact;
maelys_datalog_status_t status = maelys_datalog_program_fact(program, 0, &fact);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("static fact predicate: %s\n", fact.predicate);
status: ok
static fact predicate: user

program_fingerprint()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_fingerprint(
const maelys_datalog_program_t *program,
char out_fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES]
);
Read the compiled identity of the program into a fixed-size fingerprint buffer.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
Outputs

Written on success.

out_fingerprintchar [MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES]out
Receives the fixed-size NUL-terminated compiled fingerprint.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the compiled-program fingerprint in the caller buffer. NULL arguments return INVALID_ARGUMENT.

Example
char fingerprint[MAELYS_DATALOG_PUBLIC_FINGERPRINT_BYTES] = {0};
maelys_datalog_status_t status = maelys_datalog_program_fingerprint(program, fingerprint);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("compiled fingerprint present: %s\n", fingerprint[0] != '\0' ? "yes" : "no");
status: ok
compiled fingerprint present: yes

program_info()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_info(
const maelys_datalog_program_t *program,
maelys_datalog_program_info_t *out_info
);
Read the program identity, declaration counts, limits and required capabilities.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
Outputs

Written on success.

out_infomaelys_datalog_program_info_t *out
Receives the compiled identity, counts, bounds and capabilities.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with a copied information record. NULL arguments return INVALID_ARGUMENT.

Example
maelys_datalog_program_info_t info;
maelys_datalog_status_t status = maelys_datalog_program_info(program, &info);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("predicates: %zu\n", info.predicate_count);
printf("static facts: %zu\n", info.fact_count);
printf("rules: %zu\n", info.rule_count);
status: ok
predicates: 2
static facts: 1
rules: 1

program_predicate()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_predicate(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_predicate_t *out_predicate
);
Read a predicate descriptor by its zero-based program index.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
indexsize_tin
Zero-based index of the compiled predicate to read.
Outputs

Written on success.

out_predicatemaelys_datalog_predicate_t *out
Receives the borrowed predicate descriptor.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the indexed predicate descriptor. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.

Example
maelys_datalog_predicate_t predicate;
maelys_datalog_status_t status = maelys_datalog_program_predicate(program, 0, &predicate);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("predicate: %s\n", predicate.name);
status: ok
predicate: user

program_query()

C · functionSince v0.20.0
program_query()

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_query(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_predicate_t *out_predicate
);
Read an admitted query descriptor by its zero-based query index.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
indexsize_tin
Zero-based index of an admitted query in the effective query list.
Outputs

Written on success.

out_predicatemaelys_datalog_predicate_t *out
Receives the borrowed predicate descriptor.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with an admitted query; returns NOT_FOUND for an index outside the query count. NULL arguments return INVALID_ARGUMENT; output remains unchanged on failure.

Example
maelys_datalog_predicate_t query;
maelys_datalog_status_t status = maelys_datalog_program_query(program, 0, &query);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("admitted query: %s\n", query.name);
status: ok
admitted query: allow

program_query_count()

C · functionSince v0.20.0
program_query_count()

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_query_count(
const maelys_datalog_program_t *program,
size_t *out_count
);
Count the queries admitted by the loaded manifest and query policy.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
Outputs

Written on success.

out_countsize_t *out
Receives the number of admitted queries.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with the admitted-query count. NULL arguments return INVALID_ARGUMENT; output remains unchanged on failure.

Example
size_t count = 0;
maelys_datalog_status_t status = maelys_datalog_program_query_count(program, &count);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("admitted queries: %zu\n", count);
status: ok
admitted queries: 1

program_rule()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_rule(
const maelys_datalog_program_t *program,
size_t index,
maelys_datalog_ir_rule_t *out_rule
);
Read a normalized rule by its zero-based index.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed, read-only program view; keep its owning session alive.
indexsize_tin
Zero-based index of the normalized rule to read.
Outputs

Written on success.

out_rulemaelys_datalog_ir_rule_t *out
Receives one normalized rule.
Return status

maelys_datalog_status_t — OK on success.

Returns OK with a normalized rule. An out-of-range index returns NOT_FOUND; NULL arguments return INVALID_ARGUMENT.

Example
maelys_datalog_ir_rule_t rule;
maelys_datalog_status_t status = maelys_datalog_program_rule(program, 0, &rule);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("rule head: %s\n", rule.head.predicate);
printf("body literals: %zu\n", rule.body_count);
status: ok
rule head: allow
body literals: 1

program_rule_variable_name()

C · functionSince v0.27.0
program_rule_variable_name()

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_program_rule_variable_name(
const maelys_datalog_program_t *program,
size_t rule_index,
uint32_t variable,
const char **out_name
);
Read the source spelling of a named variable in a normalized rule.
Arguments
programconst maelys_datalog_program_t *handleread
Borrowed read-only program view; keep its owner alive.
rule_indexsize_tin
Zero-based normalized rule index. An explanation rule_id is this index plus one.
variableuint32_tin
Rule-local IR variable number read from the normalized rule; this number identifies the variable, while the output supplies its source name.
Outputs

Written on success.

out_nameconst char **out
Receives the borrowed source spelling on success; unchanged on failure.
Return status

maelys_datalog_status_t — OK on success.

Returns OK and assigns out_name to the complete, case-sensitive, NUL-terminated source spelling, borrowed until policy_free or, for a session_program view, session_free. Source buffers need not remain alive. No allocation. Returns NOT_FOUND for an out-of-range rule, a variable number of 26 or greater (including anonymous variables), an unused variable in this OR alternative, a rule built by program_add_rule, or a spelling omitted when the bounded metadata pool filled. INVALID_ARGUMENT means program, its underlying ruleset or out_name is NULL. Failure leaves out_name unchanged. Single-letter names keep their source spelling; variable numbers and explanation formats do not change. Named variables in aggregate projections and outputs, and named local variables, are covered.

Example
maelys_datalog_ir_rule_t rule;
check_status(maelys_datalog_program_rule(program, 0, &rule));
const char *name = NULL;
maelys_datalog_status_t status = maelys_datalog_program_rule_variable_name(
    program, 0, rule.head.terms[0].as.variable, &name);
printf("named lookup: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("source name: %s\n", name);

check_status(maelys_datalog_program_rule(program, 1, &rule));
const char *unchanged = name;
status = maelys_datalog_program_rule_variable_name(
    program, 1, rule.body[0].atom.terms[0].as.variable, &name);
printf("anonymous lookup: %s\n", maelys_datalog_status_name(status));
if (status != MAELYS_DATALOG_STATUS_NOT_FOUND || name != unchanged) exit(EXIT_FAILURE);
named lookup: ok
source name: User
anonymous lookup: not_found

Constants

Availability

HAS_RULE_VARIABLE_NAMES

C · constantSince v0.27.0
HAS_RULE_VARIABLE_NAMES
#define MAELYS_DATALOG_HAS_RULE_VARIABLE_NAMES 1

Reports that the SDK provides source-variable name lookup.

Value

1

Handle: session_t ↗

Creation

session_program()

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

#include <maelys/datalog_program.h>

maelys_datalog_status_t
maelys_datalog_session_program(
const maelys_datalog_session_t *session,
const maelys_datalog_program_t **out_program
);
Borrow the compiled program from a live session for read-only inspection.
Arguments
sessionconst maelys_datalog_session_t *handleread
Live session whose compiled program is borrowed.
Outputs

Written on success.

out_programconst maelys_datalog_program_t **out
Receives the borrowed program view; the session retains ownership.
Return status

maelys_datalog_status_t — OK on success.

Returns OK and a borrowed, read-only view owned by the live session. NULL arguments return INVALID_ARGUMENT.

Example
const maelys_datalog_program_t *borrowed = NULL;
maelys_datalog_status_t status = maelys_datalog_session_program(session, &borrowed);
printf("status: %s\n", maelys_datalog_status_name(status));
check_status(status);
printf("program borrowed: %s\n", borrowed != NULL ? "yes" : "no");
status: ok
program borrowed: yes

Struct: program_info_t

C · structSince v0.1.0-alpha.4
program_info_t

#include <maelys/datalog_program.h>

Identity, counts, limits and capabilities of the compiled program.
Fields
abi_versionuint32_t
Version of the program inspection ABI.
policy_idconst char *
Borrowed identifier of the compiled policy.
domainconst char *
Borrowed name of the policy domain.
fingerprintconst char *
Borrowed compiled-program fingerprint.
predicate_countsize_t
Number of declared predicates.
fact_countsize_t
Number of normalized static facts.
rule_countsize_t
Number of normalized rules.
max_input_factssize_t
Compiled limit for input facts.
max_derived_factssize_t
Compiled limit for derived facts.
max_facts_per_predicatesize_t
Compiled per-predicate fact limit.
required_capabilitiesuint64_t
Capability bits required by the compiled program.
Typedef
typedef struct {
    uint32_t abi_version;
    const char *policy_id;
    const char *domain;
    const char *fingerprint;
    size_t predicate_count, fact_count, rule_count;
    size_t max_input_facts, max_derived_facts, max_facts_per_predicate;
    uint64_t required_capabilities;
} maelys_datalog_program_info_t;

Constants

Program ABI

PROGRAM_ABI_VERSION

C · constantSince v0.1.0-alpha.4
PROGRAM_ABI_VERSION
#define MAELYS_DATALOG_PROGRAM_ABI_VERSION 2u

Version of the read-only compiled-program inspection contract.

Value

2

Struct: ir_atom_t

C · structSince v0.1.0-alpha.4
ir_atom_t

#include <maelys/datalog_program.h>

Predicate name and typed terms in a normalized atom.
Fields
predicateconst char *
Borrowed predicate name used by this atom.
aritysize_t
Number of active terms.
termsmaelys_datalog_ir_term_t[4]
Typed terms up to arity.
Typedef
typedef struct {
    const char *predicate;
    size_t arity;
    maelys_datalog_ir_term_t terms[MAELYS_DATALOG_PUBLIC_MAX_TERMS];
} maelys_datalog_ir_atom_t;

Struct: ir_rule_t

C · structSince v0.1.0-alpha.4
ir_rule_t

#include <maelys/datalog_program.h>

Normalized rule head, body, arithmetic expressions and source location.
Fields
headmaelys_datalog_ir_atom_t
Head atom produced by this normalized rule.
body_countsize_t
Number of active body literals.
bodymaelys_datalog_ir_literal_t[8]
Body literals up to body_count.
expression_countsize_t
Number of active expression nodes.
expressionsmaelys_datalog_ir_expression_t[32]
Expression nodes up to expression_count.
sourcemaelys_datalog_source_location_t
Source location when retained by the compiler.
Typedef
typedef struct {
    maelys_datalog_ir_atom_t head;
    size_t body_count;
    maelys_datalog_ir_literal_t body[MAELYS_DATALOG_IR_MAX_BODY];
    size_t expression_count;
    maelys_datalog_ir_expression_t expressions[MAELYS_DATALOG_IR_MAX_EXPRESSIONS];
    maelys_datalog_source_location_t source;
} maelys_datalog_ir_rule_t;

Constants

Rule limits

IR_MAX_BODY

C · constantSince v0.1.0-alpha.4
IR_MAX_BODY
#define MAELYS_DATALOG_IR_MAX_BODY 8u

Maximum number of literals in a normalized rule body.

Value

8

IR_MAX_EXPRESSIONS

C · constantSince v0.1.0-alpha.4
IR_MAX_EXPRESSIONS
#define MAELYS_DATALOG_IR_MAX_EXPRESSIONS 32u

Maximum number of expression nodes in a normalized rule.

Value

32

IR_MAX_VARIABLES

C · constantSince v0.1.0-alpha.4
IR_MAX_VARIABLES
#define MAELYS_DATALOG_IR_MAX_VARIABLES 32u

Maximum number of variables in a normalized rule.

Value

32

Struct: ir_literal_t

C · structSince v0.1.0-alpha.4
ir_literal_t

#include <maelys/datalog_program.h>

One normalized rule-body operation and its active operands.
Fields
kindmaelys_datalog_ir_literal_kind_t
Selects which value or operation in this record is active.
atommaelys_datalog_ir_atom_t
Atom used by an ordinary, negated or aggregate literal.
lhsmaelys_datalog_ir_term_t
Left term of a comparison or aggregate projection.
rhsmaelys_datalog_ir_term_t
Right term of a comparison or aggregate projection.
comparisonmaelys_datalog_ir_comparison_t
Comparison operator when this literal is a comparison.
has_arithmeticint
Indicates whether the expression indices carry arithmetic operands.
lhs_expressionuint32_t
Root expression index for the left operand when arithmetic is active.
rhs_expressionuint32_t
Root expression index for the right operand when arithmetic is active.
filter_nameconst char *
Name of the filter used by a filter literal.
filter_semantic_idconst char *
Optional semantic identity of the filter.
patternconst unsigned char *
Borrowed byte pattern used by a filter literal.
pattern_lengthsize_t
Length of the filter pattern in bytes.
filter_valuemaelys_datalog_ir_term_t
Typed comparison value used by a filter literal.
Typedef
typedef struct {
    maelys_datalog_ir_literal_kind_t kind;
    maelys_datalog_ir_atom_t atom;
    maelys_datalog_ir_term_t lhs, rhs;
    maelys_datalog_ir_comparison_t comparison;
    int has_arithmetic;
    uint32_t lhs_expression, rhs_expression;
    const char *filter_name;
    const char *filter_semantic_id; /* Optional on input; checked if present. */
    const unsigned char *pattern;
    size_t pattern_length;
    maelys_datalog_ir_term_t filter_value;
} maelys_datalog_ir_literal_t;

Struct: ir_expression_t

C · structSince v0.1.0-alpha.4
ir_expression_t

#include <maelys/datalog_program.h>

One node of an arithmetic expression in normalized rule order.
Fields
kindmaelys_datalog_ir_expression_kind_t
Selects which value or operation in this record is active.
leftuint32_t
Index of the first earlier expression node for a binary operation.
rightuint32_t
Index of the second earlier expression node for a binary operation.
termmaelys_datalog_ir_term_t
Literal or variable value for a leaf expression.
Typedef
typedef struct {
    maelys_datalog_ir_expression_kind_t kind;
    /* Binary operands refer to earlier nodes. Each root's expanded tree must
     * also fit MAX_EXPRESSIONS, bounding recursive evaluation of shared edges. */
    uint32_t left, right;
    maelys_datalog_ir_term_t term;
} maelys_datalog_ir_expression_t;

Constants

Expression sentinel

IR_NO_EXPRESSION

C · constantSince v0.1.0-alpha.4
IR_NO_EXPRESSION
#define MAELYS_DATALOG_IR_NO_EXPRESSION UINT32_MAX

Sentinel for a literal with no arithmetic-expression root.

Value

4294967295

Struct: ir_term_t

C · structSince v0.1.0-alpha.4
ir_term_t

#include <maelys/datalog_program.h>

A symbol, integer, Boolean or variable in a normalized program.
Fields
kindmaelys_datalog_ir_term_kind_t
Selects which value or operation in this record is active.
asunion { const char *symbol; int64_t integer; int boolean; uint32_t variable; }
Value selected by kind: symbol, integer, Boolean or variable index.
Typedef
typedef struct {
    maelys_datalog_ir_term_kind_t kind;
    union {
        const char *symbol;
        int64_t integer;
        int boolean;
        uint32_t variable;
    } as;
} maelys_datalog_ir_term_t;

Struct: source_location_t

C · structSince v0.1.0-alpha.4
source_location_t

#include <maelys/datalog_program.h>

One-based source line and column when available.
Fields
linesize_t
One-based source line, or zero when unavailable.
columnsize_t
One-based source column, or zero when unavailable.
Typedef
typedef struct {
    size_t line, column; /* 1-based, or both zero when unavailable. */
} maelys_datalog_source_location_t;

Enum: ir_literal_kind_t

C · enumSince v0.1.0-alpha.4
ir_literal_kind_t

#include <maelys/datalog_program.h>

The operation represented by a normalized rule-body literal.
Members
IR_ATOM1
Positive atom in the rule body.
IR_COMPARISON2
Comparison between terms or expressions.
IR_NEGATION3
Negated atom in the rule body.
IR_FILTER4
Named filter with a pattern and value.
IR_COUNT5Since v0.6.0
Grouped count with local projection variables; requires CAP_AGGREGATES.
IR_MIN6Since v0.7.0
Grouped minimum of projected integers; empty groups fail and CAP_MIN is required.
IR_MAX7Since v0.7.0
Grouped maximum of projected integers; empty groups fail and CAP_MAX is required.
IR_SUM8Since v0.7.0
Grouped sum of distinct complete source facts; empty groups yield zero and CAP_SUM is required.
Typedef
typedef enum {
    MAELYS_DATALOG_IR_ATOM = 1,
    MAELYS_DATALOG_IR_COMPARISON = 2,
    MAELYS_DATALOG_IR_NEGATION = 3,
    MAELYS_DATALOG_IR_FILTER = 4,
    /* atom = source pattern, lhs = local projection variable, rhs = output
     * variable, both in 0..25. Other source variables in 0..25 are group keys
     * bound by ordinary positive atoms; 26..31 are local existentials. Neither
     * projection nor local existentials may escape into the surrounding rule.
     * Other fields inactive. Requires CAP_AGGREGATES. */
    MAELYS_DATALOG_IR_COUNT = 5,
    /* Same active fields and scoping as COUNT; integer projection only.
     * MIN/MAX fail on an empty group. SUM returns zero and sums distinct
     * complete source facts (not distinct projected values). Requires the
     * corresponding CAP_MIN, CAP_MAX or CAP_SUM, independently of COUNT. */
    MAELYS_DATALOG_IR_MIN = 6,
    MAELYS_DATALOG_IR_MAX = 7,
    MAELYS_DATALOG_IR_SUM = 8
} maelys_datalog_ir_literal_kind_t;

Enum: ir_term_kind_t

C · enumSince v0.1.0-alpha.4
ir_term_kind_t

#include <maelys/datalog_program.h>

The active value of a normalized program term.
Members
IR_SYMBOL1
Symbol text is the active term value.
IR_INTEGER2
Signed integer is the active term value.
IR_BOOLEAN3
Boolean is the active term value.
IR_VARIABLE4
Variable index is the active term value.
Typedef
typedef enum {
    MAELYS_DATALOG_IR_SYMBOL = 1,
    MAELYS_DATALOG_IR_INTEGER = 2,
    MAELYS_DATALOG_IR_BOOLEAN = 3,
    MAELYS_DATALOG_IR_VARIABLE = 4
} maelys_datalog_ir_term_kind_t;

Enum: ir_comparison_t

C · enumSince v0.1.0-alpha.4
ir_comparison_t

#include <maelys/datalog_program.h>

Comparison operator used by a normalized rule literal.
Members
IR_EQ1
Equality comparison.
IR_NE2
Inequality comparison.
IR_LT3
Less-than comparison.
IR_LE4
Less-than-or-equal comparison.
IR_GT5
Greater-than comparison.
IR_GE6
Greater-than-or-equal comparison.
Typedef
typedef enum {
    MAELYS_DATALOG_IR_EQ = 1,
    MAELYS_DATALOG_IR_NE = 2,
    MAELYS_DATALOG_IR_LT = 3,
    MAELYS_DATALOG_IR_LE = 4,
    MAELYS_DATALOG_IR_GT = 5,
    MAELYS_DATALOG_IR_GE = 6
} maelys_datalog_ir_comparison_t;

Enum: ir_expression_kind_t

C · enumSince v0.1.0-alpha.4
ir_expression_kind_t

#include <maelys/datalog_program.h>

Literal, variable or arithmetic operator represented by an expression node.
Members
IR_EXPR_INTEGER1
Integer literal expression leaf.
IR_EXPR_VARIABLE2
Variable expression leaf.
IR_EXPR_ADD3
Addition of earlier expression nodes.
IR_EXPR_SUB4
Subtraction of earlier expression nodes.
IR_EXPR_MUL5
Multiplication of earlier expression nodes.
Typedef
typedef enum {
    MAELYS_DATALOG_IR_EXPR_INTEGER = 1,
    MAELYS_DATALOG_IR_EXPR_VARIABLE = 2,
    MAELYS_DATALOG_IR_EXPR_ADD = 3,
    MAELYS_DATALOG_IR_EXPR_SUB = 4,
    MAELYS_DATALOG_IR_EXPR_MUL = 5
} maelys_datalog_ir_expression_kind_t;

Usage guides