Skip to content

Commit

Permalink
libsepol/cil: Add functions to make use of cil_write_ast()
Browse files Browse the repository at this point in the history
Add the functions cil_write_parse_ast(), cil_write_build_ast(),
and cil_write_resolve_ast() that can be used outside of libsepol.

These functions take a FILE pointer and CIL db, do the CIL build
through the desired phase, and then call cil_write_ast() to write
the CIL AST at that point.

Signed-off-by: James Carter <jwcart2@gmail.com>
  • Loading branch information
jwcart2 authored and fishilico committed Apr 21, 2021
1 parent 0b31424 commit 86ec04c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libsepol/cil/include/cil/cil.h
Expand Up @@ -60,6 +60,9 @@ extern void cil_set_attrs_expand_size(struct cil_db *db, unsigned attrs_expand_s
extern void cil_set_target_platform(cil_db_t *db, int target_platform);
extern void cil_set_policy_version(cil_db_t *db, int policy_version);
extern void cil_write_policy_conf(FILE *out, struct cil_db *db);
extern int cil_write_parse_ast(FILE *out, cil_db_t *db);
extern int cil_write_build_ast(FILE *out, cil_db_t *db);
extern int cil_write_resolve_ast(FILE *out, cil_db_t *db);

enum cil_log_level {
CIL_ERR = 1,
Expand Down
92 changes: 92 additions & 0 deletions libsepol/cil/src/cil.c
Expand Up @@ -50,6 +50,7 @@
#include "cil_binary.h"
#include "cil_policy.h"
#include "cil_strpool.h"
#include "cil_write_ast.h"

int cil_sym_sizes[CIL_SYM_ARRAY_NUM][CIL_SYM_NUM] = {
{64, 64, 64, 1 << 13, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
Expand Down Expand Up @@ -572,6 +573,97 @@ int cil_compile(struct cil_db *db)
return rc;
}

int cil_write_parse_ast(FILE *out, cil_db_t *db)
{
int rc = SEPOL_ERR;

if (db == NULL) {
goto exit;
}

cil_log(CIL_INFO, "Writing Parse AST\n");
rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_PARSE, db->parse->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to write parse ast\n");
goto exit;
}

exit:
return rc;
}

int cil_write_build_ast(FILE *out, cil_db_t *db)
{
int rc = SEPOL_ERR;

if (db == NULL) {
goto exit;
}

cil_log(CIL_INFO, "Building AST from Parse Tree\n");
rc = cil_build_ast(db, db->parse->root, db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to build ast\n");
goto exit;
}

cil_log(CIL_INFO, "Destroying Parse Tree\n");
cil_tree_destroy(&db->parse);

cil_log(CIL_INFO, "Writing Build AST\n");
rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_BUILD, db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to write build ast\n");
goto exit;
}

exit:
return rc;
}

int cil_write_resolve_ast(FILE *out, cil_db_t *db)
{
int rc = SEPOL_ERR;

if (db == NULL) {
goto exit;
}

cil_log(CIL_INFO, "Building AST from Parse Tree\n");
rc = cil_build_ast(db, db->parse->root, db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to build ast\n");
goto exit;
}

cil_log(CIL_INFO, "Destroying Parse Tree\n");
cil_tree_destroy(&db->parse);

cil_log(CIL_INFO, "Resolving AST\n");
rc = cil_resolve_ast(db, db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to resolve ast\n");
goto exit;
}

cil_log(CIL_INFO, "Qualifying Names\n");
rc = cil_fqn_qualify(db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to qualify names\n");
goto exit;
}

cil_log(CIL_INFO, "Writing Resolve AST\n");
rc = cil_write_ast(out, CIL_WRITE_AST_PHASE_RESOLVE, db->ast->root);
if (rc != SEPOL_OK) {
cil_log(CIL_ERR, "Failed to write resolve ast\n");
goto exit;
}

exit:
return rc;
}

int cil_build_policydb(cil_db_t *db, sepol_policydb_t **sepol_db)
{
int rc;
Expand Down
3 changes: 3 additions & 0 deletions libsepol/src/libsepol.map.in
Expand Up @@ -269,4 +269,7 @@ LIBSEPOL_1.1 {
LIBSEPOL_3.0 {
global:
sepol_policydb_optimize;
cil_write_parse_ast;
cil_write_build_ast;
cil_write_resolve_ast;
} LIBSEPOL_1.1;

0 comments on commit 86ec04c

Please sign in to comment.