forked from google/kafel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafel.c
136 lines (106 loc) · 3.15 KB
/
kafel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Kafel
-----------------------------------------
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "kafel.h"
#include "parser.h"
#include "codegen.h"
#include "common.h"
#include "context.h"
#include "lexer.h"
#include "syscall.h"
static int parse(struct kafel_ctxt* ctxt) {
yyscan_t scanner;
if (kafel_yylex_init(&scanner)) {
// couldn't initialize
return -1;
}
YY_BUFFER_STATE buf_state;
switch (ctxt->input.type) {
case INPUT_FILE:
buf_state =
kafel_yy_create_buffer(ctxt->input.file, YY_BUF_SIZE, scanner);
kafel_yy_switch_to_buffer(buf_state, scanner);
break;
case INPUT_STRING:
buf_state = kafel_yy_scan_string(ctxt->input.string, scanner);
break;
default:
kafel_yylex_destroy(scanner);
return -1;
}
ctxt->syscalls = syscalls_lookup(ctxt->target_arch);
if (ctxt->syscalls == NULL) {
append_error(ctxt, "Cannot resolve syscall list for architecture %#x\n",
ctxt->target_arch);
return -1;
}
if (kafel_yyparse(ctxt, scanner)) {
// parse error
kafel_yylex_destroy(scanner);
return -1;
}
kafel_yylex_destroy(scanner);
return 0;
}
KAFEL_API void kafel_set_input_file(kafel_ctxt_t ctxt, FILE* file) {
ASSERT(ctxt != NULL);
ASSERT(file != NULL);
ctxt->input.type = INPUT_FILE;
ctxt->input.file = file;
}
KAFEL_API void kafel_set_input_string(kafel_ctxt_t ctxt, const char* string) {
ASSERT(ctxt != NULL);
ASSERT(string != NULL);
ctxt->input.type = INPUT_STRING;
ctxt->input.string = string;
}
KAFEL_API void kafel_set_target_arch(kafel_ctxt_t ctxt, uint32_t target_arch) {
ASSERT(ctxt != NULL);
ctxt->target_arch = target_arch;
}
KAFEL_API int kafel_compile(kafel_ctxt_t ctxt, struct sock_fprog* prog) {
if (prog == NULL) {
errno = EINVAL;
return -EINVAL;
}
kafel_ctxt_clean(ctxt);
int rv = parse(ctxt);
if (rv) {
return rv;
}
return compile_policy(ctxt, prog);
}
KAFEL_API int kafel_compile_file(FILE* file, struct sock_fprog* prog) {
if (file == NULL || prog == NULL) {
errno = EINVAL;
return -EINVAL;
}
kafel_ctxt_t ctxt = kafel_ctxt_create();
kafel_set_input_file(ctxt, file);
int rv = kafel_compile(ctxt, prog);
kafel_ctxt_destroy(&ctxt);
return rv;
}
KAFEL_API int kafel_compile_string(const char* source,
struct sock_fprog* prog) {
if (source == NULL || prog == NULL) {
errno = EINVAL;
return -EINVAL;
}
kafel_ctxt_t ctxt = kafel_ctxt_create();
kafel_set_input_string(ctxt, source);
int rv = kafel_compile(ctxt, prog);
kafel_ctxt_destroy(&ctxt);
return rv;
}