Path: blob/main/sys/tools/syscalls/core/scarg.lua
101628 views
--1-- SPDX-License-Identifier: BSD-2-Clause2--3-- Copyright (c) 2021-2024 SRI International4-- Copyright (c) 2024 Tyler Baxter <[email protected]>5-- Copyright (c) 2023 Warner Losh <[email protected]>6-- Copyright (c) 2019 Kyle Evans <[email protected]>7--89local config = require("config")10local util = require("tools.util")1112local scarg = {}1314scarg.__index = scarg1516-- Check this argument against config for ABI changes from native. Return TRUE17-- if there are.18local function checkAbiChanges(arg)19for k, v in pairs(config.known_abi_flags) do20if config.abiChanges(k) and v ~= nil then21for _, e in pairs(v) do22if arg:find(e) then23return true24end25end26end27end28return false29end3031-- Strips the Microsoft(R) SAL annotations from this argument.32local function stripArgAnnotations(arg)33arg = arg:gsub("_Contains_[^ ]*[_)] ?", "")34arg = arg:gsub("_In[^ ]*[_)] ?", "")35arg = arg:gsub("_Out[^ ]*[_)] ?", "")36return util.trim(arg)37end3839-- Preprocessing of this argument.40function scarg:init(line)41-- Trim whitespace and trailing comma. We don't want them here;42-- these can mess with our processing of this argument.43line = util.trim(line) -- This provides a clearer abort error.44self.scarg = util.trim(line, ',')4546self.arg_abi_change = checkAbiChanges(self.scarg)47self.changes_abi = self.arg_abi_change48self.scarg = stripArgAnnotations(self.scarg)4950self.name = self.scarg:match("([^* ]+)$")51-- Our pattern might produce a Lua pattern sequence; that's a malformed52-- declaration.53local status, type = pcall(function()54return util.trim(self.scarg:gsub(self.name .. "$", ""), nil)55end)56if not status then57util.abort(1, "Malformed argument line: " .. line)58end59self.type = type60end6162-- Processes this argument.63-- Flags if there's ABI changes from native, converts this argument to the64-- target ABI, and handles 64-bit argument pairing.65-- Returns TRUE if this argument is processed and ready to add.66-- Returns FALSE if it shouldn't be added (the argument type is void).67function scarg:process()68if self.type ~= "" and self.name ~= "void" then69-- util.is64bitType() needs a bare type so check it after70-- argname is removed.71self.changes_abi = self.changes_abi or72(config.abiChanges("pair_64bit") and73util.is64bitType(self.type))7475self.type = self.type:gsub("intptr_t", config.abi_intptr_t)76self.type = self.type:gsub("semid_t", config.abi_semid_t)7778if util.isPtrType(self.type) then79self.type = self.type:gsub("size_t", config.abi_size_t)80self.type = self.type:gsub("^long", config.abi_long)81self.type = self.type:gsub("^u_long", config.abi_u_long)82self.type = self.type:gsub("^const u_long", "const " ..83config.abi_u_long)84elseif self.type:find("^long$") then85self.type = config.abi_long86end8788if util.isPtrArrayType(self.type) and89config.abi_ptr_array_t ~= "" then90-- `* const *` -> `**`91self.type = self.type:gsub("[*][ ]*const[ ]*[*]", "**")92-- e.g., `struct aiocb **` -> `uint32_t *`93self.type = self.type:gsub("[^*]*[*]",94config.abi_ptr_array_t .. " ", 1)95end9697if self.arg_abi_change then98self.type = self.type:gsub("(struct [^ ]*)", "%1" ..99config.abi_type_suffix)100self.type = self.type:gsub("(union [^ ]*)", "%1" ..101config.abi_type_suffix)102end103return true104end105return false106end107108-- For pairing 64-bit arguments, pad if necessary.109-- Returns TRUE if this argument was padded.110local function pad(tbl)111if #tbl % 2 == 1 then112table.insert(tbl, {113type = "int",114name = "_pad",115})116return true117end118return false119end120121-- To append to a system call's argument table. Appends to the end.122function scarg:append(tbl)123if config.abiChanges("pair_64bit") and util.is64bitType(self.type) then124pad(tbl) -- Needs argument padding.125table.insert(tbl, {126type = "uint32_t",127name = self.name .. "1",128})129table.insert(tbl, {130type = "uint32_t",131name = self.name .. "2",132})133else134table.insert(tbl, {135type = self.type,136name = self.name,137})138end139end140141-- Returns TRUE if this argument has ABI changes from native.142-- EXAMPLE: 32-bit argument for freebsd32.143function scarg:changesAbi()144return self.changes_abi145end146147function scarg:new(obj, line)148obj = obj or { }149setmetatable(obj, self)150self.__index = self151152-- ABI changes that we only want in this scope.153self.arg_abi_change = false154-- ABI changes that we want the system call object to see.155self.changes_abi = false156157obj:init(line)158159return obj160end161162return scarg163164165