opnsense-src/sys/tools/syscalls/scripts/syscalls.lua

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
2.6 KiB
Lua
Raw Normal View History

#!/usr/libexec/flua
--
-- SPDX-License-Identifier: BSD-2-Clause
--
-- Copyright (c) 2024 Tyler Baxter <agge@FreeBSD.org>
-- Copyright (c) 2023 Warner Losh <imp@bsdimp.com>
-- Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
--
-- Setup to be a module, or ran as its own script.
local syscalls = {}
local script = not pcall(debug.getlocal, 4, 1) -- TRUE if script.
if script then
-- Add library root to the package path.
local path = arg[0]:gsub("/[^/]+.lua$", "")
package.path = package.path .. ";" .. path .. "/../?.lua"
end
local FreeBSDSyscall = require("core.freebsd-syscall")
local generator = require("tools.generator")
-- File has not been decided yet; config will decide file. Default defined as
-- /dev/null.
syscalls.file = "/dev/null"
function syscalls.generate(tbl, config, fh)
-- Grab the master system calls table.
local s = tbl.syscalls
-- Bind the generator to the parameter file.
local gen = generator:new({}, fh)
-- Write the generated preamble.
gen:preamble("System call names.")
gen:write(string.format("const char *%s[] = {\n", config.namesname))
for _, v in pairs(s) do
--print("num " .. v.num .. " name " .. v.name)
local c = v:compatLevel()
makesyscalls: Restore support for cpp in input Allow patterns like this in syscalls.master: #if 0 91 AUE_NULL RESERVED #else 91 AUE_NULL STD|CAPENABLED { int newsyscall(void); } #endif makesyscalls.lua and it's predecessor makesyscalls.sh (really an awk script with a tiny shell prolog) used a single pass parsing model where lines beginning with `#` were emitted into most generated files as they were read. I belive this was initially there to allow includes to be listed in syscalls.master, but Hyrum's Law[0] applies and people are using it for things like architecture-specific syscall definitions. This use of CPP macro is unsound and there are a number of sharp edges in both the new and old implementations. The macros are unsound because not all the files were generate are run through CPP (or if they are not in the same context) and this will increasingly be true as we generate more things. Sharp edges include the fact that anything before the first syscall would be printed at a different scope (e.g., before an array is declared). In this patch I collect each non-#include CPP directive and attach them to the syscall table or individual entries. All entries before the first syscall and after the last are attached to the prolog and epilog members. Within the syscall table all entries are attached to the next system calls's prolog member. In generators, each prolog entry is printed regardless of the system call's visibiilty which replicates the naive single pass model's behavior (including lots of empty blocks of #if/#else/#endif in the output). Unlike makesyscalls.lua, I discard none #define entries at the top of the file and print a warning as their usefulness appears limited. [0] https://www.hyrumslaw.com Reported by: kevans Reviewed by: kevans Sponsored by: DARPA, AFRL Pull Request: https://github.com/freebsd/freebsd-src/pull/1575
2025-02-18 17:02:19 -05:00
gen:write(v.prolog);
if v:native() then
gen:write(string.format([[
"%s", /* %d = %s */
]],
v.name, v.num, v.name))
elseif c >= 3 then
-- Lookup the info for this specific compat option.
local flag, descr
for _, opt in pairs(config.compat_options) do
if opt.compatlevel == c then
flag = opt.flag
flag = flag:lower()
descr = opt.descr
break
end
end
gen:write(string.format([[
"%s.%s", /* %d = %s %s */
]],
flag, v.name, v.num, descr, v.name))
elseif v.type.RESERVED then
gen:write(string.format([[
"#%d", /* %d = reserved for local use */
]],
v.num, v.num))
elseif v.type.UNIMPL then
gen:write(string.format([[
"#%d", /* %d = %s */
]],
v.num, v.num, v.alias))
elseif v.type.OBSOL then
gen:write(string.format([[
"obs_%s", /* %d = obsolete %s */
]],
v.name, v.num, v.name))
end
end
makesyscalls: Restore support for cpp in input Allow patterns like this in syscalls.master: #if 0 91 AUE_NULL RESERVED #else 91 AUE_NULL STD|CAPENABLED { int newsyscall(void); } #endif makesyscalls.lua and it's predecessor makesyscalls.sh (really an awk script with a tiny shell prolog) used a single pass parsing model where lines beginning with `#` were emitted into most generated files as they were read. I belive this was initially there to allow includes to be listed in syscalls.master, but Hyrum's Law[0] applies and people are using it for things like architecture-specific syscall definitions. This use of CPP macro is unsound and there are a number of sharp edges in both the new and old implementations. The macros are unsound because not all the files were generate are run through CPP (or if they are not in the same context) and this will increasingly be true as we generate more things. Sharp edges include the fact that anything before the first syscall would be printed at a different scope (e.g., before an array is declared). In this patch I collect each non-#include CPP directive and attach them to the syscall table or individual entries. All entries before the first syscall and after the last are attached to the prolog and epilog members. Within the syscall table all entries are attached to the next system calls's prolog member. In generators, each prolog entry is printed regardless of the system call's visibiilty which replicates the naive single pass model's behavior (including lots of empty blocks of #if/#else/#endif in the output). Unlike makesyscalls.lua, I discard none #define entries at the top of the file and print a warning as their usefulness appears limited. [0] https://www.hyrumslaw.com Reported by: kevans Reviewed by: kevans Sponsored by: DARPA, AFRL Pull Request: https://github.com/freebsd/freebsd-src/pull/1575
2025-02-18 17:02:19 -05:00
gen:write(tbl.epilog)
-- End
gen:write("};\n")
end
-- Entry of script:
if script then
local config = require("config")
if #arg < 1 or #arg > 2 then
error("usage: " .. arg[0] .. " syscall.master")
end
local sysfile, configfile = arg[1], arg[2]
config.merge(configfile)
config.mergeCompat()
-- The parsed syscall table.
local tbl = FreeBSDSyscall:new{sysfile = sysfile, config = config}
syscalls.file = config.sysnames -- change file here
syscalls.generate(tbl, config, syscalls.file)
end
-- Return the module.
return syscalls