Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages when loading rules. #150

Merged
merged 1 commit into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions userspace/engine/lua/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ function compiler.compile_macro(line, list_defs)
local ast, error_msg = parser.parse_filter(line)

if (error_msg) then
print ("Compilation error when compiling \""..line.."\": ", error_msg)
error(error_msg)
msg = "Compilation error when compiling \""..line.."\": ".. error_msg
error(msg)
end

-- Traverse the ast looking for events/syscalls in the ignored
Expand All @@ -315,8 +315,8 @@ function compiler.compile_filter(name, source, macro_defs, list_defs)
local ast, error_msg = parser.parse_filter(source)

if (error_msg) then
print ("Compilation error when compiling \""..source.."\": ", error_msg)
error(error_msg)
msg = "Compilation error when compiling \""..source.."\": "..error_msg
error(msg)
end

-- Traverse the ast looking for events/syscalls in the ignored
Expand Down
49 changes: 47 additions & 2 deletions userspace/engine/lua/rule_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,45 @@ end
-- to a rule.
local state = {macros={}, lists={}, filter_ast=nil, rules_by_name={}, n_rules=0, rules_by_idx={}}

-- From http://lua-users.org/wiki/TableUtils
--
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end
end

function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end

function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end


function load_rules(rules_content, rules_mgr, verbose, all_events)

compiler.set_verbose(verbose)
Expand All @@ -135,6 +174,10 @@ function load_rules(rules_content, rules_mgr, verbose, all_events)
return
end

if type(rules) ~= "table" then
error("Rules content \""..rules_content.."\" is not yaml")
end

for i,v in ipairs(rules) do -- iterate over yaml list

if (not (type(v) == "table")) then
Expand Down Expand Up @@ -164,9 +207,9 @@ function load_rules(rules_content, rules_mgr, verbose, all_events)

state.lists[v['list']] = items

else -- rule
elseif (v['rule']) then

if (v['rule'] == nil) then
if (v['rule'] == nil or type(v['rule']) == "table") then
error ("Missing name in rule")
end

Expand Down Expand Up @@ -217,6 +260,8 @@ function load_rules(rules_content, rules_mgr, verbose, all_events)
else
error ("Unexpected type in load_rule: "..filter_ast.type)
end
else
error ("Unknown rule object: "..table.tostring(v))
end
end

Expand Down